From a36a27805e465996794a39f90ef965d621a57aa2 Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Mon, 26 Feb 2024 13:40:42 +0100 Subject: [PATCH 1/4] Revert "Fixed eventtype create-delete loop on built in sources (#7245)" This reverts commit 93cec0f6a8c750bb63010b2baa385146c46381fb. --- .../apiserversource/apiserversource.go | 7 -- .../apiserversource/apiserversource_test.go | 71 ------------------- pkg/reconciler/pingsource/pingsource.go | 7 -- pkg/reconciler/pingsource/pingsource_test.go | 49 ------------- pkg/reconciler/source/duck/duck.go | 7 -- pkg/reconciler/source/duck/duck_test.go | 3 - .../features/apiserversource/data_plane.go | 2 +- 7 files changed, 1 insertion(+), 145 deletions(-) diff --git a/pkg/reconciler/apiserversource/apiserversource.go b/pkg/reconciler/apiserversource/apiserversource.go index ef00a49e333..3dfc32eeb27 100644 --- a/pkg/reconciler/apiserversource/apiserversource.go +++ b/pkg/reconciler/apiserversource/apiserversource.go @@ -141,13 +141,6 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1.ApiServerSour return nil } -func (r *Reconciler) FinalizeKind(ctx context.Context, source *v1.ApiServerSource) pkgreconciler.Event { - logging.FromContext(ctx).Info("Deleting source") - // Allow for eventtypes to be cleaned up - source.Status.CloudEventAttributes = []duckv1.CloudEventAttributes{} - return nil -} - func (r *Reconciler) namespacesFromSelector(src *v1.ApiServerSource) ([]string, error) { if src.Spec.NamespaceSelector == nil { return []string{src.Namespace}, nil diff --git a/pkg/reconciler/apiserversource/apiserversource_test.go b/pkg/reconciler/apiserversource/apiserversource_test.go index 691ff7ab227..dfc7d73ba15 100644 --- a/pkg/reconciler/apiserversource/apiserversource_test.go +++ b/pkg/reconciler/apiserversource/apiserversource_test.go @@ -144,12 +144,8 @@ func TestReconcile(t *testing.T) { }, WantErr: true, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeWarning, "InternalError", `insufficient permissions: User system:serviceaccount:testnamespace:default cannot get, list, watch resource "namespaces" in API group "" in Namespace "testnamespace"`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WithReactors: []clientgotesting.ReactionFunc{subjectAccessReviewCreateReactor(false)}, SkipNamespaceValidation: true, // SubjectAccessReview objects are cluster-scoped. }, { @@ -199,12 +195,6 @@ func TestReconcile(t *testing.T) { makeSubjectAccessReview("namespaces", "list", "default"), makeSubjectAccessReview("namespaces", "watch", "default"), }, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WithReactors: []clientgotesting.ReactionFunc{subjectAccessReviewCreateReactor(true)}, SkipNamespaceValidation: true, // SubjectAccessReview objects are cluster-scoped. }, { @@ -266,12 +256,8 @@ func TestReconcile(t *testing.T) { Object: makeAvailableReceiveAdapterWithNamespaces(t, []string{"test-a", "test-b"}, false), }}, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", `Deployment "apiserversource-test-apiserver-source-1234" updated`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WithReactors: []clientgotesting.ReactionFunc{subjectAccessReviewCreateReactor(true)}, SkipNamespaceValidation: true, // SubjectAccessReview objects are cluster-scoped. }, { @@ -336,12 +322,8 @@ func TestReconcile(t *testing.T) { Object: makeAvailableReceiveAdapterWithNamespaces(t, []string{"test-a", "test-b", "test-c"}, true), }}, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", `Deployment "apiserversource-test-apiserver-source-1234" updated`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WithReactors: []clientgotesting.ReactionFunc{subjectAccessReviewCreateReactor(true)}, SkipNamespaceValidation: true, // SubjectAccessReview objects are cluster-scoped. }, { @@ -393,12 +375,6 @@ func TestReconcile(t *testing.T) { makeSubjectAccessReview("namespaces", "list", "default"), makeSubjectAccessReview("namespaces", "watch", "default"), }, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WithReactors: []clientgotesting.ReactionFunc{subjectAccessReviewCreateReactor(true)}, SkipNamespaceValidation: true, // SubjectAccessReview objects are cluster-scoped. }, { @@ -448,12 +424,6 @@ func TestReconcile(t *testing.T) { makeSubjectAccessReview("namespaces", "list", "default"), makeSubjectAccessReview("namespaces", "watch", "default"), }, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WithReactors: []clientgotesting.ReactionFunc{subjectAccessReviewCreateReactor(true)}, SkipNamespaceValidation: true, // SubjectAccessReview objects are cluster-scoped. }, { @@ -473,13 +443,9 @@ func TestReconcile(t *testing.T) { }, Key: testNS + "/" + sourceName, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeWarning, "SinkNotFound", `Sink not found: {"ref":{"kind":"Channel","namespace":"testnamespace","name":"testsink","apiVersion":"messaging.knative.dev/v1"}}`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ Object: rttestingv1.NewApiServerSource(sourceName, testNS, rttestingv1.WithApiServerSourceSpec(sourcesv1.ApiServerSourceSpec{ @@ -521,15 +487,11 @@ func TestReconcile(t *testing.T) { Key: testNS + "/" + sourceName, WantErr: true, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeNormal, apiserversourceDeploymentCreated, "Deployment created, error:inducing failure for create deployments"), Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for create deployments"), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ Object: rttestingv1.NewApiServerSource(sourceName, testNS, rttestingv1.WithApiServerSourceSpec(sourcesv1.ApiServerSourceSpec{ @@ -618,12 +580,6 @@ func TestReconcile(t *testing.T) { makeSubjectAccessReview("namespaces", "list", "default"), makeSubjectAccessReview("namespaces", "watch", "default"), }, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WithReactors: []clientgotesting.ReactionFunc{subjectAccessReviewCreateReactor(true)}, SkipNamespaceValidation: true, // SubjectAccessReview objects are cluster-scoped. }, { @@ -648,12 +604,8 @@ func TestReconcile(t *testing.T) { }, Key: testNS + "/" + sourceName, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", `Deployment "apiserversource-test-apiserver-source-1234" updated`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ Object: rttestingv1.NewApiServerSource(sourceName, testNS, rttestingv1.WithApiServerSourceSpec(sourcesv1.ApiServerSourceSpec{ @@ -710,12 +662,8 @@ func TestReconcile(t *testing.T) { }, Key: testNS + "/" + sourceName, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", `Deployment "apiserversource-test-apiserver-source-1234" updated`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ Object: rttestingv1.NewApiServerSource(sourceName, testNS, rttestingv1.WithApiServerSourceSpec(sourcesv1.ApiServerSourceSpec{ @@ -772,12 +720,8 @@ func TestReconcile(t *testing.T) { }, Key: testNS + "/" + sourceName, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", `Deployment "apiserversource-test-apiserver-source-1234" updated`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ Object: rttestingv1.NewApiServerSource(sourceName, testNS, rttestingv1.WithApiServerSourceSpec(sourcesv1.ApiServerSourceSpec{ @@ -851,12 +795,6 @@ func TestReconcile(t *testing.T) { rttestingv1.WithApiServerSourceStatusNamespaces([]string{testNS}), ), }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantCreates: []runtime.Object{ makeSubjectAccessReview("namespaces", "get", "default"), makeSubjectAccessReview("namespaces", "list", "default"), @@ -1083,12 +1021,3 @@ func subjectAccessReviewCreateReactor(allowed bool) clientgotesting.ReactionFunc return false, nil, nil } } - -func patchFinalizers(name, namespace string) clientgotesting.PatchActionImpl { - action := clientgotesting.PatchActionImpl{} - action.Name = name - action.Namespace = namespace - patch := `{"metadata":{"finalizers":["apiserversources.sources.knative.dev"],"resourceVersion":""}}` - action.Patch = []byte(patch) - return action -} diff --git a/pkg/reconciler/pingsource/pingsource.go b/pkg/reconciler/pingsource/pingsource.go index ea1afae131e..9dc73e529a2 100644 --- a/pkg/reconciler/pingsource/pingsource.go +++ b/pkg/reconciler/pingsource/pingsource.go @@ -135,13 +135,6 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, source *sourcesv1.PingSo return nil } -func (r *Reconciler) FinalizeKind(ctx context.Context, source *sourcesv1.PingSource) pkgreconciler.Event { - logging.FromContext(ctx).Info("Deleting source") - // Allow for eventtypes to be cleaned up - source.Status.CloudEventAttributes = []duckv1.CloudEventAttributes{} - return nil -} - func (r *Reconciler) reconcileReceiveAdapter(ctx context.Context, source *sourcesv1.PingSource) (*appsv1.Deployment, error) { args := resources.Args{ ConfigEnvVars: r.configAcc.ToEnvVars(), diff --git a/pkg/reconciler/pingsource/pingsource_test.go b/pkg/reconciler/pingsource/pingsource_test.go index d2c099983f6..ab8aaaeda68 100644 --- a/pkg/reconciler/pingsource/pingsource_test.go +++ b/pkg/reconciler/pingsource/pingsource_test.go @@ -144,13 +144,9 @@ func TestAllCases(t *testing.T) { ), }}, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeWarning, "SinkNotFound", `Sink not found: {"ref":{"kind":"Channel","namespace":"testnamespace","name":"testsink","apiVersion":"messaging.knative.dev/v1"}}`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, }, { Name: "sink ref has no namespace", Objects: []runtime.Object{ @@ -187,13 +183,9 @@ func TestAllCases(t *testing.T) { ), }}, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeWarning, "SinkNotFound", `Sink not found: {"ref":{"kind":"Channel","namespace":"testnamespace","name":"testsink","apiVersion":"messaging.knative.dev/v1"}}`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, }, { Name: "error creating deployment", Objects: []runtime.Object{ @@ -216,12 +208,8 @@ func TestAllCases(t *testing.T) { }, Key: testNS + "/" + sourceName, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeWarning, "InternalError", "deployments.apps \"pingsource-mt-adapter\" not found"), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantErr: true, WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ Object: rtv1.NewPingSource(sourceName, testNS, @@ -283,12 +271,6 @@ func TestAllCases(t *testing.T) { rtv1.WithPingSourceStatusObservedGeneration(generation), ), }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, }, { Name: "Propagate CA certs", Objects: []runtime.Object{ @@ -339,12 +321,6 @@ func TestAllCases(t *testing.T) { rtv1.WithPingSourceStatusObservedGeneration(generation), ), }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, }, { Name: "deployment update due to env", Objects: []runtime.Object{ @@ -368,12 +344,8 @@ func TestAllCases(t *testing.T) { }, Key: testNS + "/" + sourceName, WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), Eventf(corev1.EventTypeNormal, pingSourceDeploymentUpdated, `PingSource adapter deployment updated`), }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ Object: rtv1.NewPingSource(sourceName, testNS, rtv1.WithPingSourceSpec(sourcesv1.PingSourceSpec{ @@ -439,12 +411,6 @@ func TestAllCases(t *testing.T) { rtv1.WithPingSourceStatusObservedGeneration(generation), ), }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, }, { Name: "valid with dataBase64", Objects: []runtime.Object{ @@ -487,12 +453,6 @@ func TestAllCases(t *testing.T) { rtv1.WithPingSourceStatusObservedGeneration(generation), ), }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "FinalizerUpdate", "Updated %q finalizers", sourceName), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(sourceName, testNS), - }, }, } @@ -552,12 +512,3 @@ func makeAvailableMTAdapterWithDifferentEnv() *appsv1.Deployment { WithDeploymentAvailable()(ma) return ma } - -func patchFinalizers(name, namespace string) clientgotesting.PatchActionImpl { - action := clientgotesting.PatchActionImpl{} - action.Name = name - action.Namespace = namespace - patch := `{"metadata":{"finalizers":["pingsources.sources.knative.dev"],"resourceVersion":""}}` - action.Patch = []byte(patch) - return action -} diff --git a/pkg/reconciler/source/duck/duck.go b/pkg/reconciler/source/duck/duck.go index 7ca55ec48c0..932647d3bfa 100644 --- a/pkg/reconciler/source/duck/duck.go +++ b/pkg/reconciler/source/duck/duck.go @@ -41,10 +41,6 @@ import ( "knative.dev/eventing/pkg/reconciler/source/duck/resources" ) -const ( - defaultNamespace = "default" -) - type Reconciler struct { // eventingClientSet allows us to configure Eventing objects eventingClientSet clientset.Interface @@ -215,9 +211,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 diff --git a/pkg/reconciler/source/duck/duck_test.go b/pkg/reconciler/source/duck/duck_test.go index 1bd6dc44458..2b8f58ef144 100644 --- a/pkg/reconciler/source/duck/duck_test.go +++ b/pkg/reconciler/source/duck/duck_test.go @@ -345,9 +345,6 @@ func makeEventType(ceType, ceSource string) *v1beta2.EventType { 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))), diff --git a/test/rekt/features/apiserversource/data_plane.go b/test/rekt/features/apiserversource/data_plane.go index 1f26d436b5b..eeb361058ed 100644 --- a/test/rekt/features/apiserversource/data_plane.go +++ b/test/rekt/features/apiserversource/data_plane.go @@ -309,7 +309,7 @@ func SendsEventsWithEventTypes() *feature.Feature { }) f.Requirement("ApiServerSource goes ready", apiserversource.IsReady(source)) - expectedCeTypes := sets.New(sources.ApiServerSourceEventResourceModeTypes...) + expectedCeTypes := sets.New(sources.ApiServerSourceEventReferenceModeTypes...) f.Stable("ApiServerSource as event source"). Must("delivers events on broker with URI", From 16491c0ff237265159af84171e41a5be994e42d9 Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Mon, 26 Feb 2024 13:44:45 +0100 Subject: [PATCH 2/4] Alternative fix for eventtype create-delete loop on built in sources Signed-off-by: Pierangelo Di Pilato --- pkg/apis/eventing/v1beta1/eventtype_defaults.go | 10 +++++++++- pkg/apis/eventing/v1beta2/eventtype_defaults.go | 11 +++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/apis/eventing/v1beta1/eventtype_defaults.go b/pkg/apis/eventing/v1beta1/eventtype_defaults.go index 115df98d460..07a7a3d400c 100644 --- a/pkg/apis/eventing/v1beta1/eventtype_defaults.go +++ b/pkg/apis/eventing/v1beta1/eventtype_defaults.go @@ -16,9 +16,14 @@ 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) } @@ -26,4 +31,7 @@ func (ets *EventTypeSpec) SetDefaults(ctx context.Context) { if ets.Reference == nil && ets.Broker == "" { ets.Broker = "default" } + if ets.Reference != nil { + ets.Reference.SetDefaults(ctx) + } } diff --git a/pkg/apis/eventing/v1beta2/eventtype_defaults.go b/pkg/apis/eventing/v1beta2/eventtype_defaults.go index 02b6f0f2a3c..217cc51588a 100644 --- a/pkg/apis/eventing/v1beta2/eventtype_defaults.go +++ b/pkg/apis/eventing/v1beta2/eventtype_defaults.go @@ -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) } } From 975a434d2b5336a7a6fb005dbf9b2b27a9b9dedc Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Mon, 26 Feb 2024 14:41:46 +0100 Subject: [PATCH 3/4] Use DeepDerivative Signed-off-by: Pierangelo Di Pilato --- pkg/reconciler/source/duck/duck.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/reconciler/source/duck/duck.go b/pkg/reconciler/source/duck/duck.go index 932647d3bfa..aea97fc974e 100644 --- a/pkg/reconciler/source/duck/duck.go +++ b/pkg/reconciler/source/duck/duck.go @@ -228,7 +228,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) { toDelete = append(toDelete, c) toCreate = append(toCreate, e) } From 03025941fb87c30b6a9a42d0067f08c5237915cb Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Mon, 26 Feb 2024 19:52:01 +0100 Subject: [PATCH 4/4] Add and fix tests Signed-off-by: Pierangelo Di Pilato --- pkg/reconciler/source/duck/duck_test.go | 58 ++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/pkg/reconciler/source/duck/duck_test.go b/pkg/reconciler/source/duck/duck_test.go index 2b8f58ef144..c79ab665c04 100644 --- a/pkg/reconciler/source/duck/duck_test.go +++ b/pkg/reconciler/source/duck/duck_test.go @@ -31,10 +31,6 @@ import ( "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" @@ -43,8 +39,14 @@ import ( 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 ( @@ -250,6 +252,50 @@ func TestAllCases(t *testing.T) { 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{}, + }, { + 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{}, }} logger := logtesting.TestLogger(t) @@ -340,7 +386,7 @@ func makeSourceCRD(eventTypes []eventTypeEntry) *apix1.CustomResourceDefinition } 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 {