@@ -3,6 +3,7 @@ package controller
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "sort"
6
7
7
8
apierrors "k8s.io/apimachinery/pkg/api/errors"
8
9
"k8s.io/apimachinery/pkg/runtime"
@@ -19,7 +20,6 @@ import (
19
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
21
21
22
cozyv1alpha1 "github.com/aenix-io/cozystack/api/v1alpha1"
22
- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
23
23
)
24
24
25
25
// WorkloadMonitorReconciler reconciles a WorkloadMonitor object
@@ -44,6 +44,50 @@ func (r *WorkloadMonitorReconciler) isPodReady(pod *corev1.Pod) bool {
44
44
return false
45
45
}
46
46
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
+
47
91
// reconcilePodForMonitor creates or updates a Workload object for the given Pod and WorkloadMonitor.
48
92
func (r * WorkloadMonitorReconciler ) reconcilePodForMonitor (
49
93
ctx context.Context ,
@@ -75,7 +119,6 @@ func (r *WorkloadMonitorReconciler) reconcilePodForMonitor(
75
119
annRes := map [string ]string {}
76
120
if err := json .Unmarshal ([]byte (resourcesStr ), & annRes ); err != nil {
77
121
logger .Error (err , "Failed to parse resources annotation" , "pod" , pod .Name )
78
- // We do not return an error here to keep reconciling other Pods
79
122
} else {
80
123
for k , v := range annRes {
81
124
parsed , err := resource .ParseQuantity (v )
@@ -88,30 +131,23 @@ func (r *WorkloadMonitorReconciler) reconcilePodForMonitor(
88
131
}
89
132
}
90
133
91
- // Prepare the Workload object
92
134
workload := & cozyv1alpha1.Workload {
93
135
ObjectMeta : metav1.ObjectMeta {
94
- Name : pod .Name , // or any logic to ensure uniqueness
136
+ Name : pod .Name ,
95
137
Namespace : pod .Namespace ,
96
138
},
97
139
}
98
140
99
- // CreateOrUpdate the Workload owned by this WorkloadMonitor
100
141
_ , 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 )
105
144
106
145
// Copy labels from the Pod if needed
107
146
workload .Labels = pod .Labels
108
147
109
148
// Fill Workload status fields:
110
- // - Kind and Type come from the WorkloadMonitor spec
111
149
workload .Status .Kind = monitor .Spec .Kind
112
150
workload .Status .Type = monitor .Spec .Type
113
-
114
- // Convert the resource map to the Workload's .Status.Resources
115
151
workload .Status .Resources = totalResources
116
152
workload .Status .Operational = r .isPodReady (& pod )
117
153
0 commit comments