Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate api layer apply-setters #266

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package applysetters

import (
"fmt"
Expand All @@ -10,6 +10,8 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)

const SetterCommentIdentifier = "# kpt-set: "

var _ kio.Filter = ApplySetters{}

// ApplySetters applies the setter values to the resource fields which are tagged
Expand All @@ -27,8 +29,6 @@ type Setter struct {
Value string `json:"value,omitempty" yaml:"value,omitempty"`
}

const SetterCommentIdentifier = "# kpt-set: "

// Filter implements Set as a yaml.Filter
func (as ApplySetters) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
for i := range nodes {
Expand Down Expand Up @@ -226,6 +226,11 @@ func currentSetterValues(pattern, value string) map[string]string {
// first value is just entire value, so skip it
continue
}
if setterValues[i] == "" {
// if any of the value is unresolved return empty map
// and expect users to provide all values
return make(map[string]string)
}
res[setterNames[i]] = setterValues[i]
}
return res
Expand Down Expand Up @@ -275,3 +280,10 @@ func clean(input string) string {
input = strings.TrimSpace(input)
return strings.TrimSuffix(strings.TrimPrefix(input, "${"), "}")
}

// Decode decodes the input yaml node into Set struct
func Decode(rn *yaml.RNode, fcd *ApplySetters) {
for k, v := range rn.GetDataMap() {
fcd.Setters = append(fcd.Setters, Setter{Name: k, Value: v})
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package applysetters

import (
"io/ioutil"
Expand Down Expand Up @@ -310,10 +310,7 @@ spec:
if !assert.NoError(t, err) {
t.FailNow()
}
err = decode(node, s)
if !assert.NoError(t, err) {
t.FailNow()
}
Decode(node, s)
if !assert.NoError(t, err) {
t.FailNow()
}
Expand Down Expand Up @@ -385,6 +382,18 @@ var resolvePatternCases = []patternTest{
pattern: `${image}:${tag}`,
expected: map[string]string{},
},
{
name: "setter values from pattern unresolved 2",
value: "nginx:1.2",
pattern: `${image}${tag}`,
expected: map[string]string{},
},
{
name: "setter values from pattern unresolved 3",
value: "my-project/nginx:1.2",
pattern: `${project-id}/${image}${tag}`,
expected: map[string]string{},
},
}

func TestCurrentSetterValues(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package applysetters

import (
"sigs.k8s.io/kustomize/kyaml/yaml"
Expand Down
17 changes: 5 additions & 12 deletions functions/go/apply-setters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters/applysetters"
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters/generated"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
Expand Down Expand Up @@ -37,8 +38,8 @@ func main() {
}

// getSetters retrieve the setters from input config
func getSetters(fc interface{}) (ApplySetters, error) {
var fcd ApplySetters
func getSetters(fc interface{}) (applysetters.ApplySetters, error) {
var fcd applysetters.ApplySetters
f, ok := fc.(map[string]interface{})
if !ok {
return fcd, fmt.Errorf("function config %#v is not valid", fc)
Expand All @@ -47,14 +48,6 @@ func getSetters(fc interface{}) (ApplySetters, error) {
if err != nil {
return fcd, fmt.Errorf("failed to parse input from function config: %w", err)
}

return fcd, decode(rn, &fcd)
}

// decode decodes the input yaml node into Set struct
func decode(rn *kyaml.RNode, fcd *ApplySetters) error {
for k, v := range rn.GetDataMap() {
fcd.Setters = append(fcd.Setters, Setter{Name: k, Value: v})
}
return nil
applysetters.Decode(rn, &fcd)
return fcd, nil
}