Skip to content

Commit cfa1c89

Browse files
authored
fix: disable automaxprocs logging (argoproj#20069)
* 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]>
1 parent f984569 commit cfa1c89

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

cmd/main.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"os"
55
"path/filepath"
66

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

9-
_ "go.uber.org/automaxprocs"
9+
"github.com/spf13/cobra"
1010

1111
appcontroller "github.com/argoproj/argo-cd/v2/cmd/argocd-application-controller/commands"
1212
applicationset "github.com/argoproj/argo-cd/v2/cmd/argocd-applicationset-controller/commands"
@@ -31,9 +31,12 @@ func main() {
3131
if val := os.Getenv(binaryNameEnv); val != "" {
3232
binaryName = val
3333
}
34+
35+
isCLI := false
3436
switch binaryName {
3537
case "argocd", "argocd-linux-amd64", "argocd-darwin-amd64", "argocd-windows-amd64.exe":
3638
command = cli.NewCommand()
39+
isCLI = true
3740
case "argocd-server":
3841
command = apiserver.NewCommand()
3942
case "argocd-application-controller":
@@ -42,19 +45,24 @@ func main() {
4245
command = reposerver.NewCommand()
4346
case "argocd-cmp-server":
4447
command = cmpserver.NewCommand()
48+
isCLI = true
4549
case "argocd-dex":
4650
command = dex.NewCommand()
4751
case "argocd-notifications":
4852
command = notification.NewCommand()
4953
case "argocd-git-ask-pass":
5054
command = gitaskpass.NewCommand()
55+
isCLI = true
5156
case "argocd-applicationset-controller":
5257
command = applicationset.NewCommand()
5358
case "argocd-k8s-auth":
5459
command = k8sauth.NewCommand()
60+
isCLI = true
5561
default:
5662
command = cli.NewCommand()
63+
isCLI = true
5764
}
65+
util.SetAutoMaxProcs(isCLI)
5866

5967
if err := command.Execute(); err != nil {
6068
os.Exit(1)

cmd/util/app.go

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"time"
1111

12+
"go.uber.org/automaxprocs/maxprocs"
13+
1214
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1315

1416
"github.com/argoproj/gitops-engine/pkg/utils/kube"
@@ -89,6 +91,19 @@ type AppOptions struct {
8991
ref string
9092
}
9193

94+
// SetAutoMaxProcs sets the GOMAXPROCS value based on the binary name.
95+
// It suppresses logs for CLI binaries and logs the setting for services.
96+
func SetAutoMaxProcs(isCLI bool) {
97+
if isCLI {
98+
_, _ = maxprocs.Set() // Intentionally ignore errors for CLI binaries
99+
} else {
100+
_, err := maxprocs.Set(maxprocs.Logger(log.Infof))
101+
if err != nil {
102+
log.Errorf("Error setting GOMAXPROCS: %v", err)
103+
}
104+
}
105+
}
106+
92107
func AddAppFlags(command *cobra.Command, opts *AppOptions) {
93108
command.Flags().StringVar(&opts.repoURL, "repo", "", "Repository URL, ignored if a file is set")
94109
command.Flags().StringVar(&opts.appPath, "path", "", "Path in repository to the app directory, ignored if a file is set")

cmd/util/app_test.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package util
22

33
import (
4+
"bytes"
5+
"log"
46
"os"
57
"testing"
68

7-
log "github.com/sirupsen/logrus"
89
"github.com/spf13/cobra"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
@@ -534,3 +535,27 @@ func TestFilterResources(t *testing.T) {
534535
assert.Nil(t, filteredResources)
535536
})
536537
}
538+
539+
func TestSetAutoMaxProcs(t *testing.T) {
540+
t.Run("CLI mode ignores errors", func(t *testing.T) {
541+
logBuffer := &bytes.Buffer{}
542+
oldLogger := log.Default()
543+
log.SetOutput(logBuffer)
544+
defer log.SetOutput(oldLogger.Writer())
545+
546+
SetAutoMaxProcs(true)
547+
548+
assert.Empty(t, logBuffer.String(), "Expected no log output when isCLI is true")
549+
})
550+
551+
t.Run("Non-CLI mode logs error on failure", func(t *testing.T) {
552+
logBuffer := &bytes.Buffer{}
553+
oldLogger := log.Default()
554+
log.SetOutput(logBuffer)
555+
defer log.SetOutput(oldLogger.Writer())
556+
557+
SetAutoMaxProcs(false)
558+
559+
assert.NotContains(t, logBuffer.String(), "Error setting GOMAXPROCS", "Unexpected log output detected")
560+
})
561+
}

0 commit comments

Comments
 (0)