Skip to content

Commit 2a0efd2

Browse files
hervedombyaMonPote
authored andcommitted
fix: trigger rolling updates when ConfigMaps change
Update internal/controller/scalityui/deployment.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Update internal/controller/scalityui/deployment.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> refactor: add SHA256 and hex encoding imports to deployment.go for improved hash calculations
1 parent e379d1c commit 2a0efd2

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

internal/controller/scalityui/deployed_apps_configmap.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scalityui
22

33
import (
4+
"crypto/sha256"
45
"encoding/json"
56
"fmt"
67

@@ -73,6 +74,10 @@ func newDeployedAppsConfigMapReducer(r *ScalityUIReconciler, cr ScalityUI, curre
7374
return reconcile.Result{}, fmt.Errorf("failed to marshal deployed UI apps: %w", err)
7475
}
7576

77+
h := sha256.New()
78+
h.Write(deployedAppsJSON)
79+
deployedAppsHash := fmt.Sprintf("%x", h.Sum(nil))
80+
7681
result, err := controllerutil.CreateOrUpdate(ctx, currentState.GetKubeClient(), configMap, func() error {
7782
if err := controllerutil.SetControllerReference(cr, configMap, r.Scheme); err != nil {
7883
return fmt.Errorf("failed to set controller reference: %w", err)
@@ -82,6 +87,12 @@ func newDeployedAppsConfigMapReducer(r *ScalityUIReconciler, cr ScalityUI, curre
8287
configMap.Data = make(map[string]string)
8388
}
8489
configMap.Data[deployedUIAppsKey] = string(deployedAppsJSON)
90+
91+
if configMap.Annotations == nil {
92+
configMap.Annotations = make(map[string]string)
93+
}
94+
configMap.Annotations["scality.com/deployed-apps-hash"] = deployedAppsHash
95+
8596
return nil
8697
})
8798

internal/controller/scalityui/deployment.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package scalityui
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
47
"github.com/scality/reconciler-framework/reconciler"
58
"github.com/scality/reconciler-framework/resources"
69
"github.com/scality/ui-operator/internal/utils"
@@ -154,13 +157,41 @@ func newScalityUIDeploymentReconciler(cr ScalityUI, currentState State) reconcil
154157
// Get config hash from ConfigMap if available
155158
ctx := currentState.GetContext()
156159
configMap := &corev1.ConfigMap{}
160+
var configHash string
157161
if err := currentState.GetKubeClient().Get(ctx, client.ObjectKey{
158162
Name: cr.Name,
159163
Namespace: getOperatorNamespace(),
160164
}, configMap); err == nil {
161165
if hash, exists := configMap.Annotations["scality.com/config-hash"]; exists {
162-
podTemplate.Annotations["scality.com/config-hash"] = hash
166+
configHash = hash
167+
}
168+
}
169+
170+
// Get deployed-apps hash from deployed-ui-apps ConfigMap
171+
deployedAppsConfigMap := &corev1.ConfigMap{}
172+
var deployedAppsHash string
173+
if err := currentState.GetKubeClient().Get(ctx, client.ObjectKey{
174+
Name: cr.Name + "-deployed-ui-apps",
175+
Namespace: getOperatorNamespace(),
176+
}, deployedAppsConfigMap); err == nil {
177+
if hash, exists := deployedAppsConfigMap.Annotations["scality.com/deployed-apps-hash"]; exists {
178+
deployedAppsHash = hash
179+
}
180+
}
181+
182+
// Combine both hashes to trigger rolling update when either changes
183+
if configHash != "" && deployedAppsHash != "" {
184+
// Concatenate non-empty hashes with a separator and hash the result
185+
combined := ""
186+
if configHash != "" {
187+
combined += "config:" + configHash + ";"
188+
}
189+
if deployedAppsHash != "" {
190+
combined += "apps:" + deployedAppsHash + ";"
163191
}
192+
sum := sha256.Sum256([]byte(combined))
193+
combinedHash := hex.EncodeToString(sum[:])
194+
podTemplate.Annotations["scality.com/combined-hash"] = combinedHash
164195
}
165196
},
166197
},

0 commit comments

Comments
 (0)