Commit 47d622dc authored by Brad Davidson's avatar Brad Davidson Committed by Derek Nola

Cap length of generated name used for servicelb daemonset

Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com> (cherry picked from commit 21611c56) Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent 4b0eb69d
......@@ -697,7 +697,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
......
......@@ -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 (
......@@ -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)
}
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment