-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathclient.go
More file actions
183 lines (156 loc) · 5.1 KB
/
Copy pathclient.go
File metadata and controls
183 lines (156 loc) · 5.1 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
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
package client
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/sirupsen/logrus"
"github.com/jetstack/version-checker/pkg/api"
"github.com/jetstack/version-checker/pkg/client/acr"
"github.com/jetstack/version-checker/pkg/client/docker"
"github.com/jetstack/version-checker/pkg/client/ecr"
"github.com/jetstack/version-checker/pkg/client/fallback"
"github.com/jetstack/version-checker/pkg/client/gcr"
"github.com/jetstack/version-checker/pkg/client/ghcr"
"github.com/jetstack/version-checker/pkg/client/oci"
"github.com/jetstack/version-checker/pkg/client/quay"
"github.com/jetstack/version-checker/pkg/client/selfhosted"
)
// Used for testing/mocking purposes
type ClientHandler interface {
Tags(ctx context.Context, imageURL string, opts *api.Options) ([]api.ImageTag, error)
}
// Client is a container image registry client to list tags of given image
// URLs.
type Client struct {
fallbackClient api.ImageClient
ghcrClient *ghcr.Client
ghcrHostname string
log *logrus.Entry
clients []api.ImageClient
}
// Options used to configure client authentication.
type Options struct {
ACR acr.Options
Docker docker.Options
ECR ecr.Options
GCR gcr.Options
GHCR ghcr.Options
OCI oci.Options
Quay quay.Options
Selfhosted map[string]*selfhosted.Options
Transport http.RoundTripper
}
func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error) {
log = log.WithField("component", "client")
// Setup Transporters for all remaining clients (if one is set)
if opts.Transport != nil {
opts.Quay.Transporter = opts.Transport
opts.ECR.Transporter = opts.Transport
opts.GHCR.Transporter = opts.Transport
opts.GCR.Transporter = opts.Transport
}
acrClient, err := acr.New(opts.ACR)
if err != nil {
return nil, fmt.Errorf("failed to create acr client: %w", err)
}
dockerClient, err := docker.New(opts.Docker, log)
if err != nil {
return nil, fmt.Errorf("failed to create docker client: %w", err)
}
var selfhostedClients []api.ImageClient
for _, sOpts := range opts.Selfhosted {
sClient, err := selfhosted.New(ctx, log, sOpts)
if err != nil {
return nil, fmt.Errorf("failed to create selfhosted client %q: %w",
sOpts.Host, err)
}
selfhostedClients = append(selfhostedClients, sClient)
}
// Create some of the fallback clients
ociclient, err := oci.New(&opts.OCI, log)
if err != nil {
return nil, fmt.Errorf("failed to create OCI client: %w", err)
}
anonSelfHosted, err := selfhosted.New(ctx, log, &selfhosted.Options{Transporter: opts.Transport})
if err != nil {
return nil, fmt.Errorf("failed to create anonymous Selfhosted client: %w", err)
}
annonDocker, err := docker.New(docker.Options{Transporter: opts.Transport}, log)
if err != nil {
return nil, fmt.Errorf("failed to create anonymous docker client: %w", err)
}
fallbackClient, err := fallback.New(ctx, log, []api.ImageClient{
anonSelfHosted,
annonDocker,
ociclient,
})
if err != nil {
return nil, fmt.Errorf("failed to create fallback client: %w", err)
}
ghcrClient := ghcr.New(opts.GHCR)
c := &Client{
ghcrClient: ghcrClient,
ghcrHostname: opts.GHCR.Hostname,
// Append all the clients in order of which we want to check against
clients: append(
selfhostedClients,
acrClient,
ecr.New(opts.ECR),
dockerClient,
gcr.New(opts.GCR),
ghcrClient,
quay.New(opts.Quay, log),
),
fallbackClient: fallbackClient,
log: log,
}
for _, client := range append(c.clients, fallbackClient) {
log.WithField("client", client.Name()).Debugf("registered client")
}
return c, nil
}
// Tags returns the full list of image tags available, for a given image URL.
func (c *Client) Tags(ctx context.Context, imageURL string, opts *api.Options) ([]api.ImageTag, error) {
client, host, path := c.fromImageURL(imageURL)
c.log.Debugf("using client %q for image URL %q", client.Name(), imageURL)
repo, image := client.RepoImageFromPath(path)
if opts != nil && opts.UseGitHubRelease && !opts.UseSHA {
if ghcrClient, ok := client.(*ghcr.Client); ok {
return ghcrClient.ReleaseTags(ctx, repo, image)
}
if c.ghcrClient != nil && isGHCRHost(host, c.ghcrHostname) {
repo, image := c.ghcrClient.RepoImageFromPath(path)
return c.ghcrClient.ReleaseTags(ctx, repo, image)
}
}
return client.Tags(ctx, host, repo, image)
}
func isGHCRHost(host, configuredHostname string) bool {
if configuredHostname != "" && configuredHostname == host {
return true
}
return ghcr.HostReg.MatchString(host)
}
// fromImageURL will return the appropriate registry client for a given
// image URL, and the host + path to search.
func (c *Client) fromImageURL(imageURL string) (api.ImageClient, string, string) {
var host, path string
if strings.Contains(imageURL, ".") || strings.Contains(imageURL, ":") {
split := strings.SplitN(imageURL, "/", 2)
if len(split) < 2 {
path = imageURL
} else {
host, path = split[0], split[1]
}
} else {
path = imageURL
}
for _, client := range c.clients {
if client.IsHost(host) {
return client, host, path
}
}
// fall back to selfhosted with no path split
return c.fallbackClient, host, path
}