Skip to content

Commit 2f8110a

Browse files
maiquebRamLavi
authored andcommitted
networking, virt: carry over the requested IPs from the original VM
The requested IPs for the primary UDN attachment are not in the VMI spec, but in an annotation in the VMI. Hence, we need to fetch that particular annotation, and set it in the duplicate VMI. This was implemented using the builder pattern, since I suspect in the future we will need to further customize the VMI spec / metadata; this will make it simpler to extend the framework in the future. Signed-off-by: Miguel Duarte Barroso <[email protected]>
1 parent 7d16a92 commit 2f8110a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

test/extended/networking/kubevirt/client.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ func (c *Client) GetEventsForPod(podName string) ([]string, error) {
134134
return messages, nil
135135
}
136136

137-
func (c *Client) CreateVMIFromSpec(vmNamespace, vmName string, vmiSpec map[string]interface{}) error {
137+
type Option func(map[string]interface{})
138+
139+
func (c *Client) CreateVMIFromSpec(vmNamespace, vmName string, vmiSpec map[string]interface{}, opts ...Option) error {
138140
newVMI := map[string]interface{}{
139141
"apiVersion": "kubevirt.io/v1",
140142
"kind": "VirtualMachineInstance",
@@ -145,6 +147,10 @@ func (c *Client) CreateVMIFromSpec(vmNamespace, vmName string, vmiSpec map[strin
145147
"spec": vmiSpec,
146148
}
147149

150+
for _, opt := range opts {
151+
opt(newVMI)
152+
}
153+
148154
newVMIYAML, err := yaml.Marshal(newVMI)
149155
if err != nil {
150156
return err
@@ -153,6 +159,17 @@ func (c *Client) CreateVMIFromSpec(vmNamespace, vmName string, vmiSpec map[strin
153159
return c.Apply(string(newVMIYAML))
154160
}
155161

162+
func WithAnnotations(annotations map[string]string) Option {
163+
return func(cr map[string]interface{}) {
164+
metadata, hasMetadata := cr["metadata"].(map[string]interface{})
165+
if !hasMetadata {
166+
metadata = make(map[string]interface{})
167+
cr["metadata"] = metadata
168+
}
169+
metadata["annotations"] = annotations
170+
}
171+
}
172+
156173
func ensureVirtctl(oc *exutil.CLI, dir string) (string, error) {
157174
filepath := filepath.Join(dir, "virtctl")
158175
_, err := os.Stat(filepath)

test/extended/networking/livemigration.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"github.com/openshift/origin/test/extended/util/image"
3131
)
3232

33+
const kvIPRequestsAnnot = "network.kubevirt.io/addresses"
34+
3335
var _ = Describe("[sig-network][OCPFeatureGate:PersistentIPsForVirtualization][Feature:Layer2LiveMigration] Kubevirt Virtual Machines", func() {
3436
// disable automatic namespace creation, we need to add the required UDN label
3537
oc := exutil.NewCLIWithoutNamespace("network-segmentation-e2e")
@@ -561,7 +563,20 @@ func duplicateVM(cli *kubevirt.Client, vmNamespace, vmName string) {
561563
var vmiSpec map[string]interface{}
562564
Expect(json.Unmarshal([]byte(vmiSpecJSON), &vmiSpec)).To(Succeed())
563565

564-
Expect(cli.CreateVMIFromSpec(vmNamespace, duplicateVMName, vmiSpec)).To(Succeed())
566+
originalVMIRawAnnotations, err := cli.GetJSONPath("vmi", vmName, "{.metadata.annotations}")
567+
Expect(err).NotTo(HaveOccurred())
568+
569+
originalVMIAnnotations := map[string]string{}
570+
Expect(json.Unmarshal([]byte(originalVMIRawAnnotations), &originalVMIAnnotations)).To(Succeed())
571+
572+
var vmiCreationOptions []kubevirt.Option
573+
if requestedIPs, hasIPRequests := originalVMIAnnotations[kvIPRequestsAnnot]; hasIPRequests {
574+
vmiCreationOptions = append(
575+
vmiCreationOptions,
576+
kubevirt.WithAnnotations(ipRequests(requestedIPs)),
577+
)
578+
}
579+
Expect(cli.CreateVMIFromSpec(vmNamespace, duplicateVMName, vmiSpec, vmiCreationOptions...)).To(Succeed())
565580
waitForVMPodEventWithMessage(cli, vmNamespace, duplicateVMName, "IP is already allocated", 2*time.Minute)
566581
}
567582

@@ -792,3 +807,7 @@ func formatAddressesAnnotation(preconfiguredIPs []string) (string, error) {
792807

793808
return string(staticIPs), nil
794809
}
810+
811+
func ipRequests(ips string) map[string]string {
812+
return map[string]string{kvIPRequestsAnnot: ips}
813+
}

0 commit comments

Comments
 (0)