-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
targetparser.go
52 lines (43 loc) · 1.03 KB
/
targetparser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package kube
import (
"fmt"
"strconv"
"strings"
"github.com/zeromicro/go-zero/zrpc/resolver/internal/targets"
"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 = targets.GetAuthority(target)
if len(service.Namespace) == 0 {
service.Namespace = defaultNamespace
}
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)
}
service.Name = segs[0]
port, err := strconv.Atoi(segs[1])
if err != nil {
return emptyService, err
}
service.Port = port
} else {
service.Name = endpoints
}
return service, nil
}