@@ -18,7 +18,7 @@ the following features:
18
18
### Create the go module
19
19
20
20
``` shell
21
- $ go mod init github.com/user/repo; go get sigs.k8s.io/kustomize/kyaml@v0.11.1
21
+ $ go mod init github.com/user/repo; go get sigs.k8s.io/kustomize/kyaml@v0.12.0
22
22
```
23
23
24
24
### Create the ` main.go `
@@ -31,39 +31,39 @@ provided value:
31
31
package main
32
32
33
33
import (
34
- " fmt"
35
- " os"
34
+ " fmt"
35
+ " os"
36
36
37
- " sigs.k8s.io/kustomize/kyaml/fn/framework"
38
- " sigs.k8s.io/kustomize/kyaml/fn/framework/command"
39
- " sigs.k8s.io/kustomize/kyaml/yaml"
37
+ " sigs.k8s.io/kustomize/kyaml/fn/framework"
38
+ " sigs.k8s.io/kustomize/kyaml/fn/framework/command"
39
+ " sigs.k8s.io/kustomize/kyaml/kio"
40
+ " sigs.k8s.io/kustomize/kyaml/yaml"
40
41
)
41
42
42
- var value string
43
-
44
43
func main () {
45
- mp := Annotator{}
46
- cmd := command.Build (&mp, command.StandaloneEnabled , false )
47
- cmd.Flags ().StringVar (&value, " value" , " " , " annotation value" )
48
- if err := cmd.Execute (); err != nil {
49
- fmt.Fprintln (os.Stderr , err)
50
- os.Exit (1 )
51
- }
52
- }
53
-
54
- // Annotator implements the ResourceListProcessor interface.
55
- type Annotator struct {}
56
-
57
- var _ framework.ResourceListProcessor = &Annotator{}
58
-
59
- func (mp *Annotator ) Process (resourceList *framework .ResourceList ) error {
60
- for i := range resourceList.Items {
61
- // modify the resources using the kyaml/yaml library:
62
- if err := resourceList.Items [i].PipeE (yaml.SetAnnotation (" myannotation" , value)); err != nil {
63
- return err
64
- }
65
- }
66
- return nil
44
+ // create a struct matching the structure of ResourceList.FunctionConfig to hold its data
45
+ var config struct {
46
+ Data map [string ]string ` yaml:"data"`
47
+ }
48
+ fn := func (items []*yaml.RNode ) ([]*yaml.RNode , error ) {
49
+ for i := range items {
50
+ // set the annotation on each resource item
51
+ err := items[i].PipeE (yaml.SetAnnotation (" myannotation" , config.Data [" myannotation" ]))
52
+ if err != nil {
53
+ return nil , err
54
+ }
55
+ }
56
+ return items, nil
57
+ }
58
+ p := framework.SimpleProcessor {Filter: kio.FilterFunc (fn), Config: &config}
59
+ cmd := command.Build (p, command.StandaloneDisabled , false )
60
+ // Adds a "gen" subcommand to create a Dockerfile for building the function into a container image.
61
+ command.AddGenerateDockerfile (cmd)
62
+
63
+ if err := cmd.Execute (); err != nil {
64
+ fmt.Fprintln (os.Stderr , err)
65
+ os.Exit (1 )
66
+ }
67
67
}
68
68
```
69
69
@@ -84,7 +84,7 @@ $ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/w
84
84
Test it by running the function imperatively:
85
85
86
86
``` shell
87
- $ kpt fn eval wordpress --exec ./my-fn -- value =foo
87
+ $ kpt fn eval wordpress --exec ./my-fn -- myannotation =foo
88
88
```
89
89
90
90
During iterative development, ` --exec ` flag can be used to execute the
@@ -115,7 +115,7 @@ $ docker push gcr.io/project/fn-name:tag
115
115
Run the function imperatively as a container function:
116
116
117
117
``` shell
118
- $ kpt fn eval wordpress -i gcr.io/project/fn-name:tag -- value =foo
118
+ $ kpt fn eval wordpress -i gcr.io/project/fn-name:tag -- myannotation =foo
119
119
```
120
120
121
121
## Next Steps
0 commit comments