/
service.go
73 lines (67 loc) · 2.01 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package kubernetes
import (
"bufio"
"io/ioutil"
"strings"
"github.com/anduintransaction/rivendell/utils"
"github.com/palantir/stacktrace"
yaml "gopkg.in/yaml.v2"
)
// Service operations
type Service struct {
context *Context
}
func (s *Service) ListPods(name string) ([]string, error) {
status, err := s.context.getNonPodStatus(name, "service")
if err != nil {
return nil, err
}
if status != RsStatusActive {
// We ignore errors here.
return nil, nil
}
cmdResult, err := utils.NewCommand("kubectl", s.context.completeArgs([]string{"get", "services", name, "-o", "yaml"})...).Run()
if err != nil {
return nil, err
}
if cmdResult.ExitCode != 0 {
output, _ := ioutil.ReadAll(cmdResult.Stderr)
errOutput := string(output)
return nil, stacktrace.Propagate(ErrCommandExecute{cmdResult.ExitCode, errOutput}, "error execute command")
}
output, _ := ioutil.ReadAll(cmdResult.Stdout)
serviceInfo := &serviceResourceInfo{}
err = yaml.Unmarshal(output, serviceInfo)
if err != nil {
return nil, stacktrace.Propagate(ErrInvalidResponse{err, string(output)}, "invalid response")
}
if serviceInfo.Spec == nil || len(serviceInfo.Spec.Selector) == 0 {
return nil, nil
}
selectors := []string{}
for k, v := range serviceInfo.Spec.Selector {
selectors = append(selectors, k+"="+v)
}
args := []string{"get", "pod", "-l", strings.Join(selectors, ","), "-o", "name"}
cmdResult, err = utils.NewCommand("kubectl", s.context.completeArgs(args)...).Run()
if err != nil {
return nil, err
}
if cmdResult.ExitCode != 0 {
output, _ := ioutil.ReadAll(cmdResult.Stderr)
errOutput := string(output)
return nil, stacktrace.Propagate(ErrCommandExecute{cmdResult.ExitCode, errOutput}, "error execute command")
}
pods := []string{}
scanner := bufio.NewScanner(cmdResult.Stdout)
for scanner.Scan() {
pods = append(pods, strings.TrimPrefix(scanner.Text(), "pod/"))
}
return pods, nil
}
type serviceResourceInfo struct {
Spec *serviceSpec `yaml:"spec"`
}
type serviceSpec struct {
Selector map[string]string `yaml:"selector"`
}