From b31b1dbc168b4a556428f59a2b2fa74a5de6c5c1 Mon Sep 17 00:00:00 2001 From: huyinhou Date: Tue, 5 Sep 2023 14:13:27 +0800 Subject: [PATCH] fix: failed to add cluster when the cluster server address is ipv6 (#8204) Signed-off-by: huyinhou --- util/db/cluster_test.go | 12 ++++++++++++ util/db/secrets.go | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/util/db/cluster_test.go b/util/db/cluster_test.go index 9d60a3073c3c2d..36789510f4a90b 100644 --- a/util/db/cluster_test.go +++ b/util/db/cluster_test.go @@ -25,6 +25,18 @@ func Test_URIToSecretName(t *testing.T) { name, err := URIToSecretName("cluster", "http://foo") assert.NoError(t, err) assert.Equal(t, "cluster-foo-752281925", name) + + name, err = URIToSecretName("cluster", "http://thelongestdomainnameintheworld.argocd-project.com:3000") + assert.NoError(t, err) + assert.Equal(t, "cluster-thelongestdomainnameintheworld.argocd-projec-2721640553", name) + + name, err = URIToSecretName("cluster", "http://[fe80::1ff:fe23:4567:890a]") + assert.NoError(t, err) + assert.Equal(t, "cluster-fe80-0000-0000-0000-01ff-fe23-4567-890a-3877258831", name) + + name, err = URIToSecretName("cluster", "http://[fe80::1ff:fe23:4567:890a]:8000") + assert.NoError(t, err) + assert.Equal(t, "cluster-fe80-0000-0000-0000-01ff-fe23-4567-890a-664858999", name) } func Test_secretToCluster(t *testing.T) { diff --git a/util/db/secrets.go b/util/db/secrets.go index 8ff4721e22ab89..e3a0e2de1ba327 100644 --- a/util/db/secrets.go +++ b/util/db/secrets.go @@ -3,12 +3,16 @@ package db import ( "fmt" "hash/fnv" + "net/netip" "net/url" "strconv" "strings" "time" "context" + + "regexp" + log "github.com/sirupsen/logrus" apiv1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -155,8 +159,20 @@ func URIToSecretName(uriType, uri string) (string, error) { if err != nil { return "", err } + re := regexp.MustCompile(`:\d+$`) + host := re.ReplaceAllLiteralString(parsedURI.Host, "") + if strings.Contains(host, ":") { + addr, err := netip.ParseAddr(host[1 : len(host)-1]) + if err != nil { + return "", err + } + host = strings.ReplaceAll(addr.StringExpanded(), ":", "-") + } + length := len(host) + len(uriType) + 12 + if length > 63 { + host = host[0 : len(host)-(length-63)] + } h := fnv.New32a() _, _ = h.Write([]byte(uri)) - host := strings.ToLower(strings.Split(parsedURI.Host, ":")[0]) return fmt.Sprintf("%s-%s-%v", uriType, host, h.Sum32()), nil }