4
4
// you may not use this file except in compliance with the License.
5
5
// You may obtain a copy of the License at
6
6
//
7
- // http://www.apache.org/licenses/LICENSE-2.0
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
8
//
9
9
// Unless required by applicable law or agreed to in writing, software
10
10
// distributed under the License is distributed on an "AS IS" BASIS,
14
14
package example
15
15
16
16
import (
17
- "os"
17
+ "os"
18
18
19
- "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
19
+ "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
20
20
)
21
21
22
22
var _ fn.Runner = & SetLabels {}
23
23
24
24
type SetLabels struct {
25
- Labels map [string ]string `json:"labels,omitempty"`
25
+ Labels map [string ]string `json:"labels,omitempty"`
26
26
}
27
27
28
28
// Run is the main function logic.
29
29
// `ctx` provides easy methods to add info, error or warning result to `ResourceList.Results`.
30
30
// `items` is parsed from the STDIN "ResourceList.Items".
31
31
// `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r.Labels
32
32
//
33
- // the functionConfig is validated to have kind "SetLabels" and apiVersion "fn.kpt.dev/v1alpha1"
33
+ // the functionConfig is validated to have kind "SetLabels" and apiVersion "fn.kpt.dev/v1alpha1"
34
34
func (r * SetLabels ) Run (_ * fn.Context , _ * fn.KubeObject , items fn.KubeObjects , results * fn.Results ) bool {
35
- for _ , o := range items {
36
- for k , newLabel := range r .Labels {
37
- o .SetLabel (k , newLabel )
38
- }
39
- }
40
- results .Infof ("updated labels" )
41
- return true
35
+ for _ , o := range items {
36
+ for k , newLabel := range r .Labels {
37
+ o .SetLabel (k , newLabel )
38
+ }
39
+ }
40
+ results .Infof ("updated labels" )
41
+ return true
42
42
}
43
43
44
44
// This example uses a SetLabels object, which implements `Runner.Run` methods.
@@ -50,36 +50,35 @@ func (r *SetLabels) Run(_ *fn.Context, _ *fn.KubeObject, items fn.KubeObjects, r
50
50
// - apiVersion: v1
51
51
// kind: Service
52
52
// metadata:
53
- // name: example
53
+ // name: example
54
54
//
55
55
// functionConfig:
56
- //
57
- // apiVersion: fn.kpt.dev/v1alpha1
58
- // kind: SetLabels
59
- // metadata:
60
- // name: setlabel-fn-config
56
+ // apiVersion: fn.kpt.dev/v1alpha1
57
+ // kind: SetLabels
58
+ // metadata:
59
+ // name: setlabel-fn-config
61
60
func Example_asMain () {
62
- file , _ := os .Open ("./data/setlabels-resourcelist.yaml" )
63
- defer file .Close ()
64
- os .Stdin = file
61
+ file , _ := os .Open ("./data/setlabels-resourcelist.yaml" )
62
+ defer file .Close ()
63
+ os .Stdin = file
65
64
66
- if err := fn .AsMain (& SetLabels {}); err != nil {
67
- os .Exit (1 )
68
- }
69
- // Output:
70
- // apiVersion: config.kubernetes.io/v1
71
- // kind: ResourceList
72
- // items:
73
- // - apiVersion: v1
74
- // kind: Service
75
- // metadata:
76
- // name: example
77
- // functionConfig:
78
- // apiVersion: fn.kpt.dev/v1alpha1
79
- // kind: SetLabels
80
- // metadata:
81
- // name: setlabel-fn-config
82
- // results:
83
- // - message: updated labels
84
- // severity: info
65
+ if err := fn .AsMain (& SetLabels {}); err != nil {
66
+ os .Exit (1 )
67
+ }
68
+ // Output:
69
+ // apiVersion: config.kubernetes.io/v1
70
+ // kind: ResourceList
71
+ // items:
72
+ // - apiVersion: v1
73
+ // kind: Service
74
+ // metadata:
75
+ // name: example
76
+ // functionConfig:
77
+ // apiVersion: fn.kpt.dev/v1alpha1
78
+ // kind: SetLabels
79
+ // metadata:
80
+ // name: setlabel-fn-config
81
+ // results:
82
+ // - message: updated labels
83
+ // severity: info
85
84
}
0 commit comments