go-zero/zrpc/resolver/internal/kube/targetparser.go
Kevin Wan 9d528dddd6
feat: support third party orm to interact with go-zero (#1286)
* fixes #987

* chore: fix test failure

* chore: add comments

* feat: support third party orm to interact with go-zero

* chore: refactor
2021-12-01 20:22:15 +08:00

48 lines
859 B
Go

package kube
import (
"fmt"
"strconv"
"strings"
"google.golang.org/grpc/resolver"
)
const (
colon = ":"
defaultNamespace = "default"
)
var emptyService Service
// Service represents a service with namespace, name and port.
type Service struct {
Namespace string
Name string
Port int
}
// ParseTarget parses the resolver.Target.
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
}