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
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ required = [

[[constraint]]
name = "github.com/knative/pkg"
# HEAD as of 2018-08-09
revision = "bc264c290f777cc51f53644a1996f5e8887dcba6"
# HEAD as of 2018-08-14
revision = "a3bc2db77a14d9ca6195172e81b4bf33e6190f85"

[[constraint]]
name = "github.com/google/go-containerregistry"
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/serving/v1alpha1/configuration_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
)

func (c *Configuration) Validate() *apis.FieldError {
if err := validateObjectMetadata(c.GetObjectMeta()); err != nil {
return err.ViaField("metadata")
}
return c.Spec.Validate().ViaField("spec")
}

Expand All @@ -35,5 +38,6 @@ func (cs *ConfigurationSpec) Validate() *apis.FieldError {
if cs.RevisionTemplate.Spec.ServingState != "" {
return apis.ErrDisallowedFields("revisionTemplate.spec.servingState")
}

return cs.RevisionTemplate.Validate().ViaField("revisionTemplate")
}
18 changes: 18 additions & 0 deletions pkg/apis/serving/v1alpha1/configuration_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package v1alpha1

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/pkg/apis"
)
Expand Down Expand Up @@ -119,6 +121,22 @@ func TestConfigurationValidation(t *testing.T) {
name: "empty spec",
c: &Configuration{},
want: apis.ErrMissingField("spec"),
}, {
name: "invalid name - dots",
c: &Configuration{
ObjectMeta: metav1.ObjectMeta{
Name: "do.not.use.dots",
},
},
want: &apis.FieldError{Message: "Invalid resource name: special character . must not be present", Paths: []string{"metadata.name"}},
}, {
name: "invalid name - too long",
c: &Configuration{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Repeat("a", 65),
},
},
want: &apis.FieldError{Message: "Invalid resource name: length must be no more than 63 characters", Paths: []string{"metadata.name"}},
}}

for _, test := range tests {
Expand Down
43 changes: 43 additions & 0 deletions pkg/apis/serving/v1alpha1/metadata_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2017 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"strings"

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

func validateObjectMetadata(meta metav1.Object) *apis.FieldError {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

name := meta.GetName()

if strings.Contains(name, ".") {
return &apis.FieldError{
Message: "Invalid resource name: special character . must not be present",
Paths: []string{"name"},
}
}

if len(name) > 63 {
return &apis.FieldError{
Message: "Invalid resource name: length must be no more than 63 characters",
Paths: []string{"name"},
}
}
return nil
}
3 changes: 3 additions & 0 deletions pkg/apis/serving/v1alpha1/revision_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
)

func (rt *Revision) Validate() *apis.FieldError {
if err := validateObjectMetadata(rt.GetObjectMeta()); err != nil {
return err.ViaField("metadata")
}
return rt.Spec.Validate().ViaField("spec")
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/serving/v1alpha1/revision_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package v1alpha1

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/knative/pkg/apis"
Expand Down Expand Up @@ -363,6 +365,22 @@ func TestRevisionValidation(t *testing.T) {
},
},
want: apis.ErrDisallowedFields("spec.container.name"),
}, {
name: "invalid name - dots",
r: &Revision{
ObjectMeta: metav1.ObjectMeta{
Name: "do.not.use.dots",
},
},
want: &apis.FieldError{Message: "Invalid resource name: special character . must not be present", Paths: []string{"metadata.name"}},
}, {
name: "invalid name - too long",
r: &Revision{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Repeat("a", 65),
},
},
want: &apis.FieldError{Message: "Invalid resource name: length must be no more than 63 characters", Paths: []string{"metadata.name"}},
}}

for _, test := range tests {
Expand Down
7 changes: 5 additions & 2 deletions pkg/apis/serving/v1alpha1/route_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import (
"k8s.io/apimachinery/pkg/util/validation"
)

func (rt *Route) Validate() *apis.FieldError {
return rt.Spec.Validate().ViaField("spec")
func (r *Route) Validate() *apis.FieldError {
if err := validateObjectMetadata(r.GetObjectMeta()); err != nil {
return err.ViaField("metadata")
}
return r.Spec.Validate().ViaField("spec")
}

func (rs *RouteSpec) Validate() *apis.FieldError {
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/serving/v1alpha1/route_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package v1alpha1

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/pkg/apis"
)
Expand Down Expand Up @@ -73,6 +75,22 @@ func TestRouteValidation(t *testing.T) {
"spec.traffic[0].configurationName",
},
},
}, {
name: "invalid name - dots",
r: &Route{
ObjectMeta: metav1.ObjectMeta{
Name: "do.not.use.dots",
},
},
want: &apis.FieldError{Message: "Invalid resource name: special character . must not be present", Paths: []string{"metadata.name"}},
}, {
name: "invalid name - too long",
r: &Route{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Repeat("a", 65),
},
},
want: &apis.FieldError{Message: "Invalid resource name: length must be no more than 63 characters", Paths: []string{"metadata.name"}},
}}

for _, test := range tests {
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/serving/v1alpha1/service_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
)

func (s *Service) Validate() *apis.FieldError {
if err := validateObjectMetadata(s.GetObjectMeta()); err != nil {
return err.ViaField("metadata")
}
return s.Spec.Validate().ViaField("spec")
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/serving/v1alpha1/service_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package v1alpha1

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/pkg/apis"
)
Expand Down Expand Up @@ -146,6 +148,22 @@ func TestServiceValidation(t *testing.T) {
},
},
want: apis.ErrDisallowedFields("spec.pinned.configuration.revisionTemplate.spec.container.name"),
}, {
name: "invalid name - dots",
s: &Service{
ObjectMeta: metav1.ObjectMeta{
Name: "do.not.use.dots",
},
},
want: &apis.FieldError{Message: "Invalid resource name: special character . must not be present", Paths: []string{"metadata.name"}},
}, {
name: "invalid name - too long",
s: &Service{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Repeat("a", 65),
},
},
want: &apis.FieldError{Message: "Invalid resource name: length must be no more than 63 characters", Paths: []string{"metadata.name"}},
}}

for _, test := range tests {
Expand Down
17 changes: 0 additions & 17 deletions vendor/github.com/knative/pkg/webhook/webhook.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.