diff --git a/controllers/goharbor/registry/deployments.go b/controllers/goharbor/registry/deployments.go index f569eaeec..1fa35f3f8 100644 --- a/controllers/goharbor/registry/deployments.go +++ b/controllers/goharbor/registry/deployments.go @@ -54,8 +54,9 @@ var ( const ( apiPort = 5000 // https://github.com/docker/distribution/blob/749f6afb4572201e3c37325d0ffedb6f32be8950/contrib/compose/docker-compose.yml#L15 // registry controller port. - httpsPort = 8443 - httpPort = 8080 + httpsPort = 8443 + httpPort = 8080 + registryContainerIndex = 0 ) func (r *Reconciler) GetDeployment(ctx context.Context, registry *goharborv1.Registry) (*appsv1.Deployment, error) { //nolint:funlen @@ -478,8 +479,6 @@ func (r *Reconciler) attachRegistryCtlContainer(ctx context.Context, registry *g return nil } -const registryContainerIndex = 0 - func (r *Reconciler) ApplyFilesystemStorageEnvs(ctx context.Context, registry *goharborv1.Registry, deploy *appsv1.Deployment) error { regContainer := &deploy.Spec.Template.Spec.Containers[registryContainerIndex] @@ -611,8 +610,6 @@ func (r *Reconciler) ApplyInMemoryStorageEnvs(ctx context.Context, registry *goh return nil } -var errNoStorageDriverFound = errors.New("no storage driver found") - func (r *Reconciler) ApplyStorageConfiguration(ctx context.Context, registry *goharborv1.Registry, deploy *appsv1.Deployment) error { if registry.Spec.Storage.Driver.S3 != nil { return r.ApplyS3StorageEnvs(ctx, registry, deploy) @@ -642,5 +639,5 @@ func (r *Reconciler) ApplyStorageConfiguration(ctx context.Context, registry *go return r.ApplyInMemoryStorageEnvs(ctx, registry, deploy) } - return serrors.UnrecoverrableError(errNoStorageDriverFound, serrors.InvalidSpecReason, "unable to configure storage") + return serrors.UnrecoverrableError(errors.Errorf("no storage driver found"), serrors.InvalidSpecReason, "unable to configure storage") } diff --git a/controllers/goharbor/registry/registryctl.go b/controllers/goharbor/registry/registryctl.go index 992a0f3f9..2ef7d719d 100644 --- a/controllers/goharbor/registry/registryctl.go +++ b/controllers/goharbor/registry/registryctl.go @@ -34,26 +34,26 @@ func (r *Reconciler) CleanUpRegistryCtlResources(ctx context.Context, registryCt key := client.ObjectKey{Namespace: namespace, Name: name} // clean registrycontroller deployment - if err = r.DeleteResourceIfExist(ctx, key, &appsv1.Deployment{}); err != nil { + if err = r.deleteResourceIfExist(ctx, key, &appsv1.Deployment{}); err != nil { return fmt.Errorf("clean registryctl deployment error: %w", err) } // clean registrycontroller networkpolicy - if err = r.DeleteResourceIfExist(ctx, client.ObjectKey{Namespace: namespace, Name: name + "-ingress"}, &netv1.NetworkPolicy{}); err != nil { + if err = r.deleteResourceIfExist(ctx, client.ObjectKey{Namespace: namespace, Name: name + "-ingress"}, &netv1.NetworkPolicy{}); err != nil { return fmt.Errorf("clean registryctl networkpolicy error: %w", err) } svc := &corev1.Service{} - if err = r.Client.Get(ctx, key, svc); err == nil && owneredByRegistryCtl(svc, registryCtl) { - err = r.DeleteResourceIfExist(ctx, key, svc) + if err = r.Client.Get(ctx, key, svc); err == nil && ownedByRegistryCtl(svc, registryCtl) { + err = r.deleteResourceIfExist(ctx, key, svc) if err != nil { return fmt.Errorf("clean registryctl service error: %w", err) } } cm := &corev1.ConfigMap{} - if err = r.Client.Get(ctx, key, cm); err == nil && owneredByRegistryCtl(svc, registryCtl) { - err = r.DeleteResourceIfExist(ctx, key, cm) + if err = r.Client.Get(ctx, key, cm); err == nil && ownedByRegistryCtl(svc, registryCtl) { + err = r.deleteResourceIfExist(ctx, key, cm) if err != nil { return fmt.Errorf("clean registryctl configmap error: %w", err) } @@ -62,8 +62,8 @@ func (r *Reconciler) CleanUpRegistryCtlResources(ctx context.Context, registryCt return nil } -// DeleteResourceIfExist deletes existed resources. -func (r *Reconciler) DeleteResourceIfExist(ctx context.Context, key client.ObjectKey, obj client.Object) error { +// deleteResourceIfExist deletes existed resources. +func (r *Reconciler) deleteResourceIfExist(ctx context.Context, key client.ObjectKey, obj client.Object) error { err := r.Client.Get(ctx, key, obj) if err != nil { if errors.IsNotFound(err) { @@ -76,9 +76,9 @@ func (r *Reconciler) DeleteResourceIfExist(ctx context.Context, key client.Objec return r.Client.Delete(ctx, obj) } -// owneredByRegistryCtl checks the resource whether ownered by +// ownedByRegistryCtl checks the resource whether owned by // registrycontroller. -func owneredByRegistryCtl(obj client.Object, registryCtl *goharborv1.RegistryController) bool { +func ownedByRegistryCtl(obj client.Object, registryCtl *goharborv1.RegistryController) bool { owners := obj.GetOwnerReferences() for _, o := range owners { if o.Kind == registryCtl.Kind && o.Name == registryCtl.Name && o.UID == registryCtl.UID { diff --git a/controllers/goharbor/registry/resources.go b/controllers/goharbor/registry/resources.go index 2c3c56825..baecdb56e 100644 --- a/controllers/goharbor/registry/resources.go +++ b/controllers/goharbor/registry/resources.go @@ -38,20 +38,14 @@ func (r *Reconciler) AddResources(ctx context.Context, resource resources.Resour return errors.Wrap(err, "cleanup registryctl resources error") } - service, err := r.GetService(ctx, registry) - if err != nil { - return errors.Wrap(err, "cannot get service") - } + service := r.GetService(ctx, registry) _, err = r.Controller.AddServiceToManage(ctx, service) if err != nil { return errors.Wrapf(err, "cannot add service %s", service.GetName()) } - ctlService, err := r.GetCtlService(ctx, registryCtl) - if err != nil { - return errors.Wrap(err, "cannot get ctlService") - } + ctlService := r.GetCtlService(ctx, registryCtl) _, err = r.Controller.AddServiceToManage(ctx, ctlService) if err != nil { diff --git a/controllers/goharbor/registry/services.go b/controllers/goharbor/registry/services.go index 64371387f..295f10fd3 100644 --- a/controllers/goharbor/registry/services.go +++ b/controllers/goharbor/registry/services.go @@ -11,7 +11,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" ) -func (r *Reconciler) GetService(ctx context.Context, registry *goharborv1.Registry) (*corev1.Service, error) { +func (r *Reconciler) GetService(ctx context.Context, registry *goharborv1.Registry) *corev1.Service { name := r.NormalizeName(ctx, registry.GetName()) namespace := registry.GetNamespace() @@ -52,10 +52,10 @@ func (r *Reconciler) GetService(ctx context.Context, registry *goharborv1.Regist r.Label("namespace"): namespace, }, }, - }, nil + } } -func (r *Reconciler) GetCtlService(ctx context.Context, registryCtl *goharborv1.RegistryController) (*corev1.Service, error) { +func (r *Reconciler) GetCtlService(ctx context.Context, registryCtl *goharborv1.RegistryController) *corev1.Service { name := r.NormalizeName(ctx, registryCtl.GetName()) namespace := registryCtl.GetNamespace() @@ -89,5 +89,5 @@ func (r *Reconciler) GetCtlService(ctx context.Context, registryCtl *goharborv1. r.Label("namespace"): namespace, }, }, - }, nil + } }