From 39ba5c9af851aa0d09b42dc30cce12aa6107c8af Mon Sep 17 00:00:00 2001 From: He Weiwei Date: Mon, 26 Jul 2021 17:52:18 +0000 Subject: [PATCH] fix: let jobservice of exporter be optional Signed-off-by: He Weiwei --- apis/goharbor.io/v1beta1/exporter_types.go | 4 +- .../v1beta1/zz_generated.deepcopy.go | 6 +- charts/harbor-operator/templates/crds.yaml | 1 - controllers/goharbor/exporter/deployments.go | 71 ++++++++++--------- controllers/goharbor/exporter_test.go | 2 +- controllers/goharbor/harbor/exporter.go | 2 +- manifests/cluster/deployment.yaml | 1 - manifests/harbor/deployment.yaml | 1 - 8 files changed, 48 insertions(+), 40 deletions(-) diff --git a/apis/goharbor.io/v1beta1/exporter_types.go b/apis/goharbor.io/v1beta1/exporter_types.go index 80821eb12..fff519a1a 100644 --- a/apis/goharbor.io/v1beta1/exporter_types.go +++ b/apis/goharbor.io/v1beta1/exporter_types.go @@ -56,8 +56,8 @@ type ExporterSpec struct { // +kubebuilder:validation:Required Database ExporterDatabaseSpec `json:"database"` - // +kubebuilder:validation:Required - JobService ExporterJobServiceSpec `json:"jobservice"` + // +kubebuilder:validation:Optional + JobService *ExporterJobServiceSpec `json:"jobservice,omitempty"` // +kubebuilder:validation:Optional // +kubebuilder:default=8001 diff --git a/apis/goharbor.io/v1beta1/zz_generated.deepcopy.go b/apis/goharbor.io/v1beta1/zz_generated.deepcopy.go index 07f686639..6f915f80c 100644 --- a/apis/goharbor.io/v1beta1/zz_generated.deepcopy.go +++ b/apis/goharbor.io/v1beta1/zz_generated.deepcopy.go @@ -1166,7 +1166,11 @@ func (in *ExporterSpec) DeepCopyInto(out *ExporterSpec) { in.Cache.DeepCopyInto(&out.Cache) out.Core = in.Core in.Database.DeepCopyInto(&out.Database) - in.JobService.DeepCopyInto(&out.JobService) + if in.JobService != nil { + in, out := &in.JobService, &out.JobService + *out = new(ExporterJobServiceSpec) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExporterSpec. diff --git a/charts/harbor-operator/templates/crds.yaml b/charts/harbor-operator/templates/crds.yaml index 41c1e5443..2c35f82bf 100644 --- a/charts/harbor-operator/templates/crds.yaml +++ b/charts/harbor-operator/templates/crds.yaml @@ -4246,7 +4246,6 @@ spec: required: - core - database - - jobservice type: object status: description: ComponentStatus represents the current status of the resource. https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties diff --git a/controllers/goharbor/exporter/deployments.go b/controllers/goharbor/exporter/deployments.go index 559ea0d88..756f00a2a 100644 --- a/controllers/goharbor/exporter/deployments.go +++ b/controllers/goharbor/exporter/deployments.go @@ -113,29 +113,27 @@ func (r *Reconciler) GetDeployment(ctx context.Context, exporter *goharborv1.Exp }) } - if exporter.Spec.JobService.Redis != nil { - redisURL, err := r.getJobServiceRedisURL(ctx, exporter) - if err != nil { - return nil, errors.Wrap(err, "get redis url of jobservice") - } - - redisNamespace := jobserviceRedisNamespace - if exporter.Spec.JobService.Redis.Namespace != "" { - redisNamespace = exporter.Spec.JobService.Redis.Namespace - } + redisURL, err := r.getJobServiceRedisURL(ctx, exporter) + if err != nil { + return nil, errors.Wrap(err, "get redis url of jobservice") + } - redisTimeout := jobserivceRedisTimeout - if exporter.Spec.JobService.Redis.IdleTimeout != nil { - redisTimeout = int(exporter.Spec.JobService.Redis.IdleTimeout.Seconds()) - } + redisNamespace := jobserviceRedisNamespace + if exporter.Spec.JobService.Redis.Namespace != "" { + redisNamespace = exporter.Spec.JobService.Redis.Namespace + } - envs = append(envs, []corev1.EnvVar{ - {Name: "HARBOR_REDIS_URL", Value: redisURL}, - {Name: "HARBOR_REDIS_NAMESPACE", Value: redisNamespace}, - {Name: "HARBOR_REDIS_TIMEOUT", Value: fmt.Sprintf("%d", redisTimeout)}, - }...) + redisTimeout := jobserivceRedisTimeout + if exporter.Spec.JobService.Redis.IdleTimeout != nil { + redisTimeout = int(exporter.Spec.JobService.Redis.IdleTimeout.Seconds()) } + envs = append(envs, []corev1.EnvVar{ + {Name: "HARBOR_REDIS_URL", Value: redisURL}, + {Name: "HARBOR_REDIS_NAMESPACE", Value: redisNamespace}, + {Name: "HARBOR_REDIS_TIMEOUT", Value: fmt.Sprintf("%d", redisTimeout)}, + }...) + volumes := []corev1.Volume{} volumeMounts := []corev1.VolumeMount{} @@ -249,25 +247,34 @@ func (r *Reconciler) GetDeployment(ctx context.Context, exporter *goharborv1.Exp } func (r *Reconciler) getJobServiceRedisURL(ctx context.Context, exporter *goharborv1.Exporter) (string, error) { - if exporter.Spec.JobService.Redis == nil { - return "", nil - } - - var redisPassword string - - if exporter.Spec.JobService.Redis.PasswordRef != "" { - var err error + if exporter.Spec.JobService == nil { + // compatible with the exporter converted from v1alpha3 and the upgrade to harbor v2.3.x + key := client.ObjectKey{ + Namespace: exporter.Namespace, + Name: strings.TrimSuffix(exporter.Name, "-harbor"), + } - redisPassword, err = r.getValueFromSecret(ctx, exporter.GetNamespace(), exporter.Spec.JobService.Redis.PasswordRef, jobserivceRedisPasswordKey) - if err != nil { - return "", errors.Wrap(err, "get redis password of jobservice") + var harbor goharborv1.Harbor + if err := r.Client.Get(ctx, key, &harbor); err != nil { + return "", err } - if redisPassword == "" { - logger.Get(ctx).Info("redis password secret of jobservice not found", "secret", exporter.Spec.JobService.Redis.PasswordRef) + exporter.Spec.JobService = &goharborv1.ExporterJobServiceSpec{ + Redis: &goharborv1.JobServicePoolRedisSpec{ + RedisConnection: harbor.Spec.RedisConnection(harbormetav1.JobServiceRedis), + }, } } + redisPassword, err := r.getValueFromSecret(ctx, exporter.GetNamespace(), exporter.Spec.JobService.Redis.PasswordRef, jobserivceRedisPasswordKey) + if err != nil { + return "", errors.Wrap(err, "get redis password of jobservice") + } + + if redisPassword == "" { + logger.Get(ctx).Info("redis password secret of jobservice not found", "secret", exporter.Spec.JobService.Redis.PasswordRef) + } + return exporter.Spec.JobService.Redis.GetDSNStringWithRawPassword(redisPassword), nil } diff --git a/controllers/goharbor/exporter_test.go b/controllers/goharbor/exporter_test.go index 4fc8dddfb..c192bb7aa 100644 --- a/controllers/goharbor/exporter_test.go +++ b/controllers/goharbor/exporter_test.go @@ -49,7 +49,7 @@ func setupValidExporter(ctx context.Context, ns string) (Resource, client.Object PostgresConnectionWithParameters: core.Spec.Database.PostgresConnectionWithParameters, EncryptionKeyRef: core.Spec.Database.EncryptionKeyRef, }, - JobService: goharborv1.ExporterJobServiceSpec{ + JobService: &goharborv1.ExporterJobServiceSpec{ Redis: &goharborv1.JobServicePoolRedisSpec{ RedisConnection: redis, }, diff --git a/controllers/goharbor/harbor/exporter.go b/controllers/goharbor/harbor/exporter.go index 06e0dfd0b..7f6327602 100644 --- a/controllers/goharbor/harbor/exporter.go +++ b/controllers/goharbor/harbor/exporter.go @@ -98,7 +98,7 @@ func (r *Reconciler) GetExporter(ctx context.Context, harbor *goharborv1.Harbor) PostgresConnectionWithParameters: *postgresConn, EncryptionKeyRef: encryptionKeyRef, }, - JobService: goharborv1.ExporterJobServiceSpec{ + JobService: &goharborv1.ExporterJobServiceSpec{ Redis: &goharborv1.JobServicePoolRedisSpec{ RedisConnection: harbor.Spec.RedisConnection(harbormetav1.JobServiceRedis), }, diff --git a/manifests/cluster/deployment.yaml b/manifests/cluster/deployment.yaml index 1faaa39da..aa7194b58 100644 --- a/manifests/cluster/deployment.yaml +++ b/manifests/cluster/deployment.yaml @@ -4262,7 +4262,6 @@ spec: required: - core - database - - jobservice type: object status: description: ComponentStatus represents the current status of the resource. https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties diff --git a/manifests/harbor/deployment.yaml b/manifests/harbor/deployment.yaml index 9ef53d30f..846920b55 100644 --- a/manifests/harbor/deployment.yaml +++ b/manifests/harbor/deployment.yaml @@ -4262,7 +4262,6 @@ spec: required: - core - database - - jobservice type: object status: description: ComponentStatus represents the current status of the resource. https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties