Skip to content

Commit cf77b5e

Browse files
author
Andrea Serrecchia
committed
feat(): allow finalizer cleanup when realm is already deleted
1 parent f5f5e66 commit cf77b5e

File tree

10 files changed

+71
-3
lines changed

10 files changed

+71
-3
lines changed

cmd/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ func enableOwnerRef() bool {
323323
b, err := strconv.ParseBool(val)
324324
if err != nil {
325325
setupLog.Error(err, "unable to parse ENABLE_OWNER_REF. Using default value false")
326-
327326
return false
328327
}
329328

internal/controller/helper/controller_helper.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type ControllerHelper interface {
6565
SetRealmOwnerRef(ctx context.Context, object ObjectWithRealmRef) error
6666
SetFailureCount(fc FailureCountable) time.Duration
6767
TryToDelete(ctx context.Context, obj client.Object, terminator Terminator, finalizer string) (isDeleted bool, resultErr error)
68+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
6869
GetKeycloakRealmFromRef(ctx context.Context, object ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error)
6970
CreateKeycloakClientFromRealmRef(ctx context.Context, object ObjectWithRealmRef) (keycloak.Client, error)
7071
CreateKeycloakClientFromRealm(ctx context.Context, realm *keycloakApi.KeycloakRealm) (keycloak.Client, error)
@@ -248,6 +249,18 @@ func (h *Helper) SetRealmOwnerRef(ctx context.Context, object ObjectWithRealmRef
248249
}
249250
}
250251

252+
func (h *Helper) TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error {
253+
if !obj.GetDeletionTimestamp().IsZero() {
254+
if controllerutil.RemoveFinalizer(obj, finalizer) {
255+
if err := h.client.Update(ctx, obj); err != nil {
256+
return errors.Wrap(err, "unable to update instance")
257+
}
258+
}
259+
}
260+
261+
return nil
262+
}
263+
251264
func (h *Helper) TryToDelete(ctx context.Context, obj client.Object, terminator Terminator, finalizer string) (isDeleted bool, resultErr error) {
252265
logger := ctrl.LoggerFrom(ctx)
253266

internal/controller/helper/controller_helper_auth.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
)
2626

2727
var ErrKeycloakIsNotAvailable = errors.New("keycloak is not available")
28+
var ErrKeycloakRealmNotFound = errors.New("keycloak realm is not available")
2829

2930
// KeycloakAuthData contains data for keycloak authentication.
3031
type KeycloakAuthData struct {
@@ -220,13 +221,21 @@ func (h *Helper) getKeycloakAuthDataFromRealmRef(ctx context.Context, object Obj
220221
case keycloakApi.KeycloakRealmKind:
221222
realm := &keycloakApi.KeycloakRealm{}
222223
if err := h.client.Get(ctx, types.NamespacedName{Name: name, Namespace: object.GetNamespace()}, realm); err != nil {
224+
if k8sErrors.IsNotFound(err) && object.GetDeletionTimestamp() != nil {
225+
return nil, ErrKeycloakRealmNotFound
226+
}
227+
223228
return nil, fmt.Errorf("unable to get realm: %w", err)
224229
}
225230

226231
return h.getKeycloakAuthDataFromRealm(ctx, realm)
227232
case keycloakAlpha.ClusterKeycloakRealmKind:
228233
clusterRealm := &keycloakAlpha.ClusterKeycloakRealm{}
229234
if err := h.client.Get(ctx, types.NamespacedName{Name: name}, clusterRealm); err != nil {
235+
if k8sErrors.IsNotFound(err) && object.GetDeletionTimestamp() != nil {
236+
return nil, ErrKeycloakRealmNotFound
237+
}
238+
230239
return nil, fmt.Errorf("unable to get cluster realm: %w", err)
231240
}
232241

internal/controller/keycloakauthflow/keycloakauthflow_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const finalizerName = "keycloak.authflow.operator.finalizer.name"
2727

2828
type Helper interface {
2929
SetFailureCount(fc helper.FailureCountable) time.Duration
30+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
3031
TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error)
3132
CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error)
3233
SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error
@@ -126,6 +127,12 @@ func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.Keyc
126127

127128
kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, instance)
128129
if err != nil {
130+
if errors.Is(err, helper.ErrKeycloakRealmNotFound) {
131+
return fmt.Errorf("unable to remove finalizer: %w",
132+
r.helper.TryRemoveFinalizer(ctx, instance, finalizerName),
133+
)
134+
}
135+
129136
return fmt.Errorf("unable to create keycloak client from realm ref: %w", err)
130137
}
131138

internal/controller/keycloakclient/keycloakclient_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
type Helper interface {
2626
SetFailureCount(fc helper.FailureCountable) time.Duration
27+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
2728
TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error)
2829
SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error
2930
CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error)
@@ -125,6 +126,12 @@ func (r *ReconcileKeycloakClient) tryReconcile(ctx context.Context, keycloakClie
125126

126127
kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakClient)
127128
if err != nil {
129+
if errors.Is(err, helper.ErrKeycloakRealmNotFound) {
130+
return fmt.Errorf("unable to remove finalizer: %w",
131+
r.helper.TryRemoveFinalizer(ctx, keycloakClient, keyCloakClientOperatorFinalizerName),
132+
)
133+
}
134+
128135
return fmt.Errorf("unable to create keycloak client from realm ref: %w", err)
129136
}
130137

