Skip to content

Commit c6ac24d

Browse files
zmotsoSergK
authored andcommitted
feat: Make ownerReference in Keycloak resources optional (#71)
1 parent 0ca2493 commit c6ac24d

File tree

8 files changed

+487
-73
lines changed

8 files changed

+487
-73
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ To prevent the operator from deleting resources from Keycloak, add the `edp.epam
124124
kind: Keycloak
125125
```
126126

127+
#### Resources deletion
128+
129+
To avoid resources getting stuck during deletion, it is important to delete them in the correct order:
130+
131+
1. **First**, remove realm resources `KeycloakClient`, `KeycloakRealmUser`, etc.
132+
2. **Then**, remove `KeycloakRealm`/`ClusterKeycloakRealm`.
133+
3. **Finally**, remove `Keycloak`/`ClusterKeycloak`.
134+
127135
## Local Development
128136

129137
To develop the operator, first set up a local environment, and refer to the [Local Development](https://docs.kuberocketci.io/docs/developer-guide/local-development) page.

cmd/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"strconv"
89
"strings"
910
"time"
1011

@@ -184,7 +185,7 @@ func main() {
184185
os.Exit(1)
185186
}
186187

187-
h := helper.MakeHelper(mgr.GetClient(), mgr.GetScheme(), operatorNamespace)
188+
h := helper.MakeHelper(mgr.GetClient(), mgr.GetScheme(), operatorNamespace, helper.EnableOwnerRef(enableOwnerRef()))
188189

189190
keycloakCtrl := keycloak.NewReconcileKeycloak(mgr.GetClient(), mgr.GetScheme(), h)
190191
if err = keycloakCtrl.SetupWithManager(mgr, successReconcileTimeoutValue); err != nil {
@@ -312,3 +313,19 @@ func getOperatorNamespace() (string, error) {
312313

313314
return ns, nil
314315
}
316+
317+
func enableOwnerRef() bool {
318+
val, exists := os.LookupEnv("ENABLE_OWNER_REF")
319+
if !exists {
320+
return false
321+
}
322+
323+
b, err := strconv.ParseBool(val)
324+
if err != nil {
325+
setupLog.Error(err, "unable to parse ENABLE_OWNER_REF. Using default value false")
326+
327+
return false
328+
}
329+
330+
return b
331+
}

deploy-templates/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Development versions are also available from the [snapshot helm chart repository
131131
| annotations | object | `{}` | Annotations to be added to the Deployment |
132132
| clusterReconciliationEnabled | bool | `false` | If clusterReconciliationEnabled is true, the operator reconciles all Keycloak instances in the cluster; otherwise, it only reconciles instances in the same namespace by default, and cluster-scoped resources are ignored. |
133133
| containerSecurityContext | object | `{"allowPrivilegeEscalation":false}` | Container Security Context Ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ |
134+
| enableOwnerRef | bool | `true` | If set to true, the operator will set the owner reference for all resources that have Keycloak or KeycloakRealm as reference. This is legacy behavior and not recommended for use. In the future, this will be set to false by default. |
134135
| extraVolumeMounts | list | `[]` | Additional volumeMounts to be added to the container |
135136
| extraVolumes | list | `[]` | Additional volumes to be added to the pod |
136137
| image.repository | string | `"epamedp/keycloak-operator"` | KubeRocketCI keycloak-operator Docker image name. The released image can be found on [Dockerhub](https://hub.docker.com/r/epamedp/keycloak-operator) |

deploy-templates/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ spec:
5353
valueFrom:
5454
fieldRef:
5555
fieldPath: metadata.name
56+
- name: ENABLE_OWNER_REF
57+
value: {{ .Values.enableOwnerRef | quote }}
5658
{{- if .Values.extraVolumeMounts }}
5759
volumeMounts:
5860
{{- if .Values.extraVolumeMounts }}

deploy-templates/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ extraVolumeMounts: []
5454
# -- If clusterReconciliationEnabled is true, the operator reconciles all Keycloak instances in the cluster;
5555
# otherwise, it only reconciles instances in the same namespace by default, and cluster-scoped resources are ignored.
5656
clusterReconciliationEnabled: false
57+
58+
# -- If set to true, the operator will set the owner reference for all resources that have Keycloak or KeycloakRealm as reference.
59+
# This is legacy behavior and not recommended for use. In the future, this will be set to false by default.
60+
enableOwnerRef: true

internal/controller/helper/controller_helper.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ type Helper struct {
8181
adapterBuilder adapterBuilder
8282
tokenSecretLock *sync.Mutex
8383
operatorNamespace string
84+
// enableOwnerRef is a flag to enable legacy owner reference to Keycloak and KeycloakRealm for operator objects.
85+
// This is needed for backward compatibility with the old version of the operator.
86+
enableOwnerRef bool
8487
}
8588

86-
func MakeHelper(client client.Client, scheme *runtime.Scheme, operatorNamespace string) *Helper {
87-
return &Helper{
89+
func MakeHelper(client client.Client, scheme *runtime.Scheme, operatorNamespace string, options ...func(*Helper)) *Helper {
90+
helper := &Helper{
8891
tokenSecretLock: new(sync.Mutex),
8992
client: client,
9093
scheme: scheme,
9194
operatorNamespace: operatorNamespace,
95+
enableOwnerRef: false,
9296
adapterBuilder: func(
9397
ctx context.Context,
9498
conf adapter.GoCloakConfig,
@@ -99,7 +103,7 @@ func MakeHelper(client client.Client, scheme *runtime.Scheme, operatorNamespace
99103
if adminType == keycloakApi.KeycloakAdminTypeServiceAccount {
100104
goKeycloakAdapter, err := adapter.MakeFromServiceAccount(ctx, conf, "master", log, restyClient)
101105
if err != nil {
102-
return nil, fmt.Errorf("failed to make go keycloak adapter from seviceaccount: %w", err)
106+
return nil, fmt.Errorf("failed to make go keycloak adapter from service account: %w", err)
103107
}
104108

105109
return goKeycloakAdapter, nil
@@ -113,12 +117,29 @@ func MakeHelper(client client.Client, scheme *runtime.Scheme, operatorNamespace
113117
return goKeycloakAdapter, nil
114118
},
115119
}
120+
121+
for _, option := range options {
122+
option(helper)
123+
}
124+
125+
return helper
126+
}
127+
128+
// EnableOwnerRef is an option to set the enableOwnerRef field in Helper.
129+
func EnableOwnerRef(setOwnerRef bool) func(*Helper) {
130+
return func(h *Helper) {
131+
h.enableOwnerRef = setOwnerRef
132+
}
116133
}
117134

118135
// SetKeycloakOwnerRef sets owner reference for object.
119136
//
120137
//nolint:dupl,cyclop
121138
func (h *Helper) SetKeycloakOwnerRef(ctx context.Context, object ObjectWithKeycloakRef) error {
139+
if !h.enableOwnerRef {
140+
return nil
141+
}
142+
122143
if metav1.GetControllerOf(object) != nil {
123144
return nil
124145
}
@@ -173,6 +194,10 @@ func (h *Helper) SetKeycloakOwnerRef(ctx context.Context, object ObjectWithKeycl
173194
//
174195
//nolint:dupl,cyclop
175196
func (h *Helper) SetRealmOwnerRef(ctx context.Context, object ObjectWithRealmRef) error {
197+
if !h.enableOwnerRef {
198+
return nil
199+
}
200+
176201
if metav1.GetControllerOf(object) != nil {
177202
return nil
178203
}

0 commit comments

Comments
 (0)