Small code improvements

This commit is contained in:
Tomasz Sęk
2019-08-19 16:56:17 +02:00
parent 64f6fa68eb
commit 89c46a1720
15 changed files with 118 additions and 110 deletions
@@ -127,14 +127,14 @@ func (r *ReconcileJenkinsBaseConfiguration) Reconcile() (reconcile.Result, jenki
return result, jenkinsClient, err
}
// GetJenkinsOpts put container JENKINS_OPTS env parameters in map and returns it
func GetJenkinsOpts(jenkins *v1alpha2.Jenkins) map[string]string {
// GetJenkinsOpts gets JENKINS_OPTS env parameter, parses it's values and returns it as a map`
func GetJenkinsOpts(jenkins v1alpha2.Jenkins) map[string]string {
envs := jenkins.Spec.Master.Containers[0].Env
jenkinsOpts := make(map[string]string)
for k, v := range envs {
if v.Name == "JENKINS_OPTS" {
jenkinsOptsEnv := envs[k]
for key, value := range envs {
if value.Name == "JENKINS_OPTS" {
jenkinsOptsEnv := envs[key]
jenkinsOptsWithDashes := jenkinsOptsEnv.Value
if len(jenkinsOptsWithDashes) == 0 {
return nil
@@ -781,7 +781,7 @@ func (r *ReconcileJenkinsBaseConfiguration) ensureJenkinsClient(meta metav1.Obje
jenkinsURL, err := jenkinsclient.BuildJenkinsAPIUrl(
r.jenkins.ObjectMeta.Namespace, resources.GetJenkinsHTTPServiceName(r.jenkins), r.jenkins.Spec.Service.Port, r.local, r.minikube)
if prefix, ok := GetJenkinsOpts(r.jenkins)["prefix"]; ok {
if prefix, ok := GetJenkinsOpts(*r.jenkins)["prefix"]; ok {
jenkinsURL = jenkinsURL + prefix
}
@@ -16,7 +16,7 @@ import (
func TestGetJenkinsOpts(t *testing.T) {
t.Run("JENKINS_OPTS is uninitialized", func(t *testing.T) {
jenkins := &v1alpha2.Jenkins{
jenkins := v1alpha2.Jenkins{
Spec: v1alpha2.JenkinsSpec{
Master: v1alpha2.JenkinsMaster{
Containers: []v1alpha2.Container{
@@ -35,7 +35,7 @@ func TestGetJenkinsOpts(t *testing.T) {
})
t.Run("JENKINS_OPTS is empty", func(t *testing.T) {
jenkins := &v1alpha2.Jenkins{
jenkins := v1alpha2.Jenkins{
Spec: v1alpha2.JenkinsSpec{
Master: v1alpha2.JenkinsMaster{
Containers: []v1alpha2.Container{
@@ -54,7 +54,7 @@ func TestGetJenkinsOpts(t *testing.T) {
})
t.Run("JENKINS_OPTS have --prefix argument ", func(t *testing.T) {
jenkins := &v1alpha2.Jenkins{
jenkins := v1alpha2.Jenkins{
Spec: v1alpha2.JenkinsSpec{
Master: v1alpha2.JenkinsMaster{
Containers: []v1alpha2.Container{
@@ -77,7 +77,7 @@ func TestGetJenkinsOpts(t *testing.T) {
})
t.Run("JENKINS_OPTS have --prefix and --httpPort argument", func(t *testing.T) {
jenkins := &v1alpha2.Jenkins{
jenkins := v1alpha2.Jenkins{
Spec: v1alpha2.JenkinsSpec{
Master: v1alpha2.JenkinsMaster{
Containers: []v1alpha2.Container{
@@ -103,7 +103,7 @@ func TestGetJenkinsOpts(t *testing.T) {
})
t.Run("JENKINS_OPTS have --httpPort argument", func(t *testing.T) {
jenkins := &v1alpha2.Jenkins{
jenkins := v1alpha2.Jenkins{
Spec: v1alpha2.JenkinsSpec{
Master: v1alpha2.JenkinsMaster{
Containers: []v1alpha2.Container{
@@ -126,7 +126,7 @@ func TestGetJenkinsOpts(t *testing.T) {
})
t.Run("JENKINS_OPTS have --httpPort=--8080 argument", func(t *testing.T) {
jenkins := &v1alpha2.Jenkins{
jenkins := v1alpha2.Jenkins{
Spec: v1alpha2.JenkinsSpec{
Master: v1alpha2.JenkinsMaster{
Containers: []v1alpha2.Container{
@@ -231,9 +231,9 @@ func NewJenkinsMasterContainer(jenkins *v1alpha2.Jenkins) corev1.Container {
},
},
SecurityContext: jenkinsContainer.SecurityContext,
Env: envs,
Resources: jenkinsContainer.Resources,
VolumeMounts: append(GetJenkinsMasterContainerBaseVolumeMounts(jenkins), jenkinsContainer.VolumeMounts...),
Env: envs,
Resources: jenkinsContainer.Resources,
VolumeMounts: append(GetJenkinsMasterContainerBaseVolumeMounts(jenkins), jenkinsContainer.VolumeMounts...),
}
}
@@ -61,40 +61,42 @@ func (r *ReconcileJenkinsBaseConfiguration) Validate(jenkins *v1alpha2.Jenkins)
}
func (r *ReconcileJenkinsBaseConfiguration) validateImagePullSecrets() (bool, error) {
var err error
for _, sr := range r.jenkins.Spec.Master.ImagePullSecrets {
valid, err := r.validateImagePullSecret(sr.Name)
if err != nil || !valid {
if err != nil {
return false, err
}
if !valid {
return false, nil
}
}
return true, err
return true, nil
}
func (r *ReconcileJenkinsBaseConfiguration) validateImagePullSecret(name string) (bool, error) {
func (r *ReconcileJenkinsBaseConfiguration) validateImagePullSecret(secretName string) (bool, error) {
secret := &corev1.Secret{}
err := r.k8sClient.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: r.jenkins.ObjectMeta.Namespace}, secret)
err := r.k8sClient.Get(context.TODO(), types.NamespacedName{Name: secretName, Namespace: r.jenkins.ObjectMeta.Namespace}, secret)
if err != nil && apierrors.IsNotFound(err) {
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret %s not found defined in spec.master.imagePullSecrets", name))
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret %s not found defined in spec.master.imagePullSecrets", secretName))
return false, nil
} else if err != nil && !apierrors.IsNotFound(err) {
return false, stackerr.WithStack(err)
}
if secret.Data["docker-server"] == nil {
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-server' key.", name))
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-server' key.", secretName))
return false, nil
}
if secret.Data["docker-username"] == nil {
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-username' key.", name))
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-username' key.", secretName))
return false, nil
}
if secret.Data["docker-password"] == nil {
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-password' key.", name))
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-password' key.", secretName))
return false, nil
}
if secret.Data["docker-email"] == nil {
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-email' key.", name))
r.logger.V(log.VWarn).Info(fmt.Sprintf("Secret '%s' defined in spec.master.imagePullSecrets doesn't have 'docker-email' key.", secretName))
return false, nil
}
@@ -40,16 +40,16 @@ const content = `
</body>
</html>`
// MailGun is service for sending emails
// MailGun is a sending emails notification service
type MailGun struct {
k8sClient k8sclient.Client
}
func (m MailGun) getStatusColor(logLevel LoggingLevel) StatusColor {
func (m MailGun) getStatusColor(logLevel v1alpha2.NotificationLogLevel) StatusColor {
switch logLevel {
case LogInfo:
case v1alpha2.NotificationLogLevelInfo:
return "blue"
case LogWarn:
case v1alpha2.NotificationLogLevelWarning:
return "red"
default:
return "gray"
@@ -14,7 +14,7 @@ import (
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)
// Teams is Microsoft Teams Service
// Teams is a Microsoft MicrosoftTeams notification service
type Teams struct {
k8sClient k8sclient.Client
}
@@ -40,11 +40,11 @@ type TeamsFact struct {
Value string `json:"value"`
}
func (t Teams) getStatusColor(logLevel LoggingLevel) StatusColor {
func (t Teams) getStatusColor(logLevel v1alpha2.NotificationLogLevel) StatusColor {
switch logLevel {
case LogInfo:
case v1alpha2.NotificationLogLevelInfo:
return "439FE0"
case LogWarn:
case v1alpha2.NotificationLogLevelWarning:
return "E81123"
default:
return "C8C8C8"
@@ -55,7 +55,7 @@ func (t Teams) getStatusColor(logLevel LoggingLevel) StatusColor {
func (t Teams) Send(event Event, config v1alpha2.Notification) error {
secret := &corev1.Secret{}
selector := config.Teams.URLSecretKeySelector
selector := config.Teams.WebHookURLSecretKeySelector
err := t.k8sClient.Get(context.TODO(), types.NamespacedName{Name: selector.Name, Namespace: event.Jenkins.Namespace}, secret)
if err != nil {
@@ -84,8 +84,8 @@ func TestTeams_Send(t *testing.T) {
assert.NoError(t, err)
err = teams.Send(event, v1alpha2.Notification{
Teams: v1alpha2.Teams{
URLSecretKeySelector: v1alpha2.SecretKeySelector{
Teams: &v1alpha2.MicrosoftTeams{
WebHookURLSecretKeySelector: v1alpha2.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: testSecretName,
},
+6 -12
View File
@@ -12,12 +12,6 @@ import (
)
const (
// LogWarn is warning log entry
LogWarn LoggingLevel = "warn"
// LogInfo is info log entry
LogInfo LoggingLevel = "info"
titleText = "Operator reconciled."
messageFieldName = "Message"
loggingLevelFieldName = "Logging Level"
@@ -33,7 +27,7 @@ var (
testNamespace = "default"
testMessage = "test-message"
testMessageVerbose = "detail-test-message"
testLoggingLevel = LogWarn
testLoggingLevel = v1alpha2.NotificationLogLevelWarning
client = http.Client{}
)
@@ -48,7 +42,7 @@ type LoggingLevel string
type Event struct {
Jenkins v1alpha2.Jenkins
ConfigurationType string
LogLevel LoggingLevel
LogLevel v1alpha2.NotificationLogLevel
Message string
MessageVerbose string
}
@@ -65,11 +59,11 @@ func Listen(events chan Event, k8sClient k8sclient.Client) {
var err error
var svc service
if notificationConfig.Slack != (v1alpha2.Slack{}) {
if notificationConfig.Slack != nil {
svc = Slack{k8sClient: k8sClient}
} else if notificationConfig.Teams != (v1alpha2.Teams{}) {
} else if notificationConfig.Teams != nil {
svc = Teams{k8sClient: k8sClient}
} else if notificationConfig.Mailgun != (v1alpha2.Mailgun{}) {
} else if notificationConfig.Mailgun != nil {
svc = MailGun{k8sClient: k8sClient}
} else {
logger.V(log.VWarn).Info(fmt.Sprintf("Unexpected notification `%+v`", notificationConfig))
@@ -93,7 +87,7 @@ func Listen(events chan Event, k8sClient k8sclient.Client) {
}
func notify(svc service, event Event, manifest v1alpha2.Notification) error {
if event.LogLevel == LogInfo && string(manifest.LoggingLevel) == string(LogWarn) {
if event.LogLevel == v1alpha2.NotificationLogLevelInfo && manifest.LoggingLevel == v1alpha2.NotificationLogLevelWarning {
return nil
}
@@ -14,7 +14,7 @@ import (
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)
// Slack is messaging service
// Slack is a Slack notification service
type Slack struct {
k8sClient k8sclient.Client
}
@@ -43,11 +43,11 @@ type SlackField struct {
Short bool `json:"short"`
}
func (s Slack) getStatusColor(logLevel LoggingLevel) StatusColor {
func (s Slack) getStatusColor(logLevel v1alpha2.NotificationLogLevel) StatusColor {
switch logLevel {
case LogInfo:
case v1alpha2.NotificationLogLevelInfo:
return "#439FE0"
case LogWarn:
case v1alpha2.NotificationLogLevelWarning:
return "danger"
default:
return "#c8c8c8"
@@ -57,7 +57,7 @@ func (s Slack) getStatusColor(logLevel LoggingLevel) StatusColor {
// Send is function for sending directly to API
func (s Slack) Send(event Event, config v1alpha2.Notification) error {
secret := &corev1.Secret{}
selector := config.Slack.URLSecretKeySelector
selector := config.Slack.WebHookURLSecretKeySelector
err := s.k8sClient.Get(context.TODO(), types.NamespacedName{Name: selector.Name, Namespace: event.Jenkins.Namespace}, secret)
if err != nil {
@@ -84,8 +84,8 @@ func TestSlack_Send(t *testing.T) {
assert.NoError(t, err)
err = slack.Send(event, v1alpha2.Notification{
Slack: v1alpha2.Slack{
URLSecretKeySelector: v1alpha2.SecretKeySelector{
Slack: &v1alpha2.Slack{
WebHookURLSecretKeySelector: v1alpha2.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: testSecretName,
},
@@ -93,6 +93,5 @@ func TestSlack_Send(t *testing.T) {
},
},
})
assert.NoError(t, err)
}