Skip to content

Commit 3e7378a

Browse files
chore: Address comments in PR #854 (#886)
Signed-off-by: Ishita Sequeira <ishiseq29@gmail.com>
1 parent ad9648f commit 3e7378a

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

pkg/argocd/argocd.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
12+
"github.com/argoproj-labs/argocd-image-updater/pkg/env"
1213
"github.com/argoproj-labs/argocd-image-updater/pkg/image"
1314
"github.com/argoproj-labs/argocd-image-updater/pkg/kube"
1415
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
@@ -31,7 +32,7 @@ func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v
3132
log.Debugf("Getting application %s across all namespaces", appName)
3233

3334
// List all applications across all namespaces (using empty labelSelector)
34-
appList, err := client.ListApplications("")
35+
appList, err := client.ListApplications(v1.NamespaceAll)
3536
if err != nil {
3637
return nil, fmt.Errorf("error listing applications: %w", err)
3738
}
@@ -44,7 +45,7 @@ func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v
4445
}
4546

4647
// Retrieve the application in the specified namespace
47-
return client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(ctx, app.Name, v1.GetOptions{})
48+
return app, nil
4849
}
4950

5051
// ListApplications lists all applications across all namespaces.
@@ -85,13 +86,7 @@ func (client *k8sClient) UpdateSpec(ctx context.Context, spec *application.Appli
8586
const baseDelay = 100 * time.Millisecond // Initial delay before retrying
8687

8788
// Allow overriding max retries for testing purposes
88-
maxRetries := defaultMaxRetries
89-
if overrideRetries, ok := os.LookupEnv("OVERRIDE_MAX_RETRIES"); ok {
90-
var retries int
91-
if _, err := fmt.Sscanf(overrideRetries, "%d", &retries); err == nil {
92-
maxRetries = retries
93-
}
94-
}
89+
maxRetries := env.ParseNumFromEnv("OVERRIDE_MAX_RETRIES", defaultMaxRetries, 0, 100)
9590

9691
for attempts := 0; attempts < maxRetries; attempts++ {
9792
app, err := client.GetApplication(ctx, spec.GetName())

pkg/env/env.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package env
22

33
import (
4+
"math"
45
"os"
6+
"strconv"
57
"strings"
8+
9+
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
610
)
711

812
// Package env provides some utility functions to interact with the environment
@@ -30,3 +34,33 @@ func GetStringVal(envVar string, defaultValue string) string {
3034
return defaultValue
3135
}
3236
}
37+
38+
// Helper function to parse a number from an environment variable. Returns a
39+
// default if env is not set, is not parseable to a number, exceeds max (if
40+
// max is greater than 0) or is less than min.
41+
//
42+
// nolint:unparam
43+
func ParseNumFromEnv(env string, defaultValue, min, max int) int {
44+
str := os.Getenv(env)
45+
if str == "" {
46+
return defaultValue
47+
}
48+
num, err := strconv.ParseInt(str, 10, 0)
49+
if err != nil {
50+
log.Warnf("Could not parse '%s' as a number from environment %s", str, env)
51+
return defaultValue
52+
}
53+
if num > math.MaxInt || num < math.MinInt {
54+
log.Warnf("Value in %s is %d is outside of the min and max %d allowed values. Using default %d", env, num, min, defaultValue)
55+
return defaultValue
56+
}
57+
if int(num) < min {
58+
log.Warnf("Value in %s is %d, which is less than minimum %d allowed", env, num, min)
59+
return defaultValue
60+
}
61+
if int(num) > max {
62+
log.Warnf("Value in %s is %d, which is greater than maximum %d allowed", env, num, max)
63+
return defaultValue
64+
}
65+
return int(num)
66+
}

pkg/env/env_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,30 @@ func Test_GetStringVal(t *testing.T) {
3636
assert.Equal(t, "invalid", GetStringVal("TEST_STRING_VAL", "invalid"))
3737
})
3838
}
39+
40+
func Test_ParseNumFromEnv(t *testing.T) {
41+
t.Run("Get number from existing env var within range", func(t *testing.T) {
42+
_ = os.Setenv("TEST_NUM_VAL", "5")
43+
defer os.Setenv("TEST_NUM_VAL", "")
44+
assert.Equal(t, 5, ParseNumFromEnv("TEST_NUM_VAL", 0, 1, 10))
45+
})
46+
t.Run("Get default value from non-existing env var", func(t *testing.T) {
47+
_ = os.Setenv("TEST_NUM_VAL", "")
48+
assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20))
49+
})
50+
t.Run("Get default value from env var with non-numeric value", func(t *testing.T) {
51+
_ = os.Setenv("TEST_NUM_VAL", "abc")
52+
defer os.Setenv("TEST_NUM_VAL", "")
53+
assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20))
54+
})
55+
t.Run("Get default value from env var with value less than min", func(t *testing.T) {
56+
_ = os.Setenv("TEST_NUM_VAL", "0")
57+
defer os.Setenv("TEST_NUM_VAL", "")
58+
assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20))
59+
})
60+
t.Run("Get default value from env var with value greater than max", func(t *testing.T) {
61+
_ = os.Setenv("TEST_NUM_VAL", "30")
62+
defer os.Setenv("TEST_NUM_VAL", "")
63+
assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20))
64+
})
65+
}

0 commit comments

Comments
 (0)