Skip to content
Closed
4 changes: 3 additions & 1 deletion config/300-subscription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ spec:
minLength: 1
kind:
type: string
pattern: "^Channel$"
# TODO remove this once we check for subscribable type.
# Ending with Channel. E.g., Channel, InMemoryChannel, etc.
pattern: "Channel$"
name:
type: string
minLength: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"reflect"
"strings"

"github.com/google/go-cmp/cmp"
"github.com/knative/pkg/apis"
Expand All @@ -30,21 +31,22 @@ func isChannelEmpty(f corev1.ObjectReference) bool {
}

// Valid from only contains the following fields:
// - Kind == 'Channel'
// - APIVersion == 'eventing.knative.dev/v1alpha1'
// - Kind ends in 'Channel', e.g., 'Channel', 'InMemoryChannel', etc.
// - APIVersion == 'eventing.knative.dev/v1alpha1' || 'messaging.knative.dev/v1alpha1'
// - Name == not empty
func isValidChannel(f corev1.ObjectReference) *apis.FieldError {
errs := isValidObjectReference(f)

if f.Kind != "Channel" {
// TODO check whether is subscribable instead of doing this suffix match.
if !strings.HasSuffix(f.Kind, "Channel") {
fe := apis.ErrInvalidValue(f.Kind, "kind")
fe.Paths = []string{"kind"}
fe.Details = "only 'Channel' kind is allowed"
fe.Details = "only 'Channel$' kind is allowed"
errs = errs.Also(fe)
}
if f.APIVersion != "eventing.knative.dev/v1alpha1" {
if f.APIVersion != "eventing.knative.dev/v1alpha1" && f.APIVersion != "messaging.knative.dev/v1alpha1" {
fe := apis.ErrInvalidValue(f.APIVersion, "apiVersion")
fe.Details = "only eventing.knative.dev/v1alpha1 is allowed for apiVersion"
fe.Details = "only eventing.knative.dev/v1alpha1 or messaging.knative.dev/v1alpha1 are allowed for apiVersion"
errs = errs.Also(fe)
}
return errs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var validationTests = []struct {
want: &apis.FieldError{
Message: "invalid value: Strait",
Paths: []string{"kind"},
Details: "only 'Channel' kind is allowed",
Details: "only 'Channel$' kind is allowed",
},
},
{
Expand All @@ -52,8 +52,8 @@ var validationTests = []struct {
want: &apis.FieldError{
Message: `invalid value: eventing.knative.dev/v1alpha2`,
Paths: []string{"apiVersion"},
Details: "only eventing.knative.dev/v1alpha1 " +
"is allowed for apiVersion",
Details: "only eventing.knative.dev/v1alpha1 or messaging.knative.dev/v1alpha1 " +
"are allowed for apiVersion",
},
},
{
Expand All @@ -65,6 +65,15 @@ var validationTests = []struct {
},
want: nil,
},
{
name: "valid channel messaging",
ref: corev1.ObjectReference{
Name: "boaty-mcboatface",
APIVersion: "messaging.knative.dev/v1alpha1",
Kind: "InMemoryChannel",
},
want: nil,
},
}

func TestIsChannelEmpty(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/eventing/v1alpha1/subscription_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ type SubscriptionSpec struct {
// - Kind
// - APIVersion
// - Name
// Kind must be "Channel" and APIVersion must be
// "eventing.knative.dev/v1alpha1"
// Kind must end in "Channel". E.g., "Channel", "InMemoryChannel", etc.
// APIVersion must be "eventing.knative.dev/v1alpha1" or "messaging.knative.dev/v1alpha1".
//
// This field is immutable. We have no good answer on what happens to
// the events that are currently in the channel being consumed from
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/eventing/v1alpha1/subscription_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func TestValidChannel(t *testing.T) {
},
want: func() *apis.FieldError {
fe := apis.ErrInvalidValue("", "apiVersion")
fe.Details = "only eventing.knative.dev/v1alpha1 is allowed for apiVersion"
fe.Details = "only eventing.knative.dev/v1alpha1 or messaging.knative.dev/v1alpha1 are allowed for apiVersion"
return apis.ErrMissingField("apiVersion").Also(fe)
}(),
}, {
Expand All @@ -432,7 +432,7 @@ func TestValidChannel(t *testing.T) {
},
want: func() *apis.FieldError {
fe := apis.ErrInvalidValue("", "kind")
fe.Details = "only 'Channel' kind is allowed"
fe.Details = "only 'Channel$' kind is allowed"
return apis.ErrMissingField("kind").Also(fe)
}(),
}, {
Expand All @@ -444,7 +444,7 @@ func TestValidChannel(t *testing.T) {
},
want: func() *apis.FieldError {
fe := apis.ErrInvalidValue("subscription", "kind")
fe.Details = "only 'Channel' kind is allowed"
fe.Details = "only 'Channel$' kind is allowed"
return fe
}(),
}, {
Expand All @@ -456,7 +456,7 @@ func TestValidChannel(t *testing.T) {
},
want: func() *apis.FieldError {
fe := apis.ErrInvalidValue("wrongapiversion", "apiVersion")
fe.Details = "only eventing.knative.dev/v1alpha1 is allowed for apiVersion"
fe.Details = "only eventing.knative.dev/v1alpha1 or messaging.knative.dev/v1alpha1 are allowed for apiVersion"
return fe
}(),
}, {
Expand Down