Skip to content

Commit

Permalink
fix validation so users can use generateName (#3181)
Browse files Browse the repository at this point in the history
  • Loading branch information
dprotaso authored and knative-prow-robot committed Feb 12, 2019
1 parent 352fcdd commit f6eb21f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/apis/serving/v1alpha1/metadata_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,22 @@ import (
// resources is correct.
func ValidateObjectMetadata(meta metav1.Object) *apis.FieldError {
name := meta.GetName()
generateName := meta.GetGenerateName()

msgs := validation.IsDNS1035Label(name)
if len(msgs) > 0 {
if name != "" {
msgs := validation.IsDNS1035Label(name)

if len(msgs) > 0 {
return &apis.FieldError{
Message: fmt.Sprintf("not a DNS 1035 label: %v", msgs),
Paths: []string{"name"},
}
}
}

if generateName == "" && name == "" {
return &apis.FieldError{
Message: fmt.Sprintf("not a DNS 1035 label: %v", msgs),
Message: "name or generateName is required",
Paths: []string{"name"},
}
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/apis/serving/v1alpha1/metadata_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package v1alpha1
import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/knative/pkg/apis"
"github.com/knative/serving/pkg/apis/autoscaling"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestValidateScaleBoundAnnotations(t *testing.T) {
Expand Down Expand Up @@ -96,3 +98,53 @@ func TestValidateScaleBoundAnnotations(t *testing.T) {
})
}
}

func TestValidateObjectMetadata(t *testing.T) {
cases := []struct {
name string
objectMeta metav1.Object
expectErr error
}{{
name: "invalid name - dots",
objectMeta: &metav1.ObjectMeta{
Name: "do.not.use.dots",
},
expectErr: &apis.FieldError{
Message: "not a DNS 1035 label: [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]",
Paths: []string{"name"},
},
}, {
name: "invalid name - too long",
objectMeta: &metav1.ObjectMeta{
Name: strings.Repeat("a", 64),
},
expectErr: &apis.FieldError{
Message: "not a DNS 1035 label: [must be no more than 63 characters]",
Paths: []string{"name"},
},
}, {
name: "valid generateName",
objectMeta: &metav1.ObjectMeta{
GenerateName: "some-name",
},
expectErr: (*apis.FieldError)(nil),
}, {
name: "missing name and generateName",
objectMeta: &metav1.ObjectMeta{},
expectErr: &apis.FieldError{
Message: "name or generateName is required",
Paths: []string{"name"},
},
}}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {

err := ValidateObjectMetadata(c.objectMeta)

if !reflect.DeepEqual(c.expectErr, err) {
t.Errorf("Expected: '%#v', Got: '%#v'", c.expectErr, err)
}
})
}
}

0 comments on commit f6eb21f

Please sign in to comment.