Skip to content

Commit fced786

Browse files
authored
Add finalizer only for cluster scope objects (#4574)
- Add finalizers to CR only when cluster roles and bindings for SA are created by Operator - Fixes #4367
1 parent 492ab74 commit fced786

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Add finalizers to OpenTelemetryCollector CR only when cluster roles and bindings for SA are created by Operator.
9+
10+
# One or more tracking issues related to the change
11+
issues: [4367]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext: |
17+
Finalizer usage was restricted to cluster scoped resources only. Namespaced resources no longer receive finalizers,
18+
preventing blocked namespace deletion if the operator is removed first. The change aligns finalizer behavior with
19+
cluster-level RBAC availability, ensuring finalizers are applied only when the operator has the required
20+
cluster scoped permissions.

internal/controllers/opentelemetrycollector_controller.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,10 @@ func (r *OpenTelemetryCollectorReconciler) Reconcile(ctx context.Context, req ct
285285
}
286286

287287
// Add finalizer for this CR
288-
if !controllerutil.ContainsFinalizer(&instance, collectorFinalizer) {
289-
if controllerutil.AddFinalizer(&instance, collectorFinalizer) {
290-
err = r.Update(ctx, &instance)
291-
if err != nil {
292-
return ctrl.Result{}, err
293-
}
288+
if maybeAddFinalizer(params, &instance) {
289+
err = r.Update(ctx, &instance)
290+
if err != nil {
291+
return ctrl.Result{}, err
294292
}
295293
}
296294

@@ -395,3 +393,10 @@ func (r *OpenTelemetryCollectorReconciler) finalizeCollector(ctx context.Context
395393
}
396394
return nil
397395
}
396+
397+
func maybeAddFinalizer(params manifests.Params, instance *v1beta1.OpenTelemetryCollector) bool {
398+
if params.Config.CreateRBACPermissions == rbac.Available && !controllerutil.ContainsFinalizer(instance, collectorFinalizer) {
399+
return controllerutil.AddFinalizer(instance, collectorFinalizer)
400+
}
401+
return false
402+
}

internal/controllers/opentelemetrycollector_reconciler_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
corev1 "k8s.io/api/core/v1"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
14+
15+
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
16+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
17+
"github.com/open-telemetry/opentelemetry-operator/internal/config"
18+
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
1319
)
1420

1521
func TestGetCollectorConfigMapsToKeep(t *testing.T) {
@@ -65,3 +71,55 @@ func TestGetCollectorConfigMapsToKeep(t *testing.T) {
6571
})
6672
}
6773
}
74+
75+
func TestMaybeAddFinalizer(t *testing.T) {
76+
testCases := []struct {
77+
name string
78+
rbacAvailable rbac.Availability
79+
hasFinalizer bool
80+
expectAdded bool
81+
}{
82+
{
83+
name: "adds finalizer when RBAC available and no finalizer exists",
84+
rbacAvailable: rbac.Available,
85+
hasFinalizer: false,
86+
expectAdded: true,
87+
},
88+
{
89+
name: "does not add finalizer when RBAC not available",
90+
rbacAvailable: rbac.NotAvailable,
91+
hasFinalizer: false,
92+
expectAdded: false,
93+
},
94+
{
95+
name: "does not add finalizer when already exists",
96+
rbacAvailable: rbac.Available,
97+
hasFinalizer: true,
98+
expectAdded: false,
99+
},
100+
}
101+
102+
for _, tc := range testCases {
103+
t.Run(tc.name, func(t *testing.T) {
104+
instance := &v1beta1.OpenTelemetryCollector{
105+
ObjectMeta: metav1.ObjectMeta{
106+
Name: "test-collector",
107+
Namespace: "default",
108+
},
109+
}
110+
111+
if tc.hasFinalizer {
112+
controllerutil.AddFinalizer(instance, collectorFinalizer)
113+
}
114+
115+
params := manifests.Params{
116+
Config: config.Config{
117+
CreateRBACPermissions: tc.rbacAvailable,
118+
},
119+
}
120+
121+
result := maybeAddFinalizer(params, instance)
122+
assert.Equal(t, tc.expectAdded, result)
123+
})
124+
}
125+
}

0 commit comments

Comments
 (0)