-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathquay.go
More file actions
155 lines (125 loc) · 3.71 KB
/
quay.go
File metadata and controls
155 lines (125 loc) · 3.71 KB
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
package quay
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/sirupsen/logrus"
"github.com/jetstack/version-checker/pkg/api"
"github.com/jetstack/version-checker/pkg/client/util"
leveledlogger "github.com/jetstack/version-checker/pkg/leveledlogrus"
)
const (
tagURL = "https://quay.io/api/v1/repository/%s/%s/tag/?page=%d"
manifestURL = "https://quay.io/api/v1/repository/%s/%s/manifest/%s"
)
type Options struct {
Transporter http.RoundTripper
Token string
}
type Client struct {
*retryablehttp.Client
Options
}
// Ensure that we are an ImageClient
var _ api.ImageClient = (*Client)(nil)
func New(opts Options, log *logrus.Entry) *Client {
client := retryablehttp.NewClient()
client.RetryMax = 10
client.Logger = log.WithField("client", "quay")
if opts.Transporter != nil {
client.HTTPClient.Transport = opts.Transporter
}
client.Logger = leveledlogger.Logger{Entry: log.WithField("client", "quay")}
return &Client{
Options: opts,
Client: client,
}
}
func (c *Client) Name() string {
return "quay"
}
// Fetch the image tags from an upstream repository and image.
func (c *Client) Tags(ctx context.Context, _, repo, image string) ([]api.ImageTag, error) {
p := c.newPager(repo, image)
if err := p.fetchTags(ctx); err != nil {
return nil, err
}
return p.tags, nil
}
// fetchImageManifest will lookup all manifests for a tag, if it is a list.
func (c *Client) fetchImageManifest(ctx context.Context, repo, image string, tag *responseTagItem) (*api.ImageTag, error) {
timestamp, err := time.Parse(time.RFC1123Z, tag.LastModified)
if err != nil {
return nil, err
}
iTag := &api.ImageTag{
Tag: tag.Name,
SHA: tag.ManifestDigest,
Timestamp: timestamp,
OS: "",
Architecture: "",
}
// If a multi-arch image, call manifest endpoint
if tag.IsManifestList {
url := fmt.Sprintf(manifestURL, repo, image, tag.ManifestDigest)
err := c.callManifests(ctx, timestamp, iTag, url)
if err != nil {
return nil, err
}
return iTag, nil
}
// Fallback to not using multi-arch image
iTag.OS, iTag.Architecture = util.OSArchFromTag(tag.Name)
return iTag, nil
}
// callManifests endpoint on the tags image manifest.
func (c *Client) callManifests(ctx context.Context, timestamp time.Time, tag *api.ImageTag, url string) error {
var manifestResp responseManifest
if err := c.makeRequest(ctx, url, &manifestResp); err != nil {
return err
}
// Got error on this manifest, ignore
if manifestResp.Status != nil {
return nil
}
var manifestData responseManifestData
if err := json.Unmarshal([]byte(manifestResp.ManifestData), &manifestData); err != nil {
return fmt.Errorf("failed to unmarshal manifest data %s: %#+v: %s",
tag.Tag, manifestResp, err)
}
for _, manifest := range manifestData.Manifests {
tag.Children = append(tag.Children, &api.ImageTag{
Tag: tag.Tag,
SHA: manifest.Digest,
Timestamp: timestamp,
Architecture: manifest.Platform.Architecture,
OS: manifest.Platform.OS,
})
}
return nil
}
// makeRequest will make a call and write the response to the object.
// Implements backoff.
func (c *Client) makeRequest(ctx context.Context, url string, obj interface{}) error {
req, err := retryablehttp.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
if len(c.Token) > 0 {
req.Header.Add("Authorization", "Bearer "+c.Token)
}
req.URL.Scheme = "https"
req = req.WithContext(ctx)
resp, err := c.Do(req)
if err != nil {
return fmt.Errorf("failed to make quay call %q: %s", url, err)
}
defer func() { _ = resp.Body.Close() }()
if err := json.NewDecoder(resp.Body).Decode(obj); err != nil {
return err
}
return nil
}