Skip to content

Commit

Permalink
Merge pull request #7 from phenixblue/more-updates
Browse files Browse the repository at this point in the history
Update for beta label
  • Loading branch information
phenixblue authored May 17, 2020
2 parents 79bc96f + c116764 commit 7c90441
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
11 changes: 2 additions & 9 deletions cmd/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])

}

Expand Down
10 changes: 5 additions & 5 deletions cmd/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -99,15 +99,15 @@ 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)

for _, node := range nodes.Items {

newNode := nodeObj{
name: node.Name,
az: node.GetLabels()[azLabel],
az: node.GetLabels()[label],
}

tmpNodeList[newNode.name] = newNode
Expand Down
14 changes: 4 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"kubectl-azs/pkg/printers"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
Expand Down Expand Up @@ -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]

}

Expand Down Expand Up @@ -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")

}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
47 changes: 46 additions & 1 deletion pkg/kube/kube.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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

}

0 comments on commit 7c90441

Please sign in to comment.