Skip to content

Commit 4b84ff9

Browse files
authored
Merge pull request #280 from tomasaschan/add-annotations
Add AddAnnotations companion to AddLabels
2 parents ae666b1 + db4f5be commit 4b84ff9

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package declarative
2+
3+
import (
4+
"context"
5+
6+
"sigs.k8s.io/controller-runtime/pkg/log"
7+
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
8+
)
9+
10+
// AddAnnotations returns an ObjectTransform that adds annotations to all the objects
11+
func AddAnnotations(annotations map[string]string) ObjectTransform {
12+
return func(ctx context.Context, o DeclarativeObject, manifest *manifest.Objects) error {
13+
log := log.Log
14+
for _, o := range manifest.Items {
15+
log.WithValues("object", o).WithValues("annotations", annotations).V(1).Info("add annotations to object")
16+
o.AddAnnotations(annotations)
17+
}
18+
19+
return nil
20+
}
21+
}

pkg/patterns/declarative/pkg/manifest/objects.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ func (o *Object) AddLabels(labels map[string]string) {
9595
o.json = nil
9696
}
9797

98+
func (o *Object) AddAnnotations(annotations map[string]string) {
99+
merged := make(map[string]string)
100+
for k, v := range o.object.GetAnnotations() {
101+
merged[k] = v
102+
}
103+
104+
for k, v := range annotations {
105+
merged[k] = v
106+
}
107+
108+
o.object.SetAnnotations(merged)
109+
// Invalidate cached json
110+
o.json = nil
111+
}
112+
98113
func (o *Object) GetNamespace() string {
99114
return o.namespace
100115
}

pkg/patterns/declarative/pkg/manifest/objects_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,69 @@ spec:
281281
}
282282
}
283283

284+
func Test_AddAnnotations(t *testing.T) {
285+
inputManifest := `---
286+
apiVersion: apps/v1
287+
kind: Deployment
288+
metadata:
289+
annotations:
290+
app: test-app
291+
name: frontend
292+
spec:
293+
replicas: 1
294+
selector:
295+
matchLabels:
296+
app: test-app
297+
strategy: {}
298+
template:
299+
metadata:
300+
labels:
301+
app: test-app
302+
spec:
303+
containers:
304+
- image: busybox
305+
name: busybox`
306+
tests := []struct {
307+
name string
308+
inputManifest string
309+
inputAnnotations map[string]string
310+
expectedAnnotations map[string]string
311+
}{
312+
{
313+
name: "add annotations which are all new one",
314+
inputManifest: inputManifest,
315+
inputAnnotations: map[string]string{"sample-key1": "sample-value1", "sample-key2": "sample-value2"},
316+
expectedAnnotations: map[string]string{"app": "test-app", "sample-key1": "sample-value1", "sample-key2": "sample-value2"},
317+
},
318+
{
319+
// If call AddAnnotations with a key which has exists already, value will be overwritten.
320+
name: "add annotations which has already exist in manifest",
321+
inputManifest: inputManifest,
322+
inputAnnotations: map[string]string{"app": "test-app2"},
323+
expectedAnnotations: map[string]string{"app": "test-app2"},
324+
},
325+
}
326+
327+
for _, tt := range tests {
328+
t.Run(tt.name, func(t *testing.T) {
329+
ctx := context.Background()
330+
objects, err := ParseObjects(ctx, tt.inputManifest)
331+
if err != nil {
332+
t.Fatalf("unexpected error: %v", err)
333+
}
334+
for _, o := range objects.Items {
335+
o.AddAnnotations(tt.inputAnnotations)
336+
if len(tt.expectedAnnotations) != len(o.object.GetAnnotations()) {
337+
t.Errorf("Expected length of labels to be %v but is %v", len(tt.expectedAnnotations), len(o.object.GetAnnotations()))
338+
}
339+
if diff := cmp.Diff(tt.expectedAnnotations, o.object.GetAnnotations()); diff != "" {
340+
t.Fatalf("result mismatch (-want +got):\n%s", diff)
341+
}
342+
}
343+
})
344+
}
345+
}
346+
284347
func Test_ParseJSONToObject(t *testing.T) {
285348
tests := []struct {
286349
name string

0 commit comments

Comments
 (0)