Skip to content
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
6 changes: 6 additions & 0 deletions acceptance.bats
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
[ "$output" = "PASS - fixtures/null_string.yaml contains a valid Service (frontend)" ]
}

@test "Pass when parsing a valid Kubernetes config YAML file with generate name" {
run bin/kubeval fixtures/generate_name.yaml
[ "$status" -eq 0 ]
[ "$output" = "PASS - fixtures/generate_name.yaml contains a valid Job (pi-{{ generateName }})" ]
}

@test "Pass when parsing a multi-document config file" {
run bin/kubeval fixtures/multi_valid.yaml
[ "$status" -eq 0 ]
Expand Down
13 changes: 13 additions & 0 deletions fixtures/generate_name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: batch/v1
kind: Job
metadata:
generateName: pi-
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
22 changes: 12 additions & 10 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,19 @@ func validateResource(data []byte, schemaCache map[string]*gojsonschema.Schema,
return result, body, nil
}

name, err := getStringAt(body, []string{"metadata", "name"})
if err != nil {
return result, body, fmt.Errorf("%s: %s", result.FileName, err.Error())
}
result.ResourceName = name

namespace, err := getStringAt(body, []string{"metadata", "namespace"})
if err != nil {
result.ResourceNamespace = "default"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this code had any effect in the first place because it would eventually be overwritten.

metadata, _ := getObject(body, "metadata")
if metadata != nil {
namespace, _ := getString(metadata, "namespace")
name, _ := getString(metadata, "name")
generateName, _ := getString(metadata, "generateName")

if len(name) == 0 && len(generateName) > 0 {
result.ResourceName = fmt.Sprintf("%s{{ generateName }}", generateName)
Copy link
Contributor Author

@mpon mpon Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to express that this resource name would contain the generated stuff, but I couldn't think of anything else, so if there's a better way to express it, I'd change it.

} else {
result.ResourceName = name
}
result.ResourceNamespace = namespace
}
result.ResourceNamespace = namespace

kind, err := getString(body, "kind")
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions kubeval/kubeval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestValidateValidInputs(t *testing.T) {
"comment.yaml",
"valid.yaml",
"valid.json",
"generate_name.yaml",
"multi_valid.yaml",
"int_or_string.yaml",
"null_array.yaml",
Expand Down Expand Up @@ -60,6 +61,7 @@ func TestValidateValidInputsWithCache(t *testing.T) {
"comment.yaml",
"valid.yaml",
"valid.json",
"generate_name.yaml",
"multi_valid.yaml",
"int_or_string.yaml",
"null_array.yaml",
Expand Down