Skip to content

Commit

Permalink
fix: disable automaxprocs logging (#20069) - cherry-pick 2.13 (#20718)
Browse files Browse the repository at this point in the history
* fix: disable automaxprocs logging (#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]>
(cherry picked from commit cfa1c89)

* fix(ci): ignore temporary files when checking for out of bound symlinks (#20527)

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

---------

Signed-off-by: cef <[email protected]>
Co-authored-by: Nitish Kumar <[email protected]>
Co-authored-by: ABBOUD Moncef <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent 7f45c9e commit d03ccf3
Show file tree
Hide file tree
Showing 4 changed files with 57 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 @@ -88,6 +90,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 @@ -529,3 +530,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")
})
}
6 changes: 6 additions & 0 deletions util/app/path/path.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package path

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -51,6 +52,11 @@ func CheckOutOfBoundsSymlinks(basePath string) error {
}
return filepath.Walk(absBasePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
// Ignore "no such file or directory" errors than can happen with
// temporary files such as .git/*.lock
if errors.Is(err, os.ErrNotExist) {
return nil
}
return fmt.Errorf("failed to walk for symlinks in %s: %w", absBasePath, err)
}
if files.IsSymlink(info) {
Expand Down

0 comments on commit d03ccf3

Please sign in to comment.