Skip to content

Commit

Permalink
fix config check & show
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Aug 9, 2019
1 parent c5842e7 commit fef6705
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 0 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ func (c *Config) WriteYaml() {
}
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Successfully")
}
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ var getCmd = &cobra.Command{
os.Exit(1)
}
}
ClusterStatus()
err := ClusterStatus()
if err!=nil {
fmt.Printf("Cluster check failure!\n%v", err)
}
},
}

Expand Down
25 changes: 17 additions & 8 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ Sets the current-context in a kubeconfig file
kubeYaml := Config{}
kubeYaml.ReadYaml(cfgFile)
if kubeYaml.CheckContext(context) {
tmpContext := kubeYaml.CurrentContext
kubeYaml.CurrentContext = context
cover = true
kubeYaml.WriteYaml()
fmt.Println(fmt.Sprintf("Switched to context %s .", context))
err := Formatable(nil)
err := ClusterStatus()
if err != nil {
fmt.Println(err)
fmt.Printf("Cluster check failure! Please check your kubeconfig.\n%v", err)
kubeYaml.CurrentContext = tmpContext
kubeYaml.WriteYaml()
os.Exit(1)
} else {
fmt.Println(fmt.Sprintf("Switched to context %s", context))
err := Formatable(nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
} else {
fmt.Println(fmt.Sprintf("no context exists with the name: %s", context))
Expand All @@ -58,7 +67,6 @@ Sets the current-context in a kubeconfig file
} else {
fmt.Println("Please input a CONTEXT_NAME.")
}
ClusterStatus()
},
}

Expand All @@ -76,24 +84,25 @@ func (c *Config) CheckContext(name string) bool {
return false
}

func ClusterStatus() {
func ClusterStatus() error {
config, err := clientcmd.BuildConfigFromFlags("", cfgFile)
if err != nil {
panic(err.Error())
return fmt.Errorf(err.Error())
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
return fmt.Errorf(err.Error())
}

cus, err := clientset.CoreV1().ComponentStatuses().List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
return fmt.Errorf(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)
return nil
}

0 comments on commit fef6705

Please sign in to comment.