Repository: knative-sandbox/eventing-kafka (go)
Issue: Channel names longer than 63 chars may get truncated incorrectly
**Describe the bug**
A long channel name might end on a "-" when it gets truncated to 63 chars, which is making the channel controller not be able to reconcile the KafkaChannel.

```
{
    "level": "error",
    "ts": "2020-12-30T16:29:15.031Z",
    "logger": "eventing-kafka-channel-controller",
    "caller": "kafkachannel/kafkachannel.go:62",
    "msg": "Failed To Update KafkaChannel MetaData",
    "knative.dev/pod": "eventing-kafka-channel-controller-6ccf78b5d5-vzkcr",
    "error": "KafkaChannel.messaging.knative.dev \"src-chan-out-test-truncating-long-names-labels-flow\" is invalid: metadata.labels: Invalid value: \"0000-flows-test.src-chan-out-test-truncating-long-names-labels-\": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')",
    "stacktrace": "knative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel.(*Reconciler).reconcileMetaData\n\tknative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel/kafkachannel.go:62\nknative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel.(*Reconciler).reconcileKafkaChannel\n\tknative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel/kafkachannel.go:39\nknative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel.(*Reconciler).reconcile\n\tknative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel/reconciler.go:207\nknative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel.(*Reconciler).ReconcileKind\n\tknative.dev/eventing-kafka/pkg/channel/distributed/controller/kafkachannel/reconciler.go:123\nknative.dev/eventing-kafka/pkg/client/injection/reconciler/messaging/v1beta1/kafkachannel.(*reconcilerImpl).Reconcile\n\tknative.dev/eventing-kafka/pkg/client/injection/reconciler/messaging/v1beta1/kafkachannel/reconciler.go:244\nknative.dev/pkg/controller.(*Impl).processNextWorkItem\n\tknative.dev/pkg@v0.0.0-20201103163404-5514ab0c1fdf/controller/controller.go:513\nknative.dev/pkg/controller.(*Impl).RunContext.func3\n\tknative.dev/pkg@v0.0.0-20201103163404-5514ab0c1fdf/controller/controller.go:451"
}
```

 **Expected behavior**
The truncated label should be checked to make sure it's not ending on an illegal character.

 **To Reproduce**
We have a KafkaChannel in namespace `0000-flows-test` named `src-chan-out-test-truncating-long-names-labels-flow`.

 **Knative release version**
Eventing 0.18.3
kafka channel distributed 0.19.2

 **Additional context**
Add any other context about the problem here such as proposed priority

Pull request description: Remove trailing dashes from labels
Fixes #315 

## Proposed Changes

- 🐛 Remove trailing dashes from labels before attempting to apply them to kafkachannels in the distributed reconciler


Patch:
diff --git a/go.mod b/go.mod
index c2c35d987..93df75ba7 100644
--- a/go.mod
+++ b/go.mod
@@ -33,7 +33,7 @@ require (
 	k8s.io/apimachinery v0.19.7
 	k8s.io/client-go v0.19.7
 	k8s.io/utils v0.0.0-20200729134348-d5654de09c73
-	knative.dev/eventing v0.21.1-0.20210306204021-6136d09fb405
+	knative.dev/eventing v0.21.1-0.20210309001301-65e14cf14b90
 	knative.dev/hack v0.0.0-20210305150220-f99a25560134
-	knative.dev/pkg v0.0.0-20210305173320-7f753ea1276f
+	knative.dev/pkg v0.0.0-20210308172621-185e333b69ce
 )
diff --git a/go.sum b/go.sum
index 7a3a4e167..c1908dcbb 100644
--- a/go.sum
+++ b/go.sum
@@ -1238,17 +1238,18 @@ k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6 h1:+WnxoVtG8TMiudHBSEtrVL
 k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6/go.mod h1:UuqjUnNftUyPE5H64/qeyjQoUZhGpeFDVdxjTeEVN2o=
 k8s.io/utils v0.0.0-20200729134348-d5654de09c73 h1:uJmqzgNWG7XyClnU/mLPBWwfKKF1K8Hf8whTseBgJcg=
 k8s.io/utils v0.0.0-20200729134348-d5654de09c73/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
