-
Notifications
You must be signed in to change notification settings - Fork 63
/
manager_options.go
262 lines (218 loc) · 7.78 KB
/
manager_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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package oci
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
"kraftkit.sh/config"
"kraftkit.sh/log"
"kraftkit.sh/oci/handler"
cliconfig "github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
regtypes "github.com/docker/docker/api/types/registry"
"github.com/mitchellh/go-homedir"
)
type OCIManagerOption func(context.Context, *ociManager) error
// WithDetectHandler uses internal KraftKit configuration to determine which
// underlying OCI handler implementation should be used. Ultimately, this is
// done by checking whether set configuration can ultimately invoke a relative
// client to enable the handler.
func WithDetectHandler() OCIManagerOption {
return func(ctx context.Context, manager *ociManager) error {
if contAddr := config.G[config.KraftKit](ctx).ContainerdAddr; len(contAddr) > 0 {
namespace := DefaultNamespace
if n := os.Getenv("CONTAINERD_NAMESPACE"); n != "" {
namespace = n
}
log.G(ctx).WithFields(logrus.Fields{
"addr": contAddr,
"namespace": namespace,
}).Trace("using oci containerd handler")
manager.handle = func(ctx context.Context) (context.Context, handler.Handler, error) {
return handler.NewContainerdHandler(ctx, contAddr, namespace, manager.auths)
}
return nil
}
// Fall-back to using a simpler directory/tarball-based OCI handler
ociDir := filepath.Join(config.G[config.KraftKit](ctx).RuntimeDir, "oci")
log.G(ctx).WithFields(logrus.Fields{
"path": ociDir,
}).Trace("using oci directory handler")
manager.handle = func(ctx context.Context) (context.Context, handler.Handler, error) {
handle, err := handler.NewDirectoryHandler(ociDir, manager.auths)
if err != nil {
return nil, nil, err
}
return ctx, handle, nil
}
return nil
}
}
// WithContainerd forces the use of a containerd handler by providing an address
// to the containerd daemon (whether UNIX socket or TCP socket) as well as the
// default namespace to operate within.
func WithContainerd(ctx context.Context, addr, namespace string) OCIManagerOption {
return func(ctx context.Context, manager *ociManager) error {
if n := os.Getenv("CONTAINERD_NAMESPACE"); n != "" {
namespace = n
} else if namespace == "" {
namespace = DefaultNamespace
}
log.G(ctx).WithFields(logrus.Fields{
"addr": addr,
"namespace": namespace,
}).Trace("using containerd handler")
manager.handle = func(ctx context.Context) (context.Context, handler.Handler, error) {
return handler.NewContainerdHandler(ctx, addr, namespace, manager.auths)
}
return nil
}
}
// WithDirectory forces the use of a directory handler by providing a path to
// the directory to use as the OCI root.
func WithDirectory(ctx context.Context, path string) OCIManagerOption {
return func(ctx context.Context, manager *ociManager) error {
log.G(ctx).WithFields(logrus.Fields{
"path": path,
}).Trace("using oci directory handler")
manager.handle = func(ctx context.Context) (context.Context, handler.Handler, error) {
handle, err := handler.NewDirectoryHandler(path, manager.auths)
if err != nil {
return nil, nil, err
}
return ctx, handle, nil
}
return nil
}
}
// WithDefaultRegistries sets the list of KraftKit-set registries which is
// defined through its configuration.
func WithDefaultRegistries() OCIManagerOption {
return func(ctx context.Context, manager *ociManager) error {
manager.registries = make([]string, 0)
for _, manifest := range config.G[config.KraftKit](ctx).Unikraft.Manifests {
// Use internal KraftKit knowledge of the fact that the config often lists
// the well-known path of the Manifest package manager's remote index.
// This is obviously not an OCI image registry so we can safely skip it.
// Doing this speeds up the kraft CLI and the instantiation of the OCI
// Package Manager in general by a noticeable amount, especially with
// limited internet connectivity (as none is subsequently required).
if manifest == config.DefaultManifestIndex {
continue
}
if reg, err := manager.registry(ctx, manifest); err == nil && reg.Ping(ctx) == nil {
manager.registries = append(manager.registries, manifest)
}
}
if len(manager.registries) == 0 {
manager.registries = []string{DefaultRegistry}
}
return nil
}
}
// fileExists returns true if the given path exists and is not a directory.
func fileExists(path string) bool {
fi, err := os.Stat(path)
return err == nil && !fi.IsDir()
}
// defaultAuths uses the provided context to locate possible authentication
// values which can be used when speaking with remote registries.
func defaultAuths(ctx context.Context) (map[string]config.AuthConfig, error) {
auths := make(map[string]config.AuthConfig)
// Podman users may have their container registry auth configured in a
// different location, that Docker packages aren't aware of.
// If the Docker config file isn't found, we'll fallback to look where
// Podman configures it, and parse that as a Docker auth config instead.
// First, check $HOME/.docker/
var home string
var err error
foundDockerConfig := false
// If this is run in the context of GitHub actions, use an alternative path
// for the $HOME.
if os.Getenv("GITUB_ACTION") == "yes" {
home = "/github/home"
} else {
home, err = homedir.Dir()
}
if err == nil {
foundDockerConfig = fileExists(filepath.Join(home, ".docker/config.json"))
}
// If $HOME/.docker/config.json isn't found, check $DOCKER_CONFIG (if set)
if !foundDockerConfig && os.Getenv("DOCKER_CONFIG") != "" {
foundDockerConfig = fileExists(filepath.Join(os.Getenv("DOCKER_CONFIG"), "config.json"))
}
// If either of those locations are found, load it using Docker's
// config.Load, which may fail if the config can't be parsed.
//
// If neither was found, look for Podman's auth at
// $XDG_RUNTIME_DIR/containers/auth.json and attempt to load it as a
// Docker config.
var cf *configfile.ConfigFile
if foundDockerConfig {
cf, err = cliconfig.Load(os.Getenv("DOCKER_CONFIG"))
if err != nil {
return nil, err
}
} else if f, err := os.Open(filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "containers/auth.json")); err == nil {
defer f.Close()
cf, err = cliconfig.LoadFromReader(f)
if err != nil {
return nil, err
}
}
if cf != nil {
for domain, cfg := range cf.AuthConfigs {
if cfg.Username == "" && cfg.Password == "" {
continue
}
auths[domain] = config.AuthConfig{
Endpoint: cfg.ServerAddress,
User: cfg.Username,
Token: cfg.Password,
}
}
}
for domain, auth := range config.G[config.KraftKit](ctx).Auth {
auths[domain] = auth
}
return auths, nil
}
// WithDefaultAuth uses the KraftKit-set configuration for authentication
// against remote registries.
func WithDefaultAuth() OCIManagerOption {
return func(ctx context.Context, manager *ociManager) error {
var err error
manager.auths, err = defaultAuths(ctx)
if err != nil {
return err
}
return nil
}
}
// WithRegistries sets the list of registries to use when making calls to
// non-canonically named OCI references.
func WithRegistries(registries ...string) OCIManagerOption {
return func(ctx context.Context, manager *ociManager) error {
manager.registries = registries
return nil
}
}
// WithDockerConfig sets the authentication configuration to use when making
// calls to authenticated registries.
func WithDockerConfig(auth regtypes.AuthConfig) OCIManagerOption {
return func(ctx context.Context, manager *ociManager) error {
if auth.ServerAddress == "" {
return fmt.Errorf("cannot use auth config without server address")
}
if manager.auths == nil {
manager.auths = make(map[string]config.AuthConfig, 1)
}
manager.auths[auth.ServerAddress] = config.AuthConfig{
Endpoint: auth.ServerAddress,
User: auth.Username,
Token: auth.Password,
}
return nil
}
}