diff --git a/agent/main.go b/agent/main.go index 907920203..20841dd45 100644 --- a/agent/main.go +++ b/agent/main.go @@ -398,10 +398,11 @@ func getSyncEntriesConfig() *shared.SyncEntriesConfig { } func determineLogLevel() (logLevel logging.Level) { - logLevel = logging.INFO - if os.Getenv(shared.DebugModeEnvVar) == "1" { - logLevel = logging.DEBUG + logLevel, err := logging.LogLevel(os.Getenv(shared.LogLevelEnvVar)) + if err != nil { + logLevel = logging.INFO } + return } @@ -438,7 +439,7 @@ func startMizuTapperSyncer(ctx context.Context) (*kubernetes.MizuTapperSyncer, e AgentImage: config.Config.AgentImage, TapperResources: config.Config.TapperResources, ImagePullPolicy: v1.PullPolicy(config.Config.PullPolicy), - DumpLogs: config.Config.DumpLogs, + LogLevel: config.Config.LogLevel, IgnoredUserAgents: config.Config.IgnoredUserAgents, MizuApiFilteringOptions: config.Config.MizuApiFilteringOptions, MizuServiceAccountExists: true, //assume service account exists since daemon mode will not function without it anyway diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 9c40d6647..97168ce31 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -23,6 +23,7 @@ Further info is available at https://github.com/up9inc/mizu`, if err := config.InitConfig(cmd); err != nil { logger.Log.Fatal(err) } + return nil }, } diff --git a/cli/cmd/tapRunner.go b/cli/cmd/tapRunner.go index 1dcee43d7..d027c87e5 100644 --- a/cli/cmd/tapRunner.go +++ b/cli/cmd/tapRunner.go @@ -33,7 +33,7 @@ import ( "github.com/up9inc/mizu/tap/api" ) -const cleanupTimeout = 5 * time.Minute +const cleanupTimeout = time.Minute type tapState struct { apiServerService *core.Service @@ -200,7 +200,7 @@ func startTapperSyncer(ctx context.Context, cancel context.CancelFunc, provider AgentImage: config.Config.AgentImage, TapperResources: config.Config.Tap.TapperResources, ImagePullPolicy: config.Config.ImagePullPolicy(), - DumpLogs: config.Config.DumpLogs, + LogLevel: config.Config.LogLevel(), IgnoredUserAgents: config.Config.Tap.IgnoredUserAgents, MizuApiFilteringOptions: mizuApiFilteringOptions, MizuServiceAccountExists: state.mizuServiceAccountExists, @@ -310,6 +310,7 @@ func createMizuResources(ctx context.Context, cancel context.CancelFunc, kuberne MaxEntriesDBSizeBytes: config.Config.Tap.MaxEntriesDBSizeBytes(), Resources: config.Config.Tap.ApiServerResources, ImagePullPolicy: config.Config.ImagePullPolicy(), + LogLevel: config.Config.LogLevel(), } if config.Config.Tap.DaemonMode { diff --git a/cli/config/config.go b/cli/config/config.go index 361d50068..090b45f46 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -52,6 +52,10 @@ func InitConfig(cmd *cobra.Command) error { cmd.Flags().Visit(initFlag) + if err := Config.validate(); err != nil { + return fmt.Errorf("config validation failed, err: %v", err) + } + finalConfigPrettified, _ := uiUtils.PrettyJson(Config) logger.Log.Debugf("Init config finished\n Final config: %v", finalConfigPrettified) @@ -392,7 +396,7 @@ func getMizuAgentConfig(targetNamespaces []string, mizuApiFilteringOptions *api. TargetNamespaces: targetNamespaces, AgentImage: Config.AgentImage, PullPolicy: Config.ImagePullPolicyStr, - DumpLogs: Config.DumpLogs, + LogLevel: Config.LogLevel(), IgnoredUserAgents: Config.Tap.IgnoredUserAgents, TapperResources: Config.Tap.TapperResources, MizuResourcesNamespace: Config.MizuResourcesNamespace, diff --git a/cli/config/configStruct.go b/cli/config/configStruct.go index b625141e8..f0402bf2d 100644 --- a/cli/config/configStruct.go +++ b/cli/config/configStruct.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "github.com/op/go-logging" "github.com/up9inc/mizu/cli/config/configStructs" "github.com/up9inc/mizu/cli/mizu" v1 "k8s.io/api/core/v1" @@ -32,6 +33,15 @@ type ConfigStruct struct { KubeConfigPathStr string `yaml:"kube-config-path"` ConfigFilePath string `yaml:"config-path,omitempty" readonly:""` HeadlessMode bool `yaml:"headless" default:"false"` + LogLevelStr string `yaml:"log-level,omitempty" default:"INFO" readonly:""` +} + +func(config *ConfigStruct) validate() error { + if _, err := logging.LogLevel(config.LogLevelStr); err != nil { + return fmt.Errorf("%s is not a valid log level, err: %v", config.LogLevelStr, err) + } + + return nil } func (config *ConfigStruct) SetDefaults() { @@ -60,3 +70,8 @@ func (config *ConfigStruct) KubeConfigPath() string { home := homedir.HomeDir() return filepath.Join(home, ".kube", "config") } + +func (config *ConfigStruct) LogLevel() logging.Level { + logLevel, _ := logging.LogLevel(config.LogLevelStr) + return logLevel +} diff --git a/cli/go.mod b/cli/go.mod index e8233696f..88fed6c65 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -8,6 +8,7 @@ require ( github.com/getkin/kin-openapi v0.79.0 github.com/google/go-github/v37 v37.0.0 github.com/google/uuid v1.1.2 + github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/spf13/cobra v1.1.3 github.com/spf13/pflag v1.0.5 github.com/up9inc/mizu/shared v0.0.0 diff --git a/shared/consts.go b/shared/consts.go index 6dc3067b5..97dc362d7 100644 --- a/shared/consts.go +++ b/shared/consts.go @@ -13,7 +13,7 @@ const ( ConfigFileName = "mizu-config.json" GoGCEnvVar = "GOGC" DefaultApiServerPort = 8899 - DebugModeEnvVar = "MIZU_DEBUG" + LogLevelEnvVar = "LOG_LEVEL" BasenineHost = "localhost" BaseninePort = "9099" ) diff --git a/shared/kubernetes/mizuTapperSyncer.go b/shared/kubernetes/mizuTapperSyncer.go index d6b81387a..1d67e98cd 100644 --- a/shared/kubernetes/mizuTapperSyncer.go +++ b/shared/kubernetes/mizuTapperSyncer.go @@ -3,6 +3,7 @@ package kubernetes import ( "context" "fmt" + "github.com/op/go-logging" "github.com/up9inc/mizu/shared" "github.com/up9inc/mizu/shared/debounce" "github.com/up9inc/mizu/shared/logger" @@ -36,7 +37,7 @@ type TapperSyncerConfig struct { AgentImage string TapperResources shared.Resources ImagePullPolicy core.PullPolicy - DumpLogs bool + LogLevel logging.Level IgnoredUserAgents []string MizuApiFilteringOptions api.TrafficFilteringOptions MizuServiceAccountExists bool @@ -192,7 +193,7 @@ func (tapperSyncer *MizuTapperSyncer) updateMizuTappers() error { tapperSyncer.config.TapperResources, tapperSyncer.config.ImagePullPolicy, tapperSyncer.config.MizuApiFilteringOptions, - tapperSyncer.config.DumpLogs, + tapperSyncer.config.LogLevel, ); err != nil { return err } diff --git a/shared/kubernetes/provider.go b/shared/kubernetes/provider.go index 06538f4c2..d18ad34bc 100644 --- a/shared/kubernetes/provider.go +++ b/shared/kubernetes/provider.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/op/go-logging" "github.com/up9inc/mizu/shared" "github.com/up9inc/mizu/shared/logger" "github.com/up9inc/mizu/shared/semver" @@ -179,7 +180,7 @@ type ApiServerOptions struct { MaxEntriesDBSizeBytes int64 Resources shared.Resources ImagePullPolicy core.PullPolicy - DumpLogs bool + LogLevel logging.Level } func (provider *Provider) GetMizuApiServerPodObject(opts *ApiServerOptions, mountVolumeClaim bool, volumeClaimName string) (*core.Pod, error) { @@ -248,11 +249,6 @@ func (provider *Provider) GetMizuApiServerPodObject(opts *ApiServerOptions, moun port := intstr.FromInt(shared.DefaultApiServerPort) - debugMode := "" - if opts.DumpLogs { - debugMode = "1" - } - pod := &core.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: opts.PodName, @@ -272,8 +268,8 @@ func (provider *Provider) GetMizuApiServerPodObject(opts *ApiServerOptions, moun Value: string(marshaledSyncEntriesConfig), }, { - Name: shared.DebugModeEnvVar, - Value: debugMode, + Name: shared.LogLevelEnvVar, + Value: opts.LogLevel.String(), }, }, Resources: core.ResourceRequirements{ @@ -619,7 +615,7 @@ func (provider *Provider) CreateConfigMap(ctx context.Context, namespace string, return nil } -func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespace string, daemonSetName string, podImage string, tapperPodName string, apiServerPodIp string, nodeToTappedPodIPMap map[string][]string, serviceAccountName string, resources shared.Resources, imagePullPolicy core.PullPolicy, mizuApiFilteringOptions api.TrafficFilteringOptions, dumpLogs bool) error { +func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespace string, daemonSetName string, podImage string, tapperPodName string, apiServerPodIp string, nodeToTappedPodIPMap map[string][]string, serviceAccountName string, resources shared.Resources, imagePullPolicy core.PullPolicy, mizuApiFilteringOptions api.TrafficFilteringOptions, logLevel logging.Level) error { logger.Log.Debugf("Applying %d tapper daemon sets, ns: %s, daemonSetName: %s, podImage: %s, tapperPodName: %s", len(nodeToTappedPodIPMap), namespace, daemonSetName, podImage, tapperPodName) if len(nodeToTappedPodIPMap) == 0 { @@ -645,11 +641,6 @@ func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespac "--procfs", procfsMountPath, } - debugMode := "" - if dumpLogs { - debugMode = "1" - } - agentContainer := applyconfcore.Container() agentContainer.WithName(tapperPodName) agentContainer.WithImage(podImage) @@ -657,7 +648,7 @@ func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespac agentContainer.WithSecurityContext(applyconfcore.SecurityContext().WithPrivileged(true)) agentContainer.WithCommand(mizuCmd...) agentContainer.WithEnv( - applyconfcore.EnvVar().WithName(shared.DebugModeEnvVar).WithValue(debugMode), + applyconfcore.EnvVar().WithName(shared.LogLevelEnvVar).WithValue(logLevel.String()), applyconfcore.EnvVar().WithName(shared.HostModeEnvVar).WithValue("1"), applyconfcore.EnvVar().WithName(shared.TappedAddressesPerNodeDictEnvVar).WithValue(string(nodeToTappedPodIPMapJsonStr)), applyconfcore.EnvVar().WithName(shared.GoGCEnvVar).WithValue("12800"), diff --git a/shared/models.go b/shared/models.go index 82b0abaef..5bd99d9c0 100644 --- a/shared/models.go +++ b/shared/models.go @@ -1,6 +1,7 @@ package shared import ( + "github.com/op/go-logging" "github.com/up9inc/mizu/tap/api" "io/ioutil" "log" @@ -36,7 +37,7 @@ type MizuAgentConfig struct { TargetNamespaces []string `json:"targetNamespaces"` AgentImage string `json:"agentImage"` PullPolicy string `json:"pullPolicy"` - DumpLogs bool `json:"dumpLogs"` + LogLevel logging.Level `json:"logLevel"` IgnoredUserAgents []string `json:"ignoredUserAgents"` TapperResources Resources `json:"tapperResources"` MizuResourcesNamespace string `json:"mizuResourceNamespace"`