Skip to content

Commit c0eedfe

Browse files
committed
ad wasm functionality to apply-setters and rewrite to new go kpt fn sdk
1 parent 1e5d4a1 commit c0eedfe

File tree

8 files changed

+354
-265
lines changed

8 files changed

+354
-265
lines changed

functions/go/apply-setters/applysetters/apply_setters.go

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,14 @@ import (
66
"strings"
77

88
"sigs.k8s.io/kustomize/kyaml/errors"
9-
"sigs.k8s.io/kustomize/kyaml/kio"
109
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
1110
"sigs.k8s.io/kustomize/kyaml/yaml"
1211
)
1312

1413
const SetterCommentIdentifier = "# kpt-set: "
1514

16-
var _ kio.Filter = &ApplySetters{}
17-
18-
// ApplySetters applies the setter values to the resource fields which are tagged
19-
// by the setter reference comments
20-
type ApplySetters struct {
21-
// Setters holds the user provided values for all the setters
22-
Setters []Setter
23-
24-
// Results are the results of applying setter values
25-
Results []*Result
26-
27-
// filePath file path of resource
28-
filePath string
29-
}
30-
31-
type Setter struct {
32-
// Name is the name of the setter
33-
Name string
34-
35-
// Value is the input value for setter
36-
Value string
37-
}
38-
39-
// Result holds result of search and replace operation
40-
type Result struct {
41-
// FilePath is the file path of the matching field
42-
FilePath string
43-
44-
// FieldPath is field path of the matching field
45-
FieldPath string
46-
47-
// Value of the matching field
48-
Value string
49-
}
50-
5115
// Filter implements Set as a yaml.Filter
52-
func (as *ApplySetters) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
16+
func (as *Setters) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
5317
for i := range nodes {
5418
filePath, _, err := kioutil.GetFileAnnotations(nodes[i])
5519
if err != nil {
@@ -77,14 +41,13 @@ environments: # kpt-set: ${env}
7741
- dev
7842
- stage
7943
80-
For input ApplySetters [name: env, value: "[stage, prod]"], qthe yaml node is transformed to
44+
For input Setters [name: env, value: "[stage, prod]"], qthe yaml node is transformed to
8145
8246
environments: # kpt-set: ${env}
8347
- stage
8448
- prod
85-
8649
*/
87-
func (as *ApplySetters) visitMapping(object *yaml.RNode, path string) error {
50+
func (as *Setters) visitMapping(object *yaml.RNode, path string) error {
8851
return object.VisitFields(func(node *yaml.MapNode) error {
8952
if node == nil || node.Key.IsNil() || node.Value.IsNil() {
9053
// don't do IsNilOrEmpty check as empty sequences are allowed
@@ -180,17 +143,18 @@ e.g.for input of scalar node 'nginx:1.7.1 # kpt-set: ${image}:${tag}' in the yam
180143
181144
apiVersion: v1
182145
...
183-
image: nginx:1.7.1 # kpt-set: ${image}:${tag}
184146
185-
and for input ApplySetters [[name: image, value: ubuntu], [name: tag, value: 1.8.0]]
147+
image: nginx:1.7.1 # kpt-set: ${image}:${tag}
148+
149+
and for input Setters [[name: image, value: ubuntu], [name: tag, value: 1.8.0]]
186150
The yaml node is transformed to
187151
188152
apiVersion: v1
189153
...
190-
image: ubuntu:1.8.0 # kpt-set: ${image}:${tag}
191154
155+
image: ubuntu:1.8.0 # kpt-set: ${image}:${tag}
192156
*/
193-
func (as *ApplySetters) visitScalar(object *yaml.RNode, path string) error {
157+
func (as *Setters) visitScalar(object *yaml.RNode, path string) error {
194158
if object.IsNil() {
195159
return nil
196160
}
@@ -358,7 +322,7 @@ func clean(input string) string {
358322
}
359323

360324
// Decode decodes the input yaml node into Set struct
361-
func Decode(rn *yaml.RNode, fcd *ApplySetters) {
325+
func Decode(rn *yaml.RNode, fcd *Setters) {
362326
for k, v := range rn.GetDataMap() {
363327
fcd.Setters = append(fcd.Setters, Setter{Name: k, Value: v})
364328
}

functions/go/apply-setters/applysetters/apply_setters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ roles: # kpt-set: ${roles}
454454
t.FailNow()
455455
}
456456

457-
s := &ApplySetters{}
457+
s := &Setters{}
458458
node, err := kyaml.Parse(test.config)
459459
if !assert.NoError(t, err) {
460460
t.FailNow()
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package applysetters
2+
3+
import (
4+
"fmt"
5+
"sigs.k8s.io/kustomize/kyaml/yaml"
6+
7+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
8+
)
9+
10+
const fnConfigKind = "ApplySetters"
11+
const fnConfigApiVersion = "fn.kpt.dev/v1alpha1"
12+
13+
func ApplySetters(rl *fn.ResourceList) (bool, error) {
14+
r := Setters{}
15+
return r.Process(rl)
16+
}
17+
18+
type Setters struct {
19+
// Setters holds the user provided values for all the setters
20+
Setters []Setter `yaml:"setters,omitempty" json:"setters,omitempty"`
21+
22+
// Results are the results of applying setter values
23+
Results []*Result
24+
25+
// filePath file path of resource
26+
filePath string
27+
}
28+
29+
type Setter struct {
30+
// Name is the name of the setter
31+
Name string `yaml:"name" json:"name"`
32+
33+
// Value is the input value for setter
34+
Value string `yaml:"value" json:"value"`
35+
}
36+
37+
// Result holds result of search and replace operation
38+
type Result struct {
39+
// FilePath is the file path of the matching field
40+
FilePath string
41+
42+
// FieldPath is field path of the matching field
43+
FieldPath string
44+
45+
// Value of the matching field
46+
Value string
47+
}
48+
49+
// Config initializes Setters from a functionConfig fn.KubeObject
50+
func (r *Setters) Config(functionConfig *fn.KubeObject) error {
51+
if functionConfig.IsEmpty() {
52+
return fmt.Errorf("FunctionConfig is missing. Expect `ApplySetters`")
53+
}
54+
if functionConfig.GetKind() != fnConfigKind || functionConfig.GetAPIVersion() != fnConfigApiVersion {
55+
return fmt.Errorf("received functionConfig of kind %s and apiVersion %s, "+
56+
"only functionConfig of kind %s and apiVersion %s is supported",
57+
functionConfig.GetKind(), functionConfig.GetAPIVersion(), fnConfigKind, fnConfigApiVersion)
58+
}
59+
r.Setters = []Setter{}
60+
if err := functionConfig.As(r); err != nil {
61+
return fmt.Errorf("unable to convert functionConfig to %s:\n%w",
62+
"setters", err)
63+
}
64+
return nil
65+
}
66+
67+
// Process configures the setters and transforms them.
68+
func (r *Setters) Process(rl *fn.ResourceList) (bool, error) {
69+
if err := r.Config(rl.FunctionConfig); err != nil {
70+
rl.LogResult(err)
71+
return false, nil
72+
}
73+
transformedItems, err := r.Transform(rl.Items)
74+
if err != nil {
75+
rl.LogResult(err)
76+
return false, nil
77+
}
78+
rl.Items = transformedItems
79+
return true, nil
80+
}
81+
82+
// Transform runs the setters filter in order to apply the setters - this
83+
// does the actual work.
84+
func (r *Setters) Transform(items []*fn.KubeObject) ([]*fn.KubeObject, error) {
85+
var transformedItems []*fn.KubeObject
86+
var nodes []*yaml.RNode
87+
88+
for _, obj := range items {
89+
objRN, err := yaml.Parse(obj.String())
90+
if err != nil {
91+
return nil, err
92+
}
93+
nodes = append(nodes, objRN)
94+
}
95+
96+
transformedNodes, err := r.Filter(nodes)
97+
if err != nil {
98+
return nil, err
99+
}
100+
for _, n := range transformedNodes {
101+
obj, err := fn.ParseKubeObject([]byte(n.MustString()))
102+
if err != nil {
103+
return nil, err
104+
}
105+
transformedItems = append(transformedItems, obj)
106+
}
107+
return transformedItems, nil
108+
}

functions/go/apply-setters/go.mod

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,43 @@ module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-
33
go 1.18
44

55
require (
6-
github.com/stretchr/testify v1.6.1
7-
sigs.k8s.io/kustomize/kyaml v0.10.21
6+
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220723081830-33aee7fe8a2e
7+
github.com/stretchr/testify v1.7.1
8+
sigs.k8s.io/kustomize/api v0.12.1
9+
sigs.k8s.io/kustomize/kyaml v0.13.9
810
)
911

1012
require (
13+
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 // indirect
1114
github.com/PuerkitoBio/purell v1.1.1 // indirect
1215
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
16+
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
1317
github.com/davecgh/go-spew v1.1.1 // indirect
1418
github.com/go-errors/errors v1.0.1 // indirect
15-
github.com/go-openapi/jsonpointer v0.19.3 // indirect
16-
github.com/go-openapi/jsonreference v0.19.3 // indirect
17-
github.com/go-openapi/swag v0.19.5 // indirect
19+
github.com/go-logr/logr v1.2.0 // indirect
20+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
21+
github.com/go-openapi/jsonreference v0.19.6 // indirect
22+
github.com/go-openapi/swag v0.21.1 // indirect
23+
github.com/golang/protobuf v1.5.2 // indirect
24+
github.com/google/gnostic v0.5.7-v3refs // indirect
1825
github.com/inconshreveable/mousetrap v1.0.0 // indirect
19-
github.com/mailru/easyjson v0.7.0 // indirect
26+
github.com/josharian/intern v1.0.0 // indirect
27+
github.com/mailru/easyjson v0.7.7 // indirect
28+
github.com/mitchellh/mapstructure v1.4.1 // indirect
2029
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
30+
github.com/pkg/errors v0.9.1 // indirect
2131
github.com/pmezard/go-difflib v1.0.0 // indirect
22-
github.com/spf13/cobra v1.0.0 // indirect
32+
github.com/spf13/cobra v1.4.0 // indirect
2333
github.com/spf13/pflag v1.0.5 // indirect
24-
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
25-
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
26-
golang.org/x/text v0.3.2 // indirect
34+
github.com/xlab/treeprint v1.1.0 // indirect
35+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
36+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
37+
golang.org/x/text v0.3.7 // indirect
38+
google.golang.org/protobuf v1.28.0 // indirect
2739
gopkg.in/yaml.v2 v2.4.0 // indirect
2840
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
29-
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect
41+
k8s.io/klog/v2 v2.60.1 // indirect
42+
k8s.io/kube-openapi v0.0.0-20220401212409-b28bf2818661 // indirect
43+
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
44+
sigs.k8s.io/yaml v1.3.0 // indirect
3045
)

0 commit comments

Comments
 (0)