Repository: knative-sandbox/eventing-kafka
Pull request #1174 (2022-05-09 11:55:23)

[release-1.3] Correctly handle KafkaChannels with long names
This is an automated cherry-pick of #1172

```release-note
Correctly handle KafkaChannels with long names
```

knative-prow[bot]: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/eventing-kafka/pull/1174#" title="Author self-approved">knative-prow-robot</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **davyodom** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @davyodom` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Feventing-kafka).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/knative-sandbox/eventing-kafka/blob/release-1.3/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["davyodom"]} -->
knative-prow[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/eventing-kafka/pull/1174#" title="Author self-approved">knative-prow-robot</a>*, *<a href="https://github.com/knative-sandbox/eventing-kafka/pull/1174#pullrequestreview-966009996" title="Approved">matzew</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Feventing-kafka).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative-sandbox/eventing-kafka/blob/release-1.3/OWNERS)~~ [matzew]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) Report
> Merging [#1174](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (6e63f9f) into [release-1.3](https://codecov.io/gh/knative-sandbox/eventing-kafka/commit/a262ed153f014e1daf5a19ac9a39489300f00a3b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (a262ed1) will **decrease** coverage by `2.58%`.
> The diff coverage is `100.00%`.

```diff
@@               Coverage Diff               @@
##           release-1.3    #1174      +/-   ##
===============================================
- Coverage        75.17%   72.58%   -2.59%     
===============================================
  Files              123      128       +5     
  Lines             5748     5957     +209     
===============================================
+ Hits              4321     4324       +3     
- Misses            1204     1410     +206     
  Partials           223      223              
```


| [Impacted Files](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) | Coverage Δ | |
|---|---|---|
| [...lidated/reconciler/controller/resources/service.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-cGtnL2NoYW5uZWwvY29uc29saWRhdGVkL3JlY29uY2lsZXIvY29udHJvbGxlci9yZXNvdXJjZXMvc2VydmljZS5nbw==) | `8.82% <100.00%> (ø)` | |
| [...onsolidated/dispatcher/consumer\_message\_handler.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-cGtnL2NoYW5uZWwvY29uc29saWRhdGVkL2Rpc3BhdGNoZXIvY29uc3VtZXJfbWVzc2FnZV9oYW5kbGVyLmdv) | `0.00% <0.00%> (ø)` | |
| [...ed/reconciler/controller/resources/role\_binding.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-cGtnL2NoYW5uZWwvY29uc29saWRhdGVkL3JlY29uY2lsZXIvY29udHJvbGxlci9yZXNvdXJjZXMvcm9sZV9iaW5kaW5nLmdv) | `0.00% <0.00%> (ø)` | |
| [...onciler/controller/resources/dispatcher\_service.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-cGtnL2NoYW5uZWwvY29uc29saWRhdGVkL3JlY29uY2lsZXIvY29udHJvbGxlci9yZXNvdXJjZXMvZGlzcGF0Y2hlcl9zZXJ2aWNlLmdv) | `0.00% <0.00%> (ø)` | |
| [...ated/reconciler/controller/resources/dispatcher.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-cGtnL2NoYW5uZWwvY29uc29saWRhdGVkL3JlY29uY2lsZXIvY29udHJvbGxlci9yZXNvdXJjZXMvZGlzcGF0Y2hlci5nbw==) | `0.00% <0.00%> (ø)` | |
| [...reconciler/controller/resources/service\_account.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-cGtnL2NoYW5uZWwvY29uc29saWRhdGVkL3JlY29uY2lsZXIvY29udHJvbGxlci9yZXNvdXJjZXMvc2VydmljZV9hY2NvdW50Lmdv) | `0.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox). Last update [e69679c...6e63f9f](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1174?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox).

knative-prow[bot]: @knative-prow-robot: The following test **failed**, say `/retest` to rerun all failed tests or `/retest-required` to rerun all mandatory failed tests:

Test name | Commit | Details | Required | Rerun command
--- | --- | --- | --- | ---
integration-test-mt-source_eventing-kafka_release-1.3 | 6e63f9f65be57fdad524ba5fe8cd72a6ff8fcf5c | [link](https://prow.knative.dev/view/gs/knative-prow/pr-logs/pull/knative-sandbox_eventing-kafka/1174/integration-test-mt-source_eventing-kafka_release-1.3/1523632727012675584) | false | `/test integration-test-mt-source_eventing-kafka_release-1.3`

[Your PR dashboard](https://prow.knative.dev/pr).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->

Patch:
diff --git a/pkg/channel/consolidated/reconciler/controller/resources/service.go b/pkg/channel/consolidated/reconciler/controller/resources/service.go
index 6dc5f148b..4b9d753bd 100644
--- a/pkg/channel/consolidated/reconciler/controller/resources/service.go
+++ b/pkg/channel/consolidated/reconciler/controller/resources/service.go
@@ -21,9 +21,10 @@ import (
 
 	corev1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
-	"knative.dev/eventing-kafka/pkg/apis/messaging/v1beta1"
 	"knative.dev/pkg/kmeta"
 	"knative.dev/pkg/network"
+
+	"knative.dev/eventing-kafka/pkg/apis/messaging/v1beta1"
 )
 
 const (
@@ -31,13 +32,21 @@ const (
 	portNumber         = 80
 	MessagingRoleLabel = "messaging.knative.dev/role"
 	MessagingRole      = "kafka-channel"
+
+	ChannelSuffix = "-kn-channel"
+	// MaxResourceNameLength is the maximum number of characters for a Kubernetes resource name.
+	// See vendor/k8s.io/apiserver/pkg/storage/names/generate.go
+	MaxResourceNameLength = 63
 )
 
 // ServiceOption can be used to optionally modify the K8s service in MakeK8sService.
 type ServiceOption func(*corev1.Service) error
 
 func MakeChannelServiceName(name string) string {
-	return fmt.Sprintf("%s-kn-channel", name)
+	if len(name)+len(ChannelSuffix) > MaxResourceNameLength {
+		return name
+	}
+	return fmt.Sprintf("%s%s", name, ChannelSuffix)
 }
 
 // ExternalService is a functional option for MakeK8sService to create a K8s service of type ExternalName


Test patch:
diff --git a/pkg/channel/consolidated/reconciler/controller/resources/service_test.go b/pkg/channel/consolidated/reconciler/controller/resources/service_test.go
new file mode 100644
index 000000000..dde388232
--- /dev/null
+++ b/pkg/channel/consolidated/reconciler/controller/resources/service_test.go
@@ -0,0 +1,70 @@
+/*
+Copyright 2022 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 resources
+
+import (
+	"fmt"
+	"strings"
+	"testing"
+)
+
+func TestMakeChannelServiceName(t *testing.T) {
+	tt := []struct {
+		name  string
+		given string
+		want  string
+	}{
+		{
+			name:  "63 chars",
+			given: strings.Repeat("a", 63),
+			want:  strings.Repeat("a", 63),
+		},
+		{
+			name:  "64 chars",
+			given: strings.Repeat("a", 64),
+			want:  strings.Repeat("a", 64),
+		},
+		{
+			name:  fmt.Sprintf("%d chars", 65-len(ChannelSuffix)),
+			given: strings.Repeat("a", 65-len(ChannelSuffix)),
+			want:  strings.Repeat("a", 65-len(ChannelSuffix)),
+		},
+		{
+			name:  fmt.Sprintf("%d chars", 64-len(ChannelSuffix)),
+			given: strings.Repeat("a", 64-len(ChannelSuffix)),
+			want:  strings.Repeat("a", 64-len(ChannelSuffix)),
+		},
+		{
+			name:  fmt.Sprintf("%d chars", 63-len(ChannelSuffix)),
+			given: strings.Repeat("a", 63-len(ChannelSuffix)),
+			want:  strings.Repeat("a", 63-len(ChannelSuffix)) + "-kn-channel",
+		},
+		{
+			name:  fmt.Sprintf("%d chars", 62-len(ChannelSuffix)),
+			given: strings.Repeat("a", 62-len(ChannelSuffix)),
+			want:  strings.Repeat("a", 62-len(ChannelSuffix)) + "-kn-channel",
+		},
+	}
+
+	for _, tc := range tt {
+		t.Run(tc.name, func(t *testing.T) {
+			if got := MakeChannelServiceName(tc.given); got != tc.want {
+				t.Errorf("want %s, got %s", tc.want, got)
+			}
+		})
+	}
+}