Pawel Wieczorek | 6b223c9 | 2019-05-26 15:35:02 +0200 | [diff] [blame] | 1 | // Package rancher wraps Rancher commands necessary for K8s inspection. |
| 2 | package rancher |
| 3 | |
| 4 | import ( |
| 5 | "bytes" |
| 6 | "errors" |
| 7 | "os/exec" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | bin = "rancher" |
| 12 | paramHost = "--host" |
| 13 | cmdHosts = "hosts" |
| 14 | cmdHostsParams = "--quiet" |
| 15 | cmdDocker = "docker" |
| 16 | cmdDockerCmdPs = "ps" |
| 17 | cmdDockerCmdPsParams = "--no-trunc" |
| 18 | cmdDockerCmdPsFilter = "--filter" |
| 19 | cmdDockerCmdPsFilterArgs = "label=io.rancher.stack_service.name=kubernetes/kubernetes" |
| 20 | cmdDockerCmdPsFormat = "--format" |
| 21 | cmdDockerCmdPsFormatArgs = "{{.Command}}" |
| 22 | k8sProcess = "kube-apiserver" |
| 23 | ) |
| 24 | |
| 25 | // GetK8sParams returns parameters of running Kubernetes API server. |
| 26 | // It queries default environment set in configuration file. |
| 27 | func GetK8sParams() ([]string, error) { |
| 28 | hosts, err := listHosts() |
| 29 | if err != nil { |
| 30 | return []string{}, err |
| 31 | } |
| 32 | |
| 33 | for _, host := range hosts { |
| 34 | cmd, err := getK8sCmd(host) |
| 35 | if err != nil { |
| 36 | return []string{}, err |
| 37 | } |
| 38 | |
| 39 | if len(cmd) > 0 { |
| 40 | i := bytes.Index(cmd, []byte(k8sProcess)) |
| 41 | if i == -1 { |
| 42 | return []string{}, errors.New("missing " + k8sProcess + " command") |
| 43 | } |
| 44 | return btos(cmd[i+len(k8sProcess):]), nil |
| 45 | } |
| 46 | } |
| 47 | return []string{}, nil |
| 48 | } |
| 49 | |
| 50 | // listHosts lists IDs of active hosts. |
| 51 | // It queries default environment set in configuration file. |
| 52 | func listHosts() ([]string, error) { |
| 53 | cmd := exec.Command(bin, cmdHosts, cmdHostsParams) |
| 54 | out, err := cmd.Output() |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | return btos(out), nil |
| 59 | } |
| 60 | |
| 61 | // getK8sCmd returns running Kubernetes API server command with its parameters. |
| 62 | // It queries default environment set in configuration file. |
| 63 | func getK8sCmd(host string) ([]byte, error) { |
| 64 | // Following is equivalent to: |
| 65 | // $ rancher --host $HOST \ |
| 66 | // docker ps --no-trunc \ |
| 67 | // --filter "label=io.rancher.stack_service.name=kubernetes/kubernetes" \ |
| 68 | // --format "{{.Command}}" |
| 69 | cmd := exec.Command(bin, paramHost, host, |
| 70 | cmdDocker, cmdDockerCmdPs, cmdDockerCmdPsParams, |
| 71 | cmdDockerCmdPsFilter, cmdDockerCmdPsFilterArgs, |
| 72 | cmdDockerCmdPsFormat, cmdDockerCmdPsFormatArgs) |
| 73 | out, err := cmd.Output() |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | return out, nil |
| 78 | } |
| 79 | |
| 80 | // btos converts slice of bytes to slice of strings split by white space characters. |
| 81 | func btos(in []byte) []string { |
| 82 | var out []string |
| 83 | for _, b := range bytes.Fields(in) { |
| 84 | out = append(out, string(b)) |
| 85 | } |
| 86 | return out |
| 87 | } |