-
Notifications
You must be signed in to change notification settings - Fork 239
/
options.go
154 lines (129 loc) · 3.28 KB
/
options.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
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
package containerdregistry
import (
"crypto/x509"
"os"
"path/filepath"
"sync"
contentlocal "github.com/containerd/containerd/content/local"
"github.com/containerd/containerd/metadata"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
)
type RegistryConfig struct {
Log *logrus.Entry
ResolverConfigDir string
DBPath string
CacheDir string
PreserveCache bool
SkipTLSVerify bool
PlainHTTP bool
Roots *x509.CertPool
}
func (r *RegistryConfig) apply(options []RegistryOption) {
for _, option := range options {
option(r)
}
}
func (r *RegistryConfig) complete() error {
if err := os.Mkdir(r.CacheDir, os.ModePerm); err != nil && !os.IsExist(err) {
return err
}
if r.DBPath == "" {
r.DBPath = filepath.Join(r.CacheDir, "metadata.db")
}
return nil
}
func defaultConfig() *RegistryConfig {
config := &RegistryConfig{
Log: logrus.NewEntry(logrus.New()),
ResolverConfigDir: "",
CacheDir: "cache",
}
return config
}
// NewRegistry returns a new containerd Registry and a function to destroy it after use.
// The destroy function is safe to call more than once, but is a no-op after the first call.
func NewRegistry(options ...RegistryOption) (registry *Registry, err error) {
config := defaultConfig()
config.apply(options)
if err = config.complete(); err != nil {
return
}
cs, err := contentlocal.NewStore(config.CacheDir)
if err != nil {
return
}
var bdb *bolt.DB
bdb, err = bolt.Open(config.DBPath, 0644, nil)
if err != nil {
return
}
var once sync.Once
destroy := func() (destroyErr error) {
once.Do(func() {
if destroyErr = bdb.Close(); destroyErr != nil {
return
}
if config.PreserveCache {
return
}
destroyErr = os.RemoveAll(config.CacheDir)
})
return
}
var resolver remotes.Resolver
resolver, err = NewResolver(config.ResolverConfigDir, config.SkipTLSVerify, config.PlainHTTP, config.Roots)
if err != nil {
return
}
registry = &Registry{
Store: newStore(metadata.NewDB(bdb, cs, nil)),
destroy: destroy,
log: config.Log,
resolver: resolver,
platform: platforms.Ordered(platforms.DefaultSpec(), specs.Platform{
OS: "linux",
Architecture: "amd64",
}),
}
return
}
type RegistryOption func(config *RegistryConfig)
func WithLog(log *logrus.Entry) RegistryOption {
return func(config *RegistryConfig) {
config.Log = log
}
}
func WithResolverConfigDir(path string) RegistryOption {
return func(config *RegistryConfig) {
config.ResolverConfigDir = path
}
}
func WithCacheDir(dir string) RegistryOption {
return func(config *RegistryConfig) {
config.CacheDir = dir
}
}
func WithRootCAs(pool *x509.CertPool) RegistryOption {
return func(config *RegistryConfig) {
config.Roots = pool
}
}
func PreserveCache(preserve bool) RegistryOption {
return func(config *RegistryConfig) {
config.PreserveCache = preserve
}
}
func SkipTLSVerify(skip bool) RegistryOption {
return func(config *RegistryConfig) {
config.SkipTLSVerify = skip
}
}
func WithPlainHTTP(insecure bool) RegistryOption {
return func(config *RegistryConfig) {
config.PlainHTTP = insecure
}
}