-knative.dev/eventing v0.21.1-0.20210306204021-6136d09fb405 h1:Wh0tpZR+dL4Y6Bz3JtEP13mk6uhw0UqussHLjQRH+0Q=
-knative.dev/eventing v0.21.1-0.20210306204021-6136d09fb405/go.mod h1:jDiF/tOmMPui6HjcYuPGYX7TWJ7HoBq7XcERIUgLXqg=
+knative.dev/eventing v0.21.1-0.20210309001301-65e14cf14b90 h1:sMunPDRSna68D8+kHiknpxcBYRcExi2yspoQ4Odx7vo=
+knative.dev/eventing v0.21.1-0.20210309001301-65e14cf14b90/go.mod h1:DoI7Xybl1H5Kr9RX1dxG2K7uSDDYJLMTBOwK1Go46vg=
 knative.dev/hack v0.0.0-20210203173706-8368e1f6eacf h1:u4cY4jr2LYvhoz/1HBWEPsMiLkm0HMdDTfmmw1RE8zE=
 knative.dev/hack v0.0.0-20210203173706-8368e1f6eacf/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
 knative.dev/hack v0.0.0-20210305150220-f99a25560134 h1:lUllAp28TkevQIgWrsjow8ZLnXJy3AraRzGFm/ffD2c=
 knative.dev/hack v0.0.0-20210305150220-f99a25560134/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
 knative.dev/pkg v0.0.0-20210303192215-8fbab7ebb77b h1:AynUh7MBUe44E60vh0vIyF2Bes4AOoTT2ejy9xrF0FU=
 knative.dev/pkg v0.0.0-20210303192215-8fbab7ebb77b/go.mod h1:TJSdebQOWX5N2bszohOYVi0H1QtXbtlYLuMghAFBMhY=
-knative.dev/pkg v0.0.0-20210305173320-7f753ea1276f h1:0om2ujgIbpGXMO/yn5NSu03NOd5FJuHju+MLQrP+Bqk=
-knative.dev/pkg v0.0.0-20210305173320-7f753ea1276f/go.mod h1:TJSdebQOWX5N2bszohOYVi0H1QtXbtlYLuMghAFBMhY=
-knative.dev/reconciler-test v0.0.0-20210304153715-4115949d94e6/go.mod h1:xwkxmi4f0GPlQoPrio6Q9REvg1oNx6apGARUSyJ/7jo=
+knative.dev/pkg v0.0.0-20210308052421-737401c38b22/go.mod h1:fP690UCcs5x+qQVhjJxNcm97OWIiUdFC1dqbD3Gsp64=
+knative.dev/pkg v0.0.0-20210308172621-185e333b69ce h1:7+KjtdhLCLIymzImxCYGJhDUaZlUJn3tr+arqbRa6xc=
+knative.dev/pkg v0.0.0-20210308172621-185e333b69ce/go.mod h1:fP690UCcs5x+qQVhjJxNcm97OWIiUdFC1dqbD3Gsp64=
+knative.dev/reconciler-test v0.0.0-20210305172520-cdad89dfbd9d/go.mod h1:xwkxmi4f0GPlQoPrio6Q9REvg1oNx6apGARUSyJ/7jo=
 pgregory.net/rapid v0.3.3 h1:jCjBsY4ln4Atz78QoBWxUEvAHaFyNDQg9+WU62aCn1U=
 pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
 rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
diff --git a/pkg/channel/distributed/common/k8s/util.go b/pkg/channel/distributed/common/k8s/util.go
index 661e25a74..0d85161ca 100644
--- a/pkg/channel/distributed/common/k8s/util.go
+++ b/pkg/channel/distributed/common/k8s/util.go
@@ -16,6 +16,10 @@ limitations under the License.
 
 package k8s
 
+import (
+	"regexp"
+)
+
 const (
 	K8sLabelValueMaxLength = 63
 )
@@ -26,5 +30,7 @@ func TruncateLabelValue(str string) string {
 	if len(str) > K8sLabelValueMaxLength {
 		strCopy = str[0:K8sLabelValueMaxLength]
 	}
+	// Strip any and all trailing '-' characters from the end of the string
+	strCopy = regexp.MustCompile(`^(.*[^-])-*`).ReplaceAllString(strCopy, "${1}")
 	return strCopy
 }
diff --git a/vendor/modules.txt b/vendor/modules.txt
index 3954b4b59..94c188109 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -1080,7 +1080,7 @@ k8s.io/utils/buffer
 k8s.io/utils/integer
 k8s.io/utils/pointer
 k8s.io/utils/trace
-# knative.dev/eventing v0.21.1-0.20210306204021-6136d09fb405
+# knative.dev/eventing v0.21.1-0.20210309001301-65e14cf14b90
 ## explicit
 knative.dev/eventing/pkg/adapter/v2
 knative.dev/eventing/pkg/adapter/v2/test
