Skip to content

Commit

Permalink
fix: let jobservice of exporter be optional (#722)
Browse files Browse the repository at this point in the history
Signed-off-by: He Weiwei <hweiwei@vmware.com>
  • Loading branch information
heww committed Jul 27, 2021
1 parent f344706 commit af83486
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 40 deletions.
4 changes: 2 additions & 2 deletions apis/goharbor.io/v1beta1/exporter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion apis/goharbor.io/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion charts/harbor-operator/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 39 additions & 32 deletions controllers/goharbor/exporter/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/goharbor/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion controllers/goharbor/harbor/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
1 change: 0 additions & 1 deletion manifests/cluster/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion manifests/harbor/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af83486

Please sign in to comment.