Skip to content

Commit

Permalink
fix(harbor class) Close #399
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Guyennet <simon.guyennet@corp.ovh.com>
  • Loading branch information
sguyennet committed Jan 28, 2021
1 parent f7aff7b commit 125a567
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 35 deletions.
13 changes: 2 additions & 11 deletions controllers/goharbor/chartmuseum/chartmuseum.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className := ""

configItem, err := configstore.Filter().Store(r.ConfigStore).Slice(config.HarborClassKey).GetFirstItem()
className, err := r.GetClassName(ctx)
if err != nil {
if !config.IsNotFound(err, config.HarborClassKey) {
return errors.Wrap(err, "cannot get config template path")
}
} else {
className, err = configItem.Value()
if err != nil {
return errors.Wrap(err, "invalid config template path")
}
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
2 changes: 0 additions & 2 deletions controllers/goharbor/chartmuseum/chartmuseum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ var _ = Describe("ChartMuseum", func() {
chartMuseum.ObjectMeta = metav1.ObjectMeta{
Name: test.NewName("chartmuseum"),
Namespace: ns.GetName(),
/* TODO: Enable this when HarborClass is fixed
Annotations: map[string]string{
goharborv1alpha2.HarborClassAnnotation: harborClass,
},
*/
}
})

Expand Down
6 changes: 3 additions & 3 deletions controllers/goharbor/chartmuseum/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/ovh/configstore"

"github.com/goharbor/harbor-operator/controllers"
"github.com/goharbor/harbor-operator/controllers/goharbor/chartmuseum"
"github.com/goharbor/harbor-operator/controllers/goharbor/internal/test"
"github.com/goharbor/harbor-operator/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
)

Expand Down Expand Up @@ -54,10 +56,8 @@ var _ = BeforeSuite(func(done Done) {
name := controllers.ChartMuseum.String()
harborClass = test.NewName(name)

configStore, _ := test.NewConfig(ctx, chartmuseum.ConfigTemplatePathKey, path.Base(chartmuseum.DefaultConfigTemplatePath))
/* TODO: Enable this when HarborClass is fixed
configStore, provider := test.NewConfig(ctx, chartmuseum.ConfigTemplatePathKey, path.Base(chartmuseum.DefaultConfigTemplatePath))
provider.Add(configstore.NewItem(config.HarborClassKey, harborClass, 100))
*/
configStore.Env(name)

commonReconciler, err := chartmuseum.New(ctx, name, configStore)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/harbor/harbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/jobservice/jobservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/notaryserver/notaryserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/notarysigner/notarysigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/portal/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/registryctl/registryctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/goharbor/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className, err := r.GetClassName(ctx)
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
return errors.Wrap(err, "cannot get class name")
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
)

const (
DefaultPriority = 50
DefaultPriority = 5

DefaultConcurrentReconcile = 1
DefaultHarborClass = ""
Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"context"
"fmt"
"strings"

"github.com/goharbor/harbor-operator/pkg/config"
"github.com/ovh/configstore"
"github.com/pkg/errors"
)

func (c *Controller) NormalizeName(ctx context.Context, name string, suffixes ...string) string {
Expand All @@ -15,3 +19,21 @@ func (c *Controller) NormalizeName(ctx context.Context, name string, suffixes ..

return name
}

func (c *Controller) GetClassName(ctx context.Context) (string, error) {
className := ""

configItem, err := configstore.Filter().Store(c.ConfigStore).Slice(config.HarborClassKey).GetFirstItem()
if err != nil {
if !config.IsNotFound(err, config.HarborClassKey) {
return "", errors.Wrap(err, "cannot get config template path")
}
} else {
className, err = configItem.Value()
if err != nil {
return "", errors.Wrap(err, "invalid config template path")
}
}

return className, nil
}
10 changes: 10 additions & 0 deletions pkg/controller/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

goharborv1alpha2 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1alpha2"
"github.com/goharbor/harbor-operator/pkg/controller/mutation"
"github.com/goharbor/harbor-operator/pkg/factories/logger"
"github.com/goharbor/harbor-operator/pkg/factories/owner"
Expand All @@ -30,6 +31,15 @@ const (
func (c *Controller) GlobalMutateFn(ctx context.Context) (resources.Mutable, error) {
var mutate resources.Mutable = mutation.NoOp

className, err := c.GetClassName(ctx)
if err != nil {
return nil, errors.Wrap(err, "cannot get class name")
}

if className != "" {
mutate.AppendMutation(mutation.GetAnnotationsMutation(goharborv1alpha2.HarborClassAnnotation, className))
}

mutate.AppendMutation(mutation.GetAnnotationsMutation(WarningAnnotation, fmt.Sprintf(WarningValueTmpl, c.GetName())))
mutate.AppendMutation(mutation.GetLabelsMutation(OperatorNameLabel, c.GetName(), OperatorVersionLabel, c.GetVersion()))

Expand Down

0 comments on commit 125a567

Please sign in to comment.