-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
53 lines (45 loc) · 1.35 KB
/
service.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
package kubeapp
import (
"fmt"
"strconv"
)
type ErrInvalidPort struct {
portStr string
}
func (e ErrInvalidPort) Error() string {
return fmt.Sprintf("port %s is invalid", e.portStr)
}
type Service interface {
Host() string
Port() int
HostStr() string
}
// ServiceFromEnvVars looks up the service discovery environment variables described in http://kubernetes.io/v1.1/docs/user-guide/services.html#environment-variables.
//
// Returns an error if any of the environment variables don't exist or any were invalid.
func ServiceFromEnvVars(name string) (Service, error) {
hst, err := getEnv(fmt.Sprintf("%s_SERVICE_HOST", name))
if err != nil {
return nil, err
}
prtStr, err := getEnv(fmt.Sprintf("%s_SERVICE_PORT", name))
if err != nil {
return nil, err
}
prt, err := strconv.Atoi(prtStr)
if err != nil {
return nil, ErrInvalidPort{portStr: prtStr}
}
return &svcVals{host: hst, port: prt}, nil
}
// StaticService creates a service that contains the given values. It's useful for unit testing in environments outside Kubernetes
func StaticService(host string, port int) Service {
return &svcVals{host: host, port: port}
}
type svcVals struct {
host string
port int
}
func (s *svcVals) Host() string { return s.host }
func (s *svcVals) Port() int { return s.port }
func (s *svcVals) HostStr() string { return fmt.Sprintf("%s:%d", s.host, s.port) }