The default port is used when there is no port number for k8s (#2598)

* k8s 没有端口号时使用默认端口

* Modify the not port test
master
bigrocs 2 years ago committed by GitHub
parent b1c3c21c81
commit 90828a0d4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,6 +32,7 @@ func ParseTarget(target resolver.Target) (Service, error) {
}
endpoints := targets.GetEndpoints(target)
if strings.Contains(endpoints, colon) {
segs := strings.SplitN(endpoints, colon, 2)
if len(segs) < 2 {
return emptyService, fmt.Errorf("bad endpoint: %s", endpoints)
@ -44,6 +45,8 @@ func ParseTarget(target resolver.Target) (Service, error) {
}
service.Port = port
} else {
service.Name = endpoints
}
return service, nil
}

@ -41,7 +41,10 @@ func TestParseTarget(t *testing.T) {
{
name: "no port, no colon",
input: "k8s://ns1/my-svc",
hasErr: true,
expect: Service{
Namespace: "ns1",
Name: "my-svc",
},
},
{
name: "bad port",

@ -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

Loading…
Cancel
Save