From dd727e721897272c157f2d424f7c55aafc30c718 Mon Sep 17 00:00:00 2001 From: HYH <82195407@qq.com> Date: Wed, 13 Sep 2023 00:38:35 +0800 Subject: [PATCH] fix: failed to add cluster when the cluster server address is ipv6 (#8204) (#15350) * fix: failed to add cluster when the cluster server address is ipv6 (#8204) Signed-off-by: huyinhou * fix: failed to add cluster when the cluster server address is ipv6 (#8204) Signed-off-by: huyinhou * remove unused import Signed-off-by: huyinhou * fix: lowercase URI secret name; abbreviated IPv6 address Signed-off-by: huyinhou --------- Signed-off-by: huyinhou --- util/db/cluster_test.go | 20 ++++++++++++++++++++ util/db/secrets.go | 20 +++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/util/db/cluster_test.go b/util/db/cluster_test.go index 5d99edf83d05b..f2f3761318965 100644 --- a/util/db/cluster_test.go +++ b/util/db/cluster_test.go @@ -27,6 +27,26 @@ 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-project.com-2721640553", name) + + name, err = URIToSecretName("cluster", "http://[fe80::1ff:fe23:4567:890a]") + assert.NoError(t, err) + assert.Equal(t, "cluster-fe80--1ff-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--1ff-fe23-4567-890a-664858999", name) + + name, err = URIToSecretName("cluster", "http://[FE80::1FF:FE23:4567:890A]:8000") + assert.NoError(t, err) + assert.Equal(t, "cluster-fe80--1ff-fe23-4567-890a-682802007", name) + + name, err = URIToSecretName("cluster", "http://:/abc") + assert.NoError(t, err) + assert.Equal(t, "cluster--1969338796", name) } func Test_secretToCluster(t *testing.T) { diff --git a/util/db/secrets.go b/util/db/secrets.go index 8ff4721e22ab8..7021226c4bf4c 100644 --- a/util/db/secrets.go +++ b/util/db/secrets.go @@ -3,12 +3,14 @@ package db import ( "fmt" "hash/fnv" + "net/netip" "net/url" "strconv" "strings" "time" "context" + log "github.com/sirupsen/logrus" apiv1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -155,8 +157,24 @@ func URIToSecretName(uriType, uri string) (string, error) { if err != nil { return "", err } + host := parsedURI.Host + if strings.HasPrefix(host, "[") { + last := strings.Index(host, "]") + if last >= 0 { + addr, err := netip.ParseAddr(host[1:last]) + if err != nil { + return "", err + } + host = strings.ReplaceAll(addr.String(), ":", "-") + } + } else { + last := strings.Index(host, ":") + if last >= 0 { + host = host[0:last] + } + } h := fnv.New32a() _, _ = h.Write([]byte(uri)) - host := strings.ToLower(strings.Split(parsedURI.Host, ":")[0]) + host = strings.ToLower(host) return fmt.Sprintf("%s-%s-%v", uriType, host, h.Sum32()), nil }