Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas authored and liranr23 committed Jun 9, 2024
1 parent 5492823 commit 2e538d3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
77 changes: 37 additions & 40 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
core "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
k8slabels "k8s.io/apimachinery/pkg/labels"
cdi "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
k8sutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -127,7 +127,7 @@ func (r *KubeVirt) ListVMs() ([]VirtualMachine, error) {
context.TODO(),
vList,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(planLabels),
LabelSelector: k8slabels.SelectorFromSet(planLabels),

Check warning on line 130 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L130

Added line #L130 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -231,7 +231,7 @@ func (r *KubeVirt) getImporterPods(pvc *core.PersistentVolumeClaim) (pods []core
podList,
&client.ListOptions{
Namespace: r.Plan.Spec.TargetNamespace,
LabelSelector: labels.SelectorFromSet(map[string]string{"app": "containerized-data-importer"}),
LabelSelector: k8slabels.SelectorFromSet(map[string]string{"app": "containerized-data-importer"}),

Check warning on line 234 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L234

Added line #L234 was not covered by tests
},
)
if err != nil {
Expand Down Expand Up @@ -303,7 +303,7 @@ func (r *KubeVirt) DeleteJobs(vm *plan.VMStatus) (err error) {
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(vmLabels),
LabelSelector: k8slabels.SelectorFromSet(vmLabels),

Check warning on line 306 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L306

Added line #L306 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -337,7 +337,7 @@ func (r *KubeVirt) DeleteJobs(vm *plan.VMStatus) (err error) {
context.TODO(),
podList,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{"job-name": job}),
LabelSelector: k8slabels.SelectorFromSet(map[string]string{"job-name": job}),

Check warning on line 340 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L340

Added line #L340 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -372,7 +372,7 @@ func (r *KubeVirt) EnsureVM(vm *plan.VMStatus) error {
context.TODO(),
vms,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.vmLabels(vm.Ref)),
LabelSelector: k8slabels.SelectorFromSet(r.vmLabels(vm.Ref)),

Check warning on line 375 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L375

Added line #L375 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -407,7 +407,7 @@ func (r *KubeVirt) EnsureVM(vm *plan.VMStatus) error {
context.TODO(),
dvs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.vmLabels(vm.Ref)),
LabelSelector: k8slabels.SelectorFromSet(r.vmLabels(vm.Ref)),

Check warning on line 410 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L410

