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

[release-1.4] 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/1173#" 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 **dprotaso** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @dprotaso` 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.4/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":["dprotaso"]} -->
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/1173#" title="Author self-approved">knative-prow-robot</a>*, *<a href="https://github.com/knative-sandbox/eventing-kafka/pull/1173#pullrequestreview-966010028" 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.4/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/1173?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) Report
> Merging [#1173](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1173?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (0cce9e3) into [release-1.4](https://codecov.io/gh/knative-sandbox/eventing-kafka/commit/064d72305966b2183125384f38029d999d577017?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (064d723) will **decrease** coverage by `1.91%`.
> The diff coverage is `100.00%`.

```diff
@@               Coverage Diff               @@
##           release-1.4    #1173      +/-   ##
===============================================
- Coverage        76.79%   74.88%   -1.92%     
===============================================
  Files              123      128       +5     
  Lines             8780     9011     +231     
===============================================
+ Hits              6743     6748       +5     
- Misses            1805     2031     +226     
  Partials           232      232              
```


| [Impacted Files](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1173?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/1173/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-cGtnL2NoYW5uZWwvY29uc29saWRhdGVkL3JlY29uY2lsZXIvY29udHJvbGxlci9yZXNvdXJjZXMvc2VydmljZS5nbw==) | `10.86% <100.00%> (ø)` | |
| [...reconciler/controller/resources/service\_account.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1173/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%> (ø)` | |
| [...ed/reconciler/controller/resources/role\_binding.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1173/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/1173/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/1173/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%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1173?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/1173?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox). Last update [064d723...0cce9e3](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/1173?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).


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)
+			}
+		})
+	}
+}