/
registry.go
83 lines (71 loc) · 1.89 KB
/
registry.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 registry
import (
"context"
"fmt"
"strings"
"time"
"github.com/containers/image/v5/docker"
"github.com/containers/image/v5/types"
)
// Client represents an active docker registry object
type Client struct {
opts Options
sysCtx *types.SystemContext
}
// Options holds docker registry object options
type Options struct {
Username string
Password string
InsecureTLS bool
Timeout time.Duration
UserAgent string
ImageOs string
ImageArch string
ImageVariant string
}
// New creates new docker registry client instance
func New(opts Options) (*Client, error) {
// Auth
auth := &types.DockerAuthConfig{}
if opts.Username != "" {
auth = &types.DockerAuthConfig{
Username: opts.Username,
Password: opts.Password,
}
}
// Sys context
sysCtx := &types.SystemContext{
DockerAuthConfig: auth,
DockerDaemonInsecureSkipTLSVerify: opts.InsecureTLS,
DockerInsecureSkipTLSVerify: types.NewOptionalBool(opts.InsecureTLS),
DockerRegistryUserAgent: opts.UserAgent,
OSChoice: opts.ImageOs,
ArchitectureChoice: opts.ImageArch,
VariantChoice: opts.ImageVariant,
}
return &Client{
sysCtx: sysCtx,
}, nil
}
func (c *Client) timeoutContext() (context.Context, context.CancelFunc) {
ctx := context.Background()
var cancel context.CancelFunc = func() {}
if c.opts.Timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, c.opts.Timeout)
}
return ctx, cancel
}
func (c *Client) newImage(ctx context.Context, imageStr string) (types.ImageCloser, error) {
if !strings.HasPrefix(imageStr, "//") {
imageStr = fmt.Sprintf("//%s", imageStr)
}
ref, err := docker.ParseReference(imageStr)
if err != nil {
return nil, fmt.Errorf("invalid image name %s: %v", imageStr, err)
}
img, err := ref.NewImage(ctx, c.sysCtx)
if err != nil {
return nil, err
}
return img, nil
}