diff --git a/go.mod b/go.mod index bb2ad45af1c..058bdcec58f 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( k8s.io/utils v0.0.0-20201110183641-67b214c5f920 knative.dev/hack v0.0.0-20210806075220-815cd312d65c knative.dev/hack/schema v0.0.0-20210806075220-815cd312d65c - knative.dev/pkg v0.0.0-20210914164111-4857ab6939e3 + knative.dev/pkg v0.0.0-20210915183108-52b0e2938ecb knative.dev/reconciler-test v0.0.0-20210820180205-a25de6a08087 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index d3e78c0ff98..352740b9a73 100644 --- a/go.sum +++ b/go.sum @@ -1131,8 +1131,8 @@ knative.dev/hack v0.0.0-20210806075220-815cd312d65c/go.mod h1:PHt8x8yX5Z9pPquBEf knative.dev/hack/schema v0.0.0-20210806075220-815cd312d65c h1:YqFCmijfROO3rzIO8u1EMKZXBwAFJMmIoTXcr6wdBy8= knative.dev/hack/schema v0.0.0-20210806075220-815cd312d65c/go.mod h1:ffjwmdcrH5vN3mPhO8RrF2KfNnbHeCE2C60A+2cv3U0= knative.dev/pkg v0.0.0-20210803160015-21eb4c167cc5/go.mod h1:RPk5txNA3apR9X40D4MpUOP9/VqOG8CrtABWfOwGVS4= -knative.dev/pkg v0.0.0-20210914164111-4857ab6939e3 h1:45c2VIOBQP6jpRj+pEyciuzTmBwbZpv8jBfBEf/B5oM= -knative.dev/pkg v0.0.0-20210914164111-4857ab6939e3/go.mod h1:jMSqkNMsrzuy+XR4Yr/BMy7SDVbUOl3KKB6+5MR+ZU8= +knative.dev/pkg v0.0.0-20210915183108-52b0e2938ecb h1:D105ysprqq/uufMiOPUKNXoao6F1USYCEDN32UpJCv4= +knative.dev/pkg v0.0.0-20210915183108-52b0e2938ecb/go.mod h1:jMSqkNMsrzuy+XR4Yr/BMy7SDVbUOl3KKB6+5MR+ZU8= knative.dev/reconciler-test v0.0.0-20210820180205-a25de6a08087 h1:HVcaI8P+CNHl5VgnqWgAzGoc9vbvf3D421rd2qEo3Wc= knative.dev/reconciler-test v0.0.0-20210820180205-a25de6a08087/go.mod h1:+Kovy+G5zXZNcuO/uB+zfo37vFKZzsLIlWezt/nKMz0= pgregory.net/rapid v0.3.3 h1:jCjBsY4ln4Atz78QoBWxUEvAHaFyNDQg9+WU62aCn1U= diff --git a/pkg/apis/sources/v1/apiserver_validation.go b/pkg/apis/sources/v1/apiserver_validation.go index b92fefa3753..1b4774029c0 100644 --- a/pkg/apis/sources/v1/apiserver_validation.go +++ b/pkg/apis/sources/v1/apiserver_validation.go @@ -72,6 +72,6 @@ func (cs *ApiServerSourceSpec) Validate(ctx context.Context) *apis.FieldError { errs = errs.Also(apis.ErrMissingField("kind").ViaField("owner")) } } - + errs = errs.Also(cs.SourceSpec.Validate(ctx)) return errs } diff --git a/pkg/apis/sources/v1/apiserver_validation_test.go b/pkg/apis/sources/v1/apiserver_validation_test.go index 420700b56bb..9c6b509fe88 100644 --- a/pkg/apis/sources/v1/apiserver_validation_test.go +++ b/pkg/apis/sources/v1/apiserver_validation_test.go @@ -204,6 +204,32 @@ func TestAPIServerValidation(t *testing.T) { }, }, want: errors.New("missing field(s): resources"), + }, { + name: "invalid spec ceOverrides validation", + spec: ApiServerSourceSpec{ + EventMode: "Resource", + Resources: []APIVersionKindSelector{{ + APIVersion: "v1", + Kind: "Foo", + }}, + SourceSpec: duckv1.SourceSpec{ + CloudEventOverrides: &duckv1.CloudEventOverrides{ + Extensions: map[string]string{"Invalid_type": "any value"}, + }, + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, + }, + want: apis.ErrInvalidKeyName( + "Invalid_type", + "ceOverrides.extensions", + "keys are expected to be alphanumeric", + ), }} for _, test := range tests { diff --git a/pkg/apis/sources/v1/container_validation.go b/pkg/apis/sources/v1/container_validation.go index 63c7f9cf70c..fae9e5cfd70 100644 --- a/pkg/apis/sources/v1/container_validation.go +++ b/pkg/apis/sources/v1/container_validation.go @@ -44,6 +44,7 @@ func (cs *ContainerSourceSpec) Validate(ctx context.Context) *apis.FieldError { } } } + errs = errs.Also(cs.SourceSpec.Validate(ctx)) return errs } diff --git a/pkg/apis/sources/v1/container_validation_test.go b/pkg/apis/sources/v1/container_validation_test.go index 6917b96d5ec..3ab2f719fa6 100644 --- a/pkg/apis/sources/v1/container_validation_test.go +++ b/pkg/apis/sources/v1/container_validation_test.go @@ -31,74 +31,109 @@ func TestContainerSourceValidation(t *testing.T) { name string spec ContainerSourceSpec want *apis.FieldError - }{{ - name: "missing container", - spec: ContainerSourceSpec{ - Template: corev1.PodTemplateSpec{}, - SourceSpec: duckv1.SourceSpec{ - Sink: duckv1.Destination{ - Ref: &duckv1.KReference{ - APIVersion: "v1", - Kind: "Broker", - Name: "default", + }{ + { + name: "missing container", + spec: ContainerSourceSpec{ + Template: corev1.PodTemplateSpec{}, + SourceSpec: duckv1.SourceSpec{ + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "Broker", + Name: "default", + }, }, }, }, - }, - want: func() *apis.FieldError { - var errs *apis.FieldError - fe := apis.ErrMissingField("containers") - errs = errs.Also(fe) - return errs - }(), - }, { - name: "missing container image", - spec: ContainerSourceSpec{ - Template: corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ - Containers: []corev1.Container{{ - Name: "test-name", - }}, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrMissingField("containers") + errs = errs.Also(fe) + return errs + }(), + }, { + name: "missing container image", + spec: ContainerSourceSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Name: "test-name", + }}, + }, }, - }, - SourceSpec: duckv1.SourceSpec{ - Sink: duckv1.Destination{ - Ref: &duckv1.KReference{ - APIVersion: "eventing.knative.dev/v1", - Kind: "Broker", - Name: "default", + SourceSpec: duckv1.SourceSpec{ + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "eventing.knative.dev/v1", + Kind: "Broker", + Name: "default", + }, }, }, }, - }, - want: func() *apis.FieldError { - var errs *apis.FieldError - fe := apis.ErrMissingField("containers[0].image") - errs = errs.Also(fe) - return errs - }(), - }, { - name: "empty sink", - spec: ContainerSourceSpec{ - Template: corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ - Containers: []corev1.Container{{ - Name: "name", - Image: "image", - }}, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrMissingField("containers[0].image") + errs = errs.Also(fe) + return errs + }(), + }, { + name: "empty sink", + spec: ContainerSourceSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Name: "name", + Image: "image", + }}, + }, + }, + SourceSpec: duckv1.SourceSpec{ + Sink: duckv1.Destination{}, }, }, - SourceSpec: duckv1.SourceSpec{ - Sink: duckv1.Destination{}, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrGeneric("expected at least one, got none", "sink.ref", "sink.uri") + errs = errs.Also(fe) + return errs + }(), + }, { + name: "invalid spec ceOverrides validation", + spec: ContainerSourceSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Name: "name", + Image: "image", + }}, + }, + }, + SourceSpec: duckv1.SourceSpec{ + CloudEventOverrides: &duckv1.CloudEventOverrides{ + Extensions: map[string]string{"Invalid_type": "any value"}, + }, + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, }, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrInvalidKeyName( + "Invalid_type", + "ceOverrides.extensions", + "keys are expected to be alphanumeric", + ) + errs = errs.Also(fe) + return errs + }(), }, - want: func() *apis.FieldError { - var errs *apis.FieldError - fe := apis.ErrGeneric("expected at least one, got none", "sink.ref", "sink.uri") - errs = errs.Also(fe) - return errs - }(), - }, } for _, test := range tests { diff --git a/pkg/apis/sources/v1/ping_validation.go b/pkg/apis/sources/v1/ping_validation.go index c60f49a4f95..30345e3c783 100644 --- a/pkg/apis/sources/v1/ping_validation.go +++ b/pkg/apis/sources/v1/ping_validation.go @@ -94,6 +94,7 @@ func (cs *PingSourceSpec) Validate(ctx context.Context) *apis.FieldError { } } } + errs = errs.Also(cs.SourceSpec.Validate(ctx)) return errs } diff --git a/pkg/apis/sources/v1/ping_validation_test.go b/pkg/apis/sources/v1/ping_validation_test.go index a4ee9092985..dc7f15e5472 100644 --- a/pkg/apis/sources/v1/ping_validation_test.go +++ b/pkg/apis/sources/v1/ping_validation_test.go @@ -265,7 +265,7 @@ func TestPingSourceValidation(t *testing.T) { return errs }(), }, { - name: "invalid DataBase64 is to big ", + name: "invalid DataBase64 is to big", source: PingSource{ Spec: PingSourceSpec{ Schedule: "*/2 * * * *", @@ -292,7 +292,7 @@ func TestPingSourceValidation(t *testing.T) { return errs }(), }, { - name: "invalid Data is to big ", + name: "invalid Data is to big", source: PingSource{ Spec: PingSourceSpec{ Schedule: "*/2 * * * *", @@ -320,7 +320,7 @@ func TestPingSourceValidation(t *testing.T) { return errs }(), }, { - name: "big data ok ", + name: "big data ok", source: PingSource{ Spec: PingSourceSpec{ @@ -343,7 +343,7 @@ func TestPingSourceValidation(t *testing.T) { }, want: nil, }, { - name: "big data still ok ", + name: "big data still ok", source: PingSource{ Spec: PingSourceSpec{ @@ -407,6 +407,36 @@ func TestPingSourceValidation(t *testing.T) { errs = errs.Also(fe) return errs }(), + }, { + name: "invalid spec ceOverrides validation", + source: PingSource{ + Spec: PingSourceSpec{ + Schedule: "*/2 * * * *", + Timezone: "Europe/Paris", + SourceSpec: duckv1.SourceSpec{ + CloudEventOverrides: &duckv1.CloudEventOverrides{ + Extensions: map[string]string{"Invalid_type": "any value"}, + }, + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, + }, + }, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrInvalidKeyName( + "Invalid_type", + "spec.ceOverrides.extensions", + "keys are expected to be alphanumeric", + ) + errs = errs.Also(fe) + return errs + }(), }, } diff --git a/pkg/apis/sources/v1/sinkbinding_validation.go b/pkg/apis/sources/v1/sinkbinding_validation.go index 319742d2009..860bda99f23 100644 --- a/pkg/apis/sources/v1/sinkbinding_validation.go +++ b/pkg/apis/sources/v1/sinkbinding_validation.go @@ -33,6 +33,8 @@ func (fb *SinkBinding) Validate(ctx context.Context) *apis.FieldError { // Validate implements apis.Validatable func (fbs *SinkBindingSpec) Validate(ctx context.Context) *apis.FieldError { - return fbs.Subject.Validate(ctx).ViaField("subject").Also( + err := fbs.Subject.Validate(ctx).ViaField("subject").Also( fbs.Sink.Validate(ctx).ViaField("sink")) + err = err.Also(fbs.SourceSpec.Validate(ctx)) + return err } diff --git a/pkg/apis/sources/v1/sinkbinding_validation_test.go b/pkg/apis/sources/v1/sinkbinding_validation_test.go index 2c2d3c72db3..e8676dd9e0d 100644 --- a/pkg/apis/sources/v1/sinkbinding_validation_test.go +++ b/pkg/apis/sources/v1/sinkbinding_validation_test.go @@ -110,6 +110,42 @@ func TestSinkBindingValidation(t *testing.T) { }, }, want: apis.ErrGeneric("expected at least one, got none", "spec.sink.ref", "spec.sink.uri"), + }, { + name: "invalid spec ceOverrides validation", + in: &SinkBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "gabo", + Namespace: "test", + }, + Spec: SinkBindingSpec{ + BindingSpec: duckv1.BindingSpec{ + Subject: tracker.Reference{ + APIVersion: "apps/v1", + Kind: "Deployment", + Name: "jeanne", + Namespace: "test", + }, + }, + SourceSpec: duckv1.SourceSpec{ + CloudEventOverrides: &duckv1.CloudEventOverrides{ + Extensions: map[string]string{"Invalid_type": "any value"}, + }, + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "serving.knative.dev/v1", + Kind: "Service", + Name: "gemma", + Namespace: "test", + }, + }, + }, + }, + }, + want: apis.ErrInvalidKeyName( + "Invalid_type", + "spec.ceOverrides.extensions", + "keys are expected to be alphanumeric", + ), }} for _, test := range tests { diff --git a/pkg/apis/sources/v1beta2/ping_validation.go b/pkg/apis/sources/v1beta2/ping_validation.go index c8fa47a600d..2f5a088f7fd 100644 --- a/pkg/apis/sources/v1beta2/ping_validation.go +++ b/pkg/apis/sources/v1beta2/ping_validation.go @@ -94,6 +94,7 @@ func (cs *PingSourceSpec) Validate(ctx context.Context) *apis.FieldError { } } } + errs = errs.Also(cs.SourceSpec.Validate(ctx)) return errs } diff --git a/pkg/apis/sources/v1beta2/ping_validation_test.go b/pkg/apis/sources/v1beta2/ping_validation_test.go index 664feb948ed..45b509d73d2 100644 --- a/pkg/apis/sources/v1beta2/ping_validation_test.go +++ b/pkg/apis/sources/v1beta2/ping_validation_test.go @@ -407,6 +407,36 @@ func TestPingSourceValidation(t *testing.T) { errs = errs.Also(fe) return errs }(), + }, { + name: "invalid spec ceOverrides validation", + source: PingSource{ + Spec: PingSourceSpec{ + Schedule: "*/2 * * * *", + Timezone: "Europe/Paris", + SourceSpec: duckv1.SourceSpec{ + CloudEventOverrides: &duckv1.CloudEventOverrides{ + Extensions: map[string]string{"Invalid_type": "any value"}, + }, + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, + }, + }, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrInvalidKeyName( + "Invalid_type", + "spec.ceOverrides.extensions", + "keys are expected to be alphanumeric", + ) + errs = errs.Also(fe) + return errs + }(), }, } diff --git a/vendor/knative.dev/pkg/apis/duck/v1/source_types.go b/vendor/knative.dev/pkg/apis/duck/v1/source_types.go index 00e6991564b..5c5c030a116 100644 --- a/vendor/knative.dev/pkg/apis/duck/v1/source_types.go +++ b/vendor/knative.dev/pkg/apis/duck/v1/source_types.go @@ -180,6 +180,9 @@ func (s *Source) Validate(ctx context.Context) *apis.FieldError { } func (s *SourceSpec) Validate(ctx context.Context) *apis.FieldError { + if s.CloudEventOverrides == nil { + return nil + } return s.CloudEventOverrides.Validate(ctx).ViaField("ceOverrides") } diff --git a/vendor/modules.txt b/vendor/modules.txt index 76f09a7d5de..ff5c6fb82b8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1003,7 +1003,7 @@ knative.dev/hack/schema/commands knative.dev/hack/schema/docs knative.dev/hack/schema/registry knative.dev/hack/schema/schema -# knative.dev/pkg v0.0.0-20210914164111-4857ab6939e3 +# knative.dev/pkg v0.0.0-20210915183108-52b0e2938ecb ## explicit knative.dev/pkg/apiextensions/storageversion knative.dev/pkg/apiextensions/storageversion/cmd/migrate