Skip to content

Commit df2ba6a

Browse files
Fix: handling annotations in deployments deployed by overcommit controller (#15)
* Fix: Add metrics configuration to overcommit controller and validating webhooks * Chore: bump version to 1.0.2 in Makefile, Chart.yaml, values.yaml, and catalog_source.yaml * chore: autogenerate files * Fix: Ensure proper handling of annotations in deployments * Fix: Update deployment template annotations and labels in Reconcile method * Refactor: Replace annotationsEqual and labelsEqual with mapsEqual for consistency in comparison logic
1 parent 7b58ef5 commit df2ba6a

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

bundle/manifests/k8s-overcommit.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ metadata:
3030
}
3131
]
3232
capabilities: Basic Install
33-
createdAt: "2025-07-03T11:39:06Z"
33+
createdAt: "2025-07-03T14:50:58Z"
3434
operators.operatorframework.io/builder: operator-sdk-v1.40.0
3535
operators.operatorframework.io/project_layout: go.kubebuilder.io/v4
3636
name: k8s-overcommit.v1.0.2

deploy/olm/1.0.2/k8s-overcommit.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ metadata:
3030
}
3131
]
3232
capabilities: Basic Install
33-
createdAt: "2025-07-03T11:39:06Z"
33+
createdAt: "2025-07-03T14:50:58Z"
3434
operators.operatorframework.io/builder: operator-sdk-v1.40.0
3535
operators.operatorframework.io/project_layout: go.kubebuilder.io/v4
3636
name: k8s-overcommit.v1.0.2

