Skip to content

Commit 5789637

Browse files
Merge pull request #9978 from pawanpinjarkar/create-singlenmstateconfig-multiple-hosts
OCPBUGS-77767: OVE Above/Below-the-sea integration - Network configuration not persisted
2 parents f5efa52 + c71fd62 commit 5789637

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

internal/ignition/disconnected.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ignition
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -366,6 +367,7 @@ func createNMStateConfigManifests(infraEnv *common.InfraEnv, manifestsDir string
366367
nmStateConfigInfraEnvLabelKey: infraEnv.ID.String(),
367368
}
368369

370+
var nmStateYAMLs [][]byte
369371
for i, config := range staticNetworkConfigs {
370372
if config == nil || config.NetworkYaml == "" {
371373
continue
@@ -409,9 +411,15 @@ func createNMStateConfigManifests(infraEnv *common.InfraEnv, manifestsDir string
409411
return errors.Wrapf(err, "failed to marshal NMStateConfig %d", i)
410412
}
411413

412-
filename := filepath.Join(manifestsDir, fmt.Sprintf("nmstateconfig-%d.yaml", i))
413-
if err = os.WriteFile(filename, nmStateYAML, 0o600); err != nil {
414-
return errors.Wrapf(err, "failed to write NMStateConfig %d", i)
414+
nmStateYAMLs = append(nmStateYAMLs, nmStateYAML)
415+
}
416+
417+
if len(nmStateYAMLs) > 0 {
418+
combinedYAML := bytes.Join(nmStateYAMLs, []byte("---\n"))
419+
420+
filename := filepath.Join(manifestsDir, "nmstateconfig.yaml")
421+
if err := os.WriteFile(filename, combinedYAML, 0o600); err != nil {
422+
return errors.Wrap(err, "failed to write NMStateConfig")
415423
}
416424

417425
log.Infof("Created NMStateConfig manifest: %s", filename)

internal/ignition/disconnected_test.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ignition
22

33
import (
4+
"bytes"
45
"context"
56
"os"
67
"path/filepath"
@@ -440,6 +441,18 @@ var _ = Describe("Disconnected Ignition", func() {
440441
{"mac_address": "52:54:00:aa:bb:cc", "logical_nic_name": "eth0"}
441442
],
442443
"network_yaml": "interfaces:\n- name: eth0\n type: ethernet\n state: up\n ipv4:\n enabled: true\n address:\n - ip: 192.168.1.10\n prefix-length: 24\n dhcp: false\n"
444+
},
445+
{
446+
"mac_interface_map": [
447+
{"mac_address": "53:54:00:aa:bb:cc", "logical_nic_name": "eth0"}
448+
],
449+
"network_yaml": "interfaces:\n- name: eth0\n type: ethernet\n state: up\n ipv4:\n enabled: true\n address:\n - ip: 192.168.1.11\n prefix-length: 24\n dhcp: false\n"
450+
},
451+
{
452+
"mac_interface_map": [
453+
{"mac_address": "54:54:00:aa:bb:cc", "logical_nic_name": "eth0"}
454+
],
455+
"network_yaml": "interfaces:\n- name: eth0\n type: ethernet\n state: up\n ipv4:\n enabled: true\n address:\n - ip: 192.168.1.12\n prefix-length: 24\n dhcp: false\n"
443456
}
444457
]`
445458

@@ -458,20 +471,46 @@ var _ = Describe("Disconnected Ignition", func() {
458471
oveDir := args[4]
459472

460473
By("Verifying NMStateConfig manifest was created")
461-
nmstateConfigContent, err := os.ReadFile(filepath.Join(oveDir, "cluster-manifests", "nmstateconfig-0.yaml"))
462-
Expect(err).NotTo(HaveOccurred())
474+
nmstateConfigContent, err := os.ReadFile(filepath.Join(oveDir, "cluster-manifests", "nmstateconfig.yaml"))
475+
Expect(err).NotTo(HaveOccurred())
476+
477+
// Split the YAML content by document separator
478+
// Note: We use sigs.k8s.io/yaml (not gopkg.in/yaml.v2) because Kubernetes types
479+
// use json struct tags, which sigs.k8s.io/yaml handles correctly but gopkg.in/yaml.v2 doesn't
480+
var nmstateConfigs []v1beta1.NMStateConfig
481+
docs := bytes.Split(nmstateConfigContent, []byte("---\n"))
482+
for _, doc := range docs {
483+
doc = bytes.TrimSpace(doc)
484+
if len(doc) == 0 {
485+
continue
486+
}
487+
488+
var config v1beta1.NMStateConfig
489+
err = yaml.Unmarshal(doc, &config)
490+
Expect(err).NotTo(HaveOccurred())
491+
nmstateConfigs = append(nmstateConfigs, config)
492+
}
463493

464-
var nmstateConfig v1beta1.NMStateConfig
465-
err = yaml.Unmarshal(nmstateConfigContent, &nmstateConfig)
466-
Expect(err).NotTo(HaveOccurred())
494+
Expect(nmstateConfigs).To(HaveLen(3))
467495

468-
Expect(nmstateConfig.APIVersion).To(Equal("agent-install.openshift.io/v1beta1"))
469-
Expect(nmstateConfig.Kind).To(Equal("NMStateConfig"))
470-
Expect(nmstateConfig.ObjectMeta.Name).To(Equal("nmstate-config-0"))
471-
Expect(nmstateConfig.ObjectMeta.Labels).To(HaveKeyWithValue(nmStateConfigInfraEnvLabelKey, infraEnv.ID.String()))
472-
Expect(nmstateConfig.Spec.Interfaces).To(HaveLen(1))
473-
Expect(nmstateConfig.Spec.Interfaces[0].Name).To(Equal("eth0"))
474-
Expect(nmstateConfig.Spec.Interfaces[0].MacAddress).To(Equal("52:54:00:aa:bb:cc"))
496+
expectedConfigs := []struct {
497+
name string
498+
macAddress string
499+
}{
500+
{"nmstate-config-0", "52:54:00:aa:bb:cc"},
501+
{"nmstate-config-1", "53:54:00:aa:bb:cc"},
502+
{"nmstate-config-2", "54:54:00:aa:bb:cc"},
503+
}
504+
505+
for i, expected := range expectedConfigs {
506+
Expect(nmstateConfigs[i].APIVersion).To(Equal("agent-install.openshift.io/v1beta1"))
507+
Expect(nmstateConfigs[i].Kind).To(Equal("NMStateConfig"))
508+
Expect(nmstateConfigs[i].ObjectMeta.Name).To(Equal(expected.name))
509+
Expect(nmstateConfigs[i].ObjectMeta.Labels).To(HaveKeyWithValue(nmStateConfigInfraEnvLabelKey, infraEnv.ID.String()))
510+
Expect(nmstateConfigs[i].Spec.Interfaces).To(HaveLen(1))
511+
Expect(nmstateConfigs[i].Spec.Interfaces[0].Name).To(Equal("eth0"))
512+
Expect(nmstateConfigs[i].Spec.Interfaces[0].MacAddress).To(Equal(expected.macAddress))
513+
}
475514

476515
By("Verifying InfraEnv manifest selector references NMStateConfig labels")
477516
infraEnvContent, err := os.ReadFile(filepath.Join(oveDir, "cluster-manifests", "infraenv.yaml"))
@@ -512,7 +551,7 @@ var _ = Describe("Disconnected Ignition", func() {
512551
oveDir := args[4]
513552

514553
By("Verifying no NMStateConfig manifest was created")
515-
_, err := os.Stat(filepath.Join(oveDir, "cluster-manifests", "nmstateconfig-0.yaml"))
554+
_, err := os.Stat(filepath.Join(oveDir, "cluster-manifests", "nmstateconfig.yaml"))
516555
Expect(os.IsNotExist(err)).To(BeTrue())
517556

518557
By("Verifying InfraEnv manifest does not set NMStateConfig selector")

0 commit comments

Comments
 (0)