From f0591042b79217f283ef8ac8be1dffb25f7dee09 Mon Sep 17 00:00:00 2001 From: whalecold Date: Wed, 3 Jan 2024 16:22:44 +0800 Subject: [PATCH] support parser the host contains namespace --- core/manager/bootstrap.go | 11 ++++++++-- core/manager/bootstrap_test.go | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/core/manager/bootstrap.go b/core/manager/bootstrap.go index ed6c1ec..fabb5d7 100644 --- a/core/manager/bootstrap.go +++ b/core/manager/bootstrap.go @@ -101,11 +101,18 @@ func (bc *BootstrapConfig) tryExpandFQDN(host string) string { if strings.Contains(host, ".svc.") { return host } + var b strings.Builder b.Grow(len(host) + len(bc.configNamespace) + len(bc.nodeDomain) + 10) b.WriteString(host) - b.WriteString(".") - b.WriteString(bc.configNamespace) + + // the regex for Kubernetes service is [a-z]([-a-z0-9]*[a-z0-9])?, it should not contains `.` in it + // if the host not contains namespace. + if !strings.Contains(host, ".") { + b.WriteString(".") + b.WriteString(bc.configNamespace) + } + b.WriteString(".svc.") b.WriteString(bc.nodeDomain) return b.String() diff --git a/core/manager/bootstrap_test.go b/core/manager/bootstrap_test.go index 46d013e..9fe064f 100644 --- a/core/manager/bootstrap_test.go +++ b/core/manager/bootstrap_test.go @@ -24,6 +24,44 @@ import ( "google.golang.org/protobuf/types/known/structpb" ) +func TestTryExpandFQDN(t *testing.T) { + testCases := []struct { + desc string + host string + want string + bsc *BootstrapConfig + }{ + { + desc: "success", + host: "servicea", + want: "servicea.default.svc.cluster.local", + bsc: &BootstrapConfig{ + nodeDomain: "cluster.local", + configNamespace: "default", + }, + }, + { + desc: "success", + host: "servicea.bookinfo", + want: "servicea.bookinfo.svc.cluster.local", + }, + { + desc: "fqdn", + host: "servicea.bookinfo.svc.cluster.local", + want: "servicea.bookinfo.svc.cluster.local", + }, + } + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + got := tc.bsc.tryExpandFQDN(tc.host) + if got != tc.want { + t.Errorf("tryExpandFQDN() got = %v, want %v", got, tc.want) + } + }) + } + +} + func TestParseMetaEnvs(t *testing.T) { testCases := []struct { desc string