Skip to content

Commit 67932bb

Browse files
Merge pull request #30010 from RamLavi/add_PreconfiguredUDNAddresses_FG_e2e
CORENET-6232: add PreconfiguredUDNAddresses FG tests
2 parents d7f6bf9 + b106650 commit 67932bb

File tree

5 files changed

+285
-3
lines changed

5 files changed

+285
-3
lines changed

test/extended/networking/kubevirt/template.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package kubevirt
22

33
import (
44
"bytes"
5-
"html/template"
5+
"text/template"
66
)
77

88
const (
@@ -196,6 +196,63 @@ spec:
196196
password: fedora
197197
chpasswd: { expire: False }
198198
name: cloudinitdisk
199+
`
200+
FedoraVMWithPreconfiguredPrimaryUDNAttachment = `
201+
apiVersion: kubevirt.io/v1
202+
kind: VirtualMachine
203+
metadata:
204+
name: {{ .VMName }}
205+
namespace: {{ .VMNamespace }}
206+
spec:
207+
runStrategy: Always
208+
template:
209+
{{- if .PreconfiguredIP }}
210+
metadata:
211+
annotations:
212+
network.kubevirt.io/addresses: {{ printf "%q" .PreconfiguredIP }}
213+
{{- end }}
214+
spec:
215+
domain:
216+
devices:
217+
disks:
218+
- name: containerdisk
219+
disk:
220+
bus: virtio
221+
- name: cloudinitdisk
222+
disk:
223+
bus: virtio
224+
interfaces:
225+
- name: overlay
226+
binding:
227+
name: {{ .NetBindingName }}
228+
{{- if .PreconfiguredMAC }}
229+
macAddress: "{{ .PreconfiguredMAC }}"
230+
{{- end }}
231+
machine:
232+
type: ""
233+
resources:
234+
requests:
235+
memory: 2048M
236+
networks:
237+
- name: overlay
238+
pod: {}
239+
terminationGracePeriodSeconds: 0
240+
volumes:
241+
- name: containerdisk
242+
containerDisk:
243+
image: {{ .FedoraContainterDiskImage }}
244+
- name: cloudinitdisk
245+
cloudInitNoCloud:
246+
networkData: |
247+
version: 2
248+
ethernets:
249+
eth0:
250+
dhcp4: true
251+
dhcp6: true
252+
userData: |-
253+
#cloud-config
254+
password: fedora
255+
chpasswd: { expire: False }
199256
`
200257
vmimTemplate = `
201258
apiVersion: kubevirt.io/v1
@@ -214,6 +271,8 @@ type CreationTemplateParams struct {
214271
FedoraContainterDiskImage string
215272
NetBindingName string
216273
NetworkName string
274+
PreconfiguredIP string
275+
PreconfiguredMAC string
217276
}
218277

219278
func renderVMTemplate(vmTemplateString string, params CreationTemplateParams) (string, error) {

test/extended/networking/livemigration.go

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ var _ = Describe("[sig-network][OCPFeatureGate:PersistentIPsForVirtualization][F
7070
DescribeTableSubtree("created using",
7171
func(createNetworkFn func(netConfig networkAttachmentConfigParams) networkAttachmentConfig) {
7272

73-
DescribeTable("[Suite:openshift/network/virtualization] should keep ip", func(netConfig networkAttachmentConfigParams, vmResource string, opCmd func(cli *kubevirt.Client, vmNamespace, vmName string)) {
73+
DescribeTable("[Suite:openshift/network/virtualization] should keep ip", func(netConfig networkAttachmentConfigParams, vmResource string, opCmd func(cli *kubevirt.Client, vmNamespace, vmName string), wlConfig ...workloadNetworkConfig) {
7474
var err error
75+
var workloadConfig workloadNetworkConfig
76+
if len(wlConfig) > 0 {
77+
workloadConfig = wlConfig[0]
78+
}
7579
l := map[string]string{
7680
"e2e-framework": f.BaseName,
7781
}
@@ -128,6 +132,14 @@ var _ = Describe("[sig-network][OCPFeatureGate:PersistentIPsForVirtualization][F
128132
vmCreationParams.NetworkName = nadName
129133
}
130134

135+
if len(workloadConfig.preconfiguredIPs) > 0 {
136+
var err error
137+
vmCreationParams.PreconfiguredIP, err = formatAddressesAnnotation(workloadConfig.preconfiguredIPs)
138+
Expect(err).NotTo(HaveOccurred())
139+
}
140+
if workloadConfig.preconfiguredMAC != "" {
141+
vmCreationParams.PreconfiguredMAC = workloadConfig.preconfiguredMAC
142+
}
131143
Expect(virtClient.CreateVM(vmResource, vmCreationParams)).To(Succeed())
132144
waitForVMReadiness(virtClient, vmCreationParams.VMNamespace, vmCreationParams.VMName)
133145

@@ -151,6 +163,17 @@ var _ = Describe("[sig-network][OCPFeatureGate:PersistentIPsForVirtualization][F
151163
}
152164
Expect(initialAddresses).To(HaveLen(expectedNumberOfAddresses))
153165

166+
if len(workloadConfig.preconfiguredIPs) > 0 {
167+
By("Verifying VM received the preconfigured IP address(es)")
168+
for _, expectedIP := range workloadConfig.preconfiguredIPs {
169+
expectedIP = strings.TrimSpace(expectedIP)
170+
Expect(initialAddresses).To(ContainElement(expectedIP), fmt.Sprintf("Expected IP %s not found in VM addresses %v", expectedIP, initialAddresses))
171+
}
172+
}
173+
if workloadConfig.preconfiguredMAC != "" {
174+
By("Verifying VM received the preconfigured MAC address")
175+
verifyVMMAC(virtClient, vmName, workloadConfig.preconfiguredMAC)
176+
}
154177
httpServerPodsIPs := httpServerTestPodsMultusNetworkIPs(netConfig, httpServerPods)
155178

156179
By(fmt.Sprintf("Check east/west traffic before test operation using IPs: %v", httpServerPodsIPs))
@@ -173,6 +196,10 @@ var _ = Describe("[sig-network][OCPFeatureGate:PersistentIPsForVirtualization][F
173196
ShouldNot(BeEmpty())
174197
Expect(obtainedAddresses).To(ConsistOf(initialAddresses))
175198

199+
if workloadConfig.preconfiguredMAC != "" {
200+
By("Verifying VM MAC address persisted after test operation")
201+
verifyVMMAC(virtClient, vmName, workloadConfig.preconfiguredMAC)
202+
}
176203
By("Check east/west after test operation")
177204
checkEastWestTraffic(virtClient, vmName, httpServerPodsIPs)
178205
},
@@ -241,7 +268,51 @@ var _ = Describe("[sig-network][OCPFeatureGate:PersistentIPsForVirtualization][F
241268
},
242269
kubevirt.FedoraVMWithSecondaryNetworkAttachment,
243270
restartVM,
244-
))
271+
),
272+
Entry(
273+
"[OCPFeatureGate:PreconfiguredUDNAddresses] when the VM with preconfigured IPs attached to a primary UDN is restarted",
274+
networkAttachmentConfigParams{
275+
name: nadName,
276+
topology: "layer2",
277+
role: "primary",
278+
allowPersistentIPs: true,
279+
},
280+
kubevirt.FedoraVMWithPreconfiguredPrimaryUDNAttachment,
281+
restartVM,
282+
workloadNetworkConfig{
283+
preconfiguredIPs: []string{"203.203.0.50", "2014:100:200::50"},
284+
},
285+
),
286+
Entry(
287+
"[OCPFeatureGate:PreconfiguredUDNAddresses] when the VM with preconfigured MAC attached to a primary UDN is restarted",
288+
networkAttachmentConfigParams{
289+
name: nadName,
290+
topology: "layer2",
291+
role: "primary",
292+
allowPersistentIPs: true,
293+
},
294+
kubevirt.FedoraVMWithPreconfiguredPrimaryUDNAttachment,
295+
restartVM,
296+
workloadNetworkConfig{
297+
preconfiguredMAC: "02:0A:0B:0C:0D:50",
298+
},
299+
),
300+
Entry(
301+
"[OCPFeatureGate:PreconfiguredUDNAddresses] when the VM with preconfigured IP and MAC attached to a primary UDN is migrated between nodes",
302+
networkAttachmentConfigParams{
303+
name: nadName,
304+
topology: "layer2",
305+
role: "primary",
306+
allowPersistentIPs: true,
307+
},
308+
kubevirt.FedoraVMWithPreconfiguredPrimaryUDNAttachment,
309+
migrateVM,
310+
workloadNetworkConfig{
311+
preconfiguredIPs: []string{"203.203.0.51", "2014:100:200::51"},
312+
preconfiguredMAC: "02:0A:0B:0C:0D:51",
313+
},
314+
),
315+
)
245316
},
246317
Entry("NetworkAttachmentDefinitions", func(c networkAttachmentConfigParams) networkAttachmentConfig {
247318
netConfig := newNetworkAttachmentConfig(c)
@@ -428,6 +499,14 @@ func obtainAddresses(virtClient *kubevirt.Client, vmName string) ([]string, erro
428499
return addressFromStatus(virtClient, vmName)
429500
}
430501

502+
func obtainMAC(virtClient *kubevirt.Client, vmName string) (string, error) {
503+
macStr, err := virtClient.GetJSONPath("vmi", vmName, "{@.status.interfaces[0].mac}")
504+
if err != nil {
505+
return "", fmt.Errorf("failed to extract the MAC address from VM %q: %w", vmName, err)
506+
}
507+
return strings.ToUpper(macStr), nil
508+
}
509+
431510
func restartVM(cli *kubevirt.Client, vmNamespace, vmName string) {
432511
GinkgoHelper()
433512
By(fmt.Sprintf("Restarting vmi %s/%s", vmNamespace, vmName))
@@ -442,6 +521,22 @@ func migrateVM(cli *kubevirt.Client, vmNamespace, vmName string) {
442521
waitForVMIMSuccess(cli, vmNamespace, vmName)
443522
}
444523

524+
func verifyVMMAC(virtClient *kubevirt.Client, vmName, expectedMAC string) {
525+
GinkgoHelper()
526+
var actualMAC string
527+
Eventually(func(g Gomega) string {
528+
GinkgoHelper()
529+
530+
var err error
531+
actualMAC, err = obtainMAC(virtClient, vmName)
532+
g.Expect(err).NotTo(HaveOccurred(), "Failed to obtain MAC address for VM")
533+
return actualMAC
534+
}).
535+
WithPolling(time.Second).
536+
WithTimeout(5 * time.Minute).
537+
Should(Equal(expectedMAC))
538+
}
539+
445540
func waitForPodsCondition(fr *framework.Framework, pods []*corev1.Pod, conditionFn func(g Gomega, pod *corev1.Pod)) {
446541
for _, pod := range pods {
447542
Eventually(func(g Gomega) {
@@ -627,3 +722,25 @@ func networkName(netSpecConfig string) string {
627722
Expect(json.Unmarshal([]byte(netSpecConfig), &nc)).To(Succeed())
628723
return nc.Name
629724
}
725+
726+
// formatAddressesAnnotation converts slice of IPs to the required JSON format for kubevirt addresses annotation
727+
func formatAddressesAnnotation(preconfiguredIPs []string) (string, error) {
728+
const primaryUDNNetworkName = "overlay"
729+
if len(preconfiguredIPs) == 0 {
730+
return "", nil
731+
}
732+
733+
ips := make([]string, len(preconfiguredIPs))
734+
for i, ip := range preconfiguredIPs {
735+
ips[i] = strings.TrimSpace(ip)
736+
}
737+
738+
staticIPs, err := json.Marshal(map[string][]string{
739+
primaryUDNNetworkName: ips,
740+
})
741+
if err != nil {
742+
return "", fmt.Errorf("failed to marshal static IPs: %w", err)
743+
}
744+
745+
return string(staticIPs), nil
746+
}

test/extended/networking/network_segmentation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,12 @@ type networkAttachmentConfigParams struct {
18901890
role string
18911891
}
18921892

1893+
// workloadNetworkConfig contains workload-specific network customizations
1894+
type workloadNetworkConfig struct {
1895+
preconfiguredIPs []string
1896+
preconfiguredMAC string
1897+
}
1898+
18931899
type networkAttachmentConfig struct {
18941900
networkAttachmentConfigParams
18951901
}

test/extended/util/annotate/generated/zz_generated.annotations.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)