-
Notifications
You must be signed in to change notification settings - Fork 197
/
generator.go
330 lines (303 loc) · 11.6 KB
/
generator.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
323
324
325
326
327
328
329
330
package staticnetworkconfig
import (
"archive/tar"
"bytes"
"context"
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/nmstate/nmstate/rust/src/go/nmstate"
"github.com/openshift/assisted-service/models"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"gopkg.in/ini.v1"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/json"
yamlconvertor "sigs.k8s.io/yaml"
)
type StaticNetworkConfigData struct {
FilePath string
FileContents string
}
//go:generate mockgen -source=generator.go -package=staticnetworkconfig -destination=mock_generator.go
type StaticNetworkConfig interface {
GenerateStaticNetworkConfigData(ctx context.Context, hostsYAMLS string) ([]StaticNetworkConfigData, error)
FormatStaticNetworkConfigForDB(staticNetworkConfig []*models.HostStaticNetworkConfig) (string, error)
ValidateStaticConfigParams(staticNetworkConfig []*models.HostStaticNetworkConfig) error
}
type StaticNetworkConfigGenerator struct {
log logrus.FieldLogger
nmstate *nmstate.Nmstate
}
func New(log logrus.FieldLogger) StaticNetworkConfig {
return &StaticNetworkConfigGenerator{
log: log,
nmstate: nmstate.New(),
}
}
func (s *StaticNetworkConfigGenerator) GenerateStaticNetworkConfigData(ctx context.Context, staticNetworkConfigStr string) ([]StaticNetworkConfigData, error) {
staticNetworkConfig, err := s.decodeStaticNetworkConfig(staticNetworkConfigStr)
if err != nil {
s.log.WithError(err).Errorf("Failed to decode static network config")
return nil, err
}
s.log.Infof("Start configuring static network for %d hosts", len(staticNetworkConfig))
filesList := []StaticNetworkConfigData{}
for i, hostConfig := range staticNetworkConfig {
hostFileList, err := s.generateHostStaticNetworkConfigData(hostConfig, fmt.Sprintf("host%d", i))
if err != nil {
err = errors.Wrapf(err, "failed to create static config for host %d", i)
s.log.Error(err)
return nil, err
}
filesList = append(filesList, hostFileList...)
}
return filesList, nil
}
func (s *StaticNetworkConfigGenerator) generateHostStaticNetworkConfigData(hostConfig *models.HostStaticNetworkConfig, hostDir string) ([]StaticNetworkConfigData, error) {
hostYAML := hostConfig.NetworkYaml
macInterfaceMapping := s.formatMacInterfaceMap(hostConfig.MacInterfaceMap)
result, err := s.generateConfiguration(hostYAML)
if err != nil {
return nil, err
}
filesList, err := s.createNMConnectionFiles(result, hostDir)
if err != nil {
s.log.WithError(err).Errorf("failed to create NM connection files")
return nil, err
}
mapConfigData := StaticNetworkConfigData{
FilePath: filepath.Join(hostDir, "mac_interface.ini"),
FileContents: macInterfaceMapping,
}
filesList = append(filesList, mapConfigData)
return filesList, nil
}
func (s *StaticNetworkConfigGenerator) generateConfiguration(hostYAML string) (string, error) {
if hostYAML == "" {
return "", errors.New("cannot generate configuration with an empty host YAML")
}
hostJSON, err := yamlconvertor.YAMLToJSON([]byte(hostYAML))
if err != nil {
return "", err
}
stdout, err := s.nmstate.GenerateConfiguration(string(hostJSON))
if err != nil {
s.log.WithError(err).Errorf("nmstate GenerateConfiguration failed, input yaml <%s>", hostYAML)
return "", fmt.Errorf("nmstate GenerateConfiguration failed, error: %s", err.Error())
}
return stdout, nil
}
// create NMConnectionFiles formats the nmstate output into a list of file data
// Nothing is written to the local filesystem
func (s *StaticNetworkConfigGenerator) createNMConnectionFiles(nmstateOutput, hostDir string) ([]StaticNetworkConfigData, error) {
var hostNMConnections map[string]interface{}
err := yaml.Unmarshal([]byte(nmstateOutput), &hostNMConnections)
if err != nil {
s.log.WithError(err).Errorf("Failed to unmarshal nmstate output")
return nil, err
}
if _, found := hostNMConnections["NetworkManager"]; !found {
return nil, errors.Errorf("nmstate generated an empty NetworkManager config file content")
}
filesList := []StaticNetworkConfigData{}
connectionsList := hostNMConnections["NetworkManager"].([]interface{})
if len(connectionsList) == 0 {
return nil, errors.Errorf("nmstate generated an empty NetworkManager config file content")
}
for _, connection := range connectionsList {
connectionElems := connection.([]interface{})
fileName := connectionElems[0].(string)
fileContents, err := s.formatNMConnection(connectionElems[1].(string))
if err != nil {
return nil, err
}
s.log.Infof("Adding NMConnection file <%s>", fileName)
newFile := StaticNetworkConfigData{
FilePath: filepath.Join(hostDir, fileName),
FileContents: fileContents,
}
filesList = append(filesList, newFile)
}
return filesList, nil
}
func (s *StaticNetworkConfigGenerator) formatNMConnection(nmConnection string) (string, error) {
ini.PrettyFormat = false
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true}, []byte(nmConnection))
if err != nil {
s.log.WithError(err).Errorf("Failed to load the ini format string %s", nmConnection)
return "", err
}
connectionSection := cfg.Section("connection")
_, err = connectionSection.NewKey("autoconnect", "true")
if err != nil {
s.log.WithError(err).Errorf("Failed to add autoconnect key to section connection")
return "", err
}
_, err = connectionSection.NewKey("autoconnect-priority", "1")
if err != nil {
s.log.WithError(err).Errorf("Failed to add autoconnect-priority key to section connection")
return "", err
}
buf := new(bytes.Buffer)
_, err = cfg.WriteTo(buf)
if err != nil {
s.log.WithError(err).Errorf("Failed to output nmconnection ini file to buffer")
return "", err
}
return buf.String(), nil
}
func (s *StaticNetworkConfigGenerator) validateInterfaceNamesExistence(macInterfaceMap models.MacInterfaceMap, networksConfig []StaticNetworkConfigData) error {
interfaceNames := lo.Map(macInterfaceMap, func(m *models.MacInterfaceMapItems0, _ int) string { return m.LogicalNicName })
var matched bool
for _, nc := range networksConfig {
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true}, []byte(nc.FileContents))
if err != nil {
s.log.WithError(err).Errorf("Failed to load the ini format string %s", nc.FileContents)
return err
}
connectionSection := cfg.Section("connection")
interfaceName, err := connectionSection.GetKey("interface-name")
if err != nil {
return errors.Wrapf(err, "failed to get interface-name for file %s", nc.FilePath)
}
interfaceType, err := connectionSection.GetKey("type")
if err != nil {
return errors.Wrapf(err, "failed to get interface type for interface %s", interfaceName.String())
}
if lo.Contains([]string{"802-3-ethernet", "ethernet"}, interfaceType.String()) {
if !lo.Contains(interfaceNames, interfaceName.String()) {
return errors.Errorf("mac-interface mapping for interface %s is missing", interfaceName.String())
}
matched = true
}
}
if !matched {
return errors.Errorf("no mac address mapping can be associated with the available network interfaces %v", interfaceNames)
}
return nil
}
func (s *StaticNetworkConfigGenerator) ValidateStaticConfigParams(staticNetworkConfig []*models.HostStaticNetworkConfig) error {
var err *multierror.Error
for i, hostConfig := range staticNetworkConfig {
err = multierror.Append(err, s.validateMacInterfaceName(i, hostConfig.MacInterfaceMap))
networkConfigs, validateErr := s.createConnectionFilesFromNMStateYaml(hostConfig.NetworkYaml)
if validateErr != nil {
err = multierror.Append(err, fmt.Errorf("failed to validate network yaml for host %d, %s", i, validateErr))
}
err = multierror.Append(err, s.validateInterfaceNamesExistence(hostConfig.MacInterfaceMap, networkConfigs))
}
return err.ErrorOrNil()
}
func (s *StaticNetworkConfigGenerator) validateMacInterfaceName(hostIdx int, macInterfaceMap models.MacInterfaceMap) error {
interfaceCheck := make(map[string]struct{}, len(macInterfaceMap))
macCheck := make(map[string]struct{}, len(macInterfaceMap))
for _, macInterface := range macInterfaceMap {
interfaceCheck[macInterface.LogicalNicName] = struct{}{}
macCheck[macInterface.MacAddress] = struct{}{}
}
if len(interfaceCheck) < len(macInterfaceMap) || len(macCheck) < len(macInterfaceMap) {
return fmt.Errorf("MACs and Interfaces for host %d must be unique", hostIdx)
}
return nil
}
func (s *StaticNetworkConfigGenerator) createConnectionFilesFromNMStateYaml(networkYaml string) ([]StaticNetworkConfigData, error) {
result, err := s.generateConfiguration(networkYaml)
if err != nil {
return nil, err
}
// Check that the file content can be created
// This doesn't write anything to the local filesystem
return s.createNMConnectionFiles(result, "temphostdir")
}
func compareMapInterfaces(intf1, intf2 *models.MacInterfaceMapItems0) bool {
if intf1.LogicalNicName != intf2.LogicalNicName {
return intf1.LogicalNicName < intf2.LogicalNicName
}
return intf1.MacAddress < intf2.MacAddress
}
func compareMacInterfaceMaps(map1, map2 models.MacInterfaceMap) bool {
if len(map1) != len(map2) {
return len(map1) < len(map2)
}
for i := range map1 {
less := compareMapInterfaces(map1[i], map2[i])
greater := compareMapInterfaces(map2[i], map1[i])
if less || greater {
return less
}
}
return false
}
func sortStaticNetworkConfig(staticNetworkConfig []*models.HostStaticNetworkConfig) {
for i := range staticNetworkConfig {
item := staticNetworkConfig[i]
sort.SliceStable(item.MacInterfaceMap, func(i, j int) bool {
return compareMapInterfaces(item.MacInterfaceMap[i], item.MacInterfaceMap[j])
})
}
sort.SliceStable(staticNetworkConfig, func(i, j int) bool {
hostConfig1 := staticNetworkConfig[i]
hostConfig2 := staticNetworkConfig[j]
if hostConfig1.NetworkYaml != hostConfig2.NetworkYaml {
return hostConfig1.NetworkYaml < hostConfig2.NetworkYaml
}
return compareMacInterfaceMaps(hostConfig1.MacInterfaceMap, hostConfig2.MacInterfaceMap)
})
}
func (s *StaticNetworkConfigGenerator) FormatStaticNetworkConfigForDB(staticNetworkConfig []*models.HostStaticNetworkConfig) (string, error) {
if len(staticNetworkConfig) == 0 {
return "", nil
}
sortStaticNetworkConfig(staticNetworkConfig)
b, err := json.Marshal(&staticNetworkConfig)
if err != nil {
return "", errors.Wrap(err, "Failed to JSON Marshal static network config")
}
return string(b), nil
}
func (s *StaticNetworkConfigGenerator) decodeStaticNetworkConfig(staticNetworkConfigStr string) (staticNetworkConfig []*models.HostStaticNetworkConfig, err error) {
if staticNetworkConfigStr == "" {
return
}
err = json.Unmarshal([]byte(staticNetworkConfigStr), &staticNetworkConfig)
if err != nil {
return nil, errors.Wrapf(err, "Failed to JSON Unmarshal static network config %s", staticNetworkConfigStr)
}
return
}
func (s *StaticNetworkConfigGenerator) formatMacInterfaceMap(macInterfaceMap models.MacInterfaceMap) string {
lines := make([]string, len(macInterfaceMap))
for i, entry := range macInterfaceMap {
lines[i] = fmt.Sprintf("%s=%s", entry.MacAddress, entry.LogicalNicName)
}
sort.Strings(lines)
return strings.Join(lines, "\n")
}
func GenerateStaticNetworkConfigArchive(files []StaticNetworkConfigData) (*bytes.Buffer, error) {
buffer := new(bytes.Buffer)
w := tar.NewWriter(buffer)
for _, file := range files {
path := filepath.Join("/etc/assisted/network", file.FilePath)
content := file.FileContents
// add the file content
hdr := &tar.Header{
Name: path,
Mode: 0600,
Size: int64(len(content)),
}
if err := w.WriteHeader(hdr); err != nil {
return nil, err
}
if _, err := w.Write([]byte(content)); err != nil {
return nil, err
}
}
if err := w.Close(); err != nil {
return nil, err
}
return buffer, nil
}