Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions controllers/keycloakrealmgroup/keycloakrealmgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,7 @@ func (r *ReconcileKeycloakRealmGroup) tryReconcile(ctx context.Context, keycloak
return fmt.Errorf("unable to get keycloak realm from ref: %w", err)
}

id, err := kClient.SyncRealmGroup(ctx, gocloak.PString(realm.Realm), &keycloakRealmGroup.Spec)
if err != nil {
return fmt.Errorf("unable to sync realm group: %w", err)
}

keycloakRealmGroup.Status.ID = id

if _, err := r.helper.TryToDelete(
deleted, err := r.helper.TryToDelete(
ctx,
keycloakRealmGroup,
makeTerminator(
Expand All @@ -147,10 +140,22 @@ func (r *ReconcileKeycloakRealmGroup) tryReconcile(ctx context.Context, keycloak
objectmeta.PreserveResourcesOnDeletion(keycloakRealmGroup),
),
keyCloakRealmGroupOperatorFinalizerName,
); err != nil {
)
if err != nil {
return fmt.Errorf("failed to delete keycloak realm group: %w", err)
}

if deleted {
return nil
}

id, err := kClient.SyncRealmGroup(ctx, gocloak.PString(realm.Realm), &keycloakRealmGroup.Spec)
if err != nil {
return fmt.Errorf("unable to sync realm group: %w", err)
}

keycloakRealmGroup.Status.ID = id

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("KeycloakRealmGroup controller", Ordered, func() {
Expect(adapter.SkipAlreadyExistsErr(err)).ShouldNot(HaveOccurred())

By("Creating a KeycloakRealmGroup subgroup")
group := &keycloakApi.KeycloakRealmGroup{
subgroup := &keycloakApi.KeycloakRealmGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "test-subgroup",
Namespace: ns,
Expand All @@ -44,16 +44,39 @@ var _ = Describe("KeycloakRealmGroup controller", Ordered, func() {
Path: "/test-subgroup",
},
}
Expect(k8sClient.Create(ctx, group)).Should(Succeed())
Expect(k8sClient.Create(ctx, subgroup)).Should(Succeed())
Eventually(func(g Gomega) {
createdGroup := &keycloakApi.KeycloakRealmGroup{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: subgroup.Name, Namespace: ns}, createdGroup)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(createdGroup.Status.Value).Should(Equal(helper.StatusOK))
}).WithTimeout(time.Second * 20).WithPolling(time.Second).Should(Succeed())

By("Creating a KeycloakRealmGroup subgroup2")
subgroup2 := &keycloakApi.KeycloakRealmGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "test-subgroup2",
Namespace: ns,
},
Spec: keycloakApi.KeycloakRealmGroupSpec{
Name: "test-subgroup2",
RealmRef: common.RealmRef{
Kind: keycloakApi.KeycloakRealmKind,
Name: KeycloakRealmCR,
},
Path: "/test-subgroup2",
},
}
Expect(k8sClient.Create(ctx, subgroup2)).Should(Succeed())
Eventually(func(g Gomega) {
createdGroup := &keycloakApi.KeycloakRealmGroup{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: "test-subgroup", Namespace: ns}, createdGroup)
err = k8sClient.Get(ctx, types.NamespacedName{Name: subgroup2.Name, Namespace: ns}, createdGroup)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(createdGroup.Status.Value).Should(Equal(helper.StatusOK))
}).WithTimeout(time.Second * 20).WithPolling(time.Second).Should(Succeed())

By("Creating a KeycloakRealmGroup")
group = &keycloakApi.KeycloakRealmGroup{
group := &keycloakApi.KeycloakRealmGroup{
ObjectMeta: metav1.ObjectMeta{
Name: groupCR,
Namespace: ns,
Expand Down Expand Up @@ -83,7 +106,7 @@ var _ = Describe("KeycloakRealmGroup controller", Ordered, func() {
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: ns, Name: groupCR}, group)).Should(Succeed())

By("Updating a parent KeycloakRealmGroup")
group.Spec.SubGroups = []string{}
group.Spec.SubGroups = []string{"test-subgroup2"}

Expect(k8sClient.Update(ctx, group)).Should(Succeed())
Eventually(func(g Gomega) {
Expand All @@ -93,7 +116,7 @@ var _ = Describe("KeycloakRealmGroup controller", Ordered, func() {
g.Expect(updatedGroup.Status.Value).Should(Equal(helper.StatusOK))
}, time.Minute, time.Second*5).Should(Succeed())
})
It("Should delete KeycloakRealmGroup", func() {
It("Should delete KeycloakRealmGroup and subgroup", func() {
By("Getting KeycloakRealmGroup")
group := &keycloakApi.KeycloakRealmGroup{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: ns, Name: groupCR}, group)).Should(Succeed())
Expand All @@ -105,6 +128,82 @@ var _ = Describe("KeycloakRealmGroup controller", Ordered, func() {

g.Expect(k8sErrors.IsNotFound(err)).Should(BeTrue())
}, timeout, interval).Should(Succeed())

By("Getting KeycloakRealmGroup subgroup")
subgroup := &keycloakApi.KeycloakRealmGroup{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: ns, Name: "test-subgroup2"}, subgroup)).Should(Succeed())
By("Deleting KeycloakRealmGroup subgroup")
Expect(k8sClient.Delete(ctx, subgroup)).Should(Succeed())
Eventually(func(g Gomega) {
deletedSubGroup := &keycloakApi.KeycloakRealmGroup{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: subgroup.Name, Namespace: ns}, deletedSubGroup)

g.Expect(k8sErrors.IsNotFound(err)).Should(BeTrue())
}, timeout, interval).Should(Succeed())
})
It("Should delete KeycloakRealmGroup if subgroup is deleted", func() {
By("Creating a subgroup")
subgroup := &keycloakApi.KeycloakRealmGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "test-subgroup-for-deletion",
Namespace: ns,
},
Spec: keycloakApi.KeycloakRealmGroupSpec{
Name: "test-subgroup-for-deletion",
RealmRef: common.RealmRef{
Kind: keycloakApi.KeycloakRealmKind,
Name: KeycloakRealmCR,
},
},
}
Expect(k8sClient.Create(ctx, subgroup)).Should(Succeed())
Eventually(func(g Gomega) {
createdSubGroup := &keycloakApi.KeycloakRealmGroup{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: subgroup.Name, Namespace: ns}, createdSubGroup)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(createdSubGroup.Status.Value).Should(Equal(helper.StatusOK))
}).WithTimeout(time.Second * 20).WithPolling(time.Second).Should(Succeed())

