From 6b31080f57b19212362f82f5d28db30ccbcaca65 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 22 Oct 2020 09:16:43 +0200 Subject: [PATCH 1/2] Remove history header Signed-off-by: Francesco Guardiani --- pkg/channel/history_transformer.go | 71 -------- pkg/channel/history_transformer_test.go | 155 ------------------ pkg/channel/message_receiver.go | 2 +- .../helpers/channel_data_plane_helper.go | 17 +- 4 files changed, 2 insertions(+), 243 deletions(-) delete mode 100644 pkg/channel/history_transformer.go delete mode 100644 pkg/channel/history_transformer_test.go diff --git a/pkg/channel/history_transformer.go b/pkg/channel/history_transformer.go deleted file mode 100644 index d30dbda2314..00000000000 --- a/pkg/channel/history_transformer.go +++ /dev/null @@ -1,71 +0,0 @@ -/* -Copyright 2020 The Knative Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package channel - -import ( - "regexp" - "strings" - - "github.com/cloudevents/sdk-go/v2/binding" - "github.com/cloudevents/sdk-go/v2/binding/transformer" - "github.com/cloudevents/sdk-go/v2/types" -) - -const ( - // EventHistory is the header containing all channel hosts traversed by the event. - // This is an experimental header: https://github.com/knative/eventing/issues/638. - EventHistory = "knativehistory" - EventHistorySeparator = "; " -) - -var historySplitter = regexp.MustCompile(`\s*` + regexp.QuoteMeta(EventHistorySeparator) + `\s*`) - -func AddHistory(host string) binding.Transformer { - return transformer.SetExtension(EventHistory, func(i interface{}) (interface{}, error) { - if types.IsZero(i) { - return host, nil - } - str, err := types.Format(i) - if err != nil { - return nil, err - } - h := decodeEventHistory(str) - h = append(h, host) - return encodeEventHistory(h), nil - }) -} - -func cleanupEventHistoryItem(host string) string { - return strings.Trim(host, " ") -} - -func encodeEventHistory(history []string) string { - return strings.Join(history, EventHistorySeparator) -} - -func decodeEventHistory(historyStr string) []string { - readHistory := historySplitter.Split(historyStr, -1) - // Filter and cleanup in-place - history := readHistory[:0] - for _, item := range readHistory { - cleanItem := cleanupEventHistoryItem(item) - if cleanItem != "" { - history = append(history, cleanItem) - } - } - return history -} diff --git a/pkg/channel/history_transformer_test.go b/pkg/channel/history_transformer_test.go deleted file mode 100644 index 5f3f97d6374..00000000000 --- a/pkg/channel/history_transformer_test.go +++ /dev/null @@ -1,155 +0,0 @@ -/* -Copyright 2020 The Knative Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package channel - -import ( - "context" - "testing" - - "github.com/cloudevents/sdk-go/v2/binding" - bindingtest "github.com/cloudevents/sdk-go/v2/binding/test" - "github.com/cloudevents/sdk-go/v2/test" -) - -func TestMessageHistory(t *testing.T) { - var cases = []struct { - start string - set []string - append []string - expected string - len int - }{ - { - expected: "", - len: 0, - }, - { - append: []string{"name.Ns.service.local"}, - expected: "name.Ns.service.local", - len: 1, - }, - { - append: []string{"name.withspace.service.local "}, - expected: "name.withspace.service.local", - len: 1, - }, - { - append: []string{"name1.ns1.service.local", "name2.ns2.service.local"}, - expected: "name1.ns1.service.local; name2.ns2.service.local", - len: 2, - }, - { - start: "name1.ns1.service.local", - append: []string{"name2.ns2.service.local", "name3.ns3.service.local"}, - expected: "name1.ns1.service.local; name2.ns2.service.local; name3.ns3.service.local", - len: 3, - }, - { - start: "name1.ns1.service.local; name2.ns2.service.local", - append: []string{"nameadd.nsadd.service.local"}, - expected: "name1.ns1.service.local; name2.ns2.service.local; nameadd.nsadd.service.local", - len: 3, - }, - { - start: "name1.ns1.service.local; name2.ns2.service.local", - set: []string{"name3.ns3.service.local"}, - expected: "name3.ns3.service.local", - len: 1, - }, - { - start: " ", - append: []string{"name1.ns1.service.local"}, - expected: "name1.ns1.service.local", - len: 1, - }, - { - start: " ", - append: []string{" ", "name.multispace.service.local", " "}, - expected: "name.multispace.service.local", - len: 1, - }, - } - - for _, tc := range cases { - t.Run(tc.expected, func(t *testing.T) { - val := encodeEventHistory(decodeEventHistory(tc.start)) - if tc.set != nil { - val = encodeEventHistory(tc.set) - } - for _, name := range tc.append { - val = encodeEventHistory(append(decodeEventHistory(val), decodeEventHistory(name)...)) - } - h := decodeEventHistory(val) - if len(h) != tc.len { - t.Errorf("Unexpected number of elements. Want %d, got %d", tc.len, len(h)) - } - if val != tc.expected { - t.Errorf("Unexpected history. Want %q, got %q", tc.expected, val) - } - }) - } -} - -func TestHistoryTransformer(t *testing.T) { - withoutHistory := test.MinEvent() - withoutHistoryExpected := withoutHistory.Clone() - withoutHistoryExpected.SetExtension(EventHistory, "example.com") - - withHistory := test.MinEvent() - withHistory.SetExtension(EventHistory, "knative.dev") - withHistoryExpected := withHistory.Clone() - withHistoryExpected.SetExtension(EventHistory, encodeEventHistory([]string{"knative.dev", "example.com"})) - - bindingtest.RunTransformerTests(t, context.Background(), []bindingtest.TransformerTestArgs{ - { - Name: "Add history in Mock Structured message", - InputMessage: bindingtest.MustCreateMockStructuredMessage(t, withoutHistory), - WantEvent: withoutHistoryExpected, - Transformers: binding.Transformers{AddHistory("example.com")}, - }, - { - Name: "Add history in Mock Binary message", - InputMessage: bindingtest.MustCreateMockBinaryMessage(withoutHistory), - WantEvent: withoutHistoryExpected, - Transformers: binding.Transformers{AddHistory("example.com")}, - }, - { - Name: "Add history in Event message", - InputEvent: withoutHistory, - WantEvent: withoutHistoryExpected, - Transformers: binding.Transformers{AddHistory("example.com")}, - }, - { - Name: "Update history in Mock Structured message", - InputMessage: bindingtest.MustCreateMockStructuredMessage(t, withHistory), - WantEvent: withHistoryExpected, - Transformers: binding.Transformers{AddHistory("example.com")}, - }, - { - Name: "Update history in Mock Binary message", - InputMessage: bindingtest.MustCreateMockBinaryMessage(withHistory), - WantEvent: withHistoryExpected, - Transformers: binding.Transformers{AddHistory("example.com")}, - }, - { - Name: "Update history in Event message", - InputEvent: withHistory, - WantEvent: withHistoryExpected, - Transformers: binding.Transformers{AddHistory("example.com")}, - }, - }) -} diff --git a/pkg/channel/message_receiver.go b/pkg/channel/message_receiver.go index d124e38f6a0..e4e62db8b6b 100644 --- a/pkg/channel/message_receiver.go +++ b/pkg/channel/message_receiver.go @@ -178,7 +178,7 @@ func (r *MessageReceiver) ServeHTTP(response nethttp.ResponseWriter, request *ne r.reporter.ReportEventCount(&args, nethttp.StatusBadRequest) return } - err = r.receiverFunc(request.Context(), channel, message, []binding.Transformer{AddHistory(host)}, utils.PassThroughHeaders(request.Header)) + err = r.receiverFunc(request.Context(), channel, message, []binding.Transformer{}, utils.PassThroughHeaders(request.Header)) if err != nil { if _, ok := err.(*UnknownChannelError); ok { response.WriteHeader(nethttp.StatusNotFound) diff --git a/test/conformance/helpers/channel_data_plane_helper.go b/test/conformance/helpers/channel_data_plane_helper.go index e806c31b027..f24faba7b32 100644 --- a/test/conformance/helpers/channel_data_plane_helper.go +++ b/test/conformance/helpers/channel_data_plane_helper.go @@ -28,7 +28,6 @@ import ( . "github.com/cloudevents/sdk-go/v2/test" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - eventingchannel "knative.dev/eventing/pkg/channel" testlib "knative.dev/eventing/test/lib" "knative.dev/eventing/test/lib/recordevents" "knative.dev/eventing/test/lib/resources" @@ -113,21 +112,7 @@ func channelDataPlaneSuccessTest(ctx context.Context, t *testing.T, channel meta } else { matchers = append(matchers, HasNoData()) } - // The extension matcher needs to match an eventual extension containing knativehistory extension - // (which is not mandatory by the spec) - extensions := event.Extensions() - extKeys := make([]string, 0, len(extensions)) - for k := range extensions { - extKeys = append(extKeys, k) - } - extKeys = append(extKeys, eventingchannel.EventHistory) - matchers = append(matchers, AnyOf( - HasExactlyExtensions(event.Extensions()), - AllOf( - ContainsExactlyExtensions(extKeys...), - HasExtensions(event.Extensions()), - ), - )) + matchers = append(matchers, HasExactlyExtensions(event.Extensions())) eventTracker.AssertExact( 1, From 3999e31441ac0b7e34b63058336f5faded74b52b Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 22 Oct 2020 09:41:44 +0200 Subject: [PATCH 2/2] More cleanup Signed-off-by: Francesco Guardiani --- pkg/channel/message_receiver_test.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pkg/channel/message_receiver_test.go b/pkg/channel/message_receiver_test.go index 382492fc7e0..00215ae6c3f 100644 --- a/pkg/channel/message_receiver_test.go +++ b/pkg/channel/message_receiver_test.go @@ -119,16 +119,6 @@ func TestMessageReceiver_ServeHTTP(t *testing.T) { return fmt.Errorf("test receiver func -- bad headers (-want, +got): %s", diff) } - // Check history - if h, ok := e.Extensions()[EventHistory]; !ok { - return fmt.Errorf("test receiver func -- history not added") - } else { - expectedHistory := "test-name.test-namespace.svc." + network.GetClusterDomainName() - if h != expectedHistory { - return fmt.Errorf("test receiver func -- bad history: %v", h) - } - } - return nil }, expected: nethttp.StatusAccepted,