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

Apply setters empty values checks #299

Merged
merged 2 commits into from
May 13, 2021
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
13 changes: 12 additions & 1 deletion functions/go/apply-setters/applysetters/apply_setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Setter struct {

// Filter implements Set as a yaml.Filter
func (as ApplySetters) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
if len(as.Setters) == 0 {
return nodes, fmt.Errorf("failed to configure function: input setters list cannot be empty")
}
for i := range nodes {
err := accept(&as, nodes[i])
if err != nil {
Expand Down Expand Up @@ -91,10 +94,18 @@ func (as *ApplySetters) visitMapping(object *yaml.RNode) error {
// get the setter value for the setter name in the comment
sv := setterValue(as.Setters, setterPattern)

if sv == "" {
var elements []*yaml.Node
elements = append(elements, &yaml.Node{})
node.Value.YNode().Content = elements
node.Value.YNode().Style = yaml.FoldedStyle
return nil
}

// parse the setter value as yaml node
rn, err := yaml.Parse(sv)
if err != nil {
return err
return errors.Errorf("input to array setter must be an array of values, but found %q", sv)
}

// the setter value must parse as sequence node
Expand Down
60 changes: 60 additions & 0 deletions functions/go/apply-setters/applysetters/apply_setters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,66 @@ metadata:
name: my-app-layer
spec:
host: my-app-layer.dev.demo.io # kpt-set: my-app-layer.${stage}.${domain}.${tld}
`,
},
{
name: "error: no input",
config: `
data: {}
`,
input: `apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
template:
spec:
containers:
- name: nginx
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
`,
expectedResources: `apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
template:
spec:
containers:
- name: nginx
image: nginx:1.7.9 # kpt-set: ${image}:${tag}
`,
errMsg: `failed to configure function: input setters list cannot be empty`,
},
{
name: "set empty values",
input: `apiVersion: v1
kind: Service
metadata:
name: myService # kpt-set: ${app}
namespace: foo # kpt-set: ${ns}
image: nginx:1.7.1 # kpt-set: ${image}:${tag}
env: # kpt-set: ${env}
- foo
- bar
`,
config: `
data:
app: ""
ns: ~
image: ''
env: ~
`,
expectedResources: `apiVersion: v1
kind: Service
metadata:
name: # kpt-set: ${app}
namespace: # kpt-set: ${ns}
image: :1.7.1 # kpt-set: ${image}:${tag}
env: # kpt-set: ${env}
- null
`,
},
}
Expand Down