Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Handle KafkaChannels with long names (#1174)
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <[email protected]>

Co-authored-by: Pierangelo Di Pilato <[email protected]>
  • Loading branch information
knative-prow-robot and pierDipi authored May 9, 2022
1 parent e69679c commit a5f80fc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,32 @@ 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 (
portName = "http"
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit a5f80fc

Please sign in to comment.