implement k8s service discovery (#988)
* implement k8s service discovery * simplify code * use default namespace if not provided * disable codecov bot comment * ignore adhoc dir * simplify building target in NewClient * reformat code * Fix filepath (#990) * format code, and reorg imports (#991) * add more unit test Co-authored-by: anqiansong <anqiansong@gmail.com>
This commit is contained in:
45
zrpc/internal/resolver/kube/targetparser.go
Normal file
45
zrpc/internal/resolver/kube/targetparser.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package kube
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
const (
|
||||
colon = ":"
|
||||
defaultNamespace = "default"
|
||||
)
|
||||
|
||||
var emptyService Service
|
||||
|
||||
type Service struct {
|
||||
Namespace string
|
||||
Name string
|
||||
Port int
|
||||
}
|
||||
|
||||
func ParseTarget(target resolver.Target) (Service, error) {
|
||||
var service Service
|
||||
service.Namespace = target.Authority
|
||||
if len(service.Namespace) == 0 {
|
||||
service.Namespace = defaultNamespace
|
||||
}
|
||||
|
||||
segs := strings.SplitN(target.Endpoint, colon, 2)
|
||||
if len(segs) < 2 {
|
||||
return emptyService, fmt.Errorf("bad endpoint: %s", target.Endpoint)
|
||||
}
|
||||
|
||||
service.Name = segs[0]
|
||||
port, err := strconv.Atoi(segs[1])
|
||||
if err != nil {
|
||||
return emptyService, err
|
||||
}
|
||||
|
||||
service.Port = port
|
||||
|
||||
return service, nil
|
||||
}
|
||||
Reference in New Issue
Block a user