-
Notifications
You must be signed in to change notification settings - Fork 2
/
reg.go
230 lines (187 loc) · 4.49 KB
/
reg.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/sync/semaphore"
)
type ImageNameSet map[string]interface{}
func (i ImageNameSet) HasName(name string) bool {
_, ok := i[name]
return ok
}
type LayerNameHashEntries []*LayerNameMapEntry
func (l LayerNameHashEntries) Save(file string) error {
if len(l) == 0 {
return fmt.Errorf("no entries to save")
}
if file == "" {
return fmt.Errorf("filename is empty")
}
jsonBytes, err := json.Marshal(l)
if err != nil {
return err
}
return os.WriteFile(file, jsonBytes, 0644)
}
func Load(file string) (LayerNameHashEntries, error) {
if file == "" {
return nil, fmt.Errorf("filename is empty")
}
d, e := os.ReadFile(file)
if e != nil {
return nil, e
}
l := LayerNameHashEntries{}
e = json.Unmarshal(d, &l)
return l, e
}
func (l LayerNameHashEntries) ImageNames() ImageNameSet {
ims := ImageNameSet{}
for _, entry := range l {
ims[entry.Name] = nil
}
return ims
}
type Reg struct {
URL string
Prefixes []string
}
type RepoList struct {
Repositories []string `json:"repositories"`
}
type Repo struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
func NewReg(url string, prefixes string) *Reg {
return &Reg{
URL: url,
Prefixes: strings.Split(prefixes, ","),
}
}
func (r *Reg) GetRepoList(ctx context.Context) (*RepoList, error) {
resp, err := http.Get(fmt.Sprintf("%s/v2/_catalog", r.URL))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode)
}
d, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
allRepos := &RepoList{}
err = json.Unmarshal(d, allRepos)
if len(r.Prefixes) == 0 {
return allRepos, err
}
// Filter for repos starting with prefixes
rl := &RepoList{Repositories: []string{}}
for _, repo := range allRepos.Repositories {
for _, prefix := range r.Prefixes {
if strings.HasPrefix(repo, prefix) {
rl.Repositories = append(rl.Repositories, repo)
}
}
}
return rl, err
}
func (r *Reg) GetRepoInfo(ctx context.Context, repo string) (*Repo, error) {
resp, err := http.Get(fmt.Sprintf("%s/v2/%s/tags/list", r.URL, repo))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode)
}
d, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
repoInfo := &Repo{}
err = json.Unmarshal(d, repoInfo)
return repoInfo, err
}
func (r *Reg) GetLayerNameEntry(ctx context.Context, repo, tag string) (*LayerNameMapEntry, error) {
resp, err := http.Get(fmt.Sprintf("%s/v2/%s/manifests/%s", r.URL, repo, tag))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode)
}
d, e := io.ReadAll(resp.Body)
if e != nil {
return nil, e
}
m := &ispec.Manifest{}
if err := json.Unmarshal(d, m); err != nil {
return nil, err
}
layer := m.Layers[len(m.Layers)-1]
entry := &LayerNameMapEntry{
Hash: string(layer.Digest)[7:], // trim off the "sha256:" prefix
Name: fmt.Sprintf("%s:%s", repo, tag),
}
return entry, nil
}
type RepoError struct {
Repo *Repo
Err error
}
type LayerEntryError struct {
Entry *LayerNameMapEntry
Err error
}
func (r *Reg) FetchRepoInfos(l *RepoList, repos chan RepoError) {
wg := sync.WaitGroup{}
wg.Add(len(l.Repositories))
sem := semaphore.NewWeighted(16)
ctx := context.Background()
for _, repo := range l.Repositories {
// Create only 16 concurrent requests
if err := sem.Acquire(ctx, 1); err != nil {
panic(err)
}
go func(repo string) {
defer wg.Done()
defer sem.Release(1)
r, e := r.GetRepoInfo(context.Background(), repo)
repos <- RepoError{Repo: r, Err: e}
}(repo)
}
wg.Wait()
close(repos)
}
func (r *Reg) FetchLayerEntries(repos []*Repo, entries chan LayerEntryError) {
wg := sync.WaitGroup{}
sem := semaphore.NewWeighted(16)
ctx := context.Background()
for _, repo := range repos {
for _, tag := range repo.Tags {
wg.Add(1)
if err := sem.Acquire(ctx, 1); err != nil {
panic(err)
}
go func(name, tag string) {
defer wg.Done()
defer sem.Release(1)
entry, err := r.GetLayerNameEntry(context.Background(), name, tag)
entries <- LayerEntryError{Entry: entry, Err: err}
}(repo.Name, tag)
}
}
wg.Wait()
close(entries)
}