Skip to content

Commit

Permalink
Cherry pick of #668 (#671)
Browse files Browse the repository at this point in the history
* feature(storage):clear minio pvc after HarborCluster is deleted

Signed-off-by: soulseen <zhuxiaoyang1996@gmail.com>

* fix(storage):update rbac about pvc

Signed-off-by: soulseen <zhuxiaoyang1996@gmail.com>

* fix(storage):update rbac resource

Signed-off-by: soulseen <zhuxiaoyang1996@gmail.com>
  • Loading branch information
soulseen committed Jun 7, 2021
1 parent 7d94fcc commit fd7f32c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
12 changes: 12 additions & 0 deletions charts/harbor-operator/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- persistentvolumeclaims
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
Expand Down
1 change: 1 addition & 0 deletions controllers/goharbor/harborcluster/ctrl_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Reconciler struct {
// +kubebuilder:rbac:groups=goharbor.io,resources=harbors,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch
// +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=get;list;watch;create;update;patch;delete

func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
if err := r.ctrl.SetupWithManager(ctx, mgr); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions manifests/cluster/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19921,6 +19921,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- persistentvolumeclaims
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
Expand Down
12 changes: 12 additions & 0 deletions manifests/harbor/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12925,6 +12925,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- persistentvolumeclaims
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
Expand Down
43 changes: 43 additions & 0 deletions pkg/cluster/controllers/storage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -70,6 +71,10 @@ func (m *MinIOController) Apply(ctx context.Context, harborcluster *goharborv1.H
return crs, err
}

// Set ownerReferences to MinIO PVS
if err := m.SetOwnerReferencesWithPVC(ctx, harborcluster); err != nil {
return minioUnknownStatus(), err
}
// Check readiness
mt, tenantReady, err := m.checkMinIOReady(ctx, harborcluster)
if err != nil {
Expand Down Expand Up @@ -113,6 +118,44 @@ func (m *MinIOController) Apply(ctx context.Context, harborcluster *goharborv1.H
return crs, nil
}

func (m *MinIOController) SetOwnerReferencesWithPVC(ctx context.Context, harborcluster *goharborv1.HarborCluster) (err error) {
// Get the existing minIO CR first
minioCR := &miniov2.Tenant{}
if err = m.KubeClient.Get(ctx, m.getMinIONamespacedName(harborcluster), minioCR); err != nil {
return err
}

minioPVCs := &corev1.PersistentVolumeClaimList{}
label := map[string]string{
"v1.min.io/tenant": m.getServiceName(harborcluster),
}

opts := &client.ListOptions{}
labelSelector := labels.SelectorFromSet(label)
opts.LabelSelector = labelSelector

if err = m.KubeClient.List(ctx, minioPVCs, opts); err != nil {
if errors.IsNotFound(err) {
return nil
}

return err
}

for i := range minioPVCs.Items {
minioPVCs.Items[i].OwnerReferences = []metav1.OwnerReference{
*metav1.NewControllerRef(minioCR, HarborClusterMinIOGVK),
}

err = m.KubeClient.Update(ctx, &minioPVCs.Items[i])
if err != nil {
m.Log.Error(err, "set pvc:", minioPVCs.Items[i].Name, "OwnerReferences error")
}
}

return err
}

func (m *MinIOController) Delete(ctx context.Context, harborcluster *goharborv1.HarborCluster) (*lcm.CRStatus, error) {
minioCR, err := m.generateMinIOCR(ctx, harborcluster)
if err != nil {
Expand Down

0 comments on commit fd7f32c

Please sign in to comment.