From 736f12f55a73d17dbedc040b4f559a2cd33bbff4 Mon Sep 17 00:00:00 2001 From: Ishita Sequeira <46771830+ishitasequeira@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:51:50 +0530 Subject: Update references of cache, env, health to use modules from registry-scanner (#972) Signed-off-by: Ishita Sequeira --- cmd/run.go | 4 +-- cmd/run_test.go | 2 +- go.mod | 2 +- go.sum | 4 +-- pkg/argocd/argocd.go | 2 +- pkg/cache/cache.go | 20 ------------- pkg/cache/memcache.go | 74 ---------------------------------------------- pkg/cache/memcache_test.go | 70 ------------------------------------------- pkg/env/env.go | 66 ----------------------------------------- pkg/env/env_test.go | 65 ---------------------------------------- pkg/health/health.go | 25 ---------------- pkg/health/health_test.go | 52 -------------------------------- pkg/registry/endpoints.go | 2 +- 13 files changed, 8 insertions(+), 380 deletions(-) delete mode 100644 pkg/cache/cache.go delete mode 100644 pkg/cache/memcache.go delete mode 100644 pkg/cache/memcache_test.go delete mode 100644 pkg/env/env.go delete mode 100644 pkg/env/env_test.go delete mode 100644 pkg/health/health.go delete mode 100644 pkg/health/health_test.go diff --git a/cmd/run.go b/cmd/run.go index 4e2ba32..d96a62f 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -12,11 +12,11 @@ import ( "github.com/argoproj-labs/argocd-image-updater/pkg/argocd" "github.com/argoproj-labs/argocd-image-updater/pkg/common" - "github.com/argoproj-labs/argocd-image-updater/pkg/env" - "github.com/argoproj-labs/argocd-image-updater/pkg/health" "github.com/argoproj-labs/argocd-image-updater/pkg/metrics" "github.com/argoproj-labs/argocd-image-updater/pkg/registry" "github.com/argoproj-labs/argocd-image-updater/pkg/version" + "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/env" + "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/health" "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" "github.com/argoproj/argo-cd/v2/reposerver/askpass" diff --git a/cmd/run_test.go b/cmd/run_test.go index a9e316b..ee8af5a 100644 --- a/cmd/run_test.go +++ b/cmd/run_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/argoproj-labs/argocd-image-updater/pkg/env" + "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/env" ) // TestNewRunCommand tests various flags and their default values. diff --git a/go.mod b/go.mod index 79770c1..8a689bb 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.23.0 require ( github.com/Masterminds/semver/v3 v3.3.1 - github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20241213191436-f55eeef5df63 + github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20241218013735-14e60b8a83a4 github.com/argoproj/argo-cd/v2 v2.13.1 github.com/argoproj/gitops-engine v0.7.1-0.20240905010810-bd7681ae3f8b github.com/argoproj/pkg v0.13.7-0.20230627120311-a4dd357b057e diff --git a/go.sum b/go.sum index 5582b55..62b6b13 100644 --- a/go.sum +++ b/go.sum @@ -27,8 +27,8 @@ github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20241213191436-f55eeef5df63 h1:XG/3UqAQmNqTCyinX8dGF/exFIkV5RqyXJgcVPHVGRg= -github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20241213191436-f55eeef5df63/go.mod h1:GJD+hNHt9KbYFo1+OddD3vvqys7ct5ZIOx5yHFCBAXg= +github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20241218013735-14e60b8a83a4 h1:B6z7t09pdODcT4dJ2Rndata6+OFZOWjYlOXynyexFNc= +github.com/argoproj-labs/argocd-image-updater/registry-scanner v0.0.0-20241218013735-14e60b8a83a4/go.mod h1:GJD+hNHt9KbYFo1+OddD3vvqys7ct5ZIOx5yHFCBAXg= github.com/argoproj/argo-cd/v2 v2.13.1 h1:qoa8LD5suPCAYtoDNHp+BuqODf46hl5gCuXNj2oiAy0= github.com/argoproj/argo-cd/v2 v2.13.1/go.mod h1:RC23V2744nhZstZVpLCWTQLT2gR0+IXGC3GTBCI6M+I= github.com/argoproj/gitops-engine v0.7.1-0.20240905010810-bd7681ae3f8b h1:wOPWJ5MBScQO767WpU55oUJDXObfvPL0EfAYWxogbSw= diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index 6a52e7b..6ba85d5 100644 --- a/pkg/argocd/argocd.go +++ b/pkg/argocd/argocd.go @@ -9,10 +9,10 @@ import ( "time" "github.com/argoproj-labs/argocd-image-updater/pkg/common" - "github.com/argoproj-labs/argocd-image-updater/pkg/env" "github.com/argoproj-labs/argocd-image-updater/pkg/image" "github.com/argoproj-labs/argocd-image-updater/pkg/kube" "github.com/argoproj-labs/argocd-image-updater/pkg/metrics" + "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/env" "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go deleted file mode 100644 index e59400c..0000000 --- a/pkg/cache/cache.go +++ /dev/null @@ -1,20 +0,0 @@ -package cache - -import ( - "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag" -) - -type ImageTagCache interface { - HasTag(imageName string, imageTag string) bool - GetTag(imageName string, imageTag string) (*tag.ImageTag, error) - SetTag(imageName string, imgTag *tag.ImageTag) - ClearCache() - NumEntries() int -} - -// KnownImage represents a known image and the applications using it, without -// any version/tag information. -type KnownImage struct { - ImageName string - Applications []string -} diff --git a/pkg/cache/memcache.go b/pkg/cache/memcache.go deleted file mode 100644 index 03b4080..0000000 --- a/pkg/cache/memcache.go +++ /dev/null @@ -1,74 +0,0 @@ -package cache - -import ( - "fmt" - - "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag" - - memcache "github.com/patrickmn/go-cache" -) - -type MemCache struct { - cache *memcache.Cache -} - -// NewMemCache returns a new instance of MemCache -func NewMemCache() ImageTagCache { - mc := MemCache{} - c := memcache.New(0, 0) - mc.cache = c - return &mc -} - -// HasTag returns true if cache has entry for given tag, false if not -func (mc *MemCache) HasTag(imageName string, tagName string) bool { - tag, err := mc.GetTag(imageName, tagName) - if err != nil || tag == nil { - return false - } else { - return true - } -} - -// SetTag sets a tag entry into the cache -func (mc *MemCache) SetTag(imageName string, imgTag *tag.ImageTag) { - mc.cache.Set(tagCacheKey(imageName, imgTag.TagName), *imgTag, -1) -} - -// GetTag gets a tag entry from the cache -func (mc *MemCache) GetTag(imageName string, tagName string) (*tag.ImageTag, error) { - var imgTag tag.ImageTag - e, ok := mc.cache.Get(tagCacheKey(imageName, tagName)) - if !ok { - return nil, nil - } - imgTag, ok = e.(tag.ImageTag) - if !ok { - return nil, fmt.Errorf("") - } - return &imgTag, nil -} - -func (mc *MemCache) SetImage(imageName, application string) { - mc.cache.Set(imageCacheKey(imageName), application, -1) -} - -// ClearCache clears the cache -func (mc *MemCache) ClearCache() { - for k := range mc.cache.Items() { - mc.cache.Delete(k) - } -} - -// NumEntries returns the number of entries in the cache -func (mc *MemCache) NumEntries() int { - return mc.cache.ItemCount() -} - -func tagCacheKey(imageName, imageTag string) string { - return fmt.Sprintf("tags:%s:%s", imageName, imageTag) -} - -func imageCacheKey(imageName string) string { - return fmt.Sprintf("image:%s", imageName) -} diff --git a/pkg/cache/memcache_test.go b/pkg/cache/memcache_test.go deleted file mode 100644 index ff8bc5f..0000000 --- a/pkg/cache/memcache_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package cache - -import ( - "testing" - "time" - - memcache "github.com/patrickmn/go-cache" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag" -) - -func Test_MemCache(t *testing.T) { - imageName := "foo/bar" - imageTag := "v1.0.0" - t.Run("Cache hit", func(t *testing.T) { - mc := NewMemCache() - newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "") - mc.SetTag(imageName, newTag) - cachedTag, err := mc.GetTag(imageName, imageTag) - require.NoError(t, err) - require.NotNil(t, cachedTag) - assert.Equal(t, imageTag, cachedTag.TagName) - assert.True(t, mc.HasTag(imageName, imageTag)) - assert.Equal(t, 1, mc.NumEntries()) - }) - - t.Run("Cache miss", func(t *testing.T) { - mc := NewMemCache() - newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "") - mc.SetTag(imageName, newTag) - assert.Equal(t, 1, mc.NumEntries()) - cachedTag, err := mc.GetTag(imageName, "v1.0.1") - require.NoError(t, err) - require.Nil(t, cachedTag) - assert.False(t, mc.HasTag(imageName, "v1.0.1")) - }) - - t.Run("Cache clear", func(t *testing.T) { - mc := NewMemCache() - newTag := tag.NewImageTag(imageTag, time.Unix(0, 0), "") - mc.SetTag(imageName, newTag) - cachedTag, err := mc.GetTag(imageName, imageTag) - require.NoError(t, err) - require.NotNil(t, cachedTag) - assert.Equal(t, imageTag, cachedTag.TagName) - assert.True(t, mc.HasTag(imageName, imageTag)) - assert.Equal(t, 1, mc.NumEntries()) - mc.ClearCache() - assert.Equal(t, 0, mc.NumEntries()) - cachedTag, err = mc.GetTag(imageName, imageTag) - require.NoError(t, err) - require.Nil(t, cachedTag) - }) - t.Run("Image Cache Key", func(t *testing.T) { - mc := MemCache{ - cache: memcache.New(0, 0), - } - application := "application1" - key := imageCacheKey(imageName) - mc.SetImage(imageName, application) - app, b := mc.cache.Get(key) - assert.True(t, b) - assert.Equal(t, application, app) - assert.Equal(t, 1, mc.NumEntries()) - mc.ClearCache() - assert.Equal(t, 0, mc.NumEntries()) - }) -} diff --git a/pkg/env/env.go b/pkg/env/env.go deleted file mode 100644 index f000aa2..0000000 --- a/pkg/env/env.go +++ /dev/null @@ -1,66 +0,0 @@ -package env - -import ( - "math" - "os" - "strconv" - "strings" - - "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" -) - -// Package env provides some utility functions to interact with the environment -// of the process. - -// GetBoolVal retrieves a boolean value from given environment envVar. -// Returns default value if envVar is not set. -func GetBoolVal(envVar string, defaultValue bool) bool { - if val := os.Getenv(envVar); val != "" { - if strings.ToLower(val) == "true" { - return true - } else if strings.ToLower(val) == "false" { - return false - } - } - return defaultValue -} - -// GetStringVal retrieves a string value from given environment envVar -// Returns default value if envVar is not set. -func GetStringVal(envVar string, defaultValue string) string { - if val := os.Getenv(envVar); val != "" { - return val - } else { - return defaultValue - } -} - -// Helper function to parse a number from an environment variable. Returns a -// default if env is not set, is not parseable to a number, exceeds max (if -// max is greater than 0) or is less than min. -// -// nolint:unparam -func ParseNumFromEnv(env string, defaultValue, min, max int) int { - str := os.Getenv(env) - if str == "" { - return defaultValue - } - num, err := strconv.ParseInt(str, 10, 0) - if err != nil { - log.Warnf("Could not parse '%s' as a number from environment %s", str, env) - return defaultValue - } - if num > math.MaxInt || num < math.MinInt { - log.Warnf("Value in %s is %d is outside of the min and max %d allowed values. Using default %d", env, num, min, defaultValue) - return defaultValue - } - if int(num) < min { - log.Warnf("Value in %s is %d, which is less than minimum %d allowed", env, num, min) - return defaultValue - } - if int(num) > max { - log.Warnf("Value in %s is %d, which is greater than maximum %d allowed", env, num, max) - return defaultValue - } - return int(num) -} diff --git a/pkg/env/env_test.go b/pkg/env/env_test.go deleted file mode 100644 index 41ccaf9..0000000 --- a/pkg/env/env_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package env - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -func Test_GetBoolVal(t *testing.T) { - t.Run("Get 'true' value from existing env var", func(t *testing.T) { - _ = os.Setenv("TEST_BOOL_VAL", "true") - defer os.Setenv("TEST_BOOL_VAL", "") - assert.True(t, GetBoolVal("TEST_BOOL_VAL", false)) - }) - t.Run("Get 'false' value from existing env var", func(t *testing.T) { - _ = os.Setenv("TEST_BOOL_VAL", "false") - defer os.Setenv("TEST_BOOL_VAL", "") - assert.False(t, GetBoolVal("TEST_BOOL_VAL", true)) - }) - t.Run("Get default value from non-existing env var", func(t *testing.T) { - _ = os.Setenv("TEST_BOOL_VAL", "") - assert.True(t, GetBoolVal("TEST_BOOL_VAL", true)) - }) -} - -func Test_GetStringVal(t *testing.T) { - t.Run("Get string value from existing env var", func(t *testing.T) { - _ = os.Setenv("TEST_STRING_VAL", "test") - defer os.Setenv("TEST_STRING_VAL", "") - assert.Equal(t, "test", GetStringVal("TEST_STRING_VAL", "invalid")) - }) - t.Run("Get default value from non-existing env var", func(t *testing.T) { - _ = os.Setenv("TEST_STRING_VAL", "") - defer os.Setenv("TEST_STRING_VAL", "") - assert.Equal(t, "invalid", GetStringVal("TEST_STRING_VAL", "invalid")) - }) -} - -func Test_ParseNumFromEnv(t *testing.T) { - t.Run("Get number from existing env var within range", func(t *testing.T) { - _ = os.Setenv("TEST_NUM_VAL", "5") - defer os.Setenv("TEST_NUM_VAL", "") - assert.Equal(t, 5, ParseNumFromEnv("TEST_NUM_VAL", 0, 1, 10)) - }) - t.Run("Get default value from non-existing env var", func(t *testing.T) { - _ = os.Setenv("TEST_NUM_VAL", "") - assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20)) - }) - t.Run("Get default value from env var with non-numeric value", func(t *testing.T) { - _ = os.Setenv("TEST_NUM_VAL", "abc") - defer os.Setenv("TEST_NUM_VAL", "") - assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20)) - }) - t.Run("Get default value from env var with value less than min", func(t *testing.T) { - _ = os.Setenv("TEST_NUM_VAL", "0") - defer os.Setenv("TEST_NUM_VAL", "") - assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20)) - }) - t.Run("Get default value from env var with value greater than max", func(t *testing.T) { - _ = os.Setenv("TEST_NUM_VAL", "30") - defer os.Setenv("TEST_NUM_VAL", "") - assert.Equal(t, 10, ParseNumFromEnv("TEST_NUM_VAL", 10, 1, 20)) - }) -} diff --git a/pkg/health/health.go b/pkg/health/health.go deleted file mode 100644 index cbc4977..0000000 --- a/pkg/health/health.go +++ /dev/null @@ -1,25 +0,0 @@ -package health - -// Most simple health check probe to see whether our server is still alive - -import ( - "fmt" - "net/http" - - "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" -) - -func StartHealthServer(port int) chan error { - errCh := make(chan error) - go func() { - sm := http.NewServeMux() - sm.HandleFunc("/healthz", HealthProbe) - errCh <- http.ListenAndServe(fmt.Sprintf(":%d", port), sm) - }() - return errCh -} - -func HealthProbe(w http.ResponseWriter, r *http.Request) { - log.Tracef("/healthz ping request received, replying with pong") - fmt.Fprintf(w, "OK\n") -} diff --git a/pkg/health/health_test.go b/pkg/health/health_test.go deleted file mode 100644 index b5670f5..0000000 --- a/pkg/health/health_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package health - -import ( - "fmt" - "net/http" - "net/http/httptest" - "testing" - "time" -) - -// Unit test function -func TestStartHealthServer_InvalidPort(t *testing.T) { - // Use an invalid port number - port := -1 - errCh := StartHealthServer(port) - defer close(errCh) // Close the error channel after the test completes - select { - case err := <-errCh: - if err == nil { - t.Error("Expected error, got nil") - } else if err.Error() != fmt.Sprintf("listen tcp: address %d: invalid port", port) { - t.Errorf("Expected error message about invalid port, got %v", err) - } - case <-time.After(2 * time.Second): - t.Error("Timed out waiting for error") - } -} - -func TestHealthProbe(t *testing.T) { - // Create a mock HTTP request - req, err := http.NewRequest("GET", "/healthz", nil) - if err != nil { - t.Fatalf("Failed to create request: %v", err) - } - - // Create a mock HTTP response recorder - w := httptest.NewRecorder() - - // Call the HealthProbe function directly - HealthProbe(w, req) - - // Check the response status code - if w.Code != http.StatusOK { - t.Errorf("Expected status OK; got %d", w.Code) - } - - // Check the response body - expectedBody := "OK\n" - if body := w.Body.String(); body != expectedBody { - t.Errorf("Expected body %q; got %q", expectedBody, body) - } -} diff --git a/pkg/registry/endpoints.go b/pkg/registry/endpoints.go index b694ba0..3b64fc5 100644 --- a/pkg/registry/endpoints.go +++ b/pkg/registry/endpoints.go @@ -9,7 +9,7 @@ import ( "sync" "time" - "github.com/argoproj-labs/argocd-image-updater/pkg/cache" + "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/cache" "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" "go.uber.org/ratelimit" -- cgit v1.2.3