diff --git a/README.md b/README.md index a2f2aa12..880dc229 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,14 @@ To prevent the operator from deleting resources from Keycloak, add the `edp.epam kind: Keycloak ``` +#### Resources deletion + +To avoid resources getting stuck during deletion, it is important to delete them in the correct order: + +1. **First**, remove realm resources `KeycloakClient`, `KeycloakRealmUser`, etc. +2. **Then**, remove `KeycloakRealm`/`ClusterKeycloakRealm`. +3. **Finally**, remove `Keycloak`/`ClusterKeycloak`. + ## Local Development To develop the operator, first set up a local environment, and refer to the [Local Development](https://epam.github.io/edp-install/developer-guide/local-development/) page. diff --git a/controllers/clusterkeycloakrealm/clusterkeycloakrealm_controller.go b/controllers/clusterkeycloakrealm/clusterkeycloakrealm_controller.go index e162baa6..e42ce282 100644 --- a/controllers/clusterkeycloakrealm/clusterkeycloakrealm_controller.go +++ b/controllers/clusterkeycloakrealm/clusterkeycloakrealm_controller.go @@ -23,7 +23,6 @@ type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) CreateKeycloakClientFromClusterRealm(ctx context.Context, realm *keycloakAlpha.ClusterKeycloakRealm) (keycloak.Client, error) - SetKeycloakOwnerRef(ctx context.Context, object helper.ObjectWithKeycloakRef) error InvalidateKeycloakClientTokenSecret(ctx context.Context, namespace, rootKeycloakName string) error } @@ -61,10 +60,6 @@ func (r *ClusterKeycloakRealmReconciler) Reconcile(ctx context.Context, req ctrl return ctrl.Result{}, fmt.Errorf("unable to get cluster realm: %w", err) } - if err := r.helper.SetKeycloakOwnerRef(ctx, clusterRealm); err != nil { - return ctrl.Result{}, fmt.Errorf("unable to set keycloak owner ref: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromClusterRealm(ctx, clusterRealm) if err != nil { if errors.Is(err, helper.ErrKeycloakIsNotAvailable) { diff --git a/controllers/helper/controller_helper.go b/controllers/helper/controller_helper.go index 3312f5d8..eaa81d97 100644 --- a/controllers/helper/controller_helper.go +++ b/controllers/helper/controller_helper.go @@ -10,7 +10,6 @@ import ( "github.com/go-logr/logr" "github.com/go-resty/resty/v2" "github.com/pkg/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -61,8 +60,6 @@ type adapterBuilder func( // //go:generate mockery --name ControllerHelper --filename helper_mock.go type ControllerHelper interface { - SetKeycloakOwnerRef(ctx context.Context, object ObjectWithKeycloakRef) error - SetRealmOwnerRef(ctx context.Context, object ObjectWithRealmRef) error SetFailureCount(fc FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator Terminator, finalizer string) (isDeleted bool, resultErr error) GetKeycloakRealmFromRef(ctx context.Context, object ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) @@ -115,114 +112,6 @@ func MakeHelper(client client.Client, scheme *runtime.Scheme, operatorNamespace } } -// SetKeycloakOwnerRef sets owner reference for object. -// -//nolint:dupl,cyclop -func (h *Helper) SetKeycloakOwnerRef(ctx context.Context, object ObjectWithKeycloakRef) error { - if metav1.GetControllerOf(object) != nil { - return nil - } - - kind := object.GetKeycloakRef().Kind - name := object.GetKeycloakRef().Name - - switch kind { - case keycloakApi.KeycloakKind: - kc := &keycloakApi.Keycloak{} - if err := h.client.Get(ctx, types.NamespacedName{ - Namespace: object.GetNamespace(), - Name: name, - }, kc); err != nil { - return fmt.Errorf("failed to get Keycloak: %w", err) - } - - if err := controllerutil.SetControllerReference(kc, object, h.scheme); err != nil { - return fmt.Errorf("failed to set controller reference for %s: %w", object.GetName(), err) - } - - if err := h.client.Update(ctx, object); err != nil { - return fmt.Errorf("failed to update keycloak owner reference %s: %w", kc.GetName(), err) - } - - return nil - - case keycloakAlpha.ClusterKeycloakKind: - clusterKc := &keycloakAlpha.ClusterKeycloak{} - if err := h.client.Get(ctx, types.NamespacedName{ - Name: name, - }, clusterKc); err != nil { - return fmt.Errorf("failed to get ClusterKeycloak: %w", err) - } - - if err := controllerutil.SetControllerReference(clusterKc, object, h.scheme); err != nil { - return fmt.Errorf("failed to set controller reference for %s: %w", object.GetName(), err) - } - - if err := h.client.Update(ctx, object); err != nil { - return fmt.Errorf("failed to update keycloak owner reference %s: %w", clusterKc.GetName(), err) - } - - return nil - - default: - return fmt.Errorf("unknown keycloak kind: %s", kind) - } -} - -// SetRealmOwnerRef sets owner reference for object. -// -//nolint:dupl,cyclop -func (h *Helper) SetRealmOwnerRef(ctx context.Context, object ObjectWithRealmRef) error { - if metav1.GetControllerOf(object) != nil { - return nil - } - - kind := object.GetRealmRef().Kind - name := object.GetRealmRef().Name - - switch kind { - case keycloakApi.KeycloakRealmKind: - realm := &keycloakApi.KeycloakRealm{} - if err := h.client.Get(ctx, types.NamespacedName{ - Namespace: object.GetNamespace(), - Name: name, - }, realm); err != nil { - return fmt.Errorf("failed to get KeycloakRealm: %w", err) - } - - if err := controllerutil.SetControllerReference(realm, object, h.scheme); err != nil { - return fmt.Errorf("failed to set controller reference for %s: %w", object.GetName(), err) - } - - if err := h.client.Update(ctx, object); err != nil { - return fmt.Errorf("failed to update realm owner reference %s: %w", realm.GetName(), err) - } - - return nil - - case keycloakAlpha.ClusterKeycloakRealmKind: - clusterRealm := &keycloakAlpha.ClusterKeycloakRealm{} - if err := h.client.Get(ctx, types.NamespacedName{ - Name: name, - }, clusterRealm); err != nil { - return fmt.Errorf("failed to get ClusterKeycloakRealm: %w", err) - } - - if err := controllerutil.SetControllerReference(clusterRealm, object, h.scheme); err != nil { - return fmt.Errorf("unable to set controller reference for %s: %w", object.GetName(), err) - } - - if err := h.client.Update(ctx, object); err != nil { - return fmt.Errorf("failed to update realm owner reference %s: %w", clusterRealm.GetName(), err) - } - - return nil - - default: - return fmt.Errorf("unknown realm kind: %s", kind) - } -} - func (h *Helper) TryToDelete(ctx context.Context, obj client.Object, terminator Terminator, finalizer string) (isDeleted bool, resultErr error) { logger := ctrl.LoggerFrom(ctx) diff --git a/controllers/helper/controller_helper_test.go b/controllers/helper/controller_helper_test.go index 182fd017..c1bf1c88 100644 --- a/controllers/helper/controller_helper_test.go +++ b/controllers/helper/controller_helper_test.go @@ -8,139 +8,17 @@ import ( "github.com/go-logr/logr" "github.com/go-resty/resty/v2" "github.com/pkg/errors" - "github.com/stretchr/testify/assert" - testifymock "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "github.com/epam/edp-keycloak-operator/api/common" keycloakApi "github.com/epam/edp-keycloak-operator/api/v1" "github.com/epam/edp-keycloak-operator/pkg/client/keycloak/adapter" "github.com/epam/edp-keycloak-operator/pkg/client/keycloak/mock" "github.com/epam/edp-keycloak-operator/pkg/fakehttp" ) -func TestHelper_GetOrCreateRealmOwnerRef(t *testing.T) { - mc := K8SClientMock{} - - sch := runtime.NewScheme() - utilruntime.Must(keycloakApi.AddToScheme(sch)) - - helper := MakeHelper(&mc, sch, "default") - - kcGroup := keycloakApi.KeycloakRealmGroup{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", - }, - Spec: keycloakApi.KeycloakRealmGroupSpec{ - RealmRef: common.RealmRef{ - Kind: keycloakApi.KeycloakRealmKind, - Name: "realm", - }, - }, - } - - mc.On("Get", types.NamespacedName{ - Namespace: "test", - Name: "realm", - }, &keycloakApi.KeycloakRealm{}).Return(nil) - mc.On("Update", testifymock.Anything, testifymock.Anything).Return(nil) - - err := helper.SetRealmOwnerRef(context.Background(), &kcGroup) - require.NoError(t, err) - - kcGroup = keycloakApi.KeycloakRealmGroup{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", - }, - Spec: keycloakApi.KeycloakRealmGroupSpec{ - Realm: "foo13", - RealmRef: common.RealmRef{ - Kind: keycloakApi.KeycloakRealmKind, - Name: "realm", - }, - }, - } - - mc.On("Get", types.NamespacedName{ - Namespace: "test", - Name: "foo13", - }, &keycloakApi.KeycloakRealm{}).Return(nil) - - err = helper.SetRealmOwnerRef(context.Background(), &kcGroup) - require.NoError(t, err) -} - -func TestHelper_GetOrCreateRealmOwnerRef_Failure(t *testing.T) { - mc := K8SClientMock{} - - sch := runtime.NewScheme() - utilruntime.Must(keycloakApi.AddToScheme(sch)) - - helper := MakeHelper(&mc, sch, "default") - - kcGroup := keycloakApi.KeycloakRealmGroup{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", - OwnerReferences: []metav1.OwnerReference{ - { - Name: "foo", - Kind: "KeycloakRealm", - }, - }, - }, - Spec: keycloakApi.KeycloakRealmGroupSpec{ - RealmRef: common.RealmRef{ - Kind: keycloakApi.KeycloakRealmKind, - Name: "realm", - }, - }, - } - - mockErr := errors.New("mock error") - - mc.On("Get", types.NamespacedName{ - Namespace: "test", - Name: kcGroup.Spec.RealmRef.Name, - }, &keycloakApi.KeycloakRealm{}).Return(mockErr) - - err := helper.SetRealmOwnerRef(context.Background(), &kcGroup) - if err == nil { - t.Fatal("no error on k8s client get fatal") - } - - assert.ErrorIs(t, err, mockErr) - - kcGroup = keycloakApi.KeycloakRealmGroup{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", - }, - Spec: keycloakApi.KeycloakRealmGroupSpec{ - RealmRef: common.RealmRef{ - Kind: keycloakApi.KeycloakRealmKind, - Name: "realm", - }, - }, - } - - mc.On("Get", types.NamespacedName{ - Namespace: "test", - Name: kcGroup.Spec.RealmRef.Name, - }, &keycloakApi.KeycloakRealm{}).Return(mockErr) - - err = helper.SetRealmOwnerRef(context.Background(), &kcGroup) - if err == nil { - t.Fatal("no error on k8s client get fatal") - } - - assert.ErrorIs(t, err, mockErr) -} - func TestMakeHelper(t *testing.T) { rCl := resty.New() diff --git a/controllers/keycloakauthflow/keycloakauthflow_controller.go b/controllers/keycloakauthflow/keycloakauthflow_controller.go index 59a05246..8e5d7a48 100644 --- a/controllers/keycloakauthflow/keycloakauthflow_controller.go +++ b/controllers/keycloakauthflow/keycloakauthflow_controller.go @@ -30,7 +30,6 @@ type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) } @@ -127,10 +126,6 @@ func (r *Reconcile) Reconcile(ctx context.Context, request reconcile.Request) (r } func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.KeycloakAuthFlow) error { - if err := r.helper.SetRealmOwnerRef(ctx, instance); err != nil { - return fmt.Errorf("unable to set realm owner ref: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, instance) if err != nil { return fmt.Errorf("unable to create keycloak client from realm ref: %w", err) diff --git a/controllers/keycloakclient/keycloakclient_controller.go b/controllers/keycloakclient/keycloakclient_controller.go index 1a288344..668ab67e 100644 --- a/controllers/keycloakclient/keycloakclient_controller.go +++ b/controllers/keycloakclient/keycloakclient_controller.go @@ -26,7 +26,6 @@ import ( type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) } @@ -118,11 +117,6 @@ func (r *ReconcileKeycloakClient) Reconcile(ctx context.Context, request reconci } func (r *ReconcileKeycloakClient) tryReconcile(ctx context.Context, keycloakClient *keycloakApi.KeycloakClient) error { - err := r.helper.SetRealmOwnerRef(ctx, keycloakClient) - if err != nil { - return fmt.Errorf("unable to set realm owner ref: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakClient) if err != nil { return fmt.Errorf("unable to create keycloak client from realm ref: %w", err) diff --git a/controllers/keycloakclientscope/keycloakclientscope_controller.go b/controllers/keycloakclientscope/keycloakclientscope_controller.go index 55e4030d..1a3e7390 100644 --- a/controllers/keycloakclientscope/keycloakclientscope_controller.go +++ b/controllers/keycloakclientscope/keycloakclientscope_controller.go @@ -30,7 +30,6 @@ const finalizerName = "keycloak.clientscope.operator.finalizer.name" type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) } @@ -131,11 +130,6 @@ func (r *Reconcile) Reconcile(ctx context.Context, request reconcile.Request) (r } func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.KeycloakClientScope) (string, error) { - err := r.helper.SetRealmOwnerRef(ctx, instance) - if err != nil { - return "", fmt.Errorf("unable to set realm owner ref: %w", err) - } - cl, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, instance) if err != nil { return "", fmt.Errorf("unable to create keycloak client from realm ref: %w", err) diff --git a/controllers/keycloakrealm/keycloakrealm_controller.go b/controllers/keycloakrealm/keycloakrealm_controller.go index 2ef1b4b1..c3679e8b 100644 --- a/controllers/keycloakrealm/keycloakrealm_controller.go +++ b/controllers/keycloakrealm/keycloakrealm_controller.go @@ -29,7 +29,6 @@ type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) CreateKeycloakClientFromRealm(ctx context.Context, realm *keycloakApi.KeycloakRealm) (keycloak.Client, error) - SetKeycloakOwnerRef(ctx context.Context, object helper.ObjectWithKeycloakRef) error InvalidateKeycloakClientTokenSecret(ctx context.Context, namespace, rootKeycloakName string) error } @@ -123,10 +122,6 @@ func (r *ReconcileKeycloakRealm) Reconcile(ctx context.Context, request reconcil } func (r *ReconcileKeycloakRealm) tryReconcile(ctx context.Context, realm *keycloakApi.KeycloakRealm) error { - if err := r.helper.SetKeycloakOwnerRef(ctx, realm); err != nil { - return fmt.Errorf("failed to set keycloak owner reference: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromRealm(ctx, realm) if err != nil { return fmt.Errorf("failed to create keycloak client for realm: %w", err) diff --git a/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller.go b/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller.go index c228073f..830ad87c 100644 --- a/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller.go +++ b/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller.go @@ -9,15 +9,11 @@ import ( "github.com/Nerzal/gocloak/v12" k8sErrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - "k8s.io/utils/pointer" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -35,7 +31,6 @@ const finalizerName = "keycloak.realmcomponent.operator.finalizer.name" type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) } @@ -119,15 +114,6 @@ func (r *Reconcile) Reconcile(ctx context.Context, request reconcile.Request) (r return ctrl.Result{}, nil } - err := r.helper.SetRealmOwnerRef(ctx, keycloakRealmComponent) - if err != nil { - return ctrl.Result{}, fmt.Errorf("unable to get realm owner ref: %w", err) - } - - if err = r.setComponentOwnerReference(ctx, keycloakRealmComponent); err != nil { - return reconcile.Result{}, err - } - kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakRealmComponent) if err != nil { if errors.Is(err, helper.ErrKeycloakIsNotAvailable) { @@ -291,52 +277,6 @@ func (r *Reconcile) getParentID( return "", fmt.Errorf("parent kind %s is not supported", component.Spec.ParentRef.Kind) } -// setComponentOwnerReference sets the owner reference for the component. -// In case the component has a parent component, we need to set owner reference to it -// to trigger the deletion of the child KeycloakRealmComponent. -// In the keycloak API side child component is automatically deleted, -// so we need to do the same with the KeycloakRealmComponent resource. -func (r *Reconcile) setComponentOwnerReference( - ctx context.Context, - component *keycloakApi.KeycloakRealmComponent, -) error { - if component.Spec.ParentRef == nil || component.Spec.ParentRef.Kind != keycloakApi.KeycloakRealmComponentKind { - return nil - } - - for _, ref := range component.GetOwnerReferences() { - if ref.Kind == keycloakApi.KeycloakRealmComponentKind { - return nil - } - } - - parentComponent := &keycloakApi.KeycloakRealmComponent{} - if err := r.client.Get(ctx, types.NamespacedName{Name: component.Spec.ParentRef.Name, Namespace: component.GetNamespace()}, parentComponent); err != nil { - return fmt.Errorf("unable to get parent component: %w", err) - } - - gvk, err := apiutil.GVKForObject(parentComponent, r.scheme) - if err != nil { - return fmt.Errorf("unable to get gvk for parent component: %w", err) - } - - ref := metav1.OwnerReference{ - APIVersion: gvk.GroupVersion().String(), - Kind: gvk.Kind, - Name: parentComponent.GetName(), - UID: parentComponent.GetUID(), - BlockOwnerDeletion: pointer.Bool(true), - Controller: pointer.Bool(true), - } - component.SetOwnerReferences([]v1.OwnerReference{ref}) - - if err := r.client.Update(ctx, component); err != nil { - return fmt.Errorf("failed to set owner reference %s: %w", parentComponent.Name, err) - } - - return nil -} - func (r *Reconcile) applyDefaults(ctx context.Context, instance *keycloakApi.KeycloakRealmComponent) (bool, error) { if instance.Spec.RealmRef.Name == "" { instance.Spec.RealmRef = common.RealmRef{ diff --git a/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller_test.go b/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller_test.go index 18da6603..771ba2f7 100644 --- a/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller_test.go +++ b/controllers/keycloakrealmcomponent/keycloakrealmcomponent_controller_test.go @@ -62,7 +62,6 @@ func TestReconcile_Reconcile(t *testing.T) { client := fake.NewClientBuilder().WithScheme(sch).WithRuntimeObjects(&comp).Build() h := helpermock.NewControllerHelper(t) - h.On("SetRealmOwnerRef", testifymock.Anything, testifymock.Anything).Return(nil) h.On("CreateKeycloakClientFromRealmRef", testifymock.Anything, testifymock.Anything).Return(kcAdapter, nil) h.On("TryToDelete", testifymock.Anything, testifymock.Anything, testifymock.Anything, testifymock.Anything). Return(false, nil) diff --git a/controllers/keycloakrealmgroup/keycloakrealmgroup_controller.go b/controllers/keycloakrealmgroup/keycloakrealmgroup_controller.go index 5fd4584a..c757ce40 100644 --- a/controllers/keycloakrealmgroup/keycloakrealmgroup_controller.go +++ b/controllers/keycloakrealmgroup/keycloakrealmgroup_controller.go @@ -26,7 +26,6 @@ const keyCloakRealmGroupOperatorFinalizerName = "keycloak.realmgroup.operator.fi type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) } @@ -115,11 +114,6 @@ func (r *ReconcileKeycloakRealmGroup) Reconcile(ctx context.Context, request rec } func (r *ReconcileKeycloakRealmGroup) tryReconcile(ctx context.Context, keycloakRealmGroup *keycloakApi.KeycloakRealmGroup) error { - err := r.helper.SetRealmOwnerRef(ctx, keycloakRealmGroup) - if err != nil { - return fmt.Errorf("unable to set realm owner ref: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakRealmGroup) if err != nil { return fmt.Errorf("unable to create keycloak client from realm ref: %w", err) diff --git a/controllers/keycloakrealmgroup/keycloakrealmgroup_controller_test.go b/controllers/keycloakrealmgroup/keycloakrealmgroup_controller_test.go index 1f2afcc1..cb030f5d 100644 --- a/controllers/keycloakrealmgroup/keycloakrealmgroup_controller_test.go +++ b/controllers/keycloakrealmgroup/keycloakrealmgroup_controller_test.go @@ -58,7 +58,6 @@ func TestReconcileKeycloakRealmGroup_Reconcile(t *testing.T) { h := helpermock.NewControllerHelper(t) kcMock := mocks.NewMockClient(t) - h.On("SetRealmOwnerRef", testifymock.Anything, testifymock.Anything).Return(nil) h.On("CreateKeycloakClientFromRealmRef", testifymock.Anything, testifymock.Anything).Return(kcMock, nil) h.On("GetKeycloakRealmFromRef", testifymock.Anything, testifymock.Anything, testifymock.Anything). Return(&gocloak.RealmRepresentation{ diff --git a/controllers/keycloakrealmidentityprovider/keycloakrealmidentityprovider_controller.go b/controllers/keycloakrealmidentityprovider/keycloakrealmidentityprovider_controller.go index 87c541da..802b7531 100644 --- a/controllers/keycloakrealmidentityprovider/keycloakrealmidentityprovider_controller.go +++ b/controllers/keycloakrealmidentityprovider/keycloakrealmidentityprovider_controller.go @@ -30,7 +30,6 @@ const finalizerName = "keycloak.realmidp.operator.finalizer.name" type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) } @@ -139,11 +138,6 @@ func (r *Reconcile) Reconcile(ctx context.Context, request reconcile.Request) (r } func (r *Reconcile) tryReconcile(ctx context.Context, keycloakRealmIDP *keycloakApi.KeycloakRealmIdentityProvider) error { - err := r.helper.SetRealmOwnerRef(ctx, keycloakRealmIDP) - if err != nil { - return fmt.Errorf("unable to set realm owner ref: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakRealmIDP) if err != nil { return fmt.Errorf("unable to create keycloak client from realm ref: %w", err) diff --git a/controllers/keycloakrealmrole/keycloakrealmrole_controller.go b/controllers/keycloakrealmrole/keycloakrealmrole_controller.go index de8c95c0..1dc45b97 100644 --- a/controllers/keycloakrealmrole/keycloakrealmrole_controller.go +++ b/controllers/keycloakrealmrole/keycloakrealmrole_controller.go @@ -29,7 +29,6 @@ const keyCloakRealmRoleOperatorFinalizerName = "keycloak.realmrole.operator.fina type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) } @@ -139,11 +138,6 @@ func (r *ReconcileKeycloakRealmRole) Reconcile(ctx context.Context, request reco } func (r *ReconcileKeycloakRealmRole) tryReconcile(ctx context.Context, keycloakRealmRole *keycloakApi.KeycloakRealmRole) (string, error) { - err := r.helper.SetRealmOwnerRef(ctx, keycloakRealmRole) - if err != nil { - return "", fmt.Errorf("unable to set realm owner ref: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, keycloakRealmRole) if err != nil { return "", fmt.Errorf("unable to create keycloak client from realm ref: %w", err) diff --git a/controllers/keycloakrealmrolebatch/keycloakrealmrolebatch_controller.go b/controllers/keycloakrealmrolebatch/keycloakrealmrolebatch_controller.go index 582eb4b0..54e6b21d 100644 --- a/controllers/keycloakrealmrolebatch/keycloakrealmrolebatch_controller.go +++ b/controllers/keycloakrealmrolebatch/keycloakrealmrolebatch_controller.go @@ -27,7 +27,6 @@ const keyCloakRealmRoleBatchOperatorFinalizerName = "keycloak.realmrolebatch.ope type Helper interface { TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error SetFailureCount(fc helper.FailureCountable) time.Duration } @@ -201,11 +200,6 @@ func (r *ReconcileKeycloakRealmRoleBatch) putRoles( } func (r *ReconcileKeycloakRealmRoleBatch) tryReconcile(ctx context.Context, batch *keycloakApi.KeycloakRealmRoleBatch) error { - err := r.helper.SetRealmOwnerRef(ctx, batch) - if err != nil { - return fmt.Errorf("unable to set realm owner ref: %w", err) - } - createdRoles, err := r.putRoles(ctx, batch) if err != nil { return errors.Wrap(err, "unable to put roles batch") diff --git a/controllers/keycloakrealmuser/keycloakrealmuser_controller.go b/controllers/keycloakrealmuser/keycloakrealmuser_controller.go index d944ca7d..ad678b3a 100644 --- a/controllers/keycloakrealmuser/keycloakrealmuser_controller.go +++ b/controllers/keycloakrealmuser/keycloakrealmuser_controller.go @@ -30,7 +30,6 @@ const finalizer = "keycloak.realmuser.operator.finalizer.name" type Helper interface { SetFailureCount(fc helper.FailureCountable) time.Duration TryToDelete(ctx context.Context, obj client.Object, terminator helper.Terminator, finalizer string) (isDeleted bool, resultErr error) - SetRealmOwnerRef(ctx context.Context, object helper.ObjectWithRealmRef) error GetKeycloakRealmFromRef(ctx context.Context, object helper.ObjectWithRealmRef, kcClient keycloak.Client) (*gocloak.RealmRepresentation, error) CreateKeycloakClientFromRealmRef(ctx context.Context, object helper.ObjectWithRealmRef) (keycloak.Client, error) } @@ -117,11 +116,6 @@ func (r *Reconcile) Reconcile(ctx context.Context, request reconcile.Request) (c } func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.KeycloakRealmUser) error { - err := r.helper.SetRealmOwnerRef(ctx, instance) - if err != nil { - return fmt.Errorf("unable to set realm owner ref: %w", err) - } - kClient, err := r.helper.CreateKeycloakClientFromRealmRef(ctx, instance) if err != nil { return fmt.Errorf("unable to create keycloak client from ref: %w", err) diff --git a/hack/install-kuttl.sh b/hack/install-kuttl.sh index 94326b52..e9517d42 100755 --- a/hack/install-kuttl.sh +++ b/hack/install-kuttl.sh @@ -1,5 +1,5 @@ #!/bin/bash -sudo curl -Lo /usr/local/bin/kubectl-kuttl https://github.com/kudobuilder/kuttl/releases/download/v0.15.0/kubectl-kuttl_0.15.0_linux_x86_64 +sudo curl -Lo /usr/local/bin/kubectl-kuttl https://github.com/kudobuilder/kuttl/releases/download/v0.18.0/kubectl-kuttl_0.18.0_linux_x86_64 sudo chmod +x /usr/local/bin/kubectl-kuttl export PATH=$PATH:/usr/local/bin diff --git a/tests/e2e/helm-success-path/99-cleanup.yaml b/tests/e2e/helm-success-path/99-cleanup.yaml index ebf7b93b..2b49b1e1 100644 --- a/tests/e2e/helm-success-path/99-cleanup.yaml +++ b/tests/e2e/helm-success-path/99-cleanup.yaml @@ -7,12 +7,14 @@ commands: namespaced: true - command: kubectl delete keycloakrealmidentityprovider keycloakrealmidentityprovider-sample keycloakrealmidentityprovider-sample-with-pass namespaced: true - - command: kubectl delete keycloakrealmcomponent component-sample + - command: kubectl delete keycloakrealmcomponent component-sample component-sample-child namespaced: true - command: kubectl delete keycloakrealmuser keycloakrealmuser-sample namespaced: true - command: kubectl delete keycloakrealm keycloakrealm-sample namespaced: true + - command: kubectl delete keycloak keycloak + namespaced: true # we have to uninstall helm since clusterwide resources, like ClusterRole are preserved - command: helm uninstall keycloak-operator-e2e namespaced: true