internal/controller/keycloakclientscope/keycloakclientscope_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const finalizerName = "keycloak.clientscope.operator.finalizer.name"
2828

2929
type Helper interface {
3030
SetFailureCount(fc helper.FailureCountable) time.Duration
31+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
3132
TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error)
3233
SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error
3334
GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error)
@@ -131,6 +132,12 @@ func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.Keyc
131132

132133
cl, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, instance)
133134
if err != nil {
135+
if errors.Is(err, helper.ErrKeycloakRealmNotFound) {
136+
return "", fmt.Errorf("unable to remove finalizer: %w",
137+
r.helper.TryRemoveFinalizer(ctx, instance, finalizerName),
138+
)
139+
}
140+
134141
return "", fmt.Errorf("unable to create keycloak client from realm ref: %w", err)
135142
}
136143

internal/controller/keycloakrealmgroup/keycloakrealmgroup_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const keyCloakRealmGroupOperatorFinalizerName = "keycloak.realmgroup.operator.fi
2424

2525
type Helper interface {
2626
SetFailureCount(fc helper.FailureCountable) time.Duration
27+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
2728
TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error)
2829
SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error
2930
GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error)
@@ -115,6 +116,12 @@ func (r *ReconcileKeycloakRealmGroup) tryReconcile(ctx context.Context, keycloak
115116

116117
kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakRealmGroup)
117118
if err != nil {
119+
if errors.Is(err, helper.ErrKeycloakRealmNotFound) {
120+
return fmt.Errorf("unable to remove finalizer: %w",
121+
r.helper.TryRemoveFinalizer(ctx, keycloakRealmGroup, keyCloakRealmGroupOperatorFinalizerName),
122+
)
123+
}
124+
118125
return fmt.Errorf("unable to create keycloak client from realm ref: %w", err)
119126
}
120127

internal/controller/keycloakrealmidentityprovider/keycloakrealmidentityprovider_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const finalizerName = "keycloak.realmidp.operator.finalizer.name"
2828

2929
type Helper interface {
3030
SetFailureCount(fc helper.FailureCountable) time.Duration
31+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
3132
TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error)
3233
SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error
3334
GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error)
@@ -139,6 +140,10 @@ func (r *Reconcile) tryReconcile(ctx context.Context, keycloakRealmIDP *keycloak
139140

140141
kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakRealmIDP)
141142
if err != nil {
143+
if errors.Is(err, helper.ErrKeycloakRealmNotFound) {
144+
return fmt.Errorf("unable to remove finalizer: %w", r.helper.TryRemoveFinalizer(ctx, keycloakRealmIDP, finalizerName))
145+
}
146+
142147
return fmt.Errorf("unable to create keycloak client from realm ref: %w", err)
143148
}
144149

internal/controller/keycloakrealmrole/keycloakrealmrole_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const keyCloakRealmRoleOperatorFinalizerName = "keycloak.realmrole.operator.fina
2727

2828
type Helper interface {
2929
SetFailureCount(fc helper.FailureCountable) time.Duration
30+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
3031
TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error)
3132
SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error
3233
GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error)
@@ -138,6 +139,12 @@ func (r *ReconcileKeycloakRealmRole) tryReconcile(ctx context.Context, keycloakR
138139

139140
kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakRealmRole)
140141
if err != nil {
142+
if errors.Is(err, helper.ErrKeycloakRealmNotFound) {
143+
return "", fmt.Errorf("unable to remove finalizer: %w",
144+
r.helper.TryRemoveFinalizer(ctx, keycloakRealmRole, keyCloakRealmRoleOperatorFinalizerName),
145+
)
146+
}
147+
141148
return "", fmt.Errorf("unable to create keycloak client from realm ref: %w", err)
142149
}
143150

internal/controller/keycloakrealmuser/keycloakrealmuser_controller.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import (
2424
"github.com/epam/edp-keycloak-operator/pkg/objectmeta"
2525
)
2626

27-
const finalizer = "keycloak.realmuser.operator.finalizer.name"
27+
const finalizerName = "keycloak.realmuser.operator.finalizer.name"
2828

2929
type Helper interface {
3030
SetFailureCount(fc helper.FailureCountable) time.Duration
31+
TryRemoveFinalizer(ctx context.Context, obj client.Object, finalizer string) error
3132
TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error)
3233
SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error
3334
GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error)
@@ -117,6 +118,12 @@ func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.Keyc
117118

118119
kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, instance)
119120
if err != nil {
121+
if errors.Is(err, helper.ErrKeycloakRealmNotFound) {
122+
return fmt.Errorf("unable to remove finalizer: %w",
123+
r.helper.TryRemoveFinalizer(ctx, instance, finalizerName),
124+
)
125+
}
126+
120127
return fmt.Errorf("unable to create keycloak client from ref: %w", err)
121128
}
122129

@@ -133,7 +140,7 @@ func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.Keyc
133140
kClient,
134141
objectmeta.PreserveResourcesOnDeletion(instance),
135142
),
136-
finalizer,
143+
finalizerName,
137144
)
138145
if err != nil {
139146
return fmt.Errorf("failed to delete keycloak realm user: %w", err)

0 commit comments

Comments
 (0)