Skip to content

Commit

Permalink
refactor: remove several utils functions
Browse files Browse the repository at this point in the history
`AddConfigMapDisk`, `AddSecretDisk`, `AddServiceAccountDisk`,
`AddLabelDownwardAPIVolume` and `AddWatchdog` functions
are only used together and in two places.
Let's get rid of them, replacing them with the related
`lilvmi.Option`

Signed-off-by: fossedihelm <ffossemo@redhat.com>
  • Loading branch information
fossedihelm committed Jan 29, 2024
1 parent 29f9522 commit 4576119
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 123 deletions.
18 changes: 11 additions & 7 deletions tests/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,6 @@ var _ = SIGMigrationDescribe("VM Live Migration", func() {
})
It("Migration should generate empty isos of the right size on the target", func() {
By("Creating a VMI with cloud-init and config maps")
vmi := tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
configMapName := "configmap-" + rand.String(5)
secretName := "secret-" + rand.String(5)
downwardAPIName := "downwardapi-" + rand.String(5)
Expand All @@ -2064,16 +2063,21 @@ var _ = SIGMigrationDescribe("VM Live Migration", func() {
"user": "admin",
"password": "community",
}
tests.CreateConfigMap(configMapName, vmi.Namespace, config_data)
tests.CreateSecret(secretName, vmi.Namespace, secret_data)
tests.AddConfigMapDisk(vmi, configMapName, configMapName)
tests.AddSecretDisk(vmi, secretName, secretName)
tests.AddServiceAccountDisk(vmi, "default")

tests.CreateConfigMap(configMapName, testsuite.GetTestNamespace(nil), config_data)
tests.CreateSecret(secretName, testsuite.GetTestNamespace(nil), secret_data)
vmi := libvmi.NewAlpine(
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()),
libvmi.WithNetwork(v1.DefaultPodNetwork()),
libvmi.WithConfigMapDisk(configMapName, configMapName),
libvmi.WithSecretDisk(secretName, secretName),
libvmi.WithServiceAccountDisk("default"),
libvmi.WithDownwardAPIDisk(downwardAPIName),
)
// In case there are no existing labels add labels to add some data to the downwardAPI disk
if vmi.ObjectMeta.Labels == nil {
vmi.ObjectMeta.Labels = map[string]string{downwardTestLabelKey: downwardTestLabelVal}
}
tests.AddLabelDownwardAPIVolume(vmi, downwardAPIName)

// this annotation causes virt launcher to immediately fail a migration
vmi.Annotations = map[string]string{v1.FuncTestBlockLauncherPrepareMigrationTargetAnnotation: ""}
Expand Down
27 changes: 14 additions & 13 deletions tests/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,6 @@ var _ = Describe("[Serial][sig-operator]Operator", Serial, decorators.SigOperato
generateMigratableVMIs = func(num int) []*v1.VirtualMachineInstance {
vmis := []*v1.VirtualMachineInstance{}
for i := 0; i < num; i++ {
vmi := tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskCirros))
configMapName := "configmap-" + rand.String(5)
secretName := "secret-" + rand.String(5)
downwardAPIName := "downwardapi-" + rand.String(5)
Expand All @@ -733,19 +732,21 @@ var _ = Describe("[Serial][sig-operator]Operator", Serial, decorators.SigOperato
"password": "community",
}

tests.CreateConfigMap(configMapName, vmi.Namespace, config_data)
tests.CreateSecret(secretName, vmi.Namespace, secret_data)

tests.AddUserData(vmi, "cloud-init", "#!/bin/bash\necho 'hello'\n")
tests.AddConfigMapDisk(vmi, configMapName, configMapName)
tests.AddSecretDisk(vmi, secretName, secretName)
tests.AddServiceAccountDisk(vmi, "default")
tests.CreateConfigMap(configMapName, testsuite.GetTestNamespace(nil), config_data)
tests.CreateSecret(secretName, testsuite.GetTestNamespace(nil), secret_data)
vmi := libvmi.NewCirros(
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()),
libvmi.WithNetwork(v1.DefaultPodNetwork()),
libvmi.WithConfigMapDisk(configMapName, configMapName),
libvmi.WithSecretDisk(secretName, secretName),
libvmi.WithServiceAccountDisk("default"),
libvmi.WithDownwardAPIDisk(downwardAPIName),
libvmi.WithWatchdog(v1.WatchdogActionPoweroff),
)
// In case there are no existing labels add labels to add some data to the downwardAPI disk
if vmi.ObjectMeta.Labels == nil {
vmi.ObjectMeta.Labels = map[string]string{"downwardTestLabelKey": "downwardTestLabelVal"}
}
tests.AddLabelDownwardAPIVolume(vmi, downwardAPIName)
tests.AddWatchdog(vmi, v1.WatchdogActionPoweroff)

vmis = append(vmis, vmi)
}
Expand All @@ -754,7 +755,7 @@ var _ = Describe("[Serial][sig-operator]Operator", Serial, decorators.SigOperato
vmi := vmis[lastVMIIndex]
const nadName = "secondarynet"

Expect(libnet.CreateNAD(vmi.GetNamespace(), nadName)).To(Succeed())
Expect(libnet.CreateNAD(testsuite.GetTestNamespace(vmi), nadName)).To(Succeed())

const networkName = "tenant-blue"
vmi.Spec.Domain.Devices.Interfaces = append(
Expand Down Expand Up @@ -802,7 +803,7 @@ var _ = Describe("[Serial][sig-operator]Operator", Serial, decorators.SigOperato

deleteAllVMIs = func(vmis []*v1.VirtualMachineInstance) {
for _, vmi := range vmis {
err := virtClient.VirtualMachineInstance(vmi.Namespace).Delete(context.Background(), vmi.Name, &metav1.DeleteOptions{})
err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Delete(context.Background(), vmi.Name, &metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred(), "Delete VMI successfully")
}
}
Expand All @@ -827,7 +828,7 @@ var _ = Describe("[Serial][sig-operator]Operator", Serial, decorators.SigOperato

Eventually(func() error {
for _, vmi := range vmis {
vmi, err := virtClient.VirtualMachineInstance(vmi.Namespace).Get(context.Background(), vmi.Name, &metav1.GetOptions{})
vmi, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(vmi)).Get(context.Background(), vmi.Name, &metav1.GetOptions{})
if err != nil {
return err
}
Expand Down
103 changes: 0 additions & 103 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,109 +819,6 @@ func DeletePvAndPvc(name string) {
}
}

// AddConfigMapDisk
//
// Deprecated: Use libvmi
func AddConfigMapDisk(vmi *v1.VirtualMachineInstance, configMapName string, volumeName string) {
vmi.Spec.Volumes = append(vmi.Spec.Volumes, v1.Volume{
Name: volumeName,
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: k8sv1.LocalObjectReference{
Name: configMapName,
},
VolumeLabel: "",
},
},
})
vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks, v1.Disk{
Name: volumeName,
})
}

// AddSecretDisk
//
// Deprecated: Use libvmi
func AddSecretDisk(vmi *v1.VirtualMachineInstance, secretName string, volumeName string) {
AddSecretDiskWithCustomLabel(vmi, secretName, volumeName, "")
}

// AddSecretDiskWithCustomLabel
//
// Deprecated: Use libvmi
func AddSecretDiskWithCustomLabel(vmi *v1.VirtualMachineInstance, secretName string, volumeName string, volumeLabel string) {
vmi.Spec.Volumes = append(vmi.Spec.Volumes, v1.Volume{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secretName,
VolumeLabel: volumeLabel,
},
},
})
vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks, v1.Disk{
Name: volumeName,
})
}

