forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontainerd_config.go
More file actions
285 lines (240 loc) · 8.24 KB
/
containerd_config.go
File metadata and controls
285 lines (240 loc) · 8.24 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package operatingsystemconfig
import (
"context"
"encoding/json"
"fmt"
"os"
"path"
"slices"
"github.com/go-logr/logr"
"github.com/pelletier/go-toml"
"k8s.io/utils/ptr"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/utils/structuredmap"
)
type (
containerdConfigFileVersion int
containerdConfigPathName int
containerdConfigPathMap map[containerdConfigPathName]structuredmap.Path
containerdConfigPathMapVersions map[containerdConfigFileVersion]containerdConfigPathMap
replacementMap map[*structuredmap.Path]structuredmap.Path
)
const (
registryConfigPath containerdConfigPathName = iota
importsPath
sandboxImagePath
cgroupDriverPath
cniPluginPath
)
var (
// containerdConfigPaths is a nested map that contains the paths/keys for certain configuration options that change across different config file versions.
containerdConfigPaths = containerdConfigPathMapVersions{
1: {
registryConfigPath: {"plugins", "io.containerd.grpc.v1.cri", "registry", "config_path"},
sandboxImagePath: {"plugins", "io.containerd.grpc.v1.cri", "sandbox_image"},
cgroupDriverPath: {"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", "runc", "options", "SystemdCgroup"},
cniPluginPath: {"plugins", "io.containerd.grpc.v1.cri", "cni", "bin_dir"},
},
2: {
registryConfigPath: {"plugins", "io.containerd.grpc.v1.cri", "registry", "config_path"},
sandboxImagePath: {"plugins", "io.containerd.grpc.v1.cri", "sandbox_image"},
cgroupDriverPath: {"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", "runc", "options", "SystemdCgroup"},
cniPluginPath: {"plugins", "io.containerd.grpc.v1.cri", "cni", "bin_dir"},
},
3: {
registryConfigPath: {"plugins", "io.containerd.cri.v1.images", "registry", "config_path"},
sandboxImagePath: {"plugins", "io.containerd.cri.v1.images", "pinned_images", "sandbox"},
cgroupDriverPath: {"plugins", "io.containerd.cri.v1.runtime", "containerd", "runtimes", "runc", "options", "SystemdCgroup"},
cniPluginPath: {"plugins", "io.containerd.cri.v1.runtime", "cni", "bin_dir"},
},
}
// pluginPathReplacements is a map that contains replacements for paths brought in through an osc plugin config
pluginPathReplacements = replacementMap{
{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes"}: {"plugins", "io.containerd.cri.v1.runtime", "containerd", "runtimes"},
}
)
// getContainerdConfigFileVersion obtains the containerd configuration file version from the configuration file.
func getContainerdConfigFileVersion(config map[string]any) (containerdConfigFileVersion, error) {
version, ok := config["version"]
if !ok {
// Config file versions 2 and 3 must contain a version header.
// If it cannot be found, it therefore must be version 1.
return 1, nil
}
i, ok := version.(int64)
if !ok {
return 0, fmt.Errorf("cannot assert containerd config file version \"%v\" as an int64", version)
}
if i < 1 || i > 3 {
return 0, fmt.Errorf("unsupported containerd config file version %d", i)
}
return containerdConfigFileVersion(i), nil
}
// ensureContainerdDefaultConfig invokes the 'containerd' and saves the resulting default configuration.
func (r *Reconciler) ensureContainerdDefaultConfig(ctx context.Context) error {
exists, err := r.FS.Exists(configFile)
if err != nil {
return err
}
if exists {
return nil
}
output, err := Exec(ctx, "containerd", "config", "default")
if err != nil {
return err
}
return r.FS.WriteFile(configFile, output, 0644)
}
// ensureContainerdConfiguration sets the configuration for containerd.
func (r *Reconciler) ensureContainerdConfiguration(log logr.Logger, criConfig *extensionsv1alpha1.CRIConfig) error {
config, err := r.FS.ReadFile(configFile)
if err != nil {
return fmt.Errorf("unable to read containerd config.toml: %w", err)
}
content := map[string]any{}
if err = toml.Unmarshal(config, &content); err != nil {
return fmt.Errorf("unable to decode containerd default config: %w", err)
}
configFileVersion, err := getContainerdConfigFileVersion(content)
if err != nil {
return err
}
type patch struct {
name string
path structuredmap.Path
setFn structuredmap.SetFn
}
patches := []patch{
{
name: "registry config path",
path: containerdConfigPaths[configFileVersion][registryConfigPath],
setFn: func(_ any) (any, error) {
return certsDir, nil
},
},
{
name: "imports paths",
path: structuredmap.Path{"imports"},
setFn: func(value any) (any, error) {
importPath := path.Join(configDir, "*.toml")
imports, ok := value.([]any)
if !ok {
return []string{importPath}, nil
}
for _, imp := range imports {
path, ok := imp.(string)
if !ok {
continue
}
if path == importPath {
return value, nil
}
}
return append(imports, importPath), nil
},
},
{
name: "sandbox image",
path: containerdConfigPaths[configFileVersion][sandboxImagePath],
setFn: func(value any) (any, error) {
if criConfig.Containerd == nil {
return value, nil
}
return criConfig.Containerd.SandboxImage, nil
},
},
{
name: "CNI plugin dir",
path: containerdConfigPaths[configFileVersion][cniPluginPath],
setFn: func(_ any) (any, error) {
return cniPluginDir, nil
},
},
}
if criConfig.CgroupDriver != nil {
patches = append(patches, patch{
name: "cgroup driver",
path: containerdConfigPaths[configFileVersion][cgroupDriverPath],
setFn: func(_ any) (any, error) {
return *criConfig.CgroupDriver == extensionsv1alpha1.CgroupDriverSystemd, nil
},
})
}
if criConfig.Containerd != nil {
for _, pluginConfig := range criConfig.Containerd.Plugins {
patches = append(patches, patch{
name: "plugin configuration",
path: replacePluginPath(append(structuredmap.Path{"plugins"}, pluginConfig.Path...), pluginPathReplacements, configFileVersion),
setFn: func(val any) (any, error) {
switch op := ptr.Deref(pluginConfig.Op, extensionsv1alpha1.AddPluginPathOperation); op {
case extensionsv1alpha1.AddPluginPathOperation:
values, ok := val.(map[string]any)
if !ok || values == nil {
values = map[string]any{}
}
pluginValues := pluginConfig.Values
// Return unchanged values if plugin values is not set, i.e. only create table.
if pluginValues == nil {
return values, nil
}
if err := json.Unmarshal(pluginValues.Raw, &values); err != nil {
return nil, err
}
return values, nil
case extensionsv1alpha1.RemovePluginPathOperation:
// Return nil if operation is remove, to delete the entire sub-tree.
return nil, nil
default:
return nil, fmt.Errorf("operation %q is not supported", op)
}
},
})
}
}
for _, p := range patches {
if err := structuredmap.SetMapEntry(content, p.path, p.setFn); err != nil {
return fmt.Errorf("unable setting %q in containerd config.toml: %w", p.name, err)
}
}
f, err := r.FS.OpenFile(configFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("unable to open containerd config.toml: %w", err)
}
defer func() {
if err := f.Close(); err != nil {
log.Error(err, "Failed closing file", "file", f.Name())
}
}()
return toml.NewEncoder(f).Encode(content)
}
func isConfigPathPrefix(path, prefix structuredmap.Path) bool {
if len(prefix) > len(path) {
return false
}
return slices.Equal(prefix, path[:len(prefix)])
}
func replaceConfigPathPrefix(path, prefix, replace structuredmap.Path) structuredmap.Path {
// do not perform a replace operation if the search and replace term are the same
if slices.Equal(replace, prefix) {
return path
}
if !isConfigPathPrefix(path, prefix) {
return path
}
pathStripped := path[len(prefix):]
return append(replace, pathStripped...)
}
func replacePluginPath(path structuredmap.Path, replacementMap replacementMap, version containerdConfigFileVersion) structuredmap.Path {
if version != 3 {
return path
}
for find, replace := range replacementMap {
if isConfigPathPrefix(path, *find) {
return replaceConfigPathPrefix(path, *find, replace)
}
}
return path
}