Skip to content

Commit 64c4c8b

Browse files
authored
fix: enforce K8s length limit for redis proxy service name (argoproj-labs#2071) (argoproj-labs#2116)
* fix: enforce K8s length limit for redis proxy service name (argoproj-labs#2071) Signed-off-by: Tejas Soham <tejassoham05@gmail.com> * fix: resolve redis proxy service name helper conflict Signed-off-by: Tejas Soham <tejassoham05@gmail.com> --------- Signed-off-by: Tejas Soham <tejassoham05@gmail.com>
1 parent dcb4c27 commit 64c4c8b

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

controllers/argoutil/redis.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ func loadTemplateFile(path string, params map[string]string) (string, error) {
337337
return buf.String(), nil
338338
}
339339

340+
// GenerateAgentPrincipalRedisProxyServiceName generates a deterministic Service name for the agent's principal Redis proxy.
341+
// It truncates the Custom Resource (CR) name to ensure the final appended name stays within Kubernetes length limits.
340342
func GenerateAgentPrincipalRedisProxyServiceName(crName string) string {
341-
return fmt.Sprintf("%s-agent-%s", crName, "principal-redisproxy")
343+
truncatedCRName := TruncateWithHash(crName, 36)
344+
return fmt.Sprintf("%s-agent-%s", truncatedCRName, "principal-redisproxy")
342345
}

controllers/argoutil/resource_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,37 @@ import (
2222
corev1 "k8s.io/api/core/v1"
2323
)
2424

25+
func TestGenerateAgentPrincipalRedisProxyServiceName(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
crName string
29+
expected string
30+
}{
31+
{
32+
name: "short CR name - no truncation",
33+
crName: "short-name",
34+
expected: "short-name-agent-principal-redisproxy",
35+
},
36+
{
37+
name: "long CR name - uses truncated name",
38+
// Input matches the test case from TestTruncateCRName
39+
crName: "this-is-a-very-long-argocd-instance-name-that-exceeds-37-characters",
40+
// Base name is truncated to 36 chars to accommodate the 27 char suffix
41+
expected: "this-is-a-very-long-argocd-i-657aacd-agent-principal-redisproxy",
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
result := GenerateAgentPrincipalRedisProxyServiceName(tt.crName)
48+
assert.Equal(t, tt.expected, result)
49+
50+
// Enforce K8s 63-character service name limit
51+
assert.LessOrEqual(t, len(result), 63)
52+
})
53+
}
54+
}
55+
2556
func TestTruncateWithHash(t *testing.T) {
2657
tests := []struct {
2758
name string

0 commit comments

Comments
 (0)