@@ -35,6 +35,7 @@ import (
35
35
"k8s.io/client-go/tools/record"
36
36
ctrl "sigs.k8s.io/controller-runtime"
37
37
"sigs.k8s.io/controller-runtime/pkg/client"
38
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
38
39
39
40
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
40
41
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
@@ -127,7 +128,42 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
127
128
for i := range pdbList .Items {
128
129
ownedObjects [pdbList .Items [i ].GetUID ()] = & pdbList .Items [i ]
129
130
}
131
+ if params .Config .CreateRBACPermissions () == rbac .Available {
132
+ clusterObjects , err := r .findClusterRoleObjects (ctx , params )
133
+ if err != nil {
134
+ return nil , err
135
+ }
136
+ for k , v := range clusterObjects {
137
+ ownedObjects [k ] = v
138
+ }
139
+ }
140
+ return ownedObjects , nil
141
+ }
130
142
143
+ // The cluster scope objects do not have owner reference.
144
+ func (r * OpenTelemetryCollectorReconciler ) findClusterRoleObjects (ctx context.Context , params manifests.Params ) (map [types.UID ]client.Object , error ) {
145
+ ownedObjects := map [types.UID ]client.Object {}
146
+ // Remove cluster roles and bindings.
147
+ // Users might switch off the RBAC creation feature on the operator which should remove existing RBAC.
148
+ listOpsCluster := & client.ListOptions {
149
+ LabelSelector : labels .SelectorFromSet (manifestutils .SelectorLabels (params .OtelCol .ObjectMeta , collector .ComponentOpenTelemetryCollector )),
150
+ }
151
+ clusterroleList := & rbacv1.ClusterRoleList {}
152
+ err := r .List (ctx , clusterroleList , listOpsCluster )
153
+ if err != nil {
154
+ return nil , fmt .Errorf ("error listing ClusterRoles: %w" , err )
155
+ }
156
+ for i := range clusterroleList .Items {
157
+ ownedObjects [clusterroleList .Items [i ].GetUID ()] = & clusterroleList .Items [i ]
158
+ }
159
+ clusterrolebindingList := & rbacv1.ClusterRoleBindingList {}
160
+ err = r .List (ctx , clusterrolebindingList , listOpsCluster )
161
+ if err != nil {
162
+ return nil , fmt .Errorf ("error listing ClusterRoleBIndings: %w" , err )
163
+ }
164
+ for i := range clusterrolebindingList .Items {
165
+ ownedObjects [clusterrolebindingList .Items [i ].GetUID ()] = & clusterrolebindingList .Items [i ]
166
+ }
131
167
return ownedObjects , nil
132
168
}
133
169
@@ -193,8 +229,32 @@ func (r *OpenTelemetryCollectorReconciler) Reconcile(ctx context.Context, req ct
193
229
// on deleted requests.
194
230
return ctrl.Result {}, client .IgnoreNotFound (err )
195
231
}
232
+
233
+ params , err := r .getParams (instance )
234
+ if err != nil {
235
+ log .Error (err , "Failed to create manifest.Params" )
236
+ return ctrl.Result {}, err
237
+ }
238
+
196
239
// We have a deletion, short circuit and let the deletion happen
197
240
if deletionTimestamp := instance .GetDeletionTimestamp (); deletionTimestamp != nil {
241
+ if controllerutil .ContainsFinalizer (& instance , collectorFinalizer ) {
242
+ // If the finalization logic fails, don't remove the finalizer so
243
+ // that we can retry during the next reconciliation.
244
+ if err = r .finalizeCollector (ctx , params ); err != nil {
245
+ return ctrl.Result {}, err
246
+ }
247
+
248
+ // Once all finalizers have been
249
+ // removed, the object will be deleted.
250
+ if controllerutil .RemoveFinalizer (& instance , collectorFinalizer ) {
251
+ err = r .Update (ctx , & instance )
252
+ if err != nil {
253
+ return ctrl.Result {}, err
254
+ }
255
+ }
256
+ }
257
+
198
258
return ctrl.Result {}, nil
199
259
}
200
260
@@ -204,10 +264,14 @@ func (r *OpenTelemetryCollectorReconciler) Reconcile(ctx context.Context, req ct
204
264
return ctrl.Result {}, nil
205
265
}
206
266
207
- params , err := r .getParams (instance )
208
- if err != nil {
209
- log .Error (err , "Failed to create manifest.Params" )
210
- return ctrl.Result {}, err
267
+ // Add finalizer for this CR
268
+ if ! controllerutil .ContainsFinalizer (& instance , collectorFinalizer ) {
269
+ if controllerutil .AddFinalizer (& instance , collectorFinalizer ) {
270
+ err = r .Update (ctx , & instance )
271
+ if err != nil {
272
+ return ctrl.Result {}, err
273
+ }
274
+ }
211
275
}
212
276
213
277
desiredObjects , buildErr := BuildCollector (params )
@@ -255,3 +319,17 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er
255
319
256
320
return builder .Complete (r )
257
321
}
322
+
323
+ const collectorFinalizer = "opentelemetrycollector.opentelemetry.io/finalizer"
324
+
325
+ func (r * OpenTelemetryCollectorReconciler ) finalizeCollector (ctx context.Context , params manifests.Params ) error {
326
+ // The cluster scope objects do not have owner reference. They need to be deleted explicitly
327
+ if params .Config .CreateRBACPermissions () == rbac .Available {
328
+ objects , err := r .findClusterRoleObjects (ctx , params )
329
+ if err != nil {
330
+ return err
331
+ }
332
+ return deleteObjects (ctx , r .Client , r .log , objects )
333
+ }
334
+ return nil
335
+ }
0 commit comments