@@ -19,43 +19,108 @@ import (
1919 "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
2020)
2121
22+ var _ fn.Runner = & CustomFnConfig {}
23+
24+ type CustomFnConfig struct {
25+ Owner string
26+ Org string
27+ }
28+
29+ // Run is the main function logic.
30+ // `ctx` provides easy methods to add info, error or warning result to `ResourceList.Results`.
31+ // `items` is parsed from the STDIN "ResourceList.Items".
32+ // `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value is parsed from a `CustomFnConfig` type
33+ // "owner" and "org" field.
34+ func (r * CustomFnConfig ) Run (ctx * fn.Context , functionConfig * fn.KubeObject , items fn.KubeObjects ) {
35+ for _ , o := range items {
36+ o .SetName (r .Owner )
37+ o .SetNamespace (r .Org )
38+ }
39+ ctx .ResultInfo ("updated namespace and name" , nil )
40+ }
41+
42+ // This example uses a CustomFnConfig object, which implements `Runner.Run` methods.
43+ func Example_asMainCustomFnConfig () {
44+ file , _ := os .Open ("./data/runner-customFnConfig.yaml" )
45+ defer file .Close ()
46+ os .Stdin = file
47+
48+ if err := fn .AsMain (& CustomFnConfig {}); err != nil {
49+ os .Exit (1 )
50+ }
51+ // Output:
52+ // apiVersion: config.kubernetes.io/v1
53+ // kind: ResourceList
54+ // items:
55+ // - apiVersion: v1
56+ // kind: Service
57+ // metadata:
58+ // name: kpt
59+ // namespace: google
60+ // functionConfig:
61+ // apiVersion: fn.kpt.dev/v1alpha1
62+ // kind: CustomFnConfig
63+ // metadata:
64+ // name: runner-fn-config
65+ // owner: kpt
66+ // org: google
67+ // results:
68+ // - message: updated namespace and name
69+ // severity: info
70+ }
71+
72+ func Example_asMainConfigMap () {
73+ file , _ := os .Open ("./data/runner-configmap.yaml" )
74+ defer file .Close ()
75+ os .Stdin = file
76+
77+ if err := fn .AsMain (& CustomFnConfig {}); err != nil {
78+ os .Exit (1 )
79+ }
80+ // Output:
81+ // apiVersion: config.kubernetes.io/v1
82+ // kind: ResourceList
83+ // items:
84+ // - apiVersion: v1
85+ // kind: Service
86+ // metadata:
87+ // name: kpt
88+ // namespace: google
89+ // functionConfig:
90+ // apiVersion: v1
91+ // kind: ConfigMap
92+ // metadata:
93+ // name: customConfig
94+ // data:
95+ // owner: kpt
96+ // org: google
97+ // results:
98+ // - message: updated namespace and name
99+ // severity: info
100+ }
101+
22102var _ fn.Runner = & SetLabels {}
23103
24104type SetLabels struct {
25- Labels map [string ]string `json:"labels,omitempty"`
105+ Data map [string ]string
26106}
27107
28108// Run is the main function logic.
29109// `ctx` provides easy methods to add info, error or warning result to `ResourceList.Results`.
30110// `items` is parsed from the STDIN "ResourceList.Items".
31- // `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r.Labels
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.KubeObject ) {
111+ // `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value is parsed from a `ConfigMap` type
112+ // "data" field.
113+ func (r * SetLabels ) Run (ctx * fn.Context , functionConfig * fn.KubeObject , items fn.KubeObjects ) {
34114 for _ , o := range items {
35- for k , newLabel := range r .Labels {
36- o .SetLabel (k , newLabel )
115+ for k , v := range r .Data {
116+ o .SetLabel (k , v )
37117 }
38118 }
39119 ctx .ResultInfo ("updated labels" , nil )
40120}
41121
42- // 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" )
122+ func Example_asMain_configMapData () {
123+ file , _ := os .Open ("./data/runner-configmap-general.yaml" )
59124 defer file .Close ()
60125 os .Stdin = file
61126
@@ -70,11 +135,17 @@ func Example_asMain() {
70135 // kind: Service
71136 // metadata:
72137 // name: example
138+ // labels:
139+ // project-id: kpt-dev
140+ // managed-by: kpt
73141 // functionConfig:
74- // apiVersion: fn.kpt.dev/v1alpha1
75- // kind: SetLabels
142+ // apiVersion: v1
143+ // kind: ConfigMap
76144 // metadata:
77- // name: setlabel-fn-config
145+ // name: runner-fn-config
146+ // data:
147+ // project-id: kpt-dev
148+ // managed-by: kpt
78149 // results:
79150 // - message: updated labels
80151 // severity: info
0 commit comments