forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
executable file
·69 lines (61 loc) · 1.48 KB
/
util.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package util
import (
"fmt"
"os"
"strings"
)
const (
// PODNAMESPACE means pod's namespace in kubernetes
PODNAMESPACE = "POD_NAMESPACE"
// PODNAME means pod's name in kubernetes
PODNAME = "POD_NAME"
// PODIP means pod's instance ip in kubernetes
PODIP = "INSTANCE_IP"
defaultSuffix = "svc.cluster.local"
defaultNS = "default"
)
const (
// RDSHttpProxy query all route configuration
RDSHttpProxy = "http_proxy"
// EnvoyAPIV2 defines prefix of type
EnvoyAPIV2 = "type.googleapis.com/envoy.api.v2."
// RouteType defines ADS type
RouteType = EnvoyAPIV2 + "RouteConfiguration"
)
// ServiceKey returns service key from a service name
func ServiceKey(service string) string {
ns := os.Getenv(PODNAMESPACE)
if ns == "" {
ns = defaultNS
}
return strings.Join([]string{service, ns, defaultSuffix}, ".")
}
// ServiceKeyToLabel returns label from service key
func ServiceKeyToLabel(service string) string {
ss := strings.Split(service, "|")
if len(ss) != 4 {
return ""
}
return ss[2]
}
// ServiceAndPort returns service and port
func ServiceAndPort(host string) (string, string) {
sp := strings.Split(host, ":")
if len(sp) <= 1 {
return host, "0"
}
ss := strings.Split(sp[0], ".")
if len(ss) <= 1 {
return sp[0], sp[1]
}
return ss[0], sp[1]
}
// BuildNodeID returns nodeID
func BuildNodeID() string {
ns := os.Getenv(PODNAMESPACE)
if ns == "" {
ns = defaultNS
}
return fmt.Sprintf("sidecar~%s~%s.%s~%s.svc.cluster.local",
os.Getenv(PODIP), os.Getenv(PODNAME), ns, ns)
}