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
9 changes: 8 additions & 1 deletion pkg/apis/eventing/v1/broker_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const (

func (b *Broker) Validate(ctx context.Context) *apis.FieldError {
withNS := apis.AllowDifferentNamespace(apis.WithinParent(ctx, b.ObjectMeta))
return b.Spec.Validate(withNS).ViaField("spec")

// Make sure a BrokerClassAnnotation exists
var errs *apis.FieldError
if bc, ok := b.GetAnnotations()[BrokerClassAnnotationKey]; !ok || bc == "" {
errs = errs.Also(apis.ErrMissingField(BrokerClassAnnotationKey))
}

return errs.Also(b.Spec.Validate(withNS).ViaField("spec"))
}

func (bs *BrokerSpec) Validate(ctx context.Context) *apis.FieldError {
Expand Down
44 changes: 39 additions & 5 deletions pkg/apis/eventing/v1/broker_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func TestBrokerImmutableFields(t *testing.T) {
"nil original": {
wantErr: nil,
},
"no ChannelTemplateSpec mutation": {
"no BrokerClassAnnotation mutation": {
og: current,
wantErr: nil,
},
"ChannelTemplateSpec mutated": {
"BrokerClassAnnotation mutated": {
og: original,
wantErr: &apis.FieldError{
Message: "Immutable fields changed (-old +new)",
Expand Down Expand Up @@ -80,11 +80,30 @@ func TestValidate(t *testing.T) {
b Broker
want *apis.FieldError
}{{
name: "valid empty",
name: "missing annotation",
b: Broker{},
want: apis.ErrMissingField("eventing.knative.dev/broker.class"),
}, {
name: "empty annotation",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": ""},
},
},
want: apis.ErrMissingField("eventing.knative.dev/broker.class"),
}, {
name: "valid empty",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
},
}, {
name: "valid config",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -97,6 +116,9 @@ func TestValidate(t *testing.T) {
}, {
name: "valid config, no namespace",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Name: "name",
Expand All @@ -108,6 +130,9 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid config, missing name",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -120,6 +145,9 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid config, missing apiVersion",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -132,6 +160,9 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid config, missing kind",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -144,19 +175,22 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid delivery, invalid delay string",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Delivery: &eventingduckv1.DeliverySpec{
BackoffDelay: &invalidString,
},
},
},
want: apis.ErrInvalidValue(invalidString, "spec.delivery.backoffDelay"),
}, {}}
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.b.Validate(context.Background())
if diff := cmp.Diff(test.want.Error(), got.Error()); diff != "" {
t.Errorf("BrokerSpec.Validate (-want, +got) = %v", diff)
t.Errorf("Broker.Validate (-want, +got) = %v", diff)
}
})
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/eventing/v1beta1/broker_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const (

func (b *Broker) Validate(ctx context.Context) *apis.FieldError {
withNS := apis.AllowDifferentNamespace(apis.WithinParent(ctx, b.ObjectMeta))
return b.Spec.Validate(withNS).ViaField("spec")

// Make sure a BrokerClassAnnotation exists
var errs *apis.FieldError
if bc, ok := b.GetAnnotations()[BrokerClassAnnotationKey]; !ok || bc == "" {
errs = errs.Also(apis.ErrMissingField(BrokerClassAnnotationKey))
}

return errs.Also(b.Spec.Validate(withNS).ViaField("spec"))
}

func (bs *BrokerSpec) Validate(ctx context.Context) *apis.FieldError {
Expand Down
44 changes: 39 additions & 5 deletions pkg/apis/eventing/v1beta1/broker_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func TestBrokerImmutableFields(t *testing.T) {
"nil original": {
wantErr: nil,
},
"no ChannelTemplateSpec mutation": {
"no BrokerClassAnnotation mutation": {
og: current,
wantErr: nil,
},
"ChannelTemplateSpec mutated": {
"BrokerClassAnnotation mutated": {
og: original,
wantErr: &apis.FieldError{
Message: "Immutable fields changed (-old +new)",
Expand Down Expand Up @@ -80,11 +80,30 @@ func TestValidate(t *testing.T) {
b Broker
want *apis.FieldError
}{{
name: "valid empty",
name: "missing annotation",
b: Broker{},
want: apis.ErrMissingField("eventing.knative.dev/broker.class"),
}, {
name: "empty annotation",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": ""},
},
},
want: apis.ErrMissingField("eventing.knative.dev/broker.class"),
}, {
name: "valid empty",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
},
}, {
name: "valid config",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -97,6 +116,9 @@ func TestValidate(t *testing.T) {
}, {
name: "valid config, no namespace",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Name: "name",
Expand All @@ -108,6 +130,9 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid config, missing name",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -120,6 +145,9 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid config, missing apiVersion",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -132,6 +160,9 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid config, missing kind",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Namespace: "namespace",
Expand All @@ -144,19 +175,22 @@ func TestValidate(t *testing.T) {
}, {
name: "invalid delivery, invalid delay string",
b: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
},
Spec: BrokerSpec{
Delivery: &eventingduckv1beta1.DeliverySpec{
BackoffDelay: &invalidString,
},
},
},
want: apis.ErrInvalidValue(invalidString, "spec.delivery.backoffDelay"),
}, {}}
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.b.Validate(context.Background())
if diff := cmp.Diff(test.want.Error(), got.Error()); diff != "" {
t.Errorf("BrokerSpec.Validate (-want, +got) = %v", diff)
t.Errorf("Broker.Validate (-want, +got) = %v", diff)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/mtnamespace/resources/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
func MakeBroker(ns *corev1.Namespace) *v1beta1.Broker {
return &v1beta1.Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"eventing.knative.dev/broker.class": "MTChannelBasedBroker"},
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.

There are some constants available for both of these hardcoded strings if you want to change them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(ns.GetObjectMeta(), schema.GroupVersionKind{
Group: corev1.SchemeGroupVersion.Group,
Expand Down