From 3f65deb2a15cc1f489f706ba3d6b876f85cb7f32 Mon Sep 17 00:00:00 2001 From: Joe Searcy Date: Wed, 13 Mar 2019 00:32:03 -0400 Subject: [PATCH 1/2] Added root cmd for listing AZs --- cmd/root.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 64b60a4..dd142e1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,11 @@ package cmd import ( "fmt" "os" + "sort" + "strings" + "github.com/phenixblue/kubectl-azs/pkg/k8s" + "github.com/phenixblue/kubectl-azs/pkg/printers" "github.com/spf13/cobra" ) @@ -34,6 +38,57 @@ var rootCmd = &cobra.Command{ Long: `The "azs" utility is a tool to list Kubernetes objects by Availability Zone. The utility can be used standalone or as a "kubectl" plugin. The "kubectl" utlity needs to be installed and the "KUBECONFIG" environment variable needs to be set to a valid kubeconfig file.`, + Run: func(cmd *cobra.Command, args []string) { + + kubernetes := k8s.NewKubernetesCmd(true) + out, err := kubernetes.ExecuteCommand("get", "nodes", "-l", "failure-domain.beta.kubernetes.io/zone", "-o", "custom-columns=NAME:.metadata.name,AZ:.metadata.labels.failure-domain\\.beta\\.kubernetes\\.io/zone") + + if err != nil { + + fmt.Println(string(out)) + fmt.Println(err) + os.Exit(1) + + } + + azs := make(map[string]struct{}) + nodes := strings.Split(string(out), "\n") + + for _, node := range nodes { + + if string(node) != "" { + + kv := strings.Split(string(node), " ") + + v := kv[1] + + azs[v] = struct{}{} + + } + + } + + azsSort := make([]string, 0, len(azs)) + + for az := range azs { + azsSort = append(azsSort, az) + } + + sort.Strings(azsSort) + + w := printers.GetNewTabWriter(os.Stdout) + defer w.Flush() + fmt.Fprintln(w, "AZ") + + for _, az := range azsSort { + + if az != "" { + fmt.Fprintln(w, az) + } + + } + + }, } // Execute adds all child commands to the root command and sets flags appropriately. From 875c387336f0d250d26a9a9cf8d5c6d6003dc6c8 Mon Sep 17 00:00:00 2001 From: Joe Searcy Date: Wed, 13 Mar 2019 00:38:27 -0400 Subject: [PATCH 2/2] Updated version --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index c75348b..17ad30e 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ import "github.com/phenixblue/kubectl-azs/cmd" var ( // VERSION defines the version of the utility. - VERSION = "v0.0.2" + VERSION = "v0.0.3" ) func main() {