Skip to content

Commit 0f66c09

Browse files
authored
Allow custom annotations on service accounts (#3106)
* Allow custom annotations on service accounts Signed-off-by: Pavol Loffay <[email protected]> * Fix Signed-off-by: Pavol Loffay <[email protected]> * Fix Signed-off-by: Pavol Loffay <[email protected]> * Add extra annotation Signed-off-by: Pavol Loffay <[email protected]> --------- Signed-off-by: Pavol Loffay <[email protected]>
1 parent bceb15a commit 0f66c09

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

.chloggen/main.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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: Allow annotations on service account to prevent infinite reconciliation on OpenShift and creating infinite pull secrets.
9+
10+
# One or more tracking issues related to the change
11+
issues: [3106]
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+
On OpenShift 4.16 the platform automatically adds an annotation `openshift.io/internal-registry-pull-secret-ref: <simplest-collector-dockercfg-jwq66>`
18+
to the service account which contains secret name with image pull secret.

controllers/reconcile_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
9696
},
9797
},
9898
}
99+
deploymentExtraPorts.Annotations = map[string]string{
100+
"new-annotation": "new-value",
101+
}
99102
ingressParams := testCollectorAssertNoErr(t, "test-ingress", "", testFileIngress)
100103
ingressParams.Spec.Ingress.Type = "ingress"
101104
updatedIngressParams := testCollectorAssertNoErr(t, "test-ingress", "", testFileIngress)
@@ -164,9 +167,15 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
164167
"app.kubernetes.io/managed-by": "opentelemetry-operator",
165168
"app.kubernetes.io/part-of": "opentelemetry",
166169
})
167-
exists, err = populateObjectIfExists(t, &v1.ServiceAccount{}, namespacedObjectName(naming.ServiceAccount(params.Name), params.Namespace))
170+
sa := &v1.ServiceAccount{}
171+
exists, err = populateObjectIfExists(t, sa, namespacedObjectName(naming.ServiceAccount(params.Name), params.Namespace))
168172
assert.NoError(t, err)
169173
assert.True(t, exists)
174+
assert.Equal(t, map[string]string{annotationName: "true"}, sa.Annotations)
175+
saPatch := sa.DeepCopy()
176+
saPatch.Annotations["user-defined-annotation"] = "value"
177+
err = k8sClient.Patch(ctx, saPatch, client.MergeFrom(sa))
178+
require.NoError(t, err)
170179
},
171180
},
172181
wantErr: assert.NoError,
@@ -198,6 +207,12 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
198207
"app.kubernetes.io/managed-by": "opentelemetry-operator",
199208
"app.kubernetes.io/part-of": "opentelemetry",
200209
})
210+
211+
sa := &v1.ServiceAccount{}
212+
exists, err = populateObjectIfExists(t, sa, namespacedObjectName(naming.ServiceAccount(params.Name), params.Namespace))
213+
assert.NoError(t, err)
214+
assert.True(t, exists)
215+
assert.Equal(t, map[string]string{annotationName: "true", "user-defined-annotation": "value", "new-annotation": "new-value"}, sa.Annotations)
201216
},
202217
},
203218
wantErr: assert.NoError,

internal/manifests/mutate.go

-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ func mutateConfigMap(existing, desired *corev1.ConfigMap) {
194194
}
195195

196196
func mutateServiceAccount(existing, desired *corev1.ServiceAccount) {
197-
existing.Annotations = desired.Annotations
198197
existing.Labels = desired.Labels
199198
}
200199

internal/manifests/mutate_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package manifests
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
corev1 "k8s.io/api/core/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
func TestMutateServiceAccount(t *testing.T) {
27+
existing := corev1.ServiceAccount{
28+
ObjectMeta: metav1.ObjectMeta{
29+
Name: "simplest",
30+
Annotations: map[string]string{
31+
"config.openshift.io/serving-cert-secret-name": "my-secret",
32+
},
33+
},
34+
}
35+
desired := corev1.ServiceAccount{
36+
ObjectMeta: metav1.ObjectMeta{
37+
Name: "simplest",
38+
},
39+
}
40+
41+
mutateFn := MutateFuncFor(&existing, &desired)
42+
err := mutateFn()
43+
require.NoError(t, err)
44+
assert.Equal(t, corev1.ServiceAccount{
45+
ObjectMeta: metav1.ObjectMeta{
46+
Name: "simplest",
47+
Annotations: map[string]string{"config.openshift.io/serving-cert-secret-name": "my-secret"},
48+
},
49+
}, existing)
50+
}

0 commit comments

Comments
 (0)