diff --git a/root.go b/root.go index 4013337..8279485 100644 --- a/root.go +++ b/root.go @@ -15,15 +15,16 @@ package cmd import ( - "fmt" - "log" + "context" "os" "path/filepath" "strings" + "github.com/Sirupsen/logrus" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/virtual-kubelet/virtual-kubelet/log" "github.com/virtual-kubelet/virtual-kubelet/providers" vkubelet "github.com/virtual-kubelet/virtual-kubelet/vkubelet" corev1 "k8s.io/api/core/v1" @@ -38,6 +39,7 @@ var provider string var providerConfig string var taintKey string var disableTaint bool +var logLevel string // RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ @@ -47,12 +49,13 @@ var RootCmd = &cobra.Command{ backend implementation allowing users to create kubernetes nodes without running the kubelet. This allows users to schedule kubernetes workloads on nodes that aren't running Kubernetes.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println(kubeConfig) f, err := vkubelet.New(nodeName, operatingSystem, kubeNamespace, kubeConfig, provider, providerConfig, taintKey, disableTaint) if err != nil { - log.Fatal(err) + log.L.WithError(err).Fatal("Error initializing vritual kubelet") + } + if err := f.Run(context.Background()); err != nil { + log.L.Fatal(err) } - f.Run() }, } @@ -60,8 +63,7 @@ This allows users to schedule kubernetes workloads on nodes that aren't running // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := RootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + log.GetLogger(context.TODO()).WithError(err).Fatal("Error executing root command") } } @@ -87,6 +89,7 @@ func init() { RootCmd.PersistentFlags().StringVar(&providerConfig, "provider-config", "", "cloud provider configuration file") RootCmd.PersistentFlags().StringVar(&taintKey, "taint", "", "Set node taint key") RootCmd.PersistentFlags().MarkDeprecated("taint", "Taint key should now be configured using the VK_TAINT_KEY environment variable") + RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", `set the log level, e.g. "trace", debug", "info", "warn", "error"`) // Cobra also supports local flags, which will only run // when this action is called directly. @@ -96,15 +99,13 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { if provider == "" { - fmt.Println("You must supply a cloud provider option: use --provider") - os.Exit(1) + log.G(context.TODO()).Fatal("You must supply a cloud provider option: use --provider") } // Find home directory. home, err := homedir.Dir() if err != nil { - fmt.Println(err) - os.Exit(1) + log.G(context.TODO()).WithError(err).Fatal("Error reading homedir") } if kubeletConfig != "" { @@ -120,7 +121,7 @@ func initConfig() { // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { - fmt.Println("Using config file:", viper.ConfigFileUsed()) + log.G(context.TODO()).Debugf("Using config file %s", viper.ConfigFileUsed()) } if kubeConfig == "" { @@ -135,7 +136,20 @@ func initConfig() { // Validate operating system. ok, _ := providers.ValidOperatingSystems[operatingSystem] if !ok { - fmt.Printf("Operating system '%s' not supported. Valid options are %s\n", operatingSystem, strings.Join(providers.ValidOperatingSystems.Names(), " | ")) - os.Exit(1) + log.G(context.TODO()).WithField("OperatingSystem", operatingSystem).Fatalf("Operating system not supported. Valid options are: %s", strings.Join(providers.ValidOperatingSystems.Names(), " | ")) } + + level, err := log.ParseLevel(logLevel) + if err != nil { + log.G(context.TODO()).WithField("logLevel", logLevel).Fatal("log level is not supported") + } + + logger := log.L.WithFields(logrus.Fields{ + "provider": provider, + "operatingSystem": operatingSystem, + "node": nodeName, + "namespace": kubeNamespace, + }) + logger.Level = level + log.L = logger }