Skip to content

Commit 13d5ad4

Browse files
author
Mengqi Yu
authored
update golang dev doc (#2525)
1 parent 2a9f413 commit 13d5ad4

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

site/book/05-developing-functions/02-developing-in-Go.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ the following features:
1818
### Create the go module
1919

2020
```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
2222
```
2323

2424
### Create the `main.go`
@@ -31,39 +31,39 @@ provided value:
3131
package main
3232

3333
import (
34-
"fmt"
35-
"os"
34+
"fmt"
35+
"os"
3636

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"
4041
)
4142

42-
var value string
43-
4443
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+
}
6767
}
6868
```
6969

@@ -84,7 +84,7 @@ $ kpt pkg get https://github.com/GoogleContainerTools/kpt.git/package-examples/w
8484
Test it by running the function imperatively:
8585

8686
```shell
87-
$ kpt fn eval wordpress --exec ./my-fn -- value=foo
87+
$ kpt fn eval wordpress --exec ./my-fn -- myannotation=foo
8888
```
8989

9090
During iterative development, `--exec` flag can be used to execute the
@@ -115,7 +115,7 @@ $ docker push gcr.io/project/fn-name:tag
115115
Run the function imperatively as a container function:
116116

117117
```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
119119
```
120120

121121
## Next Steps

0 commit comments

Comments
 (0)