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
- //
33
- // the functionConfig is validated to have kind "SetLabels" and apiVersion "fn.kpt.dev/v1alpha1"
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
32
+ // the functionConfig is validated to have kind "SetLabels" and apiVersion "fn.kpt.dev/v1alpha1"
33
+ func (r * SetLabels ) Run (ctx * fn.Context , functionConfig * fn.KubeObject , items fn.KubeObjects , results * fn.Results ) bool {
34
+ for _ , o := range items {
35
+ for k , newLabel := range r .Labels {
36
+ o .SetLabel (k , newLabel )
37
+ }
38
+ }
39
+ results .Infof ("updated labels" , nil )
40
+ return true
42
41
}
43
42
44
43
// This example uses a SetLabels object, which implements `Runner.Run` methods.
@@ -51,34 +50,33 @@ func (r *SetLabels) Run(_ *fn.Context, _ *fn.KubeObject, items fn.KubeObjects, r
51
50
// kind: Service
52
51
// metadata:
53
52
// name: example
54
- //
55
53
// functionConfig:
56
- // apiVersion: fn.kpt.dev/v1alpha1
57
- // kind: SetLabels
58
- // metadata:
59
- // name: setlabel-fn-config
54
+ // apiVersion: fn.kpt.dev/v1alpha1
55
+ // kind: SetLabels
56
+ // metadata:
57
+ // name: setlabel-fn-config
60
58
func Example_asMain () {
61
- file , _ := os .Open ("./data/setlabels-resourcelist.yaml" )
62
- defer file .Close ()
63
- os .Stdin = file
59
+ file , _ := os .Open ("./data/setlabels-resourcelist.yaml" )
60
+ defer file .Close ()
61
+ os .Stdin = file
64
62
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
63
+ if err := fn .AsMain (& SetLabels {}); err != nil {
64
+ os .Exit (1 )
65
+ }
66
+ // Output:
67
+ // apiVersion: config.kubernetes.io/v1
68
+ // kind: ResourceList
69
+ // items:
70
+ // - apiVersion: v1
71
+ // kind: Service
72
+ // metadata:
73
+ // name: example
74
+ // functionConfig:
75
+ // apiVersion: fn.kpt.dev/v1alpha1
76
+ // kind: SetLabels
77
+ // metadata:
78
+ // name: setlabel-fn-config
79
+ // results:
80
+ // - message: updated labels
81
+ // severity: info
84
82
}
0 commit comments