@@ -1211,7 +1211,7 @@ knative.dev/eventing/test/test_images/recordevents
 # knative.dev/hack v0.0.0-20210305150220-f99a25560134
 ## explicit
 knative.dev/hack
-# knative.dev/pkg v0.0.0-20210305173320-7f753ea1276f
+# knative.dev/pkg v0.0.0-20210308172621-185e333b69ce
 ## explicit
 knative.dev/pkg/apis
 knative.dev/pkg/apis/duck


Test patch:
diff --git a/pkg/channel/distributed/common/k8s/util_test.go b/pkg/channel/distributed/common/k8s/util_test.go
index d45aa5163..e5b07bdf5 100644
--- a/pkg/channel/distributed/common/k8s/util_test.go
+++ b/pkg/channel/distributed/common/k8s/util_test.go
@@ -38,13 +38,18 @@ func TestTruncateLabelValue(t *testing.T) {
 		{Value: "123456789012345678901234567890123456789012345678901234567890ab", Result: "123456789012345678901234567890123456789012345678901234567890ab"},    // 62 Characters
 		{Value: "123456789012345678901234567890123456789012345678901234567890abc", Result: "123456789012345678901234567890123456789012345678901234567890abc"},  // 63 Characters
 		{Value: "123456789012345678901234567890123456789012345678901234567890abcd", Result: "123456789012345678901234567890123456789012345678901234567890abc"}, // 64 Characters
+		{Value: "123456789012345678901234567890123456789012345678901234567890ab-d", Result: "123456789012345678901234567890123456789012345678901234567890ab"},  // 64 Characters
+		{Value: "1234567890123456789012345678901234567890123456789--------------d", Result: "1234567890123456789012345678901234567890123456789"},               // 64 Characters
+		{Value: "123456789-123456789-123456789-123456789-123456789-123456789-abcd", Result: "123456789-123456789-123456789-123456789-123456789-123456789-abc"}, // 64 Characters
+		{Value: "123456789-123456789-123456789-123456789-123456789-123456789-a--d", Result: "123456789-123456789-123456789-123456789-123456789-123456789-a"},   // 64 Characters
+		{Value: "123456789---", Result: "123456789"}, // 64 Characters
 	}
 
 	// Run The TestCases
 	for _, testCase := range testCases {
 		t.Run(testCase.Value, func(t *testing.T) {
 			result := TruncateLabelValue(testCase.Value)
-			assert.Equal(t, result, testCase.Result)
+			assert.Equal(t, testCase.Result, result)
 		})
 	}
 }
diff --git a/vendor/knative.dev/eventing/test/e2e-rekt-tests.sh b/vendor/knative.dev/eventing/test/e2e-rekt-tests.sh
new file mode 100644
index 000000000..9e05078f1
--- /dev/null
+++ b/vendor/knative.dev/eventing/test/e2e-rekt-tests.sh
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+
+# Copyright 2019 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.
+
+# This script runs the end-to-end tests against eventing built from source.
+
+# If you already have the *_OVERRIDE environment variables set, call
+# this script with the --run-tests arguments and it will use the cluster
+# and run the tests.
+
+# Calling this script without arguments will create a new cluster in
+# project $PROJECT_ID, start Knative eventing system, run the tests and
+# delete the cluster.
+
+export GO111MODULE=on
+
+source "$(dirname "$0")/e2e-common.sh"
+
+# Script entry point.
+
+initialize $@ --skip-istio-addon
+
+echo "Running E2E Reconciler Tests"
+go_test_e2e -timeout=30m -parallel=20 ./test/rekt || fail_test
+
+success
diff --git a/vendor/knative.dev/eventing/test/e2e-tests.sh b/vendor/knative.dev/eventing/test/e2e-tests.sh
index 3a2cdb98c..db14cb559 100644
--- a/vendor/knative.dev/eventing/test/e2e-tests.sh
+++ b/vendor/knative.dev/eventing/test/e2e-tests.sh
@@ -32,9 +32,6 @@ source "$(dirname "$0")/e2e-common.sh"
 
 initialize $@ --skip-istio-addon
 
-echo "Running E2E Reconciler Tests"
-go_test_e2e -timeout=30m -parallel=20 ./test/rekt || fail_test
-
 echo "Running E2E tests for: Multi Tenant Channel Based Broker, Channel (v1beta1, v1), InMemoryChannel (v1beta1, v1) , ApiServerSource (v1beta1, v1), ContainerSource (v1alpha2, v1) and PingSource (v1beta1, v1beta2)"
 go_test_e2e -timeout=30m -parallel=20 ./test/e2e \
   -brokerclass=MTChannelBasedBroker \