...

Source file src/sigs.k8s.io/kustomize/kyaml/kio/example_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/kio

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     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  	// setAnnotationFn
    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  	// Output:
    73  	// apiVersion: apps/v1
    74  	// kind: Deployment
    75  	// metadata:
    76  	//   name: nginx
    77  	//   labels:
    78  	//     app: nginx
    79  	//   annotations:
    80  	//     foo: 'bar'
    81  	// spec:
    82  	//   replicas: 3
    83  	//   selector:
    84  	//     matchLabels:
    85  	//       app: nginx
    86  	//   template:
    87  	//     metadata:
    88  	//       labels:
    89  	//         app: nginx
    90  	//     spec:
    91  	//       containers:
    92  	//       - name: nginx
    93  	//         image: nginx:1.7.9
    94  	//         ports:
    95  	//         - containerPort: 80
    96  	// ---
    97  	// apiVersion: v1
    98  	// kind: Service
    99  	// metadata:
   100  	//   name: nginx
   101  	//   annotations:
   102  	//     foo: 'bar'
   103  	// spec:
   104  	//   selector:
   105  	//     app: nginx
   106  	//   ports:
   107  	//   - protocol: TCP
   108  	//     port: 80
   109  	//     targetPort: 80
   110  }
   111  

View as plain text