Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
cli/cmd: make health return error instead of using Fatalf
Browse files Browse the repository at this point in the history
To soften the dependency on log.Entry and to separate the functionality
from CLI code. This also makes code easier to move around and avoids
hiding function complexity.

Part of #630

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 19, 2020
1 parent 40a69cc commit 43b3897
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cli/cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,32 @@ func runHealth(cmd *cobra.Command, args []string) {
log.SetLevel(log.DebugLevel)
}

if err := health(contextLogger); err != nil {
contextLogger.Fatalf("Checking cluster health failed: %v", err)
}
}

//nolint:funlen
func health(contextLogger *log.Entry) error {
lokoConfig, diags := getLokoConfig()
if diags.HasErrors() {
for _, diagnostic := range diags {
contextLogger.Error(diagnostic.Error())
}

contextLogger.Fatal("Errors found while loading cluster configuration")
return diags
}

kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, true)
if err != nil {
contextLogger.Debugf("Error in finding kubeconfig file: %s", err)
contextLogger.Fatal("Suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")

return fmt.Errorf("suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")
}

cs, err := k8sutil.NewClientset(kubeconfig)
if err != nil {
contextLogger.Fatalf("Error in creating Kubernetes client: %q", err)
return fmt.Errorf("creating Kubernetes client: %w", err)
}

// We can skip error checking here, as getKubeconfig() already checks it.
Expand All @@ -76,18 +84,18 @@ func runHealth(cmd *cobra.Command, args []string) {

ns, err := cluster.GetNodeStatus()
if err != nil {
contextLogger.Fatalf("Error getting node status: %q", err)
return fmt.Errorf("getting node status: %w", err)
}

ns.PrettyPrint()

if !ns.Ready() {
contextLogger.Fatalf("The cluster is not completely ready.")
return fmt.Errorf("cluster is not completely ready")
}

components, err := cluster.Health()
if err != nil {
contextLogger.Fatalf("Error in getting Lokomotive cluster health: %q", err)
return fmt.Errorf("getting Lokomotive cluster health: %w", err)
}

w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
Expand All @@ -114,4 +122,6 @@ func runHealth(cmd *cobra.Command, args []string) {

w.Flush()
}

return nil
}

0 comments on commit 43b3897

Please sign in to comment.