Skip to content

Commit

Permalink
Incorrect agent's DebugLevel when settings propagation is set (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
aruiz14 authored Sep 12, 2023
1 parent 2178cd9 commit 0d26f20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
15 changes: 6 additions & 9 deletions internal/cmd/controller/agent/manifest.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package agent

import (
"os"
"path"
"strconv"
"strings"
Expand All @@ -20,7 +19,8 @@ import (
)

var (
DebugLevel = 0
DebugEnabled bool
DebugLevel = 0
)

const (
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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",
Expand Down
19 changes: 14 additions & 5 deletions internal/cmd/controller/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"runtime/pprof"
"strconv"
"time"

"github.com/spf13/cobra"
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down

0 comments on commit 0d26f20

Please sign in to comment.