Skip to content

Commit 946fd6e

Browse files
committed
fix ownerreference reconcile
1 parent 6567f96 commit 946fd6e

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

internal/controller/workloadmonitor_controller.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controller
33
import (
44
"context"
55
"encoding/json"
6+
"sort"
67

78
apierrors "k8s.io/apimachinery/pkg/api/errors"
89
"k8s.io/apimachinery/pkg/runtime"
@@ -19,7 +20,6 @@ import (
1920
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021

2122
cozyv1alpha1 "github.com/aenix-io/cozystack/api/v1alpha1"
22-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2323
)
2424

2525
// WorkloadMonitorReconciler reconciles a WorkloadMonitor object
@@ -44,6 +44,50 @@ func (r *WorkloadMonitorReconciler) isPodReady(pod *corev1.Pod) bool {
4444
return false
4545
}
4646

47+
// updateOwnerReferences adds the given monitor as a new owner reference to the object if not already present.
48+
// It then sorts the owner references to enforce a consistent order.
49+
func updateOwnerReferences(obj metav1.Object, monitor client.Object) {
50+
// Retrieve current owner references
51+
owners := obj.GetOwnerReferences()
52+
53+
// Check if current monitor is already in owner references
54+
var alreadyOwned bool
55+
for _, ownerRef := range owners {
56+
if ownerRef.UID == monitor.GetUID() {
57+
alreadyOwned = true
58+
break
59+
}
60+
}
61+
62+
runtimeObj, ok := monitor.(runtime.Object)
63+
if !ok {
64+
return
65+
}
66+
gvk := runtimeObj.GetObjectKind().GroupVersionKind()
67+
68+
// If not already present, add new owner reference without controller flag
69+
if !alreadyOwned {
70+
newOwnerRef := metav1.OwnerReference{
71+
APIVersion: gvk.GroupVersion().String(),
72+
Kind: gvk.Kind,
73+
Name: monitor.GetName(),
74+
UID: monitor.GetUID(),
75+
// Set Controller to false to avoid conflict as multiple controllers are not allowed
76+
Controller: pointer.BoolPtr(false),
77+
BlockOwnerDeletion: pointer.BoolPtr(true),
78+
}
79+
owners = append(owners, newOwnerRef)
80+
}
81+
82+
// Sort owner references to enforce a consistent order by UID
83+
sort.SliceStable(owners, func(i, j int) bool {
84+
return owners[i].UID < owners[j].UID
85+
})
86+
87+
// Update the owner references of the object
88+
obj.SetOwnerReferences(owners)
89+
}
90+
4791
// reconcilePodForMonitor creates or updates a Workload object for the given Pod and WorkloadMonitor.
4892
func (r *WorkloadMonitorReconciler) reconcilePodForMonitor(
4993
ctx context.Context,
@@ -75,7 +119,6 @@ func (r *WorkloadMonitorReconciler) reconcilePodForMonitor(
75119
annRes := map[string]string{}
76120
if err := json.Unmarshal([]byte(resourcesStr), &annRes); err != nil {
77121
logger.Error(err, "Failed to parse resources annotation", "pod", pod.Name)
78-
// We do not return an error here to keep reconciling other Pods
79122
} else {
80123
for k, v := range annRes {
81124
parsed, err := resource.ParseQuantity(v)
@@ -88,30 +131,23 @@ func (r *WorkloadMonitorReconciler) reconcilePodForMonitor(
88131
}
89132
}
90133

91-
// Prepare the Workload object
92134
workload := &cozyv1alpha1.Workload{
93135
ObjectMeta: metav1.ObjectMeta{
94-
Name: pod.Name, // or any logic to ensure uniqueness
136+
Name: pod.Name,
95137
Namespace: pod.Namespace,
96138
},
97139
}
98140

99-
// CreateOrUpdate the Workload owned by this WorkloadMonitor
100141
_, err := ctrl.CreateOrUpdate(ctx, r.Client, workload, func() error {
101-
// Set this WorkloadMonitor as owner for garbage collection
102-
if err := controllerutil.SetControllerReference(monitor, workload, r.Scheme); err != nil {
103-
return err
104-
}
142+
// Update owner references with the new monitor
143+
updateOwnerReferences(workload.GetObjectMeta(), monitor)
105144

106145
// Copy labels from the Pod if needed
107146
workload.Labels = pod.Labels
108147

109148
// Fill Workload status fields:
110-
// - Kind and Type come from the WorkloadMonitor spec
111149
workload.Status.Kind = monitor.Spec.Kind
112150
workload.Status.Type = monitor.Spec.Type
113-
114-
// Convert the resource map to the Workload's .Status.Resources
115151
workload.Status.Resources = totalResources
116152
workload.Status.Operational = r.isPodReady(&pod)
117153

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cozystackController:
2-
image: ghcr.io/aenix-io/cozystack/cozystack-controller:latest@sha256:0638f77f16b778794bd8514a3c5a3c39408a078ad21a2ccc25c5fd8a1f66185f
2+
image: ghcr.io/aenix-io/cozystack/cozystack-controller:latest@sha256:380bb9d4402b2f2025dec29e0dccca2a6a456cf9b66b3553b328fb4498f3fea3
33
debug: false
44
disableTelemetry: false
55
cozystackVersion: "latest"

0 commit comments

Comments
 (0)