-
Notifications
You must be signed in to change notification settings - Fork 316
/
cniconfig.go
322 lines (280 loc) · 9.89 KB
/
cniconfig.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package installcni
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"github.com/containernetworking/cni/libcni"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/consul-k8s/control-plane/cni/config"
"github.com/mitchellh/mapstructure"
)
// defaultCNIConfigFile gets the the correct config file from the cni net dir.
// Adapted from kubelet: https://github.com/kubernetes/kubernetes/blob/954996e231074dc7429f7be1256a579bedd8344c/pkg/kubelet/dockershim/network/cni/cni.go#L134.
func defaultCNIConfigFile(dir string) (string, error) {
files, err := libcni.ConfFiles(dir, []string{".conf", ".conflist"})
if err != nil {
return "", fmt.Errorf("error while trying to find files in %s: %w", dir, err)
}
// No config files have shown up yet and it is ok to run this function again.
if len(files) == 0 {
return "", nil
}
sort.Strings(files)
for _, confFile := range files {
var confList *libcni.NetworkConfigList
if strings.HasSuffix(confFile, ".conflist") {
confList, err = libcni.ConfListFromFile(confFile)
if err != nil {
// Error loading CNI config list file.
continue
}
} else {
conf, err := libcni.ConfFromFile(confFile)
if err != nil {
// Error loading CNI config file.
continue
}
// Ensure the config has a "type" so we know what plugin to run.
// Also catches the case where somebody put a conflist into a conf file.
if conf.Network.Type == "" {
// Error loading CNI config file: no 'type'.
continue
}
confList, err = libcni.ConfListFromConf(conf)
if err != nil {
// Error converting CNI config file to list.
continue
}
}
if len(confList.Plugins) == 0 {
// CNI config list has no networks, skipping".
continue
}
return confFile, nil
}
// There were files but none of them were valid
return "", fmt.Errorf("no valid config files found in %s", dir)
}
// confListFileFromConfFile converts a .conf file into a .conflist file. Chained plugins use .conflist files.
func confListFileFromConfFile(cfgFile string) (string, error) {
if !strings.HasSuffix(cfgFile, ".conf") {
return "", fmt.Errorf("invalid conf file: %s", cfgFile)
}
// Convert the .conf file into a map so that we can remove pieces of it.
cfgMap, err := configFileToMap(cfgFile)
if err != nil {
return "", fmt.Errorf("could not convert .conf file to map: %w", err)
}
// Remove the cniVersion header from the conf map.
delete(cfgMap, "cniVersion")
// Create the new plugins: [] section and add the contents from cfgMap to it.
plugins := make([]map[string]interface{}, 1)
plugins[0] = cfgMap
listMap := map[string]interface{}{
"name": "k8s-pod-network",
"cniVersion": "0.3.1",
"plugins": plugins,
}
listFile := fmt.Sprintf("%s%s", cfgFile, "list")
// Marshal into a new json object.
listJSON, err := json.MarshalIndent(listMap, "", " ")
if err != nil {
return "", fmt.Errorf("error marshalling conflist: %w", err)
}
// Libcni nuance/bug. If the newline is missing, the cni plugin will throw errors saying that it cannot get parse the config.
listJSON = append(listJSON, "\n"...)
// Write the .conflist file out.
err = os.WriteFile(listFile, listJSON, os.FileMode(0o644))
if err != nil {
return "", fmt.Errorf("error writing conflist file %s: %w", listFile, err)
}
return listFile, nil
}
// The format of the main cni config file is unstructured json consisting of a header and list of plugins
//
// {
// "cniVersion": "0.3.1",
// "name": "kindnet",
// "plugins": [
// {
// <plugin 1>
// },
// {
// <plugin 2>
// }
// ]
// }
//
// appendCNIConfig appends the consul-cni configuration to the main configuration file.
func appendCNIConfig(consulCfg *config.CNIConfig, cfgFile string) error {
// Read the config file and convert it to a map.
cfgMap, err := configFileToMap(cfgFile)
if err != nil {
return fmt.Errorf("could not convert config file to map: %w", err)
}
// Get the 'plugins' map embedded inside of the exisingMap.
plugins, err := pluginsFromMap(cfgMap)
if err != nil {
return err
}
// Check to see if 'type: consul-cni' already exists and remove it before appending.
// This can happen in a CrashLoop and we prevents many duplicate entries in the config file.
for i, p := range plugins {
plugin, ok := p.(map[string]interface{})
if !ok {
return fmt.Errorf("error reading plugin from plugin list")
}
if plugin["type"] == consulCNIName {
plugins = append(plugins[:i], plugins[i+1:]...)
break
}
}
// Take the consul cni config object and convert it to a map so that we can use it with the other maps.
consulMap, err := consulMapFromConfig(consulCfg)
if err != nil {
return fmt.Errorf("error converting consul config into map: %w", err)
}
// Append the consul-cni map to the already existing plugins.
cfgMap["plugins"] = append(plugins, consulMap)
// Marshal into a new json object
cfgJSON, err := json.MarshalIndent(cfgMap, "", " ")
if err != nil {
return fmt.Errorf("error marshalling existing CNI config: %w", err)
}
// Libcni nuance/bug. If the newline is missing, the cni plugin will throw errors saying that it cannot get parse the config.
cfgJSON = append(cfgJSON, "\n"...)
// Write the file out.
err = os.WriteFile(cfgFile, cfgJSON, os.FileMode(0o644))
if err != nil {
return fmt.Errorf("error writing config file %s: %w", cfgFile, err)
}
return nil
}
// configFileToMap takes an unstructure JSON config file and converts it into a map.
func configFileToMap(cfgFile string) (map[string]interface{}, error) {
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
return nil, fmt.Errorf("config file %s does not exist: %w", cfgFile, err)
}
// Read the main config file.
cfgBytes, err := os.ReadFile(cfgFile)
if err != nil {
return nil, fmt.Errorf("could not read file %s: %w", cfgFile, err)
}
// Convert the json config file into a map. The map that is created has 2 parts:
// [0] the cni header
// [1] the plugins
var cfgMap map[string]interface{}
err = json.Unmarshal(cfgBytes, &cfgMap)
if err != nil {
return nil, fmt.Errorf("error unmarshalling existing config file %s: %w", cfgFile, err)
}
return cfgMap, nil
}
// pluginsFromMap takes an unmarshalled config JSON map, return the plugin list asserted as a []interface{}.
func pluginsFromMap(cfgMap map[string]interface{}) ([]interface{}, error) {
plugins, ok := cfgMap["plugins"].([]interface{})
if !ok {
return nil, fmt.Errorf("error getting plugins from config map")
}
return plugins, nil
}
// consulMapFromConfig converts the consul CNI config into a map.
func consulMapFromConfig(consulCfg *config.CNIConfig) (map[string]interface{}, error) {
var consulMap map[string]interface{}
err := mapstructure.Decode(consulCfg, &consulMap)
if err != nil {
return nil, fmt.Errorf("error decoding consul config into a map: %w", err)
}
return consulMap, nil
}
// removeCNIConfig removes the consul-cni config from the CNI config file. Used as part of cleanup.
func removeCNIConfig(cfgFile string) error {
// Read the config file and convert it to a map.
cfgMap, err := configFileToMap(cfgFile)
if err != nil {
return fmt.Errorf("could not convert config file to map: %w", err)
}
// Get the 'plugins' map embedded inside of the exisingMap.
plugins, err := pluginsFromMap(cfgMap)
if err != nil {
return err
}
// Find the 'consul-cni' plugin and remove it.
for i, p := range plugins {
// We do not unmarshall this into a map[string]map[string]interface{} because there is no structure to
// the plugins. They are unstructured json and can be in any format that the plugin provider wishes.
plugin, ok := p.(map[string]interface{})
if !ok {
return fmt.Errorf("error reading plugin from plugin list")
}
if plugin["type"] == consulCNIName {
cfgMap["plugins"] = append(plugins[:i], plugins[i+1:]...)
break
}
}
// Marshal into a new json file.
cfgJSON, err := json.MarshalIndent(cfgMap, "", " ")
if err != nil {
return fmt.Errorf("error marshalling existing CNI config: %w", err)
}
cfgJSON = append(cfgJSON, "\n"...)
// Write the file out.
err = os.WriteFile(cfgFile, cfgJSON, os.FileMode(0o644))
if err != nil {
return fmt.Errorf("error writing config file %s: %w", cfgFile, err)
}
return nil
}
// validConfig validates that the consul-cni config exists in the config file and it is valid. It should be the
// last plugin in the plugin chain.
func validConfig(cfg *config.CNIConfig, cfgFile string) error {
// Convert the config file into a map.
cfgMap, err := configFileToMap(cfgFile)
if err != nil {
return fmt.Errorf("could not convert config file to map: %w", err)
}
// Get the 'plugins' map embedded inside of the exisingMap.
plugins, err := pluginsFromMap(cfgMap)
if err != nil {
return err
}
// Create an empty config so that we can populate it if found.
existingCfg := &config.CNIConfig{}
// Find the 'consul-cni' plugin in the list of plugins.
found := false
num_plugins := len(plugins)
for i, p := range plugins {
plugin, ok := p.(map[string]interface{})
if !ok {
return fmt.Errorf("error reading plugin from plugin list")
}
if plugin["type"] == consulCNIName {
// Populate existingCfg with the consul-cni plugin info so that we can compare it with what
// is expected.
err := mapstructure.Decode(plugin, &existingCfg)
if err != nil {
return fmt.Errorf("error decoding consul config into a map: %w", err)
}
found = true
// Check to see that consul-cni plugin is the last plugin in the chain.
if !(num_plugins-1 == i) {
return fmt.Errorf("consul-cni config is not the last plugin in plugin chain")
}
break
}
}
if !found {
return fmt.Errorf("consul-cni config missing from config file")
}
// Compare the config that is passed to the installer to what is in the config file. There could be a
// difference if the config was corrupted or during a helm update or upgrade.
equal := cmp.Equal(existingCfg, cfg)
if !equal {
return fmt.Errorf("consul-cni config has changed")
}
return nil
}