Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fmt changes in registry package #999

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions controllers/goharbor/registry/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
20 changes: 10 additions & 10 deletions controllers/goharbor/registry/registryctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions controllers/goharbor/registry/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions controllers/goharbor/registry/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -89,5 +89,5 @@ func (r *Reconciler) GetCtlService(ctx context.Context, registryCtl *goharborv1.
r.Label("namespace"): namespace,
},
},
}, nil
}
}
Loading