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

Add the finalizer back. #28

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
37 changes: 37 additions & 0 deletions controllers/duros_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/metal-stack/duros-go"
Expand Down Expand Up @@ -88,6 +89,33 @@ func (r *DurosReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
projectID := duros.Spec.MetalProjectID
storageClasses := duros.Spec.StorageClasses

if duros.ObjectMeta.DeletionTimestamp.IsZero() {
if !containsString(duros.GetFinalizers(), DurosFinalizerName) {
controllerutil.AddFinalizer(duros, DurosFinalizerName)
if err := r.Update(ctx, duros); err != nil {
return requeue, err
}
}
} else {
// object is being deleted
// we don't pass the cancelled context here because then our deletion
// procedure will stop prematurely
deletionCtx := context.Background()

if containsString(duros.GetFinalizers(), DurosFinalizerName) {
if err := r.cleanupResources(deletionCtx); err != nil {
return requeue, err
}

controllerutil.RemoveFinalizer(duros, DurosFinalizerName)
if err := r.Update(deletionCtx, duros); err != nil {
return requeue, err
}
}

return ctrl.Result{}, nil
}

p, err := r.createProjectIfNotExist(ctx, projectID)
if err != nil {
return requeue, err
Expand Down Expand Up @@ -207,3 +235,12 @@ func validateDuros(duros *v1.Duros) error {
}
return nil
}

func containsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
63 changes: 63 additions & 0 deletions controllers/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

storagev1 "github.com/metal-stack/duros-controller/api/v1"
apps "k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policy "k8s.io/api/policy/v1beta1"
rbac "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -1017,6 +1018,68 @@ type deletionResource struct {
Object client.Object
}

func (r *DurosReconciler) cleanupResources(ctx context.Context) error {
log := r.Log.WithName("storage-csi")
log.Info("cleanup csi")

resources := []deletionResource{
{
Key: types.NamespacedName{Name: lbCSINodeName, Namespace: namespace},
Object: &appsv1.DaemonSet{},
},
{
Key: types.NamespacedName{Name: lbCSIControllerName, Namespace: namespace},
Object: &appsv1.StatefulSet{},
},
{
Key: types.NamespacedName{Name: storageClassCredentialsRef, Namespace: namespace},
Object: &corev1.Secret{},
},
}

for i := range clusterRoleBindings {
crb := clusterRoleBindings[i]
resources = append(resources, deletionResource{
Key: types.NamespacedName{Name: crb.Name, Namespace: crb.Namespace},
Object: &rbac.ClusterRoleBinding{},
})
}

for i := range clusterRoles {
cr := clusterRoles[i]
resources = append(resources, deletionResource{
Key: types.NamespacedName{Name: cr.Name, Namespace: cr.Namespace},
Object: &rbac.ClusterRole{},
})
}

for i := range serviceAccounts {
sa := serviceAccounts[i]
resources = append(resources, deletionResource{
Key: types.NamespacedName{Name: sa.Name, Namespace: sa.Namespace},
Object: &corev1.ServiceAccount{},
})
}

for i := range psps {
psp := psps[i]
resources = append(resources, deletionResource{
Key: types.NamespacedName{Name: psp.Name},
Object: &policy.PodSecurityPolicy{},
})
}

// we don't clean up the storage classes and CSI driver because there can be volumes that still reference it

for _, resource := range resources {
if err := r.deleteResourceWithWait(ctx, log, resource); err != nil {
return err
}
}

return nil
}

func (r *DurosReconciler) deleteResourceWithWait(ctx context.Context, log logr.Logger, resource deletionResource) error {
err := r.Shoot.Get(ctx, resource.Key, resource.Object)
if err != nil && apierrors.IsNotFound(err) {
Expand Down