Skip to content

Commit

Permalink
virt-controller,tests: Inline multus annotations setting
Browse files Browse the repository at this point in the history
Generating multus annotations now depends on cluster config.
Passing the tests cluster config to NewPodForVirtualMachine
makes it cumbersome and requires changing all the tests that use it.

Inline generating multus annotation only in tests that needs it.

The tests fake cluster config is moved to global scope to enable access
for tests the needs it; tests that generates multus annotations.

Signed-off-by: Or Mergi <ormergi@redhat.com>
  • Loading branch information
ormergi committed Oct 15, 2023
1 parent feda611 commit 34fae18
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions pkg/virt-controller/watch/vmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type PodVmIfaceStatus struct {
}

var _ = Describe("VirtualMachineInstance watcher", func() {
var config *virtconfig.ClusterConfig

var ctrl *gomock.Controller
var vmiInterface *kubecli.MockVirtualMachineInstanceInterface
Expand Down Expand Up @@ -262,7 +263,6 @@ var _ = Describe("VirtualMachineInstance watcher", func() {
},
}

var config *virtconfig.ClusterConfig
config, _, kvInformer = testutils.NewFakeClusterConfigUsingKVConfig(kubevirtFakeConfig)
pvcInformer, _ = testutils.NewFakeInformerFor(&k8sv1.PersistentVolumeClaim{})
cdiInformer, _ = testutils.NewFakeInformerFor(&cdiv1.CDIConfig{})
Expand Down Expand Up @@ -3285,7 +3285,9 @@ var _ = Describe("VirtualMachineInstance watcher", func() {
api.NewMinimalVMI(vmName), firstVMNetwork, firstVMInterface),
secondVMNetwork, secondVMInterface)
pod = NewPodForVirtualMachine(vmi, k8sv1.PodRunning)
Expect(pod.Annotations).To(HaveKey(networkv1.NetworkAttachmentAnnot))
multusAnnotations, _ := services.GenerateMultusCNIAnnotation(vmi.Namespace, vmi.Spec.Domain.Devices.Interfaces, vmi.Spec.Networks, config)
pod.Annotations[networkv1.NetworkAttachmentAnnot] = multusAnnotations

expectPodStatusUpdateFailed(pod)
})

Expand Down Expand Up @@ -3359,7 +3361,13 @@ var _ = Describe("VirtualMachineInstance watcher", func() {
Name: "blue",
NetworkSource: v1.NetworkSource{Multus: &virtv1.MultusNetwork{NetworkName: "blue-net"}}}}

pod = NewPodForVirtualMachine(vmi, k8sv1.PodRunning, testPodNetworkStatus...)
pod = NewPodForVirtualMachine(vmi, k8sv1.PodRunning)
multusAnnotations, _ := services.GenerateMultusCNIAnnotation(vmi.Namespace, vmi.Spec.Domain.Devices.Interfaces, vmi.Spec.Networks, config)
pod.Annotations[networkv1.NetworkAttachmentAnnot] = multusAnnotations
podIfaceStatusJSON, err := json.Marshal(testPodNetworkStatus)
Expect(err).ToNot(HaveOccurred())
pod.Annotations[networkv1.NetworkStatusAnnot] = string(podIfaceStatusJSON)

prependInjectPodPatch(pod)

Expect(controller.updateMultusAnnotation(vmi.Namespace, vmi.Spec.Domain.Devices.Interfaces, vmi.Spec.Networks, pod)).To(Succeed())
Expand Down Expand Up @@ -3413,8 +3421,14 @@ var _ = Describe("VirtualMachineInstance watcher", func() {
vmIfaceStatus = append(vmIfaceStatus, *ifaceStatus[i].vmIfaceStatus)
}
}
pod := NewPodForVirtualMachine(vmi, k8sv1.PodRunning)
multusAnnotations, _ := services.GenerateMultusCNIAnnotation(vmi.Namespace, vmi.Spec.Domain.Devices.Interfaces, vmi.Spec.Networks, config)
pod.Annotations[networkv1.NetworkAttachmentAnnot] = multusAnnotations
podIfaceStatusJSON, err := json.Marshal(podIfaceStatus)
Expect(err).ToNot(HaveOccurred())
pod.Annotations[networkv1.NetworkStatusAnnot] = string(podIfaceStatusJSON)

Expect(controller.updateInterfaceStatus(vmi, NewPodForVirtualMachine(vmi, k8sv1.PodRunning, podIfaceStatus...))).To(Succeed())
Expect(controller.updateInterfaceStatus(vmi, pod)).To(Succeed())
Expect(vmi.Status.Interfaces).To(ConsistOf(vmIfaceStatus))
},
Entry("VMI without interfaces on spec does not generate new interface status", api.NewMinimalVMI(vmName)),
Expand Down Expand Up @@ -3527,20 +3541,11 @@ func setReadyCondition(vmi *virtv1.VirtualMachineInstance, status k8sv1.Conditio
Reason: reason,
})
}
func NewPodForVirtualMachine(vmi *virtv1.VirtualMachineInstance, phase k8sv1.PodPhase, podNetworkStatus ...networkv1.NetworkStatus) *k8sv1.Pod {
multusAnnotations, _ := services.GenerateMultusCNIAnnotation(vmi.Namespace, vmi.Spec.Domain.Devices.Interfaces, vmi.Spec.Networks)

func NewPodForVirtualMachine(vmi *virtv1.VirtualMachineInstance, phase k8sv1.PodPhase) *k8sv1.Pod {
podAnnotations := map[string]string{
virtv1.DomainAnnotation: vmi.Name,
}
if multusAnnotations != "" {
podAnnotations[networkv1.NetworkAttachmentAnnot] = multusAnnotations
}
if len(podNetworkStatus) > 0 {
podCurrentNetworks, err := json.Marshal(podNetworkStatus)
if err == nil {
podAnnotations[networkv1.NetworkStatusAnnot] = string(podCurrentNetworks)
}
}
return &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand Down

0 comments on commit 34fae18

Please sign in to comment.