11package main
22
33import (
4- "fmt "
4+ "context "
55 "os"
66
77 "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
88)
99
10- // EDIT THIS FUNCTION!
11- // This is the main logic. rl is the input `ResourceList` which has the `FunctionConfig` and `Items` fields.
12- // You can modify the `Items` and add result information to `rl.Result`.
13- func Run (rl * fn.ResourceList ) (bool , error ) {
14- // Your code
10+ var _ fn.Runner = & YourFunction {}
11+
12+ type YourFunction struct {
13+ // TODO: Modify with your expected function config.
14+ // Note: The value will be parsed from the STDIN `ResourceList.FunctionConfig`. For example:
15+ /*
16+ apiVersion: config.kubernetes.io/v1
17+ kind: ResourceList
18+ functionConfig:
19+ apiVersion: fn.kpt.dev/v1alpha1
20+ kind: FunctionX
21+ metadata:
22+ ...
23+ fnConfigBool: true
24+ fnConfigInt: 31
25+ fnConfigFoo: kpt
26+ items:
27+ - ...
28+ */
29+ FnConfigBool bool
30+ FnConfigInt int
31+ FnConfigFoo string
32+ }
33+
34+ // Run is the main function logic.
35+ // `items` is parsed from the STDIN "ResourceList.Items".
36+ // `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r attributes
37+ // `results` is the "ResourceList.Results" that you can write result info to.
38+ func (r * YourFunction ) Run (ctx * fn.Context , functionConfig * fn.KubeObject , items fn.KubeObjects , results * fn.Results ) bool {
39+ // TODO: Write your code. Below is some sample code.
40+ for _ , item := range items {
41+ // Example 1, update the `Deployment` name to the functionConfig's "FnConfigFoo" value.
42+ if item .GetKind () == "Deployment" {
43+ err := item .SetName (r .FnConfigFoo )
44+ if err != nil {
45+ results .ErrorE (err )
46+ return false
47+ }
48+ }
49+ // Example 2, update the `Service` name only if the functionConfig's namespace is example.
50+ // Note, the functionConfig.NestedString("fnConfigFoo") is the same as YourFunction.FnConfigFoo. One is more convenient to
51+ // use in different cases.
52+ if item .GetKind () == "Service" {
53+ spec := item .GetMap ("spec" )
54+ pods , found , err := spec .NestedSlice ("ports" )
55+ if err != nil {
56+ results .ErrorE (err )
57+ }
58+ if ! found {
59+ continue
60+ }
61+ for _ , pod := range pods {
62+ name , _ , err := pod .NestedString ("name" )
63+ if err != nil {
64+ results .ErrorE (err )
65+ }
66+ if name != functionConfig .GetName () {
67+ pod .SetNestedString (r .FnConfigFoo , "name" )
68+ }
69+ }
70+ }
71+ }
72+ results .Infof ("function pass" )
73+ return true
1574}
1675
76+
1777func main () {
18- // CUSTOMIZE IF NEEDED
19- // `AsMain` accepts a `ResourceListProcessor` interface.
20- // You can explore other `ResourceListProcessor` structs in the SDK or define your own.
21- if err := fn .AsMain (fn .ResourceListProcessorFunc (Run )); err != nil {
78+ runner := fn .WithContext (context .Background (), & YourFunction {})
79+ if err := fn .AsMain (runner ); err != nil {
2280 os .Exit (1 )
2381 }
24- }
82+ }
0 commit comments