Skip to content

Commit

Permalink
fix: disable automaxprocs logging (argoproj#20069)
Browse files Browse the repository at this point in the history
* disable automaxprocs logging

Signed-off-by: nitishfy <[email protected]>

fix lint checks

Signed-off-by: nitishfy <[email protected]>

move maxprocs to main.go

Signed-off-by: nitishfy <[email protected]>

move set auto max procs to a function

Signed-off-by: nitishfy <[email protected]>

add info log

Signed-off-by: nitishfy <[email protected]>

* add info log

Signed-off-by: nitishfy <[email protected]>

* fix lint checks

Signed-off-by: nitishfy <[email protected]>

* fix lint checks

Signed-off-by: nitishfy <[email protected]>

* add unit test

Signed-off-by: nitishfy <[email protected]>

* fix lint issues

Signed-off-by: nitishfy <[email protected]>

---------

Signed-off-by: nitishfy <[email protected]>
  • Loading branch information
nitishfy authored Oct 9, 2024
1 parent f984569 commit cfa1c89
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
12 changes: 10 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/argoproj/argo-cd/v2/cmd/util"

_ "go.uber.org/automaxprocs"
"github.com/spf13/cobra"

appcontroller "github.com/argoproj/argo-cd/v2/cmd/argocd-application-controller/commands"
applicationset "github.com/argoproj/argo-cd/v2/cmd/argocd-applicationset-controller/commands"
Expand All @@ -31,9 +31,12 @@ func main() {
if val := os.Getenv(binaryNameEnv); val != "" {
binaryName = val
}

isCLI := false
switch binaryName {
case "argocd", "argocd-linux-amd64", "argocd-darwin-amd64", "argocd-windows-amd64.exe":
command = cli.NewCommand()
isCLI = true
case "argocd-server":
command = apiserver.NewCommand()
case "argocd-application-controller":
Expand All @@ -42,19 +45,24 @@ func main() {
command = reposerver.NewCommand()
case "argocd-cmp-server":
command = cmpserver.NewCommand()
isCLI = true
case "argocd-dex":
command = dex.NewCommand()
case "argocd-notifications":
command = notification.NewCommand()
case "argocd-git-ask-pass":
command = gitaskpass.NewCommand()
isCLI = true
case "argocd-applicationset-controller":
command = applicationset.NewCommand()
case "argocd-k8s-auth":
command = k8sauth.NewCommand()
isCLI = true
default:
command = cli.NewCommand()
isCLI = true
}
util.SetAutoMaxProcs(isCLI)

if err := command.Execute(); err != nil {
os.Exit(1)
Expand Down
15 changes: 15 additions & 0 deletions cmd/util/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"go.uber.org/automaxprocs/maxprocs"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
Expand Down Expand Up @@ -89,6 +91,19 @@ type AppOptions struct {
ref string
}

// SetAutoMaxProcs sets the GOMAXPROCS value based on the binary name.
// It suppresses logs for CLI binaries and logs the setting for services.
func SetAutoMaxProcs(isCLI bool) {
if isCLI {
_, _ = maxprocs.Set() // Intentionally ignore errors for CLI binaries
} else {
_, err := maxprocs.Set(maxprocs.Logger(log.Infof))
if err != nil {
log.Errorf("Error setting GOMAXPROCS: %v", err)
}
}
}

func AddAppFlags(command *cobra.Command, opts *AppOptions) {
command.Flags().StringVar(&opts.repoURL, "repo", "", "Repository URL, ignored if a file is set")
command.Flags().StringVar(&opts.appPath, "path", "", "Path in repository to the app directory, ignored if a file is set")
Expand Down
27 changes: 26 additions & 1 deletion cmd/util/app_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package util

import (
"bytes"
"log"
"os"
"testing"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -534,3 +535,27 @@ func TestFilterResources(t *testing.T) {
assert.Nil(t, filteredResources)
})
}

func TestSetAutoMaxProcs(t *testing.T) {
t.Run("CLI mode ignores errors", func(t *testing.T) {
logBuffer := &bytes.Buffer{}
oldLogger := log.Default()
log.SetOutput(logBuffer)
defer log.SetOutput(oldLogger.Writer())

SetAutoMaxProcs(true)

assert.Empty(t, logBuffer.String(), "Expected no log output when isCLI is true")
})

t.Run("Non-CLI mode logs error on failure", func(t *testing.T) {
logBuffer := &bytes.Buffer{}
oldLogger := log.Default()
log.SetOutput(logBuffer)
defer log.SetOutput(oldLogger.Writer())

SetAutoMaxProcs(false)

assert.NotContains(t, logBuffer.String(), "Error setting GOMAXPROCS", "Unexpected log output detected")
})
}

0 comments on commit cfa1c89

Please sign in to comment.