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

Remove assumption on default namespace for EventType reference #7724

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 9 additions & 1 deletion pkg/apis/eventing/v1beta1/eventtype_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ limitations under the License.

package v1beta1

import "context"
import (
"context"

"knative.dev/pkg/apis"
)

func (et *EventType) SetDefaults(ctx context.Context) {
ctx = apis.WithinParent(ctx, et.ObjectMeta)
et.Spec.SetDefaults(ctx)
}

func (ets *EventTypeSpec) SetDefaults(ctx context.Context) {
if ets.Reference == nil && ets.Broker == "" {
ets.Broker = "default"
}
if ets.Reference != nil {
ets.Reference.SetDefaults(ctx)
}
}
11 changes: 5 additions & 6 deletions pkg/apis/eventing/v1beta2/eventtype_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ package v1beta2

import (
"context"

"knative.dev/pkg/apis"
)

func (et *EventType) SetDefaults(ctx context.Context) {
ctx = apis.WithinParent(ctx, et.ObjectMeta)
et.Spec.SetDefaults(ctx)
setReferenceNs(et)
}

func (ets *EventTypeSpec) SetDefaults(ctx context.Context) {
}

func setReferenceNs(et *EventType) {
if et.Spec.Reference != nil && et.Spec.Reference.Namespace == "" {
et.Spec.Reference.Namespace = et.GetNamespace()
if ets.Reference != nil {
ets.Reference.SetDefaults(ctx)
}
}
11 changes: 5 additions & 6 deletions pkg/apis/eventing/v1beta3/eventtype_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ package v1beta3

import (
"context"

"knative.dev/pkg/apis"
)

func (et *EventType) SetDefaults(ctx context.Context) {
ctx = apis.WithinParent(ctx, et.ObjectMeta)
et.Spec.SetDefaults(ctx)
setReferenceNs(et)
}

func (ets *EventTypeSpec) SetDefaults(ctx context.Context) {
}

func setReferenceNs(et *EventType) {
if et.Spec.Reference != nil && et.Spec.Reference.Namespace == "" {
et.Spec.Reference.Namespace = et.GetNamespace()
if ets.Reference != nil {
ets.Reference.SetDefaults(ctx)
}
}
5 changes: 1 addition & 4 deletions pkg/reconciler/source/duck/duck.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@ func (r *Reconciler) makeEventTypes(ctx context.Context, src *duckv1.Source) []v
CeSchema: schemaURL,
Description: description,
})
if eventType.Spec.Reference.Namespace == "" {
eventType.Spec.Reference.Namespace = defaultNamespace
}
eventTypes = append(eventTypes, *eventType)
}
return eventTypes
Expand All @@ -235,7 +232,7 @@ func (r *Reconciler) computeDiff(current []v1beta2.EventType, expected []v1beta2
if c, ok := currentMap[keyFromEventType(&e)]; !ok {
toCreate = append(toCreate, e)
} else {
if !equality.Semantic.DeepEqual(e.Spec, c.Spec) {
if !equality.Semantic.DeepDerivative(e.Spec, c.Spec) {
Cali0707 marked this conversation as resolved.
Show resolved Hide resolved
toDelete = append(toDelete, c)
toCreate = append(toCreate, e)
}
Expand Down
61 changes: 52 additions & 9 deletions pkg/reconciler/source/duck/duck_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in pkg/reconciler/source/duck/duck_test.go

View workflow job for this annotation

GitHub Actions / style / Golang / Auto-format and Check

Please run goimports. diff --git a/pkg/reconciler/source/duck/duck_test.go b/pkg/reconciler/source/duck/duck_test.go index c79ab66..c68482a 100644 --- a/pkg/reconciler/source/duck/duck_test.go +++ b/pkg/reconciler/source/duck/duck_test.go @@ -270,7 +270,7 @@ func TestAllCases(t *testing.T) { }(), makeEventType("my-type-1", "http://my-source-1"), }, - Key: testNS + "/" + sourceName, + Key: testNS + "/" + sourceName, WantCreates: []runtime.Object{}, }, { Name: "no op with namespace", @@ -294,7 +294,7 @@ func TestAllCases(t *testing.T) { return et }(), }, - Key: testNS + "/" + sourceName, + Key: testNS + "/" + sourceName, WantCreates: []runtime.Object{}, }}
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,10 +31,6 @@
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientgotesting "k8s.io/client-go/testing"
v1 "knative.dev/eventing/pkg/apis/duck/v1"
"knative.dev/eventing/pkg/apis/eventing"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/reconciler/source/duck/resources"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/client/injection/ducks/duck/v1/source"
Expand All @@ -43,8 +39,14 @@
logtesting "knative.dev/pkg/logging/testing"
"knative.dev/pkg/ptr"

. "knative.dev/eventing/pkg/reconciler/testing/v1beta2"
v1 "knative.dev/eventing/pkg/apis/duck/v1"
"knative.dev/eventing/pkg/apis/eventing"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/reconciler/source/duck/resources"

. "knative.dev/pkg/reconciler/testing"

. "knative.dev/eventing/pkg/reconciler/testing/v1beta2"
)

const (
Expand Down Expand Up @@ -250,6 +252,50 @@
WantCreates: []runtime.Object{
makeEventType("my-type-1", "http://my-source-1"),
},
}, {
Name: "no op",
Objects: []runtime.Object{
makeSource([]duckv1.CloudEventAttributes{{
Type: "my-type-1",
Source: "http://my-source-1",
}}),
func() runtime.Object {
s := makeSourceCRD([]eventTypeEntry{{
Type: "my-type-1",
Schema: "/some-schema-from-crd",
Description: "This came from the annotation in a crd for the source.",
}})
s.Annotations[eventing.EventTypesAnnotationKey] = "something that is not valid json"
return s
}(),
makeEventType("my-type-1", "http://my-source-1"),
},
Key: testNS + "/" + sourceName,
WantCreates: []runtime.Object{},
pierDipi marked this conversation as resolved.
Show resolved Hide resolved
}, {
Name: "no op with namespace",
Objects: []runtime.Object{
makeSource([]duckv1.CloudEventAttributes{{
Type: "my-type-1",
Source: "http://my-source-1",
}}),
func() runtime.Object {
s := makeSourceCRD([]eventTypeEntry{{
Type: "my-type-1",
Schema: "/some-schema-from-crd",
Description: "This came from the annotation in a crd for the source.",
}})
s.Annotations[eventing.EventTypesAnnotationKey] = "something that is not valid json"
return s
}(),
func() runtime.Object {
et := makeEventType("my-type-1", "http://my-source-1")
et.Spec.Reference.Namespace = testNS
return et
}(),
},
Key: testNS + "/" + sourceName,
WantCreates: []runtime.Object{},
pierDipi marked this conversation as resolved.
Show resolved Hide resolved
}}

logger := logtesting.TestLogger(t)
Expand Down Expand Up @@ -340,14 +386,11 @@
}

func makeEventType(ceType, ceSource string) *v1beta2.EventType {
return makeEventTypeWithReference(ceType, ceSource, brokerDest.Ref)
return makeEventTypeWithReference(ceType, ceSource, brokerDest.Ref.DeepCopy())
}

func makeEventTypeWithReference(ceType, ceSource string, ref *duckv1.KReference) *v1beta2.EventType {
ceSourceURL, _ := apis.ParseURL(ceSource)
if ref.Namespace == "" {
ref.Namespace = "default"
}
return &v1beta2.EventType{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%x", md5.Sum([]byte(ceType+ceSource+sourceUID))),
Expand Down
Loading