Skip to content

Commit

Permalink
Provide deletion flow and proper JSON logging (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Sep 15, 2021
1 parent f7b77f6 commit 438506b
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 109 deletions.
41 changes: 39 additions & 2 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 All @@ -38,6 +39,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
DurosFinalizerName = "storage.metal-stack.io/finalizer"
)

// DurosReconciler reconciles a Duros object
type DurosReconciler struct {
client.Client
Expand Down Expand Up @@ -84,6 +89,29 @@ 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
if containsString(duros.GetFinalizers(), DurosFinalizerName) {
if err := r.cleanupResources(ctx); err != nil {
return requeue, err
}

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

return ctrl.Result{}, nil
}

p, err := r.createProjectIfNotExist(ctx, projectID)
if err != nil {
return requeue, err
Expand All @@ -101,8 +129,8 @@ func (r *DurosReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if err != nil {
return requeue, err
}
// Deploy StorageClass
err = r.deployStorageClass(ctx, projectID, storageClasses)
// Deploy CSI
err = r.deployCSI(ctx, projectID, storageClasses)
if err != nil {
return requeue, err
}
Expand Down Expand Up @@ -206,3 +234,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
}
Loading

0 comments on commit 438506b

Please sign in to comment.