Skip to content

Commit

Permalink
Cap length of generated name used for servicelb daemonset
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Jul 24, 2024
1 parent 891e72f commit 21611c5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/cloudprovider/servicelb.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,17 @@ func (k *k3s) getPriorityClassName(svc *core.Service) string {

// generateName generates a distinct name for the DaemonSet based on the service name and UID
func generateName(svc *core.Service) string {
return fmt.Sprintf("svclb-%s-%s", svc.Name, svc.UID[:8])
name := svc.Name
// ensure that the service name plus prefix and uuid aren't overly long, but
// don't cut the service name at a trailing hyphen.
if len(name) > 48 {
trimlen := 48
for name[trimlen-1] == '-' {
trimlen--
}
name = name[0:trimlen]
}
return fmt.Sprintf("svclb-%s-%s", name, svc.UID[:8])
}

// ingressToString converts a list of LoadBalancerIngress entries to strings
Expand Down
49 changes: 49 additions & 0 deletions pkg/cloudprovider/servicelb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

const (
Expand Down Expand Up @@ -111,3 +113,50 @@ func Test_UnitFilterByIPFamily_Ordering(t *testing.T) {
t.Errorf("filterByIPFamily() = %+v\nWant = %+v", got, want)
}
}

func Test_UnitGenerateName(t *testing.T) {
uid := types.UID("35a5ccb3-4a82-40b7-8d83-cda9582e4251")
tests := []struct {
name string
svc *core.Service
want string
}{
{
name: "Short name",
svc: &core.Service{
ObjectMeta: meta.ObjectMeta{
Name: "a-service",
UID: uid,
},
},
want: "svclb-a-service-35a5ccb3",
},
{
name: "Long name",
svc: &core.Service{
ObjectMeta: meta.ObjectMeta{
Name: "a-service-with-a-very-veeeeeery-long-yet-valid-name",
UID: uid,
},
},
want: "svclb-a-service-with-a-very-veeeeeery-long-yet-valid-n-35a5ccb3",
},
{
name: "Long hypenated name",
svc: &core.Service{
ObjectMeta: meta.ObjectMeta{
Name: "a-service-with-a-name-with-inconvenient------------hypens",
UID: uid,
},
},
want: "svclb-a-service-with-a-name-with-inconvenient-35a5ccb3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := generateName(tt.svc); got != tt.want {
t.Errorf("generateName() = %+v\nWant = %+v", got, tt.want)
}
})
}
}

0 comments on commit 21611c5

Please sign in to comment.