Added line #L410 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
})
if err != nil {
Expand Down Expand Up @@ -440,7 +440,7 @@ func (r *KubeVirt) DeleteSecret(vm *plan.VMStatus) (err error) {
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(vmLabels),
LabelSelector: k8slabels.SelectorFromSet(vmLabels),

Check warning on line 443 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L443

Added line #L443 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand All @@ -465,7 +465,7 @@ func (r *KubeVirt) DeleteConfigMap(vm *plan.VMStatus) (err error) {
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(vmLabels),
LabelSelector: k8slabels.SelectorFromSet(vmLabels),

Check warning on line 468 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L468

Added line #L468 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand All @@ -490,7 +490,7 @@ func (r *KubeVirt) DeleteVM(vm *plan.VMStatus) (err error) {
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(vmLabels),
LabelSelector: k8slabels.SelectorFromSet(vmLabels),

Check warning on line 493 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L493

Added line #L493 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -523,7 +523,8 @@ func (r *KubeVirt) DeleteVM(vm *plan.VMStatus) (err error) {
}

func (r *KubeVirt) DataVolumes(vm *plan.VMStatus) (dataVolumes []cdi.DataVolume, err error) {
secret, err := r.ensureSecret(vm.Ref, r.secretDataSetterForCDI(vm.Ref), false)
labels := r.vmLabels(vm.Ref)
secret, err := r.ensureSecret(vm.Ref, r.secretDataSetterForCDI(vm.Ref), labels)

Check warning on line 527 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L526-L527

Added lines #L526 - L527 were not covered by tests
if err != nil {
return
}
Expand All @@ -540,7 +541,8 @@ func (r *KubeVirt) DataVolumes(vm *plan.VMStatus) (dataVolumes []cdi.DataVolume,
}

func (r *KubeVirt) PopulatorVolumes(vmRef ref.Ref) (pvcs []*core.PersistentVolumeClaim, err error) {
secret, err := r.ensureSecret(vmRef, r.copyDataFromProviderSecret, false)
labels := r.vmLabels(vmRef)
secret, err := r.ensureSecret(vmRef, r.copyDataFromProviderSecret, labels)

Check warning on line 545 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L544-L545

Added lines #L544 - L545 were not covered by tests
if err != nil {
err = liberr.Wrap(err)
return
Expand All @@ -561,7 +563,7 @@ func (r *KubeVirt) EnsureDataVolumes(vm *plan.VMStatus, dataVolumes []cdi.DataVo
context.TODO(),
dataVolumeList,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.vmLabels(vm.Ref)),
LabelSelector: k8slabels.SelectorFromSet(r.vmLabels(vm.Ref)),

Check warning on line 566 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L566

Added line #L566 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
})
if err != nil {
Expand Down Expand Up @@ -618,7 +620,7 @@ func (r *KubeVirt) getDVs(vm *plan.VMStatus) (edvs []ExtendedDataVolume, err err
context.TODO(),
dvsList,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.vmLabels(vm.Ref)),
LabelSelector: k8slabels.SelectorFromSet(r.vmLabels(vm.Ref)),

Check warning on line 623 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L623

Added line #L623 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
})

Expand All @@ -644,7 +646,7 @@ func (r *KubeVirt) getPVCs(vmRef ref.Ref) (pvcs []*core.PersistentVolumeClaim, e
context.TODO(),
pvcsList,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{
LabelSelector: k8slabels.SelectorFromSet(map[string]string{
"migration": string(r.Migration.UID),
kVM: vmRef.ID,
}),
Expand Down Expand Up @@ -781,7 +783,8 @@ func (r *KubeVirt) getListOptionsNamespaced() (listOptions *client.ListOptions)

// Ensure the guest conversion (virt-v2v) pod exists on the destination.
func (r *KubeVirt) EnsureGuestConversionPod(vm *plan.VMStatus, vmCr *VirtualMachine, pvcs []*core.PersistentVolumeClaim) (err error) {
v2vSecret, err := r.ensureSecret(vm.Ref, r.secretDataSetterForCDI(vm.Ref), false)
labels := r.vmLabels(vm.Ref)
v2vSecret, err := r.ensureSecret(vm.Ref, r.secretDataSetterForCDI(vm.Ref), labels)

Check warning on line 787 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L786-L787

Added lines #L786 - L787 were not covered by tests
if err != nil {
return
}
Expand Down Expand Up @@ -834,7 +837,7 @@ func (r *KubeVirt) EnsureOVAVirtV2VPVCStatus(vmID string) (ready bool, err error
context.TODO(),
pvcs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(pvcLabels),
LabelSelector: k8slabels.SelectorFromSet(pvcLabels),

Check warning on line 840 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L840

Added line #L840 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -880,7 +883,7 @@ func (r *KubeVirt) GetGuestConversionPod(vm *plan.VMStatus) (pod *core.Pod, err
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.conversionLabels(vm.Ref, false)),
LabelSelector: k8slabels.SelectorFromSet(r.conversionLabels(vm.Ref, false)),

Check warning on line 886 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L886

Added line #L886 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
})
if err != nil {
Expand Down Expand Up @@ -986,7 +989,7 @@ func (r *KubeVirt) GetPodsWithLabels(podLabels map[string]string) (pods *core.Po
context.TODO(),
pods,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(podLabels),
LabelSelector: k8slabels.SelectorFromSet(podLabels),

Check warning on line 992 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L992

Added line #L992 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -1027,7 +1030,7 @@ func (r *KubeVirt) DeleteHookJobs(vm *plan.VMStatus) (err error) {
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(vmLabels),
LabelSelector: k8slabels.SelectorFromSet(vmLabels),

Check warning on line 1033 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1033

Added line #L1033 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -1507,7 +1510,7 @@ func (r *KubeVirt) findTemplate(vm *plan.VMStatus) (tmpl *template.Template, err
context.TODO(),
templateList,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(templateLabels),
LabelSelector: k8slabels.SelectorFromSet(templateLabels),

Check warning on line 1513 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1513

Added line #L1513 was not covered by tests
Namespace: "openshift",
})
if err != nil {
Expand Down Expand Up @@ -1762,9 +1765,11 @@ func (r *KubeVirt) podVolumeMounts(vmVolumes []cnv.Volume, configMap *core.Confi
},
})
if vm.LUKS.Name != "" {
secret, erro := r.ensureSecret(vm.Ref, r.secretLUKS(vm.LUKS.Name, r.Plan.Namespace), true)
if erro != nil {
err = liberr.Wrap(erro)
labels := r.vmLabels(vm.Ref)
labels[kLUKS] = "true"
var secret *core.Secret
if secret, err = r.ensureSecret(vm.Ref, r.secretLUKS(vm.LUKS.Name, r.Plan.Namespace), labels); err != nil {
err = liberr.Wrap(err)
return

Check warning on line 1773 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1767-L1773

Added lines #L1767 - L1773 were not covered by tests
}
volumes = append(volumes, core.Volume{
Expand Down Expand Up @@ -1867,7 +1872,7 @@ func (r *KubeVirt) ensureConfigMap(vmRef ref.Ref) (configMap *core.ConfigMap, er
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.vmLabels(vmRef)),
LabelSelector: k8slabels.SelectorFromSet(r.vmLabels(vmRef)),

Check warning on line 1875 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1875

Added line #L1875 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -1976,27 +1981,23 @@ func (r *KubeVirt) secretLUKS(name, namespace string) func(*core.Secret) error {
}

// Ensure the credential secret for the data transfer exists on the destination.
func (r *KubeVirt) ensureSecret(vmRef ref.Ref, setSecretData func(*core.Secret) error, isLUKS bool) (secret *core.Secret, err error) {
func (r *KubeVirt) ensureSecret(vmRef ref.Ref, setSecretData func(*core.Secret) error, labels map[string]string) (secret *core.Secret, err error) {

Check warning on line 1984 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1984

Added line #L1984 was not covered by tests
_, err = r.Source.Inventory.VM(&vmRef)
if err != nil {
return
}

newSecret, err := r.secret(vmRef, setSecretData, isLUKS)
newSecret, err := r.secret(vmRef, setSecretData, labels)

Check warning on line 1990 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1990

Added line #L1990 was not covered by tests
if err != nil {
return
}

list := &core.SecretList{}
secretLabels := r.vmLabels(vmRef)
if isLUKS {
secretLabels[kLUKS] = "true"
}
err = r.Destination.Client.List(
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(secretLabels),
LabelSelector: k8slabels.SelectorFromSet(labels),

Check warning on line 2000 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2000

Added line #L2000 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
},
)
Expand Down Expand Up @@ -2041,11 +2042,7 @@ func (r *KubeVirt) ensureSecret(vmRef ref.Ref, setSecretData func(*core.Secret)
}

// Build the credential secret for the data transfer (CDI importer / popoulator pod).
func (r *KubeVirt) secret(vmRef ref.Ref, setSecretData func(*core.Secret) error, isLUKS bool) (secret *core.Secret, err error) {
labels := r.vmLabels(vmRef)
if isLUKS {
labels[kLUKS] = "true"
}
func (r *KubeVirt) secret(vmRef ref.Ref, setSecretData func(*core.Secret) error, labels map[string]string) (secret *core.Secret, err error) {

Check warning on line 2045 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2045

Added line #L2045 was not covered by tests
secret = &core.Secret{
ObjectMeta: meta.ObjectMeta{
Labels: labels,

Check warning on line 2048 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2048

Added line #L2048 was not covered by tests
Expand Down Expand Up @@ -2210,7 +2207,7 @@ func (r *KubeVirt) EnsurePersistentVolume(vmRef ref.Ref, persistentVolumes []cor
context.TODO(),
list,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.vmLabels(vmRef)),
LabelSelector: k8slabels.SelectorFromSet(r.vmLabels(vmRef)),

Check warning on line 2210 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2210

Added line #L2210 was not covered by tests
Namespace: r.Plan.Spec.TargetNamespace,
})
if err != nil {
Expand Down Expand Up @@ -2257,7 +2254,7 @@ func GetOvaPvListNfs(dClient client.Client, planID string) (pvs *core.Persistent
context.TODO(),
pvs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(pvLabels),
LabelSelector: k8slabels.SelectorFromSet(pvLabels),

Check warning on line 2257 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2257

Added line #L2257 was not covered by tests
},
)
if err != nil {
Expand All @@ -2281,7 +2278,7 @@ func GetOvaPvcListNfs(dClient client.Client, planID string, planNamespace string
context.TODO(),
pvcs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(pvcLabels),
LabelSelector: k8slabels.SelectorFromSet(pvcLabels),

Check warning on line 2281 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2281

Added line #L2281 was not covered by tests
Namespace: planNamespace,
},
)
Expand Down
11 changes: 4 additions & 7 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,7 @@ func buildCommand() []string {
}
}
// Adds LUKS keys, if exist.
if _, err := os.Stat(LUKSDIR); os.IsNotExist(err) {
// do nothing
} else {
if err != nil {
fmt.Println("Error accessing the LUKS directory ", err)
os.Exit(1)
}
if _, err := os.Stat(LUKSDIR); err == nil {
files, err := getFilesInPath(LUKSDIR)
if err != nil {
fmt.Println("Error reading files in LUKS directory ", err)
Expand All @@ -144,6 +138,9 @@ func buildCommand() []string {
for _, file := range files {
virtV2vArgs = append(virtV2vArgs, "--key", fmt.Sprintf("all:file:%s", file))

Check warning on line 139 in virt-v2v/cold/entrypoint.go

View check run for this annotation

Codecov / codecov/patch

virt-v2v/cold/entrypoint.go#L138-L139

Added lines #L138 - L139 were not covered by tests
}
} else if !os.IsNotExist(err) {
fmt.Println("Error accessing the LUKS directory ", err)
os.Exit(1)

Check warning on line 143 in virt-v2v/cold/entrypoint.go

View check run for this annotation

Codecov / codecov/patch

virt-v2v/cold/entrypoint.go#L142-L143

Added lines #L142 - L143 were not covered by tests
}

if info, err := os.Stat(VDDK); err == nil && info.IsDir() {
Expand Down

0 comments on commit 2e538d3

Please sign in to comment.