Skip to content

Commit

Permalink
OVA, vSphere: move vm new name generation to the init phase to pass i…
Browse files Browse the repository at this point in the history
…t to virt-v2v.

Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy committed Mar 11, 2024
1 parent d278661 commit 7fda24d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ spec:
description: The VM Namespace Only relevant for an openshift
source.
type: string
newName:
description: The new name of the VM after matching DNS1123 requirements.
type: string
phase:
description: Phase
type: string
Expand Down
4 changes: 4 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_plans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ spec:
description: The VM Namespace Only relevant for an openshift
source.
type: string
newName:
description: The new name of the VM after matching DNS1123
requirements.
type: string
phase:
description: Phase
type: string
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/forklift/v1beta1/plan/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type VMStatus struct {
RestorePowerState VMPowerState `json:"restorePowerState,omitempty"`
// The firmware type detected from the OVF file produced by virt-v2v.
Firmware string `json:"firmware,omitempty"`
// The new name of the VM after matching DNS1123 requirements.
NewName string `json:"newName,omitempty"`

// Conditions.
libcnd.Conditions `json:",inline"`
Expand Down
15 changes: 8 additions & 7 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8svalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/wait"
cnv "kubevirt.io/api/core/v1"
instancetype "kubevirt.io/api/instancetype/v1beta1"
Expand Down Expand Up @@ -1168,13 +1167,9 @@ func (r *KubeVirt) virtualMachine(vm *plan.VMStatus) (object *cnv.VirtualMachine
//convention it will be automatically changed.
var originalName string

if errs := k8svalidation.IsDNS1123Label(vm.Name); len(errs) > 0 {
if vm.NewName != "" {
originalName = vm.Name
vm.Name, err = r.changeVmNameDNS1123(vm.Name, r.Plan.Spec.TargetNamespace)
if err != nil {
r.Log.Error(err, "Failed to update the VM name to meet DNS1123 protocol requirements.")
return
}
vm.Name = vm.NewName
r.Log.Info("VM name is incompatible with DNS1123 RFC, renaming",
"originalName", originalName, "newName", vm.Name)
}
Expand Down Expand Up @@ -1545,6 +1540,12 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume,
if err != nil {
return
}
environment = append(environment,
core.EnvVar{
Name: "V2V_NewName",
Value: vm.NewName,
})

// pod annotations
annotations := map[string]string{}
if r.Plan.Spec.TransferNetwork != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
k8svalidation "k8s.io/apimachinery/pkg/util/validation"
cdi "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -709,6 +710,13 @@ func (r *Migration) execute(vm *plan.VMStatus) (err error) {
err = nil
break
}
if errs := k8svalidation.IsDNS1123Label(vm.Name); len(errs) > 0 {
vm.NewName, err = r.kubevirt.changeVmNameDNS1123(vm.Name, r.Plan.Spec.TargetNamespace)
if err != nil {
r.Log.Error(err, "Failed to update the VM name to meet DNS1123 protocol requirements.")
return
}
}
vm.Phase = r.next(vm.Phase)
case PreHook, PostHook:
runner := HookRunner{Context: r.Context}
Expand Down
7 changes: 6 additions & 1 deletion virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ func main() {
fmt.Println("Error creating directory ", err)
os.Exit(1)
}
virtV2vArgs = append(virtV2vArgs, "-o", "kubevirt", "-os", DIR)
virtV2vArgs = append(virtV2vArgs, "-o", "kubevirt")

if checkEnvVariablesSet("V2V_NewName") {
virtV2vArgs = append(virtV2vArgs, "-on", os.Getenv("V2V_NewName"))
}

virtV2vArgs = append(virtV2vArgs, "-os", DIR)
//Disks on filesystem storage.
if err := LinkDisks(FS, 15); err != nil {
os.Exit(1)
Expand Down

0 comments on commit 7fda24d

Please sign in to comment.