From 90828a0d4ae28fffd18f4d3e7c53246c802e7c1c Mon Sep 17 00:00:00 2001 From: bigrocs Date: Sun, 27 Nov 2022 09:00:11 +0800 Subject: [PATCH] The default port is used when there is no port number for k8s (#2598) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * k8s 没有端口号时使用默认端口 * Modify the not port test --- zrpc/resolver/internal/kube/targetparser.go | 27 ++++++++++--------- .../internal/kube/targetparser_test.go | 9 ++++--- zrpc/resolver/internal/kubebuilder.go | 10 +++++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/zrpc/resolver/internal/kube/targetparser.go b/zrpc/resolver/internal/kube/targetparser.go index 570ad7d5..d74cfba3 100644 --- a/zrpc/resolver/internal/kube/targetparser.go +++ b/zrpc/resolver/internal/kube/targetparser.go @@ -32,18 +32,21 @@ func ParseTarget(target resolver.Target) (Service, error) { } endpoints := targets.GetEndpoints(target) - segs := strings.SplitN(endpoints, colon, 2) - if len(segs) < 2 { - return emptyService, fmt.Errorf("bad endpoint: %s", endpoints) + if strings.Contains(endpoints, colon) { + segs := strings.SplitN(endpoints, colon, 2) + if len(segs) < 2 { + return emptyService, fmt.Errorf("bad endpoint: %s", endpoints) + } + + service.Name = segs[0] + port, err := strconv.Atoi(segs[1]) + if err != nil { + return emptyService, err + } + + service.Port = port + } else { + service.Name = endpoints } - - service.Name = segs[0] - port, err := strconv.Atoi(segs[1]) - if err != nil { - return emptyService, err - } - - service.Port = port - return service, nil } diff --git a/zrpc/resolver/internal/kube/targetparser_test.go b/zrpc/resolver/internal/kube/targetparser_test.go index 4dce9e43..ef69053c 100644 --- a/zrpc/resolver/internal/kube/targetparser_test.go +++ b/zrpc/resolver/internal/kube/targetparser_test.go @@ -39,9 +39,12 @@ func TestParseTarget(t *testing.T) { hasErr: true, }, { - name: "no port, no colon", - input: "k8s://ns1/my-svc", - hasErr: true, + name: "no port, no colon", + input: "k8s://ns1/my-svc", + expect: Service{ + Namespace: "ns1", + Name: "my-svc", + }, }, { name: "bad port", diff --git a/zrpc/resolver/internal/kubebuilder.go b/zrpc/resolver/internal/kubebuilder.go index 1a78eda5..38c1f775 100644 --- a/zrpc/resolver/internal/kubebuilder.go +++ b/zrpc/resolver/internal/kubebuilder.go @@ -40,6 +40,14 @@ func (b *kubeBuilder) Build(target resolver.Target, cc resolver.ClientConn, return nil, err } + if svc.Port == 0 { + endpoints, err := cs.CoreV1().Endpoints(svc.Namespace).Get(context.Background(), svc.Name, v1.GetOptions{}) + if err != nil { + return nil, err + } + svc.Port = int(endpoints.Subsets[0].Ports[0].Port) + } + handler := kube.NewEventHandler(func(endpoints []string) { var addrs []resolver.Address for _, val := range subset(endpoints, subsetSize) { @@ -64,12 +72,10 @@ func (b *kubeBuilder) Build(target resolver.Target, cc resolver.ClientConn, threading.GoSafe(func() { inf.Start(proc.Done()) }) - endpoints, err := cs.CoreV1().Endpoints(svc.Namespace).Get(context.Background(), svc.Name, v1.GetOptions{}) if err != nil { return nil, err } - handler.Update(endpoints) return &nopResolver{cc: cc}, nil