Skip to content

Commit 08a35ef

Browse files
committed
Modify functionRunner
1 parent 78748f0 commit 08a35ef

File tree

5 files changed

+85
-38
lines changed

5 files changed

+85
-38
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: config.kubernetes.io/v1
2+
kind: ResourceList
3+
items:
4+
- apiVersion: v1
5+
kind: Service
6+
metadata:
7+
name: example
8+
functionConfig:
9+
apiVersion: v1
10+
kind: ConfigMap
11+
metadata:
12+
name: customConfig
13+
data:
14+
owner: kpt
15+
org: google

go/fn/examples/data/setlabels-resourcelist.yaml renamed to go/fn/examples/data/runner-customFnConfig.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ items:
77
name: example
88
functionConfig:
99
apiVersion: fn.kpt.dev/v1alpha1
10-
kind: SetLabels
10+
kind: CustomFnConfig
1111
metadata:
12-
name: setlabel-fn-config
12+
name: runner-fn-config
13+
owner: kpt
14+
org: google

go/fn/examples/example_asmain_runner_test.go

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

go/fn/functionrunner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
package fn
1616

1717
type Runner interface {
18-
Run(context *Context, functionConfig *KubeObject, items []*KubeObject)
18+
Run(context *Context, functionConfig *KubeObject, items KubeObjects)
1919
}

go/fn/runnerProcessor.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package fn
1717
import (
1818
"fmt"
1919
"reflect"
20+
"strings"
2021
)
2122

2223
type runnerProcessor struct {
@@ -39,9 +40,19 @@ func (r *runnerProcessor) config(ctx *Context, o *KubeObject) {
3940
data := o.NestedStringMapOrDie("data")
4041
fnRunnerElem := reflect.ValueOf(r.fnRunner).Elem()
4142
for i := 0; i < fnRunnerElem.NumField(); i++ {
42-
if fnRunnerElem.Field(i).Kind() == reflect.Map {
43-
fnRunnerElem.Field(i).Set(reflect.ValueOf(data))
44-
break
43+
switch fnRunnerElem.Field(i).Kind() {
44+
case reflect.String:
45+
for k, v := range data {
46+
lowerKey := strings.ToLower(k)
47+
if lowerKey == strings.ToLower(fnRunnerElem.Type().Field(i).Name) {
48+
fnRunnerElem.Field(i).SetString(v)
49+
break
50+
}
51+
}
52+
case reflect.Map:
53+
if "data" == strings.ToLower(fnRunnerElem.Type().Field(i).Name) {
54+
fnRunnerElem.Field(i).Set(reflect.ValueOf(data))
55+
}
4556
}
4657
}
4758
case o.IsGVK("fn.kpt.dev", "v1alpha1", fnName):

0 commit comments

Comments
 (0)