Skip to content

Commit

Permalink
Merge pull request #9 from pubg/feature/tag-to-digest
Browse files Browse the repository at this point in the history
Feature/tag to digest
  • Loading branch information
bitofsky authored Sep 27, 2021
2 parents 507c72b + 5eb746f commit d9f27df
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
10 changes: 4 additions & 6 deletions remoteRegistry/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions remoteRegistry/docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
54 changes: 39 additions & 15 deletions util/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,64 @@ 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.mutex.Lock()
c.cache[key] = cache
c.mutex.Unlock()

c.cache[key] = res

return res.value, res.err
cache.mutex.Lock()
value, err := getter() // getter 획득동안 lock 유지
cache.value = value
cache.err = err
cache.time = time.Now()
cache.mutex.Unlock()
return value, err
}

}
6 changes: 3 additions & 3 deletions util/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit d9f27df

Please sign in to comment.