-
Notifications
You must be signed in to change notification settings - Fork 120
/
images.go
89 lines (79 loc) · 2.04 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
package stages
import (
"context"
"fmt"
"strings"
"github.com/argoproj/argo-cd/v2/util/git"
"github.com/pkg/errors"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/images"
"github.com/akuity/kargo/internal/logging"
)
func (r *reconciler) getLatestImages(
ctx context.Context,
namespace string,
subs []kargoapi.ImageSubscription,
) ([]kargoapi.Image, error) {
imgs := make([]kargoapi.Image, len(subs))
for i, sub := range subs {
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, errors.Wrapf(
err,
"error obtaining credentials for image repo %q",
sub.RepoURL,
)
}
var regCreds *images.Credentials
if ok {
regCreds = &images.Credentials{
Username: creds.Username,
Password: creds.Password,
}
logger.Debug("obtained credentials for image repo")
} else {
logger.Debug("found no credentials for image repo")
}
tag, err := r.getLatestTagFn(
sub.RepoURL,
images.ImageUpdateStrategy(sub.UpdateStrategy),
sub.SemverConstraint,
sub.AllowTags,
sub.IgnoreTags,
sub.Platform,
regCreds,
)
if err != nil {
return nil, errors.Wrapf(
err,
"error getting latest suitable tag for image %q",
sub.RepoURL,
)
}
imgs[i] = kargoapi.Image{
RepoURL: sub.RepoURL,
GitRepoURL: r.getImageSourceURL(sub.GitRepoURL, tag),
Tag: tag,
}
logger.WithField("tag", tag).
Debug("found latest suitable image tag")
}
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.NormalizeGitURL(gitRepoURL), tag)
}