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

Add validation to the revisions provided in the revision list. #3045

Merged
merged 3 commits into from
Jan 31, 2019
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
8 changes: 8 additions & 0 deletions pkg/apis/serving/v1alpha1/service_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package v1alpha1

import (
"fmt"
"strconv"

"github.com/knative/pkg/apis"
"k8s.io/apimachinery/pkg/util/validation"
)

// Validate validates the fields belonging to Service
Expand Down Expand Up @@ -96,6 +98,12 @@ func (rt *ReleaseType) Validate() *apis.FieldError {
if numRevisions > 2 {
errs = errs.Also(apis.ErrOutOfBoundsValue(strconv.Itoa(numRevisions), "1", "2", "revisions"))
}
for i, r := range rt.Revisions {
if msgs := validation.IsDNS1035Label(r); len(msgs) > 0 {
errs = errs.Also(apis.ErrInvalidValue(
fmt.Sprintf("not a DNS 1035 label: %v", msgs), apis.CurrentField).ViaFieldIndex("revisions", i))
}
}

if numRevisions < 2 && rt.RolloutPercent != 0 {
errs = errs.Also(apis.ErrInvalidValue(strconv.Itoa(rt.RolloutPercent), "rolloutPercent"))
Expand Down
48 changes: 47 additions & 1 deletion pkg/apis/serving/v1alpha1/service_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/knative/pkg/apis"
)

const incorrectDNS1035Label = "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])?')]"

func TestServiceValidation(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -244,6 +246,50 @@ func TestServiceValidation(t *testing.T) {
},
},
want: apis.ErrMissingField("spec.release.revisions"),
}, {
name: "invalid release -- revision name invalid, long",
s: &Service{
ObjectMeta: metav1.ObjectMeta{
Name: "valid",
},
Spec: ServiceSpec{
Release: &ReleaseType{
Revisions: []string{strings.Repeat("a", 64)},
Configuration: ConfigurationSpec{
RevisionTemplate: RevisionTemplateSpec{
Spec: RevisionSpec{
Container: corev1.Container{
Image: "hellworld",
},
},
},
},
},
},
},
want: apis.ErrInvalidValue("not a DNS 1035 label: [must be no more than 63 characters]", "spec.release.revisions[0]"),
}, {
name: "invalid release -- revision name invalid, incorrect",
s: &Service{
ObjectMeta: metav1.ObjectMeta{
Name: "valid",
},
Spec: ServiceSpec{
Release: &ReleaseType{
Revisions: []string{".negative"},
Configuration: ConfigurationSpec{
RevisionTemplate: RevisionTemplateSpec{
Spec: RevisionSpec{
Container: corev1.Container{
Image: "hellworld",
},
},
},
},
},
},
},
want: apis.ErrInvalidValue(incorrectDNS1035Label, "spec.release.revisions[0]"),
}, {
name: "invalid release -- too few revisions; empty slice",
s: &Service{
Expand Down Expand Up @@ -378,7 +424,7 @@ func TestServiceValidation(t *testing.T) {
},
},
want: &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])?')]",
Message: incorrectDNS1035Label,
Paths: []string{"metadata.name"},
},
}, {
Expand Down