-
Notifications
You must be signed in to change notification settings - Fork 114
/
images.go
130 lines (118 loc) · 2.99 KB
/
images.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
package warehouses
import (
"context"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/git"
"github.com/akuity/kargo/internal/image"
"github.com/akuity/kargo/internal/logging"
)
func (r *reconciler) selectImages(
ctx context.Context,
namespace string,
subs []kargoapi.RepoSubscription,
) ([]kargoapi.Image, error) {
imgs := make([]kargoapi.Image, 0, len(subs))
for _, s := range subs {
if s.Image == nil {
continue
}
sub := s.Image
logger := logging.LoggerFromContext(ctx).WithField("repo", sub.RepoURL)
creds, ok, err :=
r.credentialsDB.Get(ctx, namespace, credentials.TypeImage, sub.RepoURL)
if err != nil {
return nil, fmt.Errorf(
"error obtaining credentials for image repo %q: %w",
sub.RepoURL,
err,
)
}
var regCreds *image.Credentials
if ok {
regCreds = &image.Credentials{
Username: creds.Username,
Password: creds.Password,
}
logger.Debug("obtained credentials for image repo")
} else {
logger.Debug("found no credentials for image repo")
}
tag, digest, err := r.getImageRefsFn(ctx, *sub, regCreds)
if err != nil {
return nil, fmt.Errorf(
"error getting latest suitable image %q: %w",
sub.RepoURL,
err,
)
}
imgs = append(
imgs,
kargoapi.Image{
RepoURL: sub.RepoURL,
GitRepoURL: r.getImageSourceURL(sub.GitRepoURL, tag),
Tag: tag,
Digest: digest,
},
)
logger.WithFields(log.Fields{
"tag": tag,
"digest": digest,
}).Debug("found latest suitable image")
}
return imgs, nil
}
const (
githubURLPrefix = "https://github.com"
)
func (r *reconciler) getImageSourceURL(gitRepoURL, tag string) string {
for baseUrl, fn := range r.imageSourceURLFnsByBaseURL {
if strings.HasPrefix(gitRepoURL, baseUrl) {
return fn(gitRepoURL, tag)
}
}
return ""
}
func getGithubImageSourceURL(gitRepoURL, tag string) string {
return fmt.Sprintf("%s/tree/%s", git.NormalizeURL(gitRepoURL), tag)
}
func getImageRefs(
ctx context.Context,
sub kargoapi.ImageSubscription,
creds *image.Credentials,
) (string, string, error) {
imageSelector, err := image.NewSelector(
sub.RepoURL,
image.SelectionStrategy(sub.ImageSelectionStrategy),
&image.SelectorOptions{
Constraint: sub.SemverConstraint,
AllowRegex: sub.AllowTags,
Ignore: sub.IgnoreTags,
Platform: sub.Platform,
Creds: creds,
InsecureSkipTLSVerify: sub.InsecureSkipTLSVerify,
},
)
if err != nil {
return "", "", fmt.Errorf(
"error creating image selector for image %q: %w",
sub.RepoURL,
err,
)
}
img, err := imageSelector.Select(ctx)
if err != nil {
return "", "", fmt.Errorf(
"error fetching newest applicable image %q: %w",
sub.RepoURL,
err,
)
}
if img == nil {
return "", "", fmt.Errorf("found no applicable image %q", sub.RepoURL)
}
return img.Tag, img.Digest.String(), nil
}