/
tag-finder.go
276 lines (237 loc) · 7.49 KB
/
tag-finder.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"context"
"fmt"
"net/http"
"os"
"path"
"regexp"
"sort"
"strings"
semver "github.com/Masterminds/semver/v3"
"github.com/google/go-github/v39/github"
"github.com/heroku/docker-registry-client/registry"
"golang.org/x/oauth2"
)
type ImageRef struct {
name string
reference string
tag string
}
// GetDeclarationLine produces a line of text intended for use in a Go constant declaration file.
func (ir ImageRef) GetDeclarationLine() string {
return fmt.Sprintf("\t%s = \"%s:%s\"", getConstantName(ir.name), ir.reference, ir.tag)
}
// GetEnvironmentLine generates a line of text intended for use in an .env file.
func (ir ImageRef) GetEnvironmentLine() string {
return fmt.Sprintf("%s='%s'", getEnvironmentName(ir.name), ir.tag)
}
// GetMakefileLine generates a line of text intended for use in a Makefile file.
func (ir ImageRef) GetMakefileLine() string {
return fmt.Sprintf("%s ?= %s", getMakefileVarName(ir.name), ir.tag)
}
// GetDockerfileLine generates a line of text intended for use in a Dockerfile file.
func (ir ImageRef) GetDockerfileLine() string {
return fmt.Sprintf("ARG %s=%s", getDockerfileVarName(ir.name), ir.tag)
}
type getTagsFn func(string) ([]string, error)
type getReleaseFn func(string, string) ([]*github.RepositoryRelease, error)
type tagFinderFn func(inputLine string) (*ImageRef, error)
type filterFn func(tag string) bool
func getFilter(expression string) (filterFn, error) {
expr, err := regexp.Compile(expression)
if err != nil {
return nil, err
}
return func(tag string) bool {
if expr.MatchString(tag) {
return true
}
return false
}, err
}
// persists function pointers to external resources, github, registries etc.
type configuration struct {
repositoryTagsFinder getTagsFn
releaseFinder getReleaseFn
}
// pass to getTagFinder to override the repository tag finder
func withRepoGetTags(fn getTagsFn) func(c *configuration) {
return func(c *configuration) {
c.repositoryTagsFinder = fn
}
}
// pass to getTagFinder to override the github release tag finder.
func withGithubReleaseTagFinder(fn getReleaseFn) func(c *configuration) {
return func(c *configuration) {
c.releaseFinder = fn
}
}
// returns a tag finder function that returns information about an image and it's latest tag.
func getTagFinder(opts ...func(c *configuration)) tagFinderFn {
// set defaults
config := configuration{
repositoryTagsFinder: getRegistryTags,
releaseFinder: getReleases,
}
// apply options
for _, opt := range opts {
opt(&config)
}
return func(inputLine string) (*ImageRef, error) {
parts := strings.Split(inputLine, " ")
if len(parts) < 2 {
return nil, fmt.Errorf("malformed input line %q", inputLine)
}
// everything matches by default
matcherFn := func(_ string) bool {
return true
}
var err error
// if we have three parts, the third is regular expression for the matcher
if len(parts) > 2 {
matcherFn, err = getFilter(parts[2])
if err != nil {
return nil, fmt.Errorf("unable to parse regex %q from input file %w", parts[2], err)
}
}
imageName, untaggedRef := parts[0], parts[1]
var latestReleaseTag string
switch imageName {
case minioReference:
latestReleaseTag, err = getLatestTagFromRegistry("kotsadm/minio", config.repositoryTagsFinder, matcherFn)
if err != nil {
return nil, fmt.Errorf("failed to get release tag for %s %w", imageName, err)
}
case dexReference:
latestReleaseTag, err = getLatestTagFromRegistry("kotsadm/dex", config.repositoryTagsFinder, matcherFn)
if err != nil {
return nil, fmt.Errorf("failed to get release tag for %s %w", imageName, err)
}
case rqliteReference:
latestReleaseTag, err = getLatestTagFromRegistry("kotsadm/rqlite", config.repositoryTagsFinder, matcherFn)
if err != nil {
return nil, fmt.Errorf("failed to get release tag for %s %w", imageName, err)
}
case schemaheroReference:
latestReleaseTag, err = getLatestTagFromRegistry("schemahero/schemahero", config.repositoryTagsFinder, matcherFn)
if err != nil {
return nil, fmt.Errorf("failed to get release tag for %s %w", imageName, err)
}
case lvpReference:
latestReleaseTag, err = getLatestTagFromRegistry("replicated/local-volume-provider", config.repositoryTagsFinder, matcherFn)
if err != nil {
return nil, fmt.Errorf("failed to get release tag for %s %w", imageName, err)
}
default:
return nil, fmt.Errorf("don't know how to deal with %q image", imageName)
}
return &ImageRef{
name: imageName,
reference: untaggedRef,
tag: latestReleaseTag,
}, nil
}
}
type GithubReleaseSorter []*github.RepositoryRelease
func (r GithubReleaseSorter) Len() int { return len(r) }
func (r GithubReleaseSorter) Less(i, j int) bool {
return r[i].PublishedAt.Before(r[j].PublishedAt.Time)
}
func (r GithubReleaseSorter) Swap(i, j int) {
tmp := r[i]
r[i] = r[j]
r[j] = tmp
}
func getLatestTagFromRegistry(imageUri string, getTags getTagsFn, match filterFn) (string, error) {
tags, err := getTags(imageUri)
if err != nil {
return "", err
}
var versions []*semver.Version
for _, tag := range tags {
if match(tag) {
v, err := semver.NewVersion(tag)
if err != nil {
return "", err
}
versions = append(versions, v)
}
}
sort.Sort(semver.Collection(versions))
if len(versions) == 0 {
return "", fmt.Errorf("no versions found")
}
return versions[len(versions)-1].Original(), nil
}
func getLatestTagFromGithub(getReleases getReleaseFn, owner, repo string, match filterFn) (string, error) {
releases, err := getReleases(owner, repo)
if err != nil {
return "", err
}
if len(releases) == 0 {
return "", fmt.Errorf("no releases found for %s/%s", owner, repo)
}
var matches []*github.RepositoryRelease
for _, release := range releases {
if release.TagName != nil && match(*release.TagName) {
// exclude pre-releases
if release.Prerelease != nil && *release.Prerelease {
continue
}
matches = append(matches, release)
}
}
s := GithubReleaseSorter(matches)
sort.Sort(s)
return *s[len(s)-1].TagName, nil
}
func getReleases(owner, repo string) ([]*github.RepositoryRelease, error) {
var httpClient *http.Client
if token, ok := os.LookupEnv(githubAuthTokenEnvironmentVarName); ok {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
httpClient = oauth2.NewClient(context.Background(), ts)
}
client := github.NewClient(httpClient)
var releases []*github.RepositoryRelease
listOptions := github.ListOptions{
Page: 0,
PerPage: githubPageSize,
}
for {
page, response, err := client.Repositories.ListReleases(context.Background(), owner, repo, &listOptions)
if err != nil {
return nil, err
}
if len(page) > 0 {
releases = append(releases, page...)
}
if response.NextPage == 0 {
break
}
listOptions.Page = response.NextPage
}
return releases, nil
}
// getRegistryTags queries a Docker Registry HTTP API V2 compliant registry to get the tags for an image.
func getRegistryTags(untaggedRef string) ([]string, error) {
registryUri := dockerRegistryUrl
imageRef := untaggedRef
userName, password := "", ""
parts := strings.Split(untaggedRef, "/")
if len(parts) > 2 {
registryUri = fmt.Sprintf("https://%s", parts[0])
imageRef = path.Join(parts[1:]...)
}
hub, err := registry.New(registryUri, userName, password)
if err != nil {
return nil, fmt.Errorf("could not connect to registry %q %w", registryUri, err)
}
tags, err := hub.Tags(imageRef)
if err != nil {
return nil, fmt.Errorf("could not fetch tags for image %q %w", imageRef, err)
}
return tags, nil
}