From f56a9cc2c3c97ce14bb3500d0c16031f98c9cc8c Mon Sep 17 00:00:00 2001 From: Bum-Seok Hwang Date: Mon, 27 Sep 2021 04:35:32 +0000 Subject: [PATCH 1/3] add docker test --- remoteRegistry/docker/docker_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 remoteRegistry/docker/docker_test.go diff --git a/remoteRegistry/docker/docker_test.go b/remoteRegistry/docker/docker_test.go new file mode 100644 index 0000000..ad7a7d4 --- /dev/null +++ b/remoteRegistry/docker/docker_test.go @@ -0,0 +1,19 @@ +package docker + +import ( + "strings" + "testing" +) + +func TestDocker(t *testing.T) { + r := NewRemoteRegistry() + + if s, err := r.GetImageString("busybox", "1.34.*", "linux/amd64"); err != nil { + t.Fatalf("GetImageString asterisk err: %v", err) + } else if !strings.Contains(s, "@sha256:") { + t.Fatalf("GetImageString asterisk no digest: %s", s) + } else { + t.Logf("GetImageString asterisk success: %s", s) + } + +} From 76bcc700bb4c3012554f1ab6ea3bdda6f4966e1a Mon Sep 17 00:00:00 2001 From: Bum-Seok Hwang Date: Mon, 27 Sep 2021 04:35:39 +0000 Subject: [PATCH 2/3] update --- remoteRegistry/docker/docker.go | 10 +++---- util/cache.go | 51 +++++++++++++++++++++++---------- util/version.go | 6 ++-- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/remoteRegistry/docker/docker.go b/remoteRegistry/docker/docker.go index 6439f13..84ce883 100644 --- a/remoteRegistry/docker/docker.go +++ b/remoteRegistry/docker/docker.go @@ -112,27 +112,25 @@ func (d *RemoteRegistryDocker) getImageHighestVersionTag(url, tag, platformStrin } cacheKey := url + "___" + tag - target, err := d.cache.Get(cacheKey, func() (interface{}, error) { + image, err := d.cache.Get(cacheKey, func() (interface{}, error) { tags, err := remote.List(repo, remote.WithAuthFromKeychain(authKeyChain)) if nil != err { return "", err } - tag, err := util.GetHighestVersionWithFilter(tags, tag) + t, err := util.GetHighestVersionWithFilter(tags, tag) if nil != err { return "", err } - return d.getImageDigestHash(url, tag, platformString) + return d.getImageDigestHash(url, t, platformString) }) if nil != err { return "", err } - fullUrl := fmt.Sprintf("%s:%s", url, target.(string)) - - return fullUrl, nil + return image.(string), nil } func (d *RemoteRegistryDocker) parsePlatform(platformString string) (*v1.Platform, error) { diff --git a/util/cache.go b/util/cache.go index c4a8965..9cd6e6c 100644 --- a/util/cache.go +++ b/util/cache.go @@ -8,40 +8,61 @@ import ( type Cache struct { TTL uint - cache map[string]cacheResult - mutex sync.Mutex + cache map[string]*cacheResult + mutex *sync.Mutex } type cacheResult struct { value interface{} err error time time.Time + mutex *sync.Mutex } func NewCache(ttlSeconds uint) *Cache { return &Cache{ TTL: ttlSeconds, - cache: make(map[string]cacheResult), + cache: make(map[string]*cacheResult), + mutex: &sync.Mutex{}, } } func (c *Cache) Get(key string, getter func() (interface{}, error)) (interface{}, error) { c.mutex.Lock() - defer c.mutex.Unlock() + cache, ok := c.cache[key] + c.mutex.Unlock() - if res, ok := c.cache[key]; ok && time.Since(res.time) < time.Duration(c.TTL)*time.Second { - return res.value, res.err + if ok && time.Since(cache.time) < time.Duration(c.TTL)*time.Second { + cache.mutex.Lock() + value, err := cache.value, cache.err + cache.mutex.Unlock() + return value, err } - value, err := getter() - res := cacheResult{ - value: value, - err: err, - time: time.Now(), - } + if ok { + cache.mutex.Lock() + value, err := getter() // getter 획득동안 lock 유지 + cache.value = value + cache.err = err + cache.time = time.Now() + cache.mutex.Unlock() + return value, err + } else { + cache := &cacheResult{ + value: nil, + err: nil, + time: time.Now(), + mutex: &sync.Mutex{}, + } - c.cache[key] = res - - return res.value, res.err + cache.mutex.Lock() + c.cache[key] = cache + value, err := getter() // getter 획득동안 lock 유지 + cache.value = value + cache.err = err + cache.time = time.Now() + cache.mutex.Unlock() + return value, err + } } diff --git a/util/version.go b/util/version.go index a4326c7..3860899 100644 --- a/util/version.go +++ b/util/version.go @@ -23,7 +23,7 @@ func GetHighestVersionWithFilter(versions []string, filter string) (string, erro // fmt.Println(v) matches := patt.FindStringSubmatch(v) - if 0 == len(matches) { + if len(matches) == 0 { continue } @@ -32,14 +32,14 @@ func GetHighestVersionWithFilter(versions []string, filter string) (string, erro continue } - if targetVer >= ver { + if targetVer > ver { continue } targetTag, targetVer = v, ver } - if "" == targetTag { + if targetTag == "" { return "", ERR_NOT_FOUND } From 5eb746f52018f7c86eebade3fa65667fd3e60281 Mon Sep 17 00:00:00 2001 From: Bum-Seok Hwang Date: Mon, 27 Sep 2021 04:37:34 +0000 Subject: [PATCH 3/3] update --- util/cache.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/util/cache.go b/util/cache.go index 9cd6e6c..a326a8b 100644 --- a/util/cache.go +++ b/util/cache.go @@ -55,8 +55,11 @@ func (c *Cache) Get(key string, getter func() (interface{}, error)) (interface{} mutex: &sync.Mutex{}, } - cache.mutex.Lock() + c.mutex.Lock() c.cache[key] = cache + c.mutex.Unlock() + + cache.mutex.Lock() value, err := getter() // getter 획득동안 lock 유지 cache.value = value cache.err = err