Skip to content

Commit 8c6c0b4

Browse files
committed
Refactor and Test InjectAnnotationsIntoDeployment
1 parent 76bf38b commit 8c6c0b4

File tree

2 files changed

+197
-6
lines changed

2 files changed

+197
-6
lines changed

pkg/controller/operators/olm/overrides/inject/inject.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,26 @@ func InjectAnnotationsIntoDeployment(deployment *appsv1.Deployment, newAnnotatio
312312

313313
// do not override existing annotations
314314
if newAnnotations != nil {
315-
// Inject Into Deployment
316-
mergedDeploymentAnnotations := newAnnotations
317-
for k, v := range deployment.Annotations {
315+
mergedDeploymentAnnotations := map[string]string{}
316+
mergedPodAnnotations := map[string]string{}
317+
318+
// add newAnnotations first to prevent them from overwriting the defaults
319+
for k, v := range newAnnotations {
318320
mergedDeploymentAnnotations[k] = v
321+
mergedPodAnnotations[k] = v
319322
}
320-
deployment.SetAnnotations(mergedDeploymentAnnotations)
321323

322-
// Inject Into Pod Spec
323-
mergedPodAnnotations := newAnnotations
324+
// then replace any duplicate annotations with the default annotation
325+
for k, v := range deployment.Annotations {
326+
mergedDeploymentAnnotations[k] = v
327+
}
324328
for k, v := range deployment.Spec.Template.GetAnnotations() {
325329
mergedPodAnnotations[k] = v
326330
}
331+
332+
// Inject Into Deployment
333+
deployment.SetAnnotations(mergedDeploymentAnnotations)
334+
// Inject Into Pod Spec
327335
deployment.Spec.Template.SetAnnotations(mergedPodAnnotations)
328336
}
329337

pkg/controller/operators/olm/overrides/inject/inject_test.go

+183
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
appsv1 "k8s.io/api/apps/v1"
910
corev1 "k8s.io/api/core/v1"
1011
"k8s.io/apimachinery/pkg/api/resource"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113

1214
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject"
1315
)
@@ -1288,3 +1290,184 @@ func TestAffinityAPIChanges(t *testing.T) {
12881290
value := reflect.ValueOf(corev1.Affinity{})
12891291
assert.Equal(t, 3, value.NumField(), "It seems the corev1.Affinity API has changed. Please revisit the inject.OverrideDeploymentAffinity implementation")
12901292
}
1293+
1294+
func TestInjectAnnotationsIntoDeployment(t *testing.T) {
1295+
tests := []struct {
1296+
name string
1297+
deployment *appsv1.Deployment
1298+
annotations map[string]string
1299+
expected *appsv1.Deployment
1300+
}{
1301+
{
1302+
// Nil Deployment is injected with annotations
1303+
// Expected: Deployment is nil
1304+
name: "WithNilDeployment",
1305+
deployment: nil,
1306+
annotations: map[string]string{"foo": "bar"},
1307+
expected: nil, // raises an error
1308+
},
1309+
{
1310+
// Deployment with no Annotations is injected with annotations
1311+
// Expected: Annotations is empty
1312+
name: "WithEmptyAnnotations",
1313+
deployment: &appsv1.Deployment{},
1314+
annotations: map[string]string{"foo": "bar"},
1315+
expected: &appsv1.Deployment{
1316+
ObjectMeta: metav1.ObjectMeta{
1317+
Annotations: map[string]string{"foo": "bar"},
1318+
},
1319+
Spec: appsv1.DeploymentSpec{
1320+
Template: corev1.PodTemplateSpec{
1321+
ObjectMeta: metav1.ObjectMeta{
1322+
Annotations: map[string]string{
1323+
"foo": "bar",
1324+
},
1325+
},
1326+
},
1327+
},
1328+
},
1329+
},
1330+
{
1331+
// Deployment with existing Annotations is injected with annotations
1332+
// Expected: Existing Annotations are not overwritten
1333+
name: "WithExistingAnnotations",
1334+
deployment: &appsv1.Deployment{
1335+
ObjectMeta: metav1.ObjectMeta{
1336+
Annotations: map[string]string{
1337+
"common": "default-deploy-annotation",
1338+
"deploy": "default-deploy-annotation",
1339+
},
1340+
},
1341+
Spec: appsv1.DeploymentSpec{
1342+
Template: corev1.PodTemplateSpec{
1343+
ObjectMeta: metav1.ObjectMeta{
1344+
Annotations: map[string]string{
1345+
"common": "default-pod-annotation",
1346+
"pod": "default-pod-annotation",
1347+
},
1348+
},
1349+
},
1350+
},
1351+
},
1352+
annotations: map[string]string{
1353+
"common": "override-annotation",
1354+
"deploy": "override-annotation",
1355+
"pod": "override-annotation",
1356+
"foo": "bar",
1357+
},
1358+
expected: &appsv1.Deployment{
1359+
ObjectMeta: metav1.ObjectMeta{
1360+
Annotations: map[string]string{
1361+
// no overrides
1362+
"common": "default-deploy-annotation",
1363+
"deploy": "default-deploy-annotation",
1364+
// there was no default for "pod" on the deployment
1365+
"pod": "override-annotation",
1366+
"foo": "bar",
1367+
},
1368+
},
1369+
Spec: appsv1.DeploymentSpec{
1370+
Template: corev1.PodTemplateSpec{
1371+
ObjectMeta: metav1.ObjectMeta{
1372+
Annotations: map[string]string{
1373+
// no overrides
1374+
"common": "default-pod-annotation",
1375+
"pod": "default-pod-annotation",
1376+
// there was no default for "deploy" on the pod
1377+
"deploy": "override-annotation",
1378+
"foo": "bar",
1379+
},
1380+
},
1381+
},
1382+
},
1383+
},
1384+
},
1385+
{
1386+
// Existing Deployment is left alone if annotations is nil
1387+
// Expected: Deployment is not changed
1388+
name: "WithNilAnnotations",
1389+
deployment: &appsv1.Deployment{
1390+
ObjectMeta: metav1.ObjectMeta{
1391+
Annotations: map[string]string{
1392+
"deploy": "default-annotation",
1393+
},
1394+
},
1395+
Spec: appsv1.DeploymentSpec{
1396+
Template: corev1.PodTemplateSpec{
1397+
ObjectMeta: metav1.ObjectMeta{
1398+
Annotations: map[string]string{
1399+
"pod": "default-annotation",
1400+
},
1401+
},
1402+
},
1403+
},
1404+
},
1405+
annotations: nil,
1406+
expected: &appsv1.Deployment{
1407+
ObjectMeta: metav1.ObjectMeta{
1408+
Annotations: map[string]string{
1409+
"deploy": "default-annotation",
1410+
},
1411+
},
1412+
Spec: appsv1.DeploymentSpec{
1413+
Template: corev1.PodTemplateSpec{
1414+
ObjectMeta: metav1.ObjectMeta{
1415+
Annotations: map[string]string{
1416+
"pod": "default-annotation",
1417+
},
1418+
},
1419+
},
1420+
},
1421+
},
1422+
},
1423+
{
1424+
// Existing Deployment retains its annotations if the annotations is an empty map
1425+
// Expected: Deployment annotations are not changed
1426+
name: "WithEmptyAnnotations",
1427+
deployment: &appsv1.Deployment{
1428+
ObjectMeta: metav1.ObjectMeta{
1429+
Annotations: map[string]string{
1430+
"deploy": "default-annotation",
1431+
},
1432+
},
1433+
Spec: appsv1.DeploymentSpec{
1434+
Template: corev1.PodTemplateSpec{
1435+
ObjectMeta: metav1.ObjectMeta{
1436+
Annotations: map[string]string{
1437+
"pod": "default-annotation",
1438+
},
1439+
},
1440+
},
1441+
},
1442+
},
1443+
annotations: map[string]string{},
1444+
expected: &appsv1.Deployment{
1445+
ObjectMeta: metav1.ObjectMeta{
1446+
Annotations: map[string]string{
1447+
"deploy": "default-annotation",
1448+
},
1449+
},
1450+
Spec: appsv1.DeploymentSpec{
1451+
Template: corev1.PodTemplateSpec{
1452+
ObjectMeta: metav1.ObjectMeta{
1453+
Annotations: map[string]string{
1454+
"pod": "default-annotation",
1455+
},
1456+
},
1457+
},
1458+
},
1459+
},
1460+
},
1461+
}
1462+
1463+
for _, tt := range tests {
1464+
t.Run(tt.name, func(t *testing.T) {
1465+
inject.InjectAnnotationsIntoDeployment(tt.deployment, tt.annotations)
1466+
1467+
podSpecWant := tt.expected
1468+
podSpecGot := tt.deployment
1469+
1470+
assert.Equal(t, podSpecWant, podSpecGot)
1471+
})
1472+
}
1473+
}

0 commit comments

Comments
 (0)