Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MTV 1595 | PVC name template #1310

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,20 @@ spec:
- progress
type: object
type: array
pvcNameTemplate:
description: |-
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RichardHoch hi, can you have a look at the descriptions ?

PVCNameTemplate is a template for generating PVC names for VM disks.
It follows Go template syntax and has access to the following variables:
- .VmName: name of the VM
- .PlanName: name of the migration plan
- .DiskIndex: initial volume index of the disk
- .RootDiskIndex: index of the root disk
Note:
This template overrides the plan level template.
Examples:
"{{.VmName}}-disk-{{.DiskIndex}}"
"{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
type: string
restorePowerState:
description: Source VM power state before migration.
type: string
Expand Down
42 changes: 42 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_plans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ spec:
- destination
- source
type: object
pvcNameTemplate:
description: |-
PVCNameTemplate is a template for generating PVC names for VM disks.
It follows Go template syntax and has access to the following variables:
- .VmName: name of the VM
- .PlanName: name of the migration plan
- .DiskIndex: initial volume index of the disk
- .RootDiskIndex: index of the root disk
Note:
This template can be overridden at the individual VM level.
Examples:
"{{.VmName}}-disk-{{.DiskIndex}}"
"{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
type: string
targetNamespace:
description: Target namespace.
type: string
Expand Down Expand Up @@ -432,6 +446,20 @@ spec:
The VM Namespace
Only relevant for an openshift source.
type: string
pvcNameTemplate:
description: |-
PVCNameTemplate is a template for generating PVC names for VM disks.
It follows Go template syntax and has access to the following variables:
- .VmName: name of the VM
- .PlanName: name of the migration plan
- .DiskIndex: initial volume index of the disk
- .RootDiskIndex: index of the root disk
Note:
This template overrides the plan level template.
Examples:
"{{.VmName}}-disk-{{.DiskIndex}}"
"{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
type: string
rootDisk:
description: Choose the primary disk the VM boots from
type: string
Expand Down Expand Up @@ -1024,6 +1052,20 @@ spec:
- progress
type: object
type: array
pvcNameTemplate:
description: |-
PVCNameTemplate is a template for generating PVC names for VM disks.
It follows Go template syntax and has access to the following variables:
- .VmName: name of the VM
- .PlanName: name of the migration plan
- .DiskIndex: initial volume index of the disk
- .RootDiskIndex: index of the root disk
Note:
This template overrides the plan level template.
Examples:
"{{.VmName}}-disk-{{.DiskIndex}}"
"{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
type: string
restorePowerState:
description: Source VM power state before migration.
type: string
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/forklift/v1beta1/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ type PlanSpec struct {
PreserveClusterCPUModel bool `json:"preserveClusterCpuModel,omitempty"`
// Preserve static IPs of VMs in vSphere
PreserveStaticIPs bool `json:"preserveStaticIPs,omitempty"`
// PVCNameTemplate is a template for generating PVC names for VM disks.
// It follows Go template syntax and has access to the following variables:
// - .VmName: name of the VM
// - .PlanName: name of the migration plan
// - .DiskIndex: initial volume index of the disk
// - .RootDiskIndex: index of the root disk
// Note:
// This template can be overridden at the individual VM level.
// Examples:
// "{{.VmName}}-disk-{{.DiskIndex}}"
// "{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
// +optional
PVCNameTemplate string `json:"pvcNameTemplate,omitempty"`
}

// Find a planned VM.
Expand Down Expand Up @@ -141,3 +154,11 @@ func (r *Plan) IsSourceProviderOCP() bool {
}

func (r *Plan) IsSourceProviderVSphere() bool { return r.Provider.Source.Type() == VSphere }

