@@ -19,47 +19,33 @@ import (
1919 "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
2020)
2121
22- var _ fn.Runner = & SetLabels {}
22+ var _ fn.Runner = & CustomFnConfig {}
2323
24- type SetLabels struct {
25- Labels map [string ]string `json:"labels,omitempty"`
24+ type CustomFnConfig struct {
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".
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 ) {
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 ) {
3435 for _ , o := range items {
35- for k , newLabel := range r .Labels {
36- o .SetLabel (k , newLabel )
37- }
36+ o .SetName (r .Owner )
37+ o .SetNamespace (r .Org )
3838 }
39- ctx .ResultInfo ("updated labels " , nil )
39+ ctx .ResultInfo ("updated namespace and name " , nil )
4040}
4141
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" )
42+ // This example uses a CustomFnConfig object, which implements `Runner.Run` methods.
43+ func Example_asMainCustomFnConfig () {
44+ file , _ := os .Open ("./data/runner-customFnConfig.yaml" )
5945 defer file .Close ()
6046 os .Stdin = file
6147
62- if err := fn .AsMain (& SetLabels {}); err != nil {
48+ if err := fn .AsMain (& CustomFnConfig {}); err != nil {
6349 os .Exit (1 )
6450 }
6551 // Output:
@@ -69,13 +55,46 @@ func Example_asMain() {
6955 // - apiVersion: v1
7056 // kind: Service
7157 // metadata:
72- // name: example
58+ // name: kpt
59+ // namespace: google
7360 // functionConfig:
7461 // apiVersion: fn.kpt.dev/v1alpha1
75- // kind: SetLabels
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
7692 // metadata:
77- // name: setlabel-fn-config
93+ // name: customConfig
94+ // data:
95+ // owner: kpt
96+ // org: google
7897 // results:
79- // - message: updated labels
98+ // - message: updated namespace and name
8099 // severity: info
81100}
0 commit comments