metropolis/cli/metroctl: refactor to use RunE instead of log.Fatal
Change-Id: Id5ca65980816e1715a8f08afcdf712292117012a
Reviewed-on: https://review.monogon.dev/c/monogon/+/3441
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Tested-by: Jenkins CI
diff --git a/metropolis/cli/metroctl/cmd_k8s_configure.go b/metropolis/cli/metroctl/cmd_k8s_configure.go
index cd90416..5ce4032 100644
--- a/metropolis/cli/metroctl/cmd_k8s_configure.go
+++ b/metropolis/cli/metroctl/cmd_k8s_configure.go
@@ -2,6 +2,7 @@
import (
"context"
+ "fmt"
"log"
"os"
"os/exec"
@@ -24,36 +25,35 @@
to connect to a Metropolis cluster. A cluster endpoint must be provided with the
--endpoints parameter.`,
Args: PrintUsageOnWrongArgs(cobra.ExactArgs(0)),
- Run: doK8sConfigure,
-}
-
-func doK8sConfigure(cmd *cobra.Command, _ []string) {
- ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
- if len(flags.clusterEndpoints) < 1 {
- log.Fatalf("k8s configure requires at least one cluster endpoint to be provided with the --endpoints parameter.")
- }
-
- contextName, err := cmd.Flags().GetString("context")
- if err != nil || contextName == "" {
- log.Fatalf("k8s configure requires a valid context name to be provided with the --context parameter.")
- }
-
- // If the user has metroctl in their path, use the metroctl from path as
- // a credential plugin. Otherwise use the path to the currently-running
- // metroctl.
- metroctlPath := "metroctl"
- if _, err := exec.LookPath("metroctl"); err != nil {
- metroctlPath, err = os.Executable()
- if err != nil {
- log.Fatalf("Failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %v", err)
+ RunE: func(cmd *cobra.Command, _ []string) error {
+ ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
+ if len(flags.clusterEndpoints) < 1 {
+ return fmt.Errorf("k8s configure requires at least one cluster endpoint to be provided with the --endpoints parameter")
}
- }
- // TODO(q3k, issues/144): this only works as long as all nodes are kubernetes controller
- // nodes. This won't be the case for too long. Figure this out.
- if err := core.InstallKubeletConfig(ctx, metroctlPath, connectOptions(), contextName, flags.clusterEndpoints[0]); err != nil {
- log.Fatalf("Failed to install metroctl/k8s integration: %v", err)
- }
- log.Printf("Success! kubeconfig is set up. You can now run kubectl --context=%s ... to access the Kubernetes cluster.", contextName)
+
+ contextName, err := cmd.Flags().GetString("context")
+ if err != nil || contextName == "" {
+ return fmt.Errorf("k8s configure requires a valid context name to be provided with the --context parameter")
+ }
+
+ // If the user has metroctl in their path, use the metroctl from path as
+ // a credential plugin. Otherwise use the path to the currently-running
+ // metroctl.
+ metroctlPath := "metroctl"
+ if _, err := exec.LookPath("metroctl"); err != nil {
+ metroctlPath, err = os.Executable()
+ if err != nil {
+ return fmt.Errorf("failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %w", err)
+ }
+ }
+ // TODO(q3k, issues/144): this only works as long as all nodes are kubernetes controller
+ // nodes. This won't be the case for too long. Figure this out.
+ if err := core.InstallKubeletConfig(ctx, metroctlPath, connectOptions(), contextName, flags.clusterEndpoints[0]); err != nil {
+ return fmt.Errorf("failed to install metroctl/k8s integration: %w", err)
+ }
+ log.Printf("Success! kubeconfig is set up. You can now run kubectl --context=%s ... to access the Kubernetes cluster.", contextName)
+ return nil
+ },
}
func init() {