From 0d26f206b3c521fdb78218285ffb69a91c6e5c81 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz <4057165+aruiz14@users.noreply.github.com> Date: Tue, 12 Sep 2023 12:57:14 +0200 Subject: [PATCH] Incorrect agent's DebugLevel when settings propagation is set (#1776) --- internal/cmd/controller/agent/manifest.go | 15 ++++++--------- internal/cmd/controller/root.go | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/cmd/controller/agent/manifest.go b/internal/cmd/controller/agent/manifest.go index d36f4f714c..f6c36b1ac1 100644 --- a/internal/cmd/controller/agent/manifest.go +++ b/internal/cmd/controller/agent/manifest.go @@ -1,7 +1,6 @@ package agent import ( - "os" "path" "strconv" "strings" @@ -20,7 +19,8 @@ import ( ) var ( - DebugLevel = 0 + DebugEnabled bool + DebugLevel = 0 ) const ( @@ -89,10 +89,7 @@ func Manifest(namespace string, agentScope string, opts ManifestOptions) []runti }, } - // if debug is enabled in controller, enable in agents too (unless otherwise specified) - propagateDebug, _ := strconv.ParseBool(os.Getenv("FLEET_PROPAGATE_DEBUG_SETTINGS_TO_AGENTS")) - debug := logrus.IsLevelEnabled(logrus.DebugLevel) && propagateDebug - deployment := agentDeployment(namespace, agentScope, opts, debug) + deployment := agentDeployment(namespace, agentScope, opts) networkPolicy := &networkv1.NetworkPolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -132,7 +129,7 @@ func resolve(global, prefix, image string) string { return image } -func agentDeployment(namespace string, agentScope string, opts ManifestOptions, debug bool) *appsv1.Deployment { +func agentDeployment(namespace string, agentScope string, opts ManifestOptions) *appsv1.Deployment { name := DefaultName serviceAccount := DefaultName image := resolve(opts.SystemDefaultRegistry, opts.PrivateRepoURL, opts.AgentImage) @@ -213,7 +210,7 @@ func agentDeployment(namespace string, agentScope string, opts ManifestOptions, }, } - if !debug { + if !DebugEnabled { for _, container := range dep.Spec.Template.Spec.Containers { container.SecurityContext = &corev1.SecurityContext{ AllowPrivilegeEscalation: &[]bool{false}[0], @@ -247,7 +244,7 @@ func agentDeployment(namespace string, agentScope string, opts ManifestOptions, dep.Spec.Template.Spec.Containers[0].Env = append(dep.Spec.Template.Spec.Containers[0].Env, opts.AgentEnvVars...) } - if debug { + if DebugEnabled { dep.Spec.Template.Spec.Containers[0].Command = []string{ "fleetagent", "--debug", diff --git a/internal/cmd/controller/root.go b/internal/cmd/controller/root.go index 01ee5eb05e..1f6e79d926 100644 --- a/internal/cmd/controller/root.go +++ b/internal/cmd/controller/root.go @@ -8,6 +8,7 @@ import ( "os" "path" "runtime/pprof" + "strconv" "time" "github.com/spf13/cobra" @@ -33,19 +34,15 @@ type FleetManager struct { } func (f *FleetManager) Run(cmd *cobra.Command, args []string) error { + setupDebug() setupCpuPprof(cmd.Context()) go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) // nolint:gosec // Debugging only }() - debugConfig.MustSetupDebug() if err := start(cmd.Context(), f.Namespace, f.Kubeconfig, f.DisableGitops, f.DisableBootstrap); err != nil { return err } - if debugConfig.Debug { - agent.DebugLevel = debugConfig.DebugLevel - } - <-cmd.Context().Done() return nil } @@ -57,6 +54,18 @@ func App() *cobra.Command { return command.AddDebug(cmd, &debugConfig) } +// setupDebug parses debug flags and configures the relevant log levels +func setupDebug() { + debugConfig.MustSetupDebug() + + // if debug is enabled in controller, enable in agents too (unless otherwise specified) + propagateDebug, _ := strconv.ParseBool(os.Getenv("FLEET_PROPAGATE_DEBUG_SETTINGS_TO_AGENTS")) + if propagateDebug && debugConfig.Debug { + agent.DebugEnabled = true + agent.DebugLevel = debugConfig.DebugLevel + } +} + // setupCpuPprof starts a goroutine that captures a cpu pprof profile // into FLEET_CPU_PPROF_DIR every FLEET_CPU_PPROF_PERIOD func setupCpuPprof(ctx context.Context) {