builds its own []command from json

Signed-off-by: Derek McQuay <derekmcquay@gmail.com>
This commit is contained in:
Derek McQuay 2017-05-15 10:48:25 -07:00
parent 56b562f6cc
commit 849279a07f
No known key found for this signature in database
GPG Key ID: 50CA472F40A5AFED

129
main.go
View File

@ -8,39 +8,57 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"sync" "sync"
"github.com/fatih/color"
) )
type command struct { type command struct {
node string node string
port int hc string
url string
health string
} }
type dcosJSON struct { type healthCheck struct {
Name string `json:"name"`
PodSpecs []struct {
Type string `json:"type"`
TaskSpecs []struct {
HealthCheckSpec struct {
Command string `json:"command"`
} `json:"health-check-spec"`
} `json:"task-specs"`
} `json:"pod-specs"`
}
type serviceIPs struct {
Name string `json:"name"` Name string `json:"name"`
Statuses []struct { Statuses []struct {
ContainerStatus struct { ContainerStatus struct {
ContainerID struct {
Value string `json:"value"`
} `json:"container_id"`
NetworkInfos []struct { NetworkInfos []struct {
IPAddresses []struct { IPAddresses []struct {
IPAddress string `json:"ip_address"` IPAddress string `json:"ip_address"`
} `json:"ip_addresses"` } `json:"ip_addresses"`
} `json:"network_infos"` } `json:"network_infos"`
} `json:"container_status"` } `json:"container_status"`
State string `json:"state"`
Timestamp float64 `json:"timestamp"`
} `json:"statuses"` } `json:"statuses"`
} }
type apiserverIPs struct { func getHealthCheck() healthCheck {
name string cmd := exec.Command("dcos", "kubernetes", "config", "target")
ip string var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
} }
func getAPIServerIPs() []apiserverIPs { h := healthCheck{}
if err := json.Unmarshal([]byte(out.String()), &h); err != nil {
panic(err)
}
return h
}
func getServiceIPs() []serviceIPs {
cmd := exec.Command("dcos", "task", "--json") cmd := exec.Command("dcos", "task", "--json")
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
@ -49,64 +67,75 @@ func getAPIServerIPs() []apiserverIPs {
log.Fatal(err) log.Fatal(err)
} }
d := []dcosJSON{} s := []serviceIPs{}
if err := json.Unmarshal([]byte(out.String()), &d); err != nil { if err := json.Unmarshal([]byte(out.String()), &s); err != nil {
panic(err) panic(err)
} }
ips := []apiserverIPs{}
for _, i := range d { // get rid of kubernetes pod
if strings.Contains(i.Name, "apiserver") { for i, n := range s {
ips = append( if n.Name == "kubernetes" {
ips, s = append(s[:i], s[i+1:]...)
apiserverIPs{
name: i.Name,
ip: i.Statuses[0].ContainerStatus.NetworkInfos[0].IPAddresses[0].IPAddress,
},
)
} }
} }
return ips return s
} }
func addAPIServer(n string, ips []apiserverIPs) string { func addAPIServer(n string, ips []serviceIPs) string {
for _, i := range ips { for _, i := range ips {
if strings.Contains(i.name, n) { if strings.Contains(i.Name, n) {
return i.ip return i.Statuses[0].ContainerStatus.NetworkInfos[0].IPAddresses[0].IPAddress
} }
} }
return "" return ""
} }
func stripHealthCheck(n string, hcs healthCheck) string {
for _, j := range hcs.PodSpecs {
if strings.Contains(n, j.Type) {
if len(j.TaskSpecs) != 0 {
return j.TaskSpecs[0].HealthCheckSpec.Command
}
return ""
}
}
return ""
}
func buildCommands(hcs healthCheck, ips []serviceIPs) []command {
commands := []command{}
for _, s := range ips {
if strings.Contains(s.Name, "apiserver") || strings.Contains(s.Name, "etcd") {
ip := s.Statuses[0].ContainerStatus.NetworkInfos[0].IPAddresses[0].IPAddress
c := strings.Replace(stripHealthCheck(s.Name, hcs), "$LIBPROCESS_IP", ip, -1)
commands = append(commands, command{node: s.Name, hc: c})
} else {
commands = append(commands, command{node: s.Name, hc: stripHealthCheck(s.Name, hcs)})
}
}
fmt.Println(commands)
return commands
}
func main() { func main() {
var wg sync.WaitGroup var wg sync.WaitGroup
ips := getAPIServerIPs() hcs := getHealthCheck()
hosts := []command{ ips := getServiceIPs()
command{node: "kube-controller-manager-0-node", port: 10252, url: "localhost", health: "healthz"}, hosts := buildCommands(hcs, ips)
command{node: "kube-controller-manager-1-node", port: 10252, url: "localhost", health: "healthz"},
command{node: "kube-controller-manager-2-node", port: 10252, url: "localhost", health: "healthz"},
command{node: "kube-scheduler-0-node", port: 10252, url: "localhost", health: "healthz"},
command{node: "kube-scheduler-1-node", port: 10251, url: "localhost", health: "healthz"},
command{node: "kube-scheduler-2-node", port: 10251, url: "localhost", health: "healthz"},
command{node: "kube-apiserver-0-node", port: 9000, url: addAPIServer("0", ips), health: "healthz"},
command{node: "kube-apiserver-1-node", port: 9000, url: addAPIServer("1", ips), health: "healthz"},
command{node: "kube-apiserver-2-node", port: 9000, url: addAPIServer("2", ips), health: "healthz"},
command{node: "etcd-0-node", port: 2379, url: "localhost", health: "health"},
command{node: "etcd-1-node", port: 2379, url: "localhost", health: "health"},
command{node: "etcd-2-node", port: 2379, url: "localhost", health: "health"},
}
for _, n := range hosts { for _, n := range hosts {
wg.Add(1) wg.Add(1)
go func(c command) { go func(c command) {
defer wg.Done() defer wg.Done()
s := strings.Split(c.hc, " ")
cmd := exec.Command( cmd := exec.Command(
"dcos", "dcos",
"task", "task",
"exec", "exec",
c.node, c.node,
"/usr/bin/curl", s[0],
"-sSf", s[1],
fmt.Sprintf("%s:%d/%s", c.url, c.port, c.health), s[2],
) )
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
@ -114,7 +143,11 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("%v: %q\n", c, out.String()) if strings.Contains(out.String(), "ok") || strings.Contains(out.String(), "true") {
color.Green(fmt.Sprintf("%v: %q\n", c, out.String()))
} else {
color.Red(fmt.Sprintf("%v: %q\n", c, out.String()))
}
}(n) }(n)
} }
wg.Wait() wg.Wait()