Use standard logging package (#323)
This commit is contained in:
42
root.go
42
root.go
@@ -15,15 +15,16 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
homedir "github.com/mitchellh/go-homedir"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||||
"github.com/virtual-kubelet/virtual-kubelet/providers"
|
"github.com/virtual-kubelet/virtual-kubelet/providers"
|
||||||
vkubelet "github.com/virtual-kubelet/virtual-kubelet/vkubelet"
|
vkubelet "github.com/virtual-kubelet/virtual-kubelet/vkubelet"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
@@ -38,6 +39,7 @@ var provider string
|
|||||||
var providerConfig string
|
var providerConfig string
|
||||||
var taintKey string
|
var taintKey string
|
||||||
var disableTaint bool
|
var disableTaint bool
|
||||||
|
var logLevel string
|
||||||
|
|
||||||
// RootCmd represents the base command when called without any subcommands
|
// RootCmd represents the base command when called without any subcommands
|
||||||
var RootCmd = &cobra.Command{
|
var RootCmd = &cobra.Command{
|
||||||
@@ -47,12 +49,13 @@ var RootCmd = &cobra.Command{
|
|||||||
backend implementation allowing users to create kubernetes nodes without running the kubelet.
|
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.`,
|
This allows users to schedule kubernetes workloads on nodes that aren't running Kubernetes.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println(kubeConfig)
|
|
||||||
f, err := vkubelet.New(nodeName, operatingSystem, kubeNamespace, kubeConfig, provider, providerConfig, taintKey, disableTaint)
|
f, err := vkubelet.New(nodeName, operatingSystem, kubeNamespace, kubeConfig, provider, providerConfig, taintKey, disableTaint)
|
||||||
if err != nil {
|
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.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
func Execute() {
|
func Execute() {
|
||||||
if err := RootCmd.Execute(); err != nil {
|
if err := RootCmd.Execute(); err != nil {
|
||||||
fmt.Println(err)
|
log.GetLogger(context.TODO()).WithError(err).Fatal("Error executing root command")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +89,7 @@ func init() {
|
|||||||
RootCmd.PersistentFlags().StringVar(&providerConfig, "provider-config", "", "cloud provider configuration file")
|
RootCmd.PersistentFlags().StringVar(&providerConfig, "provider-config", "", "cloud provider configuration file")
|
||||||
RootCmd.PersistentFlags().StringVar(&taintKey, "taint", "", "Set node taint key")
|
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().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
|
// Cobra also supports local flags, which will only run
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
@@ -96,15 +99,13 @@ func init() {
|
|||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
if provider == "" {
|
if provider == "" {
|
||||||
fmt.Println("You must supply a cloud provider option: use --provider")
|
log.G(context.TODO()).Fatal("You must supply a cloud provider option: use --provider")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find home directory.
|
// Find home directory.
|
||||||
home, err := homedir.Dir()
|
home, err := homedir.Dir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.G(context.TODO()).WithError(err).Fatal("Error reading homedir")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if kubeletConfig != "" {
|
if kubeletConfig != "" {
|
||||||
@@ -120,7 +121,7 @@ func initConfig() {
|
|||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
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 == "" {
|
if kubeConfig == "" {
|
||||||
@@ -135,7 +136,20 @@ func initConfig() {
|
|||||||
// Validate operating system.
|
// Validate operating system.
|
||||||
ok, _ := providers.ValidOperatingSystems[operatingSystem]
|
ok, _ := providers.ValidOperatingSystems[operatingSystem]
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Printf("Operating system '%s' not supported. Valid options are %s\n", operatingSystem, strings.Join(providers.ValidOperatingSystems.Names(), " | "))
|
log.G(context.TODO()).WithField("OperatingSystem", operatingSystem).Fatalf("Operating system not supported. Valid options are: %s", strings.Join(providers.ValidOperatingSystems.Names(), " | "))
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user