By("Creating a group with subgroup")
group := &keycloakApi.KeycloakRealmGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "test-group-for-deletion",
Namespace: ns,
},
Spec: keycloakApi.KeycloakRealmGroupSpec{
Name: "test-group-for-deletion",
RealmRef: common.RealmRef{
Kind: keycloakApi.KeycloakRealmKind,
Name: KeycloakRealmCR,
},
SubGroups: []string{"test-subgroup-for-deletion"},
},
}
Expect(k8sClient.Create(ctx, group)).Should(Succeed())
Eventually(func(g Gomega) {
createdGroup := &keycloakApi.KeycloakRealmGroup{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: group.Name, Namespace: ns}, createdGroup)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(createdGroup.Status.Value).Should(Equal(helper.StatusOK))
}).WithTimeout(time.Second * 20).WithPolling(time.Second).Should(Succeed())

By("Deleting subgroup")
Expect(k8sClient.Delete(ctx, subgroup)).Should(Succeed())
Eventually(func(g Gomega) {
deletedSubGroup := &keycloakApi.KeycloakRealmGroup{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: subgroup.Name, Namespace: ns}, deletedSubGroup)

g.Expect(k8sErrors.IsNotFound(err)).Should(BeTrue())
}, timeout, interval).Should(Succeed())

By("Deleting group")
Expect(k8sClient.Delete(ctx, group)).Should(Succeed())
Eventually(func(g Gomega) {
deletedGroup := &keycloakApi.KeycloakRealmGroup{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: group.Name, Namespace: ns}, deletedGroup)

g.Expect(k8sErrors.IsNotFound(err)).Should(BeTrue())
}, timeout, interval).Should(Succeed())
})
It("Should preserve group with annotation", func() {
By("Creating a KeycloakRealmGroup")
Expand Down
7 changes: 7 additions & 0 deletions controllers/keycloakrealmgroup/terminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"

"github.com/epam/edp-keycloak-operator/pkg/client/keycloak"
"github.com/epam/edp-keycloak-operator/pkg/client/keycloak/adapter"
)

type terminator struct {
Expand All @@ -26,6 +27,12 @@ func (t *terminator) DeleteResource(ctx context.Context) error {
log.Info("Start deleting group")

if err := t.kClient.DeleteGroup(ctx, t.realmName, t.groupName); err != nil {
if adapter.IsErrNotFound(err) {
log.Info("Group not found, skipping deletion")

return nil
}

return fmt.Errorf("unable to delete group %w", err)
}

Expand Down
33 changes: 20 additions & 13 deletions controllers/keycloakrealmuser/keycloakrealmuser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.Keyc
return fmt.Errorf("unable to get keycloak realm from ref: %w", err)
}

if instance.Spec.KeepResource {
deleted, err := r.helper.TryToDelete(ctx, instance,
makeTerminator(
gocloak.PString(realm.Realm),
instance.Spec.Username,
kClient,
objectmeta.PreserveResourcesOnDeletion(instance),
),
finalizer,
)
if err != nil {
return fmt.Errorf("failed to delete keycloak realm user: %w", err)
}

if deleted {
return nil
}
}

password, getPasswordErr := r.getPassword(ctx, instance)
if getPasswordErr != nil {
return fmt.Errorf("unable to get password: %w", getPasswordErr)
Expand All @@ -153,19 +172,7 @@ func (r *Reconcile) tryReconcile(ctx context.Context, instance *keycloakApi.Keyc
return errors.Wrap(err, "unable to sync realm user")
}

if instance.Spec.KeepResource {
if _, err := r.helper.TryToDelete(ctx, instance,
makeTerminator(
gocloak.PString(realm.Realm),
instance.Spec.Username,
kClient,
objectmeta.PreserveResourcesOnDeletion(instance),
),
finalizer,
); err != nil {
return errors.Wrap(err, "unable to set finalizers")
}
} else {
if !instance.Spec.KeepResource {
if err := r.client.Delete(ctx, instance); err != nil {
return errors.Wrap(err, "unable to delete instance of keycloak realm user")
}
Expand Down
Loading