// AddLabelDownwardAPIVolume
//
// Deprecated: Use libvmi
func AddLabelDownwardAPIVolume(vmi *v1.VirtualMachineInstance, volumeName string) {
vmi.Spec.Volumes = append(vmi.Spec.Volumes, v1.Volume{
Name: volumeName,
VolumeSource: v1.VolumeSource{
DownwardAPI: &v1.DownwardAPIVolumeSource{
Fields: []k8sv1.DownwardAPIVolumeFile{
{
Path: "labels",
FieldRef: &k8sv1.ObjectFieldSelector{
FieldPath: "metadata.labels",
},
},
},
VolumeLabel: "",
},
},
})

vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks, v1.Disk{
Name: volumeName,
})
}

// AddServiceAccountDisk
//
// Deprecated: Use libvmi
func AddServiceAccountDisk(vmi *v1.VirtualMachineInstance, serviceAccountName string) {
volumeName := serviceAccountName + "-disk"
vmi.Spec.Volumes = append(vmi.Spec.Volumes, v1.Volume{
Name: volumeName,
VolumeSource: v1.VolumeSource{
ServiceAccount: &v1.ServiceAccountVolumeSource{
ServiceAccountName: serviceAccountName,
},
},
})
vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks, v1.Disk{
Name: serviceAccountName + "-disk",
})
}

// AddWatchdog
//
// Deprecated: Use libvmi
func AddWatchdog(vmi *v1.VirtualMachineInstance, action v1.WatchdogAction) {
vmi.Spec.Domain.Devices.Watchdog = &v1.Watchdog{
Name: "watchdog",
WatchdogDevice: v1.WatchdogDevice{
I6300ESB: &v1.I6300ESBWatchdog{
Action: action,
},
},
}
}

func NewRandomReplicaSetFromVMI(vmi *v1.VirtualMachineInstance, replicas int32) *v1.VirtualMachineInstanceReplicaSet {
name := "replicaset" + rand.String(5)
rs := &v1.VirtualMachineInstanceReplicaSet{
Expand Down

0 comments on commit 4576119

Please sign in to comment.