From 471470969c2f8593db75c7c77c48a2fe814acabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20S=C4=99k?= Date: Mon, 16 Dec 2019 14:04:35 +0100 Subject: [PATCH] #229 Handle deprecated masterAnnotations --- pkg/apis/jenkins/v1alpha2/jenkins_types.go | 10 +++- .../jenkins/v1alpha2/zz_generated.deepcopy.go | 7 +++ pkg/controller/jenkins/jenkins_controller.go | 51 +++++++++++++++---- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/pkg/apis/jenkins/v1alpha2/jenkins_types.go b/pkg/apis/jenkins/v1alpha2/jenkins_types.go index 90d35cd6..e1f7c2fa 100644 --- a/pkg/apis/jenkins/v1alpha2/jenkins_types.go +++ b/pkg/apis/jenkins/v1alpha2/jenkins_types.go @@ -222,7 +222,15 @@ type JenkinsMaster struct { // queryable and should be preserved when modifying objects. // More info: http://kubernetes.io/docs/user-guide/annotations // +optional - Annotations map[string]string `json:"masterAnnotations,omitempty"` + Annotations map[string]string `json:"annotations,omitempty"` + + // Annotations is an unstructured key value map stored with a resource that may be + // set by external tools to store and retrieve arbitrary metadata. They are not + // queryable and should be preserved when modifying objects. + // More info: http://kubernetes.io/docs/user-guide/annotations + // Deprecated: will be removed in the future, please use Annotations(annotations) + // +optional + AnnotationsDeprecated map[string]string `json:"masterAnnotations,omitempty"` // NodeSelector is a selector which must be true for the pod to fit on a node. // Selector which must match a node's labels for the pod to be scheduled on that node. diff --git a/pkg/apis/jenkins/v1alpha2/zz_generated.deepcopy.go b/pkg/apis/jenkins/v1alpha2/zz_generated.deepcopy.go index 0bb8fec6..74432e05 100644 --- a/pkg/apis/jenkins/v1alpha2/zz_generated.deepcopy.go +++ b/pkg/apis/jenkins/v1alpha2/zz_generated.deepcopy.go @@ -279,6 +279,13 @@ func (in *JenkinsMaster) DeepCopyInto(out *JenkinsMaster) { (*out)[key] = val } } + if in.AnnotationsDeprecated != nil { + in, out := &in.AnnotationsDeprecated, &out.AnnotationsDeprecated + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } if in.NodeSelector != nil { in, out := &in.NodeSelector, &out.NodeSelector *out = make(map[string]string, len(*in)) diff --git a/pkg/controller/jenkins/jenkins_controller.go b/pkg/controller/jenkins/jenkins_controller.go index 5f6016fe..9993aec0 100644 --- a/pkg/controller/jenkins/jenkins_controller.go +++ b/pkg/controller/jenkins/jenkins_controller.go @@ -193,7 +193,8 @@ func (r *ReconcileJenkins) Reconcile(request reconcile.Request) (reconcile.Resul func (r *ReconcileJenkins) reconcile(request reconcile.Request, logger logr.Logger) (reconcile.Result, *v1alpha2.Jenkins, error) { // Fetch the Jenkins instance jenkins := &v1alpha2.Jenkins{} - err := r.client.Get(context.TODO(), request.NamespacedName, jenkins) + var err error + err = r.client.Get(context.TODO(), request.NamespacedName, jenkins) if err != nil { if apierrors.IsNotFound(err) { // Request object not found, could have been deleted after reconcile request. @@ -204,10 +205,22 @@ func (r *ReconcileJenkins) reconcile(request reconcile.Request, logger logr.Logg // Error reading the object - requeue the request. return reconcile.Result{}, nil, errors.WithStack(err) } - err = r.setDefaults(jenkins, logger) + var requeue bool + requeue, err = r.setDefaults(jenkins, logger) if err != nil { return reconcile.Result{}, jenkins, err } + if requeue { + return reconcile.Result{Requeue: true}, jenkins, nil + } + + requeue, err = r.handleDeprecatedData(jenkins, logger) + if err != nil { + return reconcile.Result{}, jenkins, err + } + if requeue { + return reconcile.Result{Requeue: true}, jenkins, nil + } config := configuration.Configuration{ Client: r.client, @@ -220,7 +233,8 @@ func (r *ReconcileJenkins) reconcile(request reconcile.Request, logger logr.Logg // Reconcile base configuration baseConfiguration := base.New(config, logger, r.local, r.minikube, &r.config) - baseMessages, err := baseConfiguration.Validate(jenkins) + var baseMessages []string + baseMessages, err = baseConfiguration.Validate(jenkins) if err != nil { return reconcile.Result{}, jenkins, err } @@ -239,7 +253,9 @@ func (r *ReconcileJenkins) reconcile(request reconcile.Request, logger logr.Logg return reconcile.Result{}, jenkins, nil // don't requeue } - result, jenkinsClient, err := baseConfiguration.Reconcile() + var result reconcile.Result + var jenkinsClient jenkinsclient.Jenkins + result, jenkinsClient, err = baseConfiguration.Reconcile() if err != nil { return reconcile.Result{}, jenkins, err } @@ -271,7 +287,8 @@ func (r *ReconcileJenkins) reconcile(request reconcile.Request, logger logr.Logg // Reconcile user configuration userConfiguration := user.New(config, jenkinsClient, logger, r.config) - messages, err := userConfiguration.Validate(jenkins) + var messages []string + messages, err = userConfiguration.Validate(jenkins) if err != nil { return reconcile.Result{}, jenkins, err } @@ -324,7 +341,7 @@ func (r *ReconcileJenkins) buildLogger(jenkinsName string) logr.Logger { return log.Log.WithValues("cr", jenkinsName) } -func (r *ReconcileJenkins) setDefaults(jenkins *v1alpha2.Jenkins, logger logr.Logger) error { +func (r *ReconcileJenkins) setDefaults(jenkins *v1alpha2.Jenkins, logger logr.Logger) (requeue bool, err error) { changed := false var jenkinsContainer v1alpha2.Container @@ -333,7 +350,7 @@ func (r *ReconcileJenkins) setDefaults(jenkins *v1alpha2.Jenkins, logger logr.Lo jenkinsContainer = v1alpha2.Container{Name: resources.JenkinsMasterContainerName} } else { if jenkins.Spec.Master.Containers[0].Name != resources.JenkinsMasterContainerName { - return errors.Errorf("first container in spec.master.containers must be Jenkins container with name '%s', please correct CR", resources.JenkinsMasterContainerName) + return false, errors.Errorf("first container in spec.master.containers must be Jenkins container with name '%s', please correct CR", resources.JenkinsMasterContainerName) } jenkinsContainer = jenkins.Spec.Master.Containers[0] } @@ -479,9 +496,9 @@ func (r *ReconcileJenkins) setDefaults(jenkins *v1alpha2.Jenkins, logger logr.Lo } if changed { - return errors.WithStack(r.client.Update(context.TODO(), jenkins)) + return changed, errors.WithStack(r.client.Update(context.TODO(), jenkins)) } - return nil + return changed, nil } func isJavaOpsVariableNotSet(container v1alpha2.Container) bool { @@ -534,3 +551,19 @@ func basePlugins() (result []v1alpha2.Plugin) { } return } + +func (r *ReconcileJenkins) handleDeprecatedData(jenkins *v1alpha2.Jenkins, logger logr.Logger) (requeue bool, err error) { + changed := false + + if len(jenkins.Spec.Master.AnnotationsDeprecated) > 0 { + changed = true + jenkins.Spec.Master.Annotations = jenkins.Spec.Master.AnnotationsDeprecated + jenkins.Spec.Master.AnnotationsDeprecated = map[string]string{} + logger.V(log.VWarn).Info("spec.master.masterAnnotations is deprecated, the annotations have been moved to spec.master.annotations") + } + + if changed { + return changed, errors.WithStack(r.client.Update(context.TODO(), jenkins)) + } + return changed, nil +}