...
1
2
3
4 package kio_test
5
6 import (
7 "bytes"
8 "log"
9 "os"
10
11 "sigs.k8s.io/kustomize/kyaml/kio"
12 "sigs.k8s.io/kustomize/kyaml/yaml"
13 )
14
15 func Example() {
16 input := bytes.NewReader([]byte(`apiVersion: apps/v1
17 kind: Deployment
18 metadata:
19 name: nginx
20 labels:
21 app: nginx
22 spec:
23 replicas: 3
24 selector:
25 matchLabels:
26 app: nginx
27 template:
28 metadata:
29 labels:
30 app: nginx
31 spec:
32 containers:
33 - name: nginx
34 image: nginx:1.7.9
35 ports:
36 - containerPort: 80
37 ---
38 apiVersion: v1
39 kind: Service
40 metadata:
41 name: nginx
42 spec:
43 selector:
44 app: nginx
45 ports:
46 - protocol: TCP
47 port: 80
48 targetPort: 80
49 `))
50
51
52 setAnnotationFn := kio.FilterFunc(func(operand []*yaml.RNode) ([]*yaml.RNode, error) {
53 for i := range operand {
54 resource := operand[i]
55 _, err := resource.Pipe(yaml.SetAnnotation("foo", "bar"))
56 if err != nil {
57 return nil, err
58 }
59 }
60 return operand, nil
61 })
62
63 err := kio.Pipeline{
64 Inputs: []kio.Reader{&kio.ByteReader{Reader: input}},
65 Filters: []kio.Filter{setAnnotationFn},
66 Outputs: []kio.Writer{kio.ByteWriter{Writer: os.Stdout}},
67 }.Execute()
68 if err != nil {
69 log.Fatal(err)
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 }
111
View as plain text