From c5842e766838edbe2ba63d2993e30d22073c827b Mon Sep 17 00:00:00 2001 From: guoxudong Date: Thu, 8 Aug 2019 18:14:47 +0800 Subject: [PATCH] add config check & show --- cmd/get.go | 1 + cmd/use.go | 26 ++++++++++++++++++++++++++ go.mod | 1 + go.sum | 3 +++ 4 files changed, 31 insertions(+) diff --git a/cmd/get.go b/cmd/get.go index b05d56f5..d7d8ebae 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -48,6 +48,7 @@ var getCmd = &cobra.Command{ os.Exit(1) } } + ClusterStatus() }, } diff --git a/cmd/use.go b/cmd/use.go index 7ac6fe5b..734a11a4 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -17,6 +17,9 @@ package cmd import ( "fmt" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" "os" "github.com/spf13/cobra" @@ -55,6 +58,7 @@ Sets the current-context in a kubeconfig file } else { fmt.Println("Please input a CONTEXT_NAME.") } + ClusterStatus() }, } @@ -71,3 +75,25 @@ func (c *Config) CheckContext(name string) bool { } return false } + +func ClusterStatus() { + config, err := clientcmd.BuildConfigFromFlags("", cfgFile) + if err != nil { + panic(err.Error()) + } + + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + panic(err.Error()) + } + + cus, err := clientset.CoreV1().ComponentStatuses().List(metav1.ListOptions{}) + if err != nil { + panic(err.Error()) + } + var names []string + for _, k := range cus.Items { + names = append(names, k.Name) + } + fmt.Printf("Cluster check succeeded!\nContains components: %v \n", names) +} diff --git a/go.mod b/go.mod index 2534ebc8..24d65127 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( github.com/bndr/gotabulate v1.1.2 github.com/gogo/protobuf v1.2.1 // indirect + github.com/googleapis/gnostic v0.3.0 // indirect github.com/imdario/mergo v0.3.7 // indirect github.com/json-iterator/go v1.1.7 // indirect github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index b2d6b55c..8a205542 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gnostic v0.0.0-20170426233943-68f4ded48ba9/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= +github.com/googleapis/gnostic v0.3.0 h1:CcQijm0XKekKjP/YCz28LXVSpgguuB+nCxaSjCe09y0= +github.com/googleapis/gnostic v0.3.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= @@ -153,6 +155,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +k8s.io/api v0.0.0-20190313235455-40a48860b5ab h1:DG9A67baNpoeweOy2spF1OWHhnVY5KR7/Ek/+U1lVZc= k8s.io/api v0.0.0-20190313235455-40a48860b5ab/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= k8s.io/api v0.0.0-20190803060717-3ce214556aa9/go.mod h1:SgXHCRh94q+5GrRf9Dty2ZG8+wCVmqvQbZJXXcAswkw= k8s.io/api v0.0.0-20190806064354-8b51d7113622 h1:/ukNCVAmzoFiS9couF8B08fY4Y5s0LR5e5e6lyEQAFE=