Skip to content

Commit

Permalink
add finalizers in controller (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelmaliy authored Mar 10, 2022
1 parent 1f4c159 commit 4485049
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
8 changes: 0 additions & 8 deletions api/v1/webhooks/servicebinding_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package webhooks
import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/SAP/sap-btp-service-operator/api"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

servicesv1 "github.com/SAP/sap-btp-service-operator/api/v1"
v1admission "k8s.io/api/admission/v1"
v1 "k8s.io/api/authentication/v1"
Expand All @@ -33,11 +30,6 @@ func (s *ServiceBindingDefaulter) Handle(_ context.Context, req admission.Reques
return admission.Errored(http.StatusBadRequest, err)
}

if binding.DeletionTimestamp.IsZero() && !controllerutil.ContainsFinalizer(binding, api.FinalizerName) {
controllerutil.AddFinalizer(binding, api.FinalizerName)
bindinglog.Info(fmt.Sprintf("added finalizer '%s' to service binding", api.FinalizerName))
}

// mutate the fields
if len(binding.Spec.ExternalName) == 0 {
bindinglog.Info("externalName not provided, defaulting to k8s name", "name", binding.Name)
Expand Down
10 changes: 0 additions & 10 deletions api/v1/webhooks/serviceinstance_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ package webhooks
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"

"github.com/SAP/sap-btp-service-operator/api"

v1admission "k8s.io/api/admission/v1"
v1 "k8s.io/api/authentication/v1"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

servicesv1 "github.com/SAP/sap-btp-service-operator/api/v1"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand All @@ -35,11 +30,6 @@ func (s *ServiceInstanceDefaulter) Handle(_ context.Context, req admission.Reque
return admission.Errored(http.StatusBadRequest, err)
}

if instance.DeletionTimestamp.IsZero() && !controllerutil.ContainsFinalizer(instance, api.FinalizerName) {
controllerutil.AddFinalizer(instance, api.FinalizerName)
instancelog.Info(fmt.Sprintf("added finalizer '%s' to service instance", api.FinalizerName))
}

// mutate the fields
if len(instance.Spec.ExternalName) == 0 {
instancelog.Info("externalName not provided, defaulting to k8s name", "name", instance.Name)
Expand Down
8 changes: 8 additions & 0 deletions controllers/servicebinding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func (r *ServiceBindingReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return r.delete(ctx, smClient, serviceBinding)
}

if !controllerutil.ContainsFinalizer(serviceBinding, api.FinalizerName) {
controllerutil.AddFinalizer(serviceBinding, api.FinalizerName)
log.Info(fmt.Sprintf("added finalizer '%s' to service binding", api.FinalizerName))
if err := r.Update(ctx, serviceBinding); err != nil {
return ctrl.Result{}, err
}
}

if serviceBinding.Status.Ready == metav1.ConditionTrue {
if r.isStaleServiceBinding(serviceBinding) {
return ctrl.Result{}, r.Delete(ctx, serviceBinding)
Expand Down
8 changes: 8 additions & 0 deletions controllers/serviceinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return r.deleteInstance(ctx, smClient, serviceInstance)
}

if !controllerutil.ContainsFinalizer(serviceInstance, api.FinalizerName) {
controllerutil.AddFinalizer(serviceInstance, api.FinalizerName)
log.Info(fmt.Sprintf("added finalizer '%s' to service instance", api.FinalizerName))
if err := r.Update(ctx, serviceInstance); err != nil {
return ctrl.Result{}, err
}
}

if serviceInstance.Generation == serviceInstance.Status.ObservedGeneration && !isInProgress(serviceInstance) {
log.Info(fmt.Sprintf("Spec is not changed - ignoring... Generation is - %v", serviceInstance.Generation))
return ctrl.Result{}, nil
Expand Down

0 comments on commit 4485049

Please sign in to comment.