Skip to content

Commit 0be5bdb

Browse files
committed
fix: recreate existing reconcilers if the realmspec changed
1 parent 007f67e commit 0be5bdb

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

internal/controllers/keycloakrealm_controller.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ func (r *KeycloakRealmReconciler) podReconcile(ctx context.Context, realm infrav
258258
if current, ok := secret.Data["realm.json"]; ok {
259259
needUpdate = raw != string(current)
260260
}
261+
262+
r.Log.Info("xxx", "pod", pod.Annotations)
263+
264+
specVersion, ok := pod.Annotations["keycloak-controller/realm-spec-version"]
265+
if !needUpdate && podErr == nil && ok {
266+
needUpdate = specVersion != fmt.Sprintf("%d", realm.Generation)
267+
}
268+
269+
if !ok {
270+
needUpdate = true
271+
}
261272
}
262273

263274
if secretErr != nil && !apierrors.IsNotFound(secretErr) {
@@ -381,6 +392,11 @@ func (r *KeycloakRealmReconciler) podReconcile(ctx context.Context, realm infrav
381392
template.ResourceVersion = ""
382393
template.UID = ""
383394

395+
if template.Annotations == nil {
396+
template.Annotations = make(map[string]string)
397+
}
398+
template.Annotations["keycloak-controller/realm-spec-version"] = fmt.Sprintf("%d", realm.Generation)
399+
384400
usernameField := "username"
385401
passwordField := "password"
386402

internal/controllers/keycloakrealm_controller_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,60 @@ var _ = Describe("KeycloakRealm controller", func() {
14451445
Expect(pod.Spec.Containers[0].Env).Should(Equal(envs))
14461446
Expect(reconciledInstance.Status.SubResourceCatalog).Should(HaveLen(0))
14471447
})
1448+
1449+
It("recreates the running reconciler pod if the spec changed", func() {
1450+
By("waiting for the reconciliation")
1451+
instanceLookupKey := types.NamespacedName{Name: realmName, Namespace: "default"}
1452+
reconciledInstance := &v1beta1.KeycloakRealm{}
1453+
Expect(k8sClient.Get(ctx, instanceLookupKey, reconciledInstance)).Should(BeNil())
1454+
1455+
beforeChangeStatus := reconciledInstance.Status
1456+
reconciledInstance.Spec.ReconcilerTemplate.Spec.Containers[1].Image = "new-image:v1"
1457+
Expect(k8sClient.Update(ctx, reconciledInstance)).Should(Succeed())
1458+
1459+
Eventually(func() bool {
1460+
err := k8sClient.Get(ctx, instanceLookupKey, reconciledInstance)
1461+
if err != nil {
1462+
return false
1463+
}
1464+
1465+
return beforeChangeStatus.Reconciler != reconciledInstance.Status.Reconciler
1466+
}, timeout, interval).Should(BeTrue())
1467+
1468+
By("making sure there is a reconciler pod")
1469+
pod := &corev1.Pod{}
1470+
Expect(k8sClient.Get(ctx, types.NamespacedName{
1471+
Name: reconciledInstance.Status.Reconciler,
1472+
Namespace: reconciledInstance.Namespace,
1473+
}, pod)).Should(Succeed())
1474+
1475+
Expect(pod.Spec.Containers[1].Image).Should(Equal("new-image:v1"))
1476+
})
1477+
1478+
It("recreates the running reconciler pod if the spec annotation is not present", func() {
1479+
By("waiting for the reconciliation")
1480+
instanceLookupKey := types.NamespacedName{Name: realmName, Namespace: "default"}
1481+
reconciledInstance := &v1beta1.KeycloakRealm{}
1482+
Expect(k8sClient.Get(ctx, instanceLookupKey, reconciledInstance)).Should(BeNil())
1483+
beforeChangeStatus := reconciledInstance.Status
1484+
1485+
pod := &corev1.Pod{}
1486+
Expect(k8sClient.Get(ctx, types.NamespacedName{
1487+
Name: reconciledInstance.Status.Reconciler,
1488+
Namespace: reconciledInstance.Namespace,
1489+
}, pod)).Should(Succeed())
1490+
pod.Annotations = nil
1491+
Expect(k8sClient.Update(ctx, pod)).Should(Succeed())
1492+
1493+
Eventually(func() bool {
1494+
err := k8sClient.Get(ctx, instanceLookupKey, reconciledInstance)
1495+
if err != nil {
1496+
return false
1497+
}
1498+
1499+
return beforeChangeStatus.Reconciler != reconciledInstance.Status.Reconciler
1500+
}, timeout, interval).Should(BeTrue())
1501+
})
14481502
})
14491503

14501504
When("a realm without auth credentials is reconciled", func() {

0 commit comments

Comments
 (0)