-
Notifications
You must be signed in to change notification settings - Fork 117
/
digest_selector.go
83 lines (73 loc) · 2 KB
/
digest_selector.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
package image
import (
"context"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/akuity/kargo/internal/logging"
)
// digestSelector implements the Selector interface for SelectionStrategyDigest.
type digestSelector struct {
repoClient *repositoryClient
constraint string
platform *platformConstraint
}
// newDigestSelector returns an implementation of the Selector interface for
// SelectionStrategyDigest.
func newDigestSelector(
repoClient *repositoryClient,
constraint string,
platform *platformConstraint,
) (Selector, error) {
if constraint == "" {
return nil, errors.New("digest selection strategy requires a constraint")
}
return &digestSelector{
repoClient: repoClient,
constraint: constraint,
platform: platform,
}, nil
}
// Select implements the Selector interface.
func (d *digestSelector) Select(ctx context.Context) (*Image, error) {
logger := logging.LoggerFromContext(ctx).WithFields(log.Fields{
"registry": d.repoClient.registry.name,
"image": d.repoClient.image,
"selectionStrategy": SelectionStrategyDigest,
"platformConstrained": d.platform != nil,
})
logger.Trace("selecting image")
ctx = logging.ContextWithLogger(ctx, logger)
tags, err := d.repoClient.getTags(ctx)
if err != nil {
return nil, fmt.Errorf("error listing tags: %w", err)
}
if len(tags) == 0 {
logger.Trace("found no tags")
return nil, nil
}
logger.Trace("got all tags")
for _, tag := range tags {
if tag != d.constraint {
continue
}
image, err := d.repoClient.getImageByTag(ctx, tag, d.platform)
if err != nil {
return nil, fmt.Errorf("error retrieving image with tag %q: %w", tag, err)
}
if image == nil {
logger.Tracef(
"image with tag %q was found, but did not match platform constraint",
tag,
)
return nil, nil
}
logger.WithFields(log.Fields{
"tag": image.Tag,
"digest": image.Digest.String(),
}).Trace("found image")
return image, nil
}
logger.Trace("no images matched criteria")
return nil, nil
}