Skip to content

Commit

Permalink
Merge pull request #416 from fluxcd/add-finalizers
Browse files Browse the repository at this point in the history
Add finalizers to the custom resources
  • Loading branch information
stefanprodan authored Sep 9, 2022
2 parents 8151979 + 071dda5 commit 31008d1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/v1beta1/condition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package v1beta1

const NotificationFinalizer = "finalizers.fluxcd.io"

const (
// InitializedReason represents the fact that a given resource has been initialized.
InitializedReason string = "Initialized"
Expand Down
13 changes: 13 additions & 0 deletions controllers/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
Expand Down Expand Up @@ -154,6 +155,18 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu
r.Metrics.RecordDuration(ctx, alert, start)
}()

if !controllerutil.ContainsFinalizer(alert, v1beta1.NotificationFinalizer) {
controllerutil.AddFinalizer(alert, v1beta1.NotificationFinalizer)
result = ctrl.Result{Requeue: true}
return
}

if !alert.ObjectMeta.DeletionTimestamp.IsZero() {
controllerutil.RemoveFinalizer(alert, v1beta1.NotificationFinalizer)
result = ctrl.Result{}
return
}

return r.reconcile(ctx, alert)
}

Expand Down
13 changes: 13 additions & 0 deletions controllers/provider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -133,6 +134,18 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r

}()

if !controllerutil.ContainsFinalizer(provider, v1beta1.NotificationFinalizer) {
controllerutil.AddFinalizer(provider, v1beta1.NotificationFinalizer)
result = ctrl.Result{Requeue: true}
return
}

if !provider.ObjectMeta.DeletionTimestamp.IsZero() {
controllerutil.RemoveFinalizer(provider, v1beta1.NotificationFinalizer)
result = ctrl.Result{}
return
}

return r.reconcile(ctx, provider)
}

Expand Down
13 changes: 13 additions & 0 deletions controllers/receiver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"

"github.com/fluxcd/pkg/apis/meta"
Expand Down Expand Up @@ -128,6 +129,18 @@ func (r *ReceiverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r

}()

if !controllerutil.ContainsFinalizer(receiver, v1beta1.NotificationFinalizer) {
controllerutil.AddFinalizer(receiver, v1beta1.NotificationFinalizer)
result = ctrl.Result{Requeue: true}
return
}

if !receiver.ObjectMeta.DeletionTimestamp.IsZero() {
controllerutil.RemoveFinalizer(receiver, v1beta1.NotificationFinalizer)
result = ctrl.Result{}
return
}

return r.reconcile(ctx, receiver)
}

Expand Down

0 comments on commit 31008d1

Please sign in to comment.