-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathhosts.go
More file actions
161 lines (138 loc) · 4.13 KB
/
Copy pathhosts.go
File metadata and controls
161 lines (138 loc) · 4.13 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package containerd
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path"
"github.com/pelletier/go-toml/v2"
"github.com/rs/zerolog"
"github.com/spf13/afero"
)
const (
backupDir = "_backup"
)
type hostFile struct {
Server string `toml:"server"`
HostConfigs map[string]hostConfig `toml:"host"`
}
type hostConfig struct {
Capabilities []string `toml:"capabilities"`
SkipVerify bool `toml:"skip_verify"`
}
// AddHostsConfiguration adds mirror configuration to containerd for the specified URLs.
// Refer to containerd registry configuration documentation for mor information about required configuration.
// https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration
// https://github.com/containerd/containerd/blob/main/docs/hosts.md#registry-configuration---examples
func AddHostsConfiguration(ctx context.Context, fs afero.Fs, configPath string, registryURLs, mirrorURLs []url.URL, resolveTags bool) error {
log := zerolog.Ctx(ctx).With().Str("component", "containerd-mirror").Logger()
if err := validate(registryURLs); err != nil {
return err
}
// Create config path dir if it does not exist
ok, err := afero.DirExists(fs, configPath)
if err != nil {
return err
}
if !ok {
err := fs.MkdirAll(configPath, 0755)
if err != nil {
return err
}
}
// Backup files and directories in config path
backupDirPath := path.Join(configPath, backupDir)
if _, err := fs.Stat(backupDirPath); os.IsNotExist(err) {
files, err := afero.ReadDir(fs, configPath)
if err != nil {
return err
}
if len(files) > 0 {
err = fs.MkdirAll(backupDirPath, 0755)
if err != nil {
return err
}
for _, fi := range files {
oldPath := path.Join(configPath, fi.Name())
newPath := path.Join(backupDirPath, fi.Name())
err := fs.Rename(oldPath, newPath)
if err != nil {
return err
}
log.Info().Str("path", oldPath).Str("target", newPath).Msg("backing up Containerd host configuration")
}
}
}
// Remove all content from config path to start from a clean slate
files, err := afero.ReadDir(fs, configPath)
if err != nil {
return err
}
for _, fi := range files {
if fi.Name() == backupDir {
continue
}
filePath := path.Join(configPath, fi.Name())
err := fs.RemoveAll(filePath)
if err != nil {
return err
}
}
// Write mirror configuration
capabilities := []string{"pull"}
if resolveTags {
capabilities = append(capabilities, "resolve")
}
for _, registryURL := range registryURLs {
// Need a special case for Docker Hub as docker.io is just an alias.
server := registryURL.String()
if registryURL.String() == "https://docker.io" {
server = "https://registry-1.docker.io"
}
hostConfigs := map[string]hostConfig{}
for _, u := range mirrorURLs {
hostConfigs[u.String()] = hostConfig{Capabilities: capabilities, SkipVerify: true} // nolint: gosec. TODO avtakkar: configure TLS.
}
cfg := hostFile{
Server: server,
HostConfigs: hostConfigs,
}
b, err := toml.Marshal(&cfg)
if err != nil {
return err
}
fp := path.Join(configPath, registryURL.Host, "hosts.toml")
err = fs.MkdirAll(path.Dir(fp), 0755)
if err != nil {
return err
}
err = afero.WriteFile(fs, fp, b, 0644)
if err != nil {
return err
}
log.Info().Str("host", registryURL.String()).Str("path", fp).Msg("added containerd mirror configuration")
}
return nil
}
// validate validates registry URLs.
func validate(urls []url.URL) error {
errs := []error{}
for _, u := range urls {
if u.Scheme != "http" && u.Scheme != "https" {
errs = append(errs, fmt.Errorf("invalid registry url, scheme must be http or https, got: %s", u.String()))
}
if u.Path != "" {
errs = append(errs, fmt.Errorf("invalid registry url, path has to be empty, got: %s", u.String()))
}
if len(u.Query()) != 0 {
errs = append(errs, fmt.Errorf("invalid registry url, query has to be empty, got: %s", u.String()))
}
if u.User != nil {
errs = append(errs, fmt.Errorf("invalid registry url, user has to be empty, got: %s", u.String()))
}
}
return errors.Join(errs...)
}