1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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
|
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package kio_test
import (
"bytes"
"log"
"os"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
func Example() {
input := bytes.NewReader([]byte(`apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
`))
// setAnnotationFn
setAnnotationFn := kio.FilterFunc(func(operand []*yaml.RNode) ([]*yaml.RNode, error) {
for i := range operand {
resource := operand[i]
_, err := resource.Pipe(yaml.SetAnnotation("foo", "bar"))
if err != nil {
return nil, err
}
}
return operand, nil
})
err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: input}},
Filters: []kio.Filter{setAnnotationFn},
Outputs: []kio.Writer{kio.ByteWriter{Writer: os.Stdout}},
}.Execute()
if err != nil {
log.Fatal(err)
}
// Output:
// apiVersion: apps/v1
// kind: Deployment
// metadata:
// name: nginx
// labels:
// app: nginx
// annotations:
// foo: 'bar'
// spec:
// replicas: 3
// selector:
// matchLabels:
// app: nginx
// template:
// metadata:
// labels:
// app: nginx
// spec:
// containers:
// - name: nginx
// image: nginx:1.7.9
// ports:
// - containerPort: 80
// ---
// apiVersion: v1
// kind: Service
// metadata:
// name: nginx
// annotations:
// foo: 'bar'
// spec:
// selector:
// app: nginx
// ports:
// - protocol: TCP
// port: 80
// targetPort: 80
}
|