@@ -22,40 +22,26 @@ import (
2222var _ fn.Runner = & SetLabels {}
2323
2424type SetLabels struct {
25- Labels map [string ]string `json:"labels,omitempty"`
25+ Owner string
26+ Org string
2627}
2728
2829// Run is the main function logic.
2930// `ctx` provides easy methods to add info, error or warning result to `ResourceList.Results`.
3031// `items` is parsed from the STDIN "ResourceList.Items".
3132// `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r.Labels
3233// 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.KubeObject ) {
34+ func (r * SetLabels ) Run (ctx * fn.Context , functionConfig * fn.KubeObject , items fn.KubeObjects ) {
3435 for _ , o := range items {
35- for k , newLabel := range r .Labels {
36- o .SetLabel (k , newLabel )
37- }
36+ o .SetLabel ("owner" , r .Owner )
37+ o .SetLabel ("org" , r .Org )
3838 }
3939 ctx .ResultInfo ("updated labels" , nil )
4040}
4141
4242// This example uses a SetLabels object, which implements `Runner.Run` methods.
43- //
44- // The input from ./data/setlabels-resourcelist.yaml:
45- // apiVersion: config.kubernetes.io/v1
46- // kind: ResourceList
47- // items:
48- // - apiVersion: v1
49- // kind: Service
50- // metadata:
51- // name: example
52- // functionConfig:
53- // apiVersion: fn.kpt.dev/v1alpha1
54- // kind: SetLabels
55- // metadata:
56- // name: setlabel-fn-config
57- func Example_asMain () {
58- file , _ := os .Open ("./data/setlabels-resourcelist.yaml" )
43+ func Example_asMainCustomFnConfig () {
44+ file , _ := os .Open ("./data/setlabels-customFnConfig.yaml" )
5945 defer file .Close ()
6046 os .Stdin = file
6147
@@ -70,11 +56,48 @@ func Example_asMain() {
7056 // kind: Service
7157 // metadata:
7258 // name: example
59+ // labels:
60+ // owner: kpt
61+ // org: google
7362 // functionConfig:
7463 // apiVersion: fn.kpt.dev/v1alpha1
7564 // kind: SetLabels
7665 // metadata:
7766 // name: setlabel-fn-config
67+ // owner: kpt
68+ // org: google
69+ // results:
70+ // - message: updated labels
71+ // severity: info
72+ }
73+
74+ func Example_asMainConfigMap () {
75+ file , _ := os .Open ("./data/setlabels-configmap.yaml" )
76+ defer file .Close ()
77+ os .Stdin = file
78+
79+ if err := fn .AsMain (& SetLabels {}); err != nil {
80+ os .Exit (1 )
81+ }
82+ // Output:
83+ // apiVersion: config.kubernetes.io/v1
84+ // kind: ResourceList
85+ // items:
86+ // - apiVersion: v1
87+ // kind: Service
88+ // metadata:
89+ // name: example
90+ // labels:
91+ // owner: kpt
92+ // org: google
93+ // functionConfig:
94+ // apiVersion: v1
95+ // kind: ConfigMap
96+ // metadata:
97+ // name: setlabel
98+ // data:
99+ // owner: kpt
100+ // org: google
78101 // results:
79102 // - message: updated labels
80103 // severity: info
0 commit comments