Skip to content

Commit

Permalink
fix(ctrl):update harbor ctrl to fix #275
Browse files Browse the repository at this point in the history
fix #275

Signed-off-by: Steven Zou <szou@vmware.com>
  • Loading branch information
steven-zou authored and bitsf committed Dec 4, 2020
1 parent ef0fe20 commit 57461ef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apis/goharbor.io/v1alpha2/harbor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type HarborSpec struct {
// Skip OpenAPI schema validation
// Use validating webhook to do verification (field required)
// +kubebuilder:validation:Optional
ImageChartStorage HarborStorageImageChartStorageSpec `json:"imageChartStorage"`
ImageChartStorage *HarborStorageImageChartStorageSpec `json:"imageChartStorage"`

// +kubebuilder:validation:Optional
// +kubebuilder:default="info"
Expand Down
6 changes: 5 additions & 1 deletion apis/goharbor.io/v1alpha2/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion controllers/goharbor/harbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func setupValidHarbor(ctx context.Context, ns string) (Resource, client.ObjectKe
ExternalURL: publicURL.String(),
HarborAdminPasswordRef: adminSecretName,
EncryptionKeyRef: "encryption-key",
ImageChartStorage: goharborv1alpha2.HarborStorageImageChartStorageSpec{
ImageChartStorage: &goharborv1alpha2.HarborStorageImageChartStorageSpec{
FileSystem: &goharborv1alpha2.HarborStorageImageChartStorageFileSystemSpec{
RegistryPersistentVolume: goharborv1alpha2.HarborStorageRegistryPersistentVolumeSpec{
HarborStoragePersistentVolumeSpec: goharborv1alpha2.HarborStoragePersistentVolumeSpec{
Expand Down
41 changes: 28 additions & 13 deletions pkg/cluster/controllers/harbor/harbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Controller struct {
Ctx context.Context
Log logr.Logger
Scheme *runtime.Scheme
ComponentToCRStatus sync.Map
ComponentToCRStatus *sync.Map
}

func (harbor *Controller) Apply(ctx context.Context, harborcluster *v1alpha2.HarborCluster) (*lcm.CRStatus, error) {
Expand All @@ -31,11 +31,15 @@ func (harbor *Controller) Apply(ctx context.Context, harborcluster *v1alpha2.Har
// Use the ctx from the parameter
harbor.KubeClient.WithContext(ctx)

err := harbor.KubeClient.Get(harbor.getHarborCRNamespacedName(harborcluster), harborCR)
nsdName := harbor.getHarborCRNamespacedName(harborcluster)

err := harbor.KubeClient.Get(nsdName, harborCR)
if err != nil {
if errors.IsNotFound(err) {
harborCR = harbor.getHarborCR(harborcluster)

harbor.Log.Info("create harbor service", "name", nsdName)

err = harbor.KubeClient.Create(harborCR)
if err != nil {
return harborClusterCRNotReadyStatus(CreateHarborCRError, err.Error()), err
Expand All @@ -45,12 +49,18 @@ func (harbor *Controller) Apply(ctx context.Context, harborcluster *v1alpha2.Har
}
} else {
harborCR = harbor.getHarborCR(harborcluster)

// TODO: maybe we still need to do actual and desired status here to determine if we need to do update
harbor.Log.Info("update harbor service", "name", nsdName)

err = harbor.KubeClient.Update(harborCR)
if err != nil {
return harborClusterCRNotReadyStatus(UpdateHarborCRError, err.Error()), err
}
}

harbor.Log.Info("harbor service is ready", "name", nsdName)

return harborClusterCRStatus(harborCR), nil
}

Expand All @@ -75,10 +85,11 @@ func NewHarborController(ctx context.Context, options ...k8s.Option) *Controller
}

return &Controller{
Ctx: ctx,
KubeClient: o.Client,
Log: o.Log,
Scheme: o.Scheme,
Ctx: ctx,
KubeClient: o.Client,
Log: o.Log,
Scheme: o.Scheme,
ComponentToCRStatus: &sync.Map{},
}
}

Expand All @@ -100,17 +111,21 @@ func (harbor *Controller) getHarborCR(harborcluster *v1alpha2.HarborCluster) *v1
Spec: harborcluster.Spec.HarborSpec,
}

// use incluster spec in first priority
if harborcluster.Spec.InClusterDatabase != nil {
harborcluster.Spec.Database = harbor.getDatabaseSpec()
// Use incluster spec in first priority.
// Check based on the case that if the related dependent services are created
if db := harbor.getDatabaseSpec(); db != nil {
harbor.Log.Info("use incluster database", "database", db.Hosts)
harborCR.Spec.Database = db
}

if harborcluster.Spec.InClusterCache != nil {
harborcluster.Spec.Redis = harbor.getCacheSpec()
if cache := harbor.getCacheSpec(); cache != nil {
harbor.Log.Info("use incluster cache", "cache", cache.Host)
harborCR.Spec.Redis = cache
}

if harborcluster.Spec.InClusterStorage != nil {
harborcluster.Spec.ImageChartStorage = *harbor.getStorageSpec()
if storage := harbor.getStorageSpec(); storage != nil {
harbor.Log.Info("use incluster storage", "storage", storage.S3.RegionEndpoint)
harborCR.Spec.ImageChartStorage = storage
}

return harborCR
Expand Down

0 comments on commit 57461ef

Please sign in to comment.