internal/controller/overcommit/overcommit_controller.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ func (r *OvercommitReconciler) Reconcile(ctx context.Context, req ctrl.Request)
164164
overcommitClassDeployment.Spec.Template.Spec.Containers[0].Env = updatedDeployment.Spec.Template.Spec.Containers[0].Env
165165
updated = true
166166
}
167+
// Update template annotations if they changed
168+
if !mapsEqual(updatedDeployment.Spec.Template.Annotations, overcommitClassDeployment.Spec.Template.Annotations) {
169+
overcommitClassDeployment.Spec.Template.Annotations = updatedDeployment.Spec.Template.Annotations
170+
updated = true
171+
}
172+
// Update template labels if they changed
173+
if !mapsEqual(updatedDeployment.Spec.Template.Labels, overcommitClassDeployment.Spec.Template.Labels) {
174+
overcommitClassDeployment.Spec.Template.Labels = updatedDeployment.Spec.Template.Labels
175+
updated = true
176+
}
167177
// Only set controller reference if we actually updated something
168178
if updated {
169179
return ctrl.SetControllerReference(overcommit, overcommitClassDeployment, r.Scheme)
@@ -250,6 +260,16 @@ func (r *OvercommitReconciler) Reconcile(ctx context.Context, req ctrl.Request)
250260
validatingPodDeployment.Spec.Template.Spec.Containers[0].Env = updatedDeployment.Spec.Template.Spec.Containers[0].Env
251261
updated = true
252262
}
263+
// Update template annotations if they changed
264+
if !mapsEqual(updatedDeployment.Spec.Template.Annotations, validatingPodDeployment.Spec.Template.Annotations) {
265+
validatingPodDeployment.Spec.Template.Annotations = updatedDeployment.Spec.Template.Annotations
266+
updated = true
267+
}
268+
// Update template labels if they changed
269+
if !mapsEqual(updatedDeployment.Spec.Template.Labels, validatingPodDeployment.Spec.Template.Labels) {
270+
validatingPodDeployment.Spec.Template.Labels = updatedDeployment.Spec.Template.Labels
271+
updated = true
272+
}
253273
// Only set controller reference if we actually updated something
254274
if updated {
255275
return ctrl.SetControllerReference(overcommit, validatingPodDeployment, r.Scheme)
@@ -309,6 +329,7 @@ func (r *OvercommitReconciler) Reconcile(ctx context.Context, req ctrl.Request)
309329
occontroller.Spec = updatedDeployment.Spec
310330
occontroller.ObjectMeta.Labels = updatedDeployment.ObjectMeta.Labels
311331
occontroller.ObjectMeta.Annotations = updatedDeployment.ObjectMeta.Annotations
332+
logger.Info("Creating new OvercommitClass Controller deployment")
312333
return ctrl.SetControllerReference(overcommit, occontroller, r.Scheme)
313334
} else {
314335
// Existing deployment, only update specific fields if needed
@@ -322,6 +343,16 @@ func (r *OvercommitReconciler) Reconcile(ctx context.Context, req ctrl.Request)
322343
occontroller.Spec.Template.Spec.Containers[0].Env = updatedDeployment.Spec.Template.Spec.Containers[0].Env
323344
updated = true
324345
}
346+
// Update template annotations if they changed
347+
if !mapsEqual(updatedDeployment.Spec.Template.Annotations, occontroller.Spec.Template.Annotations) {
348+
occontroller.Spec.Template.Annotations = updatedDeployment.Spec.Template.Annotations
349+
updated = true
350+
}
351+
// Update template labels if they changed
352+
if !mapsEqual(updatedDeployment.Spec.Template.Labels, occontroller.Spec.Template.Labels) {
353+
occontroller.Spec.Template.Labels = updatedDeployment.Spec.Template.Labels
354+
updated = true
355+
}
325356
// Only set controller reference if we actually updated something
326357
if updated {
327358
return ctrl.SetControllerReference(overcommit, occontroller, r.Scheme)

internal/controller/overcommit/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,26 @@ func envVarsEqual(a, b []corev1.EnvVar) bool {
290290

291291
return true
292292
}
293+
294+
// annotationsEqual compares two annotation maps to see if they're equal
295+
func mapsEqual(a, b map[string]string) bool {
296+
// Handle nil cases
297+
if a == nil && b == nil {
298+
return true
299+
}
300+
if a == nil || b == nil {
301+
return false
302+
}
303+
304+
if len(a) != len(b) {
305+
return false
306+
}
307+
308+
for key, valueA := range a {
309+
if valueB, exists := b[key]; !exists || valueA != valueB {
310+
return false
311+
}
312+
}
313+
314+
return true
315+
}

internal/resources/generate_resources_overcommit_class_controller_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ func GenerateOvercommitClassControllerDeployment(overcommitObject overcommit.Ove
2121
labels = make(map[string]string)
2222
}
2323
labels["app"] = "overcommit-controller"
24+
25+
// Ensure annotations are properly handled
26+
annotations := overcommitObject.Spec.Annotations
27+
if annotations == nil {
28+
annotations = make(map[string]string)
29+
}
30+
2431
return &appsv1.Deployment{
2532
ObjectMeta: metav1.ObjectMeta{
2633
Name: "k8s-overcommit-overcommitclass-controller",
@@ -36,7 +43,7 @@ func GenerateOvercommitClassControllerDeployment(overcommitObject overcommit.Ove
3643
Template: corev1.PodTemplateSpec{
3744
ObjectMeta: metav1.ObjectMeta{
3845
Labels: labels,
39-
Annotations: overcommitObject.Annotations,
46+
Annotations: annotations,
4047
},
4148
Spec: corev1.PodSpec{
4249
ServiceAccountName: os.Getenv("SERVICE_ACCOUNT_NAME"),

internal/resources/generate_resources_validating_webhooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func GeneratePodValidatingDeployment(overcommitObject overcommit.Overcommit) *ap
6666
Template: corev1.PodTemplateSpec{
6767
ObjectMeta: metav1.ObjectMeta{
6868
Labels: labels,
69-
Annotations: overcommitObject.Annotations,
69+
Annotations: overcommitObject.Spec.Annotations,
7070
},
7171
Spec: corev1.PodSpec{
7272
ServiceAccountName: os.Getenv("SERVICE_ACCOUNT_NAME"),
@@ -241,7 +241,7 @@ func GenerateOvercommitClassValidatingDeployment(overcommitObject overcommit.Ove
241241
Template: corev1.PodTemplateSpec{
242242
ObjectMeta: metav1.ObjectMeta{
243243
Labels: labels,
244-
Annotations: overcommitObject.Annotations,
244+
Annotations: overcommitObject.Spec.Annotations,
245245
},
246246
Spec: corev1.PodSpec{
247247
ServiceAccountName: os.Getenv("SERVICE_ACCOUNT_NAME"),

0 commit comments

Comments
 (0)