// PVCNameTemplateData contains fields used in naming templates.
type PVCNameTemplateData struct {
VmName string `json:"vmName"`
PlanName string `json:"planName"`
DiskIndex int `json:"diskIndex"`
RootDiskIndex int `json:"rootDiskIndex"`
}
13 changes: 13 additions & 0 deletions pkg/apis/forklift/v1beta1/plan/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ type VM struct {
// Selected InstanceType that will override the VM properties.
// +optional
InstanceType string `json:"instanceType,omitempty"`
// PVCNameTemplate is a template for generating PVC names for VM disks.
// It follows Go template syntax and has access to the following variables:
// - .VmName: name of the VM
// - .PlanName: name of the migration plan
// - .DiskIndex: initial volume index of the disk
// - .RootDiskIndex: index of the root disk
// Note:
// This template overrides the plan level template.
// Examples:
// "{{.VmName}}-disk-{{.DiskIndex}}"
// "{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
// +optional
PVCNameTemplate string `json:"pvcNameTemplate,omitempty"`
}

// Find a Hook for the specified step.
Expand Down
15 changes: 15 additions & 0 deletions pkg/apis/forklift/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 79 additions & 1 deletion pkg/controller/plan/adapter/vsphere/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vsphere

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -11,6 +12,7 @@ import (
"regexp"
"sort"
"strings"
"text/template"

api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan"
Expand Down Expand Up @@ -430,7 +432,7 @@ func (r *Builder) DataVolumes(vmRef ref.Ref, secret *core.Secret, _ *core.Config
err = fErr
return
}
for _, disk := range vm.Disks {
for diskIndex, disk := range vm.Disks {
if disk.Datastore.ID == ds.ID {
storageClass := mapped.Destination.StorageClass
var dvSource cdi.DataVolumeSource
Expand Down Expand Up @@ -483,6 +485,32 @@ func (r *Builder) DataVolumes(vmRef ref.Ref, secret *core.Secret, _ *core.Config
dv.ObjectMeta.Annotations = make(map[string]string)
}
dv.ObjectMeta.Annotations[planbase.AnnDiskSource] = r.baseVolume(disk.File)

// if exists, get the PVC generate name from the PlanSpec, generate the name
// and update the GenerateName field in the DataVolume object.
pvcNameTemplate := r.getPVCNameTemplate(vm)
if pvcNameTemplate != "" {
// Get the VM root disk index
planVM := r.getPlanVM(vm)
rootDiskIndex := 0
if planVM != nil {
rootDiskIndex = utils.GetDeviceNumber(planVM.RootDisk)
}

// Create template data
templateData := api.PVCNameTemplateData{
VmName: vm.Name,
PlanName: r.Plan.Name,
DiskIndex: diskIndex,
RootDiskIndex: rootDiskIndex,
}

generatedName, err := r.executeTemplate(pvcNameTemplate, &templateData)
if err == nil && generatedName != "" {
dv.ObjectMeta.GenerateName = generatedName
}
}

dvs = append(dvs, *dv)
}
}
Expand Down Expand Up @@ -981,3 +1009,53 @@ func (r *Builder) GetPopulatorTaskName(pvc *core.PersistentVolumeClaim) (taskNam
err = planbase.VolumePopulatorNotSupportedError
return
}

// Get the plan VM for the given vsphere VM
func (r *Builder) getPlanVM(vm *model.VM) *plan.VM {
for _, planVM := range r.Plan.Spec.VMs {
if planVM.ID == vm.ID {
return &planVM
}
}

return nil
}

func (r *Builder) executeTemplate(templateText string, templateData any) (string, error) {
var buf bytes.Buffer

// Parse template syntax
tmpl, err := template.New("template").Parse(templateText)
if err != nil {
return "", err
}

// Execute template
err = tmpl.Execute(&buf, templateData)
if err != nil {
return "", err
}

return buf.String(), nil
}

// GetPVCNameTemplate returns the PVC name template
func (r *Builder) getPVCNameTemplate(vm *model.VM) string {
// Get plan VM
planVM := r.getPlanVM(vm)
if planVM == nil {
return ""
}

// if vm.PVCNameTemplate is set, use it
if planVM.PVCNameTemplate != "" {
return planVM.PVCNameTemplate
}

// if planSpec.PVCNameTemplate is set, use it
if r.Plan.Spec.PVCNameTemplate != "" {
return r.Plan.Spec.PVCNameTemplate
}

return ""
}
Loading
Loading