diff --git a/cmd/nodes.go b/cmd/nodes.go index 06294bf..5853661 100644 --- a/cmd/nodes.go +++ b/cmd/nodes.go @@ -22,7 +22,6 @@ import ( "kubectl-azs/pkg/printers" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // nodesCmd represents the nodes command @@ -38,24 +37,18 @@ var nodesCmd = &cobra.Command{ os.Exit(1) } - nodes, err := client.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: azLabel}) + nodes, label, err := kube.GetNodes(client, azLabel, cmd.Flags().Changed("label")) if err != nil { fmt.Println(err) os.Exit(1) } - - if len(nodes.Items) < 1 { - fmt.Printf("No nodes with target AZ label (%q) found\n", azLabel) - os.Exit(1) - } - w := printers.GetNewTabWriter(os.Stdout) defer w.Flush() fmt.Fprintln(w, "NODE NAME\tAZ\t") for _, node := range nodes.Items { - fmt.Fprintf(w, "%v\t%v\t\n", node.GetName(), node.GetLabels()[azLabel]) + fmt.Fprintf(w, "%v\t%v\t\n", node.GetName(), node.GetLabels()[label]) } diff --git a/cmd/pods.go b/cmd/pods.go index ae60366..6e7142f 100644 --- a/cmd/pods.go +++ b/cmd/pods.go @@ -59,7 +59,7 @@ where the pods are scheduled.`, os.Exit(1) } - nodes, err := client.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: azLabel}) + nodes, label, err := kube.GetNodes(client, azLabel, cmd.Flags().Changed("label")) if err != nil { fmt.Println(err) os.Exit(1) @@ -71,11 +71,11 @@ where the pods are scheduled.`, os.Exit(1) } - nodeList := buildNodeList(nodes) + nodeList := buildNodeList(nodes, label) podList := buildPodList(nodeList, pods) if len(nodeList) < 1 { - fmt.Printf("No nodes with target AZ label (%q) found\n", azLabel) + fmt.Printf("No nodes with target AZ label (%q) found\n", label) os.Exit(1) } else if len(podList) < 1 { fmt.Printf("No pods were found in namespace %q\n", namespace) @@ -99,7 +99,7 @@ func init() { rootCmd.AddCommand(podsCmd) } -func buildNodeList(nodes *v1.NodeList) map[string]nodeObj { +func buildNodeList(nodes *v1.NodeList, label string) map[string]nodeObj { tmpNodeList := make(map[string]nodeObj) @@ -107,7 +107,7 @@ func buildNodeList(nodes *v1.NodeList) map[string]nodeObj { newNode := nodeObj{ name: node.Name, - az: node.GetLabels()[azLabel], + az: node.GetLabels()[label], } tmpNodeList[newNode.name] = newNode diff --git a/cmd/root.go b/cmd/root.go index 002902c..2a6dae8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,7 +23,6 @@ import ( "kubectl-azs/pkg/printers" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) var ( @@ -54,20 +53,15 @@ be used standalone or as a "kubectl" plugin`, os.Exit(1) } - nodes, err := client.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: azLabel}) + nodes, label, err := kube.GetNodes(client, azLabel, cmd.Flags().Changed("label")) if err != nil { fmt.Println(err) os.Exit(1) } - if len(nodes.Items) < 1 { - fmt.Printf("No nodes with target AZ label (%q) found\n", azLabel) - os.Exit(1) - } - for _, node := range nodes.Items { - azs[node.GetLabels()[azLabel]] = node.GetLabels()[azLabel] + azs[node.GetLabels()[label]] = node.GetLabels()[label] } @@ -113,7 +107,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", "", "Kubernetes configuration file") rootCmd.PersistentFlags().StringVar(&configContext, "context", "", "The name of the kubeconfig context to use") - rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "The Namespace where the proxyctl ConfigMap is located") - rootCmd.PersistentFlags().StringVarP(&azLabel, "label", "l", "failure-domain.kubernetes.io/zone", "The target label that defines the Availability Zone on nodes") + rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "", "The Namespace to use when listing Pods") + rootCmd.PersistentFlags().StringVarP(&azLabel, "label", "l", "failure-domain.kubernetes.io/zone", "The target label that defines the Availability Zone on nodes. The default value also includes\nthe beta version of the same label") } diff --git a/main.go b/main.go index ea43261..ef9d3d2 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ import "kubectl-azs/cmd" var ( // VERSION defines the version of the utility. - VERSION = "v0.0.7" + VERSION = "v0.0.8" ) func main() { diff --git a/pkg/kube/kube.go b/pkg/kube/kube.go index 9d05d39..dc530b6 100644 --- a/pkg/kube/kube.go +++ b/pkg/kube/kube.go @@ -1,10 +1,15 @@ package kube import ( + "fmt" + "os" + "strings" + + v1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // Import all auth client plugins - _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/client-go/tools/clientcmd" ) @@ -24,3 +29,43 @@ func CreateKubeClient(kubeconfig string, configContext string) (kubernetes.Inter return clientset, err } + +// GetNodes gets a list of Kubernetes nodes filtered by the target AZ label +func GetNodes(client kubernetes.Interface, label string, labelChanged bool) (*v1.NodeList, string, error) { + + var nodes *v1.NodeList + origLabel := label + + nodes, err := client.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: label}) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if len(nodes.Items) < 1 { + + if !labelChanged { + + label = strings.Replace(label, "failure-domain", "failure-domain.beta", 1) + + fmt.Printf("Beta Label -> %v\n", label) + + nodes, err = client.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: label}) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + } + + if len(nodes.Items) < 1 { + + fmt.Printf("No nodes with target AZ label (%q) found\n", origLabel) + os.Exit(1) + } + + } + + return nodes, label, err + +}