From 56b562f6cc29a53cca2b1a72dc98270ffcee827e Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Fri, 12 May 2017 13:22:42 -0700 Subject: [PATCH] initial commit Signed-off-by: Derek McQuay --- main.go | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..a429ec5 --- /dev/null +++ b/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "log" + "os/exec" + "strings" + "sync" +) + +type command struct { + node string + port int + url string + health string +} + +type dcosJSON struct { + Name string `json:"name"` + Statuses []struct { + ContainerStatus struct { + ContainerID struct { + Value string `json:"value"` + } `json:"container_id"` + NetworkInfos []struct { + IPAddresses []struct { + IPAddress string `json:"ip_address"` + } `json:"ip_addresses"` + } `json:"network_infos"` + } `json:"container_status"` + State string `json:"state"` + Timestamp float64 `json:"timestamp"` + } `json:"statuses"` +} + +type apiserverIPs struct { + name string + ip string +} + +func getAPIServerIPs() []apiserverIPs { + cmd := exec.Command("dcos", "task", "--json") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + log.Fatal(err) + } + + d := []dcosJSON{} + if err := json.Unmarshal([]byte(out.String()), &d); err != nil { + panic(err) + } + ips := []apiserverIPs{} + for _, i := range d { + if strings.Contains(i.Name, "apiserver") { + ips = append( + ips, + apiserverIPs{ + name: i.Name, + ip: i.Statuses[0].ContainerStatus.NetworkInfos[0].IPAddresses[0].IPAddress, + }, + ) + } + } + return ips +} + +func addAPIServer(n string, ips []apiserverIPs) string { + for _, i := range ips { + if strings.Contains(i.name, n) { + return i.ip + } + } + return "" +} + +func main() { + var wg sync.WaitGroup + ips := getAPIServerIPs() + hosts := []command{ + command{node: "kube-controller-manager-0-node", port: 10252, url: "localhost", health: "healthz"}, + 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 { + wg.Add(1) + go func(c command) { + defer wg.Done() + cmd := exec.Command( + "dcos", + "task", + "exec", + c.node, + "/usr/bin/curl", + "-sSf", + fmt.Sprintf("%s:%d/%s", c.url, c.port, c.health), + ) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + log.Fatal(err) + } + fmt.Printf("%v: %q\n", c, out.String()) + }(n) + } + wg.Wait() +}