-
Notifications
You must be signed in to change notification settings - Fork 202
/
docker_registry_with_cache.go
154 lines (125 loc) · 4.38 KB
/
docker_registry_with_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package docker_registry
import (
"context"
"fmt"
"strings"
"sync"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/util"
)
type DockerRegistryWithCache struct {
Interface
cachedTagsMap *sync.Map
cachedTagsMutexMap *sync.Map
}
func newDockerRegistryWithCache(dockerRegistry Interface) *DockerRegistryWithCache {
return &DockerRegistryWithCache{
Interface: dockerRegistry,
cachedTagsMap: &sync.Map{},
cachedTagsMutexMap: &sync.Map{},
}
}
func (r *DockerRegistryWithCache) Tags(ctx context.Context, reference string, opts ...Option) ([]string, error) {
o := makeOptions(opts...)
return r.withCachedTags(reference, func(cachedTags []string, isExist bool) ([]string, error) {
if isExist && o.cachedTags {
return cachedTags, nil
}
return r.Interface.Tags(ctx, reference, opts...)
})
}
func (r *DockerRegistryWithCache) IsTagExist(ctx context.Context, reference string, opts ...Option) (bool, error) {
referenceParts, err := r.parseReferenceParts(reference)
if err != nil {
return false, err
}
referenceTag := referenceParts.tag
if referenceTag == "" {
panic(fmt.Sprintf("unexpected reference %q: tag required", reference))
}
repositoryAddress := strings.Join([]string{referenceParts.registry, referenceParts.repository}, "/")
tags, err := r.Tags(ctx, repositoryAddress, opts...)
if err != nil {
return false, err
}
for _, tag := range tags {
if referenceTag == tag {
return true, nil
}
}
return false, nil
}
func (r *DockerRegistryWithCache) TagRepoImage(ctx context.Context, repoImage *image.Info, tag string) error {
defer r.mustAddTagToCachedTags(repoImage.Name)
return r.Interface.TagRepoImage(ctx, repoImage, tag)
}
func (r *DockerRegistryWithCache) PushImage(ctx context.Context, reference string, opts *PushImageOptions) error {
defer r.mustAddTagToCachedTags(reference)
return r.Interface.PushImage(ctx, reference, opts)
}
func (r *DockerRegistryWithCache) MutateAndPushImage(ctx context.Context, sourceReference, destinationReference string, mutateConfigFunc func(v1.Config) (v1.Config, error)) error {
defer r.mustAddTagToCachedTags(destinationReference)
return r.Interface.MutateAndPushImage(ctx, sourceReference, destinationReference, mutateConfigFunc)
}
func (r *DockerRegistryWithCache) DeleteRepoImage(ctx context.Context, repoImage *image.Info) error {
defer r.mustDeleteTagFromCachedTags(repoImage.Name)
return r.Interface.DeleteRepoImage(ctx, repoImage)
}
func (r *DockerRegistryWithCache) mustAddTagToCachedTags(reference string) {
_, err := r.withCachedTags(reference, func(tags []string, isExist bool) ([]string, error) {
referenceParts, err := r.parseReferenceParts(reference)
if err != nil {
return nil, fmt.Errorf("unable to parse reference parts %q: %w", reference, err)
}
if !isExist {
return nil, nil
}
tags = append(tags, referenceParts.tag)
return tags, nil
})
if err != nil {
panic(fmt.Sprintf("unexpected err: %s", err))
}
}
func (r *DockerRegistryWithCache) mustDeleteTagFromCachedTags(reference string) {
_, err := r.withCachedTags(reference, func(tags []string, isExist bool) ([]string, error) {
referenceParts, err := r.parseReferenceParts(reference)
if err != nil {
return nil, fmt.Errorf("unable to parse reference parts %q: %w", reference, err)
}
if !isExist {
return nil, nil
}
tags = util.ExcludeFromStringArray(tags, referenceParts.tag)
return tags, nil
})
if err != nil {
panic(fmt.Sprintf("unexpected err: %s", err))
}
}
func (r *DockerRegistryWithCache) withCachedTags(reference string, f func([]string, bool) ([]string, error)) ([]string, error) {
cachedTagsID := r.mustGetCachedTagsID(reference)
mutex := util.MapLoadOrCreateMutex(r.cachedTagsMutexMap, cachedTagsID)
mutex.Lock()
defer mutex.Unlock()
value, isExist := r.cachedTagsMap.Load(cachedTagsID)
var tags []string
if isExist {
tags = value.([]string)
}
newTags, err := f(tags, isExist)
if err != nil {
return nil, err
}
r.cachedTagsMap.Store(cachedTagsID, newTags)
return newTags, nil
}
func (r *DockerRegistryWithCache) mustGetCachedTagsID(reference string) string {
referenceParts, err := r.parseReferenceParts(reference)
if err != nil {
panic(fmt.Sprintf("unexpected reference %q: %s", reference, err))
}
repositoryAddress := strings.Join([]string{referenceParts.registry, referenceParts.repository}, "/")
return repositoryAddress
}