Skip to content

Commit

Permalink
Merge pull request #36 from rjsadow/name-truncate
Browse files Browse the repository at this point in the history
Added name length cap for loadbalancer name
  • Loading branch information
aojea authored Apr 1, 2024
2 parents e1e7f1f + 43972d1 commit 48ee5b8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ const (
FixedNetworkName = "kind"
// NodeCCMLabelKey
NodeCCMLabelKey = "io.x-k8s.cloud-provider-kind.cluster"
// NodeNameLabelKey
NodeNameLabelKey = "io.x-k8s.cloud-provider-kind.node.name"
)
16 changes: 14 additions & 2 deletions pkg/loadbalancer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package loadbalancer

import (
"context"
"crypto/sha256"
"encoding/base32"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -113,9 +115,17 @@ func (s *Server) EnsureLoadBalancerDeleted(ctx context.Context, clusterName stri
return container.Delete(loadBalancerName(clusterName, service))
}

// loadbalancer name = cluster-name + service.namespace + service.name
// loadbalancer name is a unique name for the loadbalancer container
func loadBalancerName(clusterName string, service *v1.Service) string {
return constants.ContainerPrefix + "-" + clusterName + "-" + service.Namespace + "-" + service.Name
hash := sha256.Sum256([]byte(loadBalancerSimpleName(clusterName, service)))
encoded := base32.StdEncoding.EncodeToString(hash[:])
name := constants.ContainerPrefix + "-" + encoded[:40]

return name
}

func loadBalancerSimpleName(clusterName string, service *v1.Service) string {
return clusterName + "-" + service.Namespace + "-" + service.Name
}

// createLoadBalancer create a docker container with a loadbalancer
Expand All @@ -132,6 +142,8 @@ func createLoadBalancer(clusterName string, service *v1.Service, image string) e
"--tty", // allocate a tty for entrypoint logs
// label the node with the cluster ID
"--label", fmt.Sprintf("%s=%s", constants.NodeCCMLabelKey, clusterName),
// label the node with the load balancer name
"--label", fmt.Sprintf("%s=%s", constants.NodeNameLabelKey, loadBalancerSimpleName(clusterName, service)),
// user a user defined docker network so we get embedded DNS
"--net", networkName,
"--init=false",
Expand Down
39 changes: 39 additions & 0 deletions pkg/loadbalancer/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package loadbalancer

import (
"testing"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/cloud-provider-kind/pkg/constants"
)

func TestLoadBalancerName(t *testing.T) {
tests := []struct {
name string
cluster string
service *v1.Service
expected string
expectedLen int
}{
{
name: "simple",
cluster: "test-cluster",
service: &v1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "test-namespace", Name: "test-service"}},
expected: constants.ContainerPrefix + "-AWEHRL6IVEFOWQI2DXZDWR4QZPCNHGSVJCV5UBM6",
expectedLen: 48,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := loadBalancerName(test.cluster, test.service)
if actual != test.expected {
t.Errorf("expected %q, got %q", test.expected, actual)
}
if len(actual) != test.expectedLen {
t.Errorf("expected length %d, got %d", test.expectedLen, len(actual))
}
})
}
}

0 comments on commit 48ee5b8

Please sign in to comment.