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-1596 | Add new VolumeNameTemplate field to plan CR #1332

Merged
Merged
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
13 changes: 13 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,19 @@ spec:
type:
description: Type used to qualify the name.
type: string
volumeNameTemplate:
description: |-
VolumeNameTemplate is a template for generating volume interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .PVCName: name of the PVC mounted to the VM using this volume
- .VolumeIndex: sequential index of the volume interface (0-based)
Note:
- This template will override at the plan level template
- If not specified on VM level and on Plan leverl, default naming conventions will be used
Examples:
"disk-{{.VolumeIndex}}"
"pvc-{{.PVCName}}"
type: string
warm:
description: Warm migration status
properties:
Expand Down
39 changes: 39 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_plans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,34 @@ spec:
type:
description: Type used to qualify the name.
type: string
volumeNameTemplate:
description: |-
VolumeNameTemplate is a template for generating volume interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .PVCName: name of the PVC mounted to the VM using this volume
- .VolumeIndex: sequential index of the volume interface (0-based)
Note:
- This template will override at the plan level template
- If not specified on VM level and on Plan leverl, default naming conventions will be used
Examples:
"disk-{{.VolumeIndex}}"
"pvc-{{.PVCName}}"
type: string
type: object
type: array
volumeNameTemplate:
description: |-
VolumeNameTemplate is a template for generating volume interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .PVCName: name of the PVC mounted to the VM using this volume
- .VolumeIndex: sequential index of the volume interface (0-based)
Note:
- This template can be overridden at the individual VM level
- If not specified on VM level and on Plan leverl, default naming conventions will be used
Examples:
"disk-{{.VolumeIndex}}"
"pvc-{{.PVCName}}"
type: string
warm:
description: Whether this is a warm migration.
type: boolean
Expand Down Expand Up @@ -1089,6 +1115,19 @@ spec:
type:
description: Type used to qualify the name.
type: string
volumeNameTemplate:
description: |-
VolumeNameTemplate is a template for generating volume interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .PVCName: name of the PVC mounted to the VM using this volume
- .VolumeIndex: sequential index of the volume interface (0-based)
Note:
- This template will override at the plan level template
- If not specified on VM level and on Plan leverl, default naming conventions will be used
Examples:
"disk-{{.VolumeIndex}}"
"pvc-{{.PVCName}}"
type: string
warm:
description: Warm migration status
properties:
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/forklift/v1beta1/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ type PlanSpec struct {
// "{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
// +optional
PVCNameTemplate string `json:"pvcNameTemplate,omitempty"`
// VolumeNameTemplate is a template for generating volume interface names in the target virtual machine.
// It follows Go template syntax and has access to the following variables:
// - .PVCName: name of the PVC mounted to the VM using this volume
// - .VolumeIndex: sequential index of the volume interface (0-based)
// Note:
// - This template can be overridden at the individual VM level
// - If not specified on VM level and on Plan leverl, default naming conventions will be used
// Examples:
// "disk-{{.VolumeIndex}}"
// "pvc-{{.PVCName}}"
// +optional
VolumeNameTemplate string `json:"volumeNameTemplate,omitempty"`
}

// Find a planned VM.
Expand Down Expand Up @@ -173,3 +185,9 @@ type PVCNameTemplateData struct {
DiskIndex int `json:"diskIndex"`
RootDiskIndex int `json:"rootDiskIndex"`
}

// VolumeNameTemplateData contains fields used in naming templates.
type VolumeNameTemplateData struct {
PVCName string `json:"pvcName,omitempty"`
VolumeIndex int `json:"volumeIndex,omitempty"`
}
12 changes: 12 additions & 0 deletions pkg/apis/forklift/v1beta1/plan/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ type VM struct {
// "{{if eq .DiskIndex .RootDiskIndex}}root{{else}}data{{end}}-{{.DiskIndex}}"
// +optional
PVCNameTemplate string `json:"pvcNameTemplate,omitempty"`
// VolumeNameTemplate is a template for generating volume interface names in the target virtual machine.
// It follows Go template syntax and has access to the following variables:
// - .PVCName: name of the PVC mounted to the VM using this volume
// - .VolumeIndex: sequential index of the volume interface (0-based)
// Note:
// - This template will override at the plan level template
// - If not specified on VM level and on Plan leverl, default naming conventions will be used
// Examples:
// "disk-{{.VolumeIndex}}"
// "pvc-{{.PVCName}}"
// +optional
VolumeNameTemplate string `json:"volumeNameTemplate,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.

42 changes: 42 additions & 0 deletions pkg/controller/plan/adapter/vsphere/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ func (r *Builder) removeSharedDisks(vm *model.VM) {
func (r *Builder) mapDisks(vm *model.VM, vmRef ref.Ref, persistentVolumeClaims []*core.PersistentVolumeClaim, object *cnv.VirtualMachineSpec) {
var kVolumes []cnv.Volume
var kDisks []cnv.Disk
var templateErr error

disks := vm.Disks
sort.Slice(disks, func(i, j int) bool {
Expand Down Expand Up @@ -842,6 +843,26 @@ func (r *Builder) mapDisks(vm *model.VM, vmRef ref.Ref, persistentVolumeClaims [
for i, disk := range disks {
pvc := pvcMap[r.baseVolume(disk.File)]
volumeName := fmt.Sprintf("vol-%v", i)

// If the volume name template is set, use it to generate the volume name.
volumeNameTemplate := r.getVolumeNameTemplate(vm)
if volumeNameTemplate != "" {
// Create template data
templateData := api.VolumeNameTemplateData{
PVCName: pvc.Name,
VolumeIndex: i,
}

volumeName, templateErr = r.executeTemplate(volumeNameTemplate, &templateData)
if templateErr != nil {
// Failed to generate volume name using template
r.Log.Info("Failed to generate volume name using template, using default name", "template", volumeNameTemplate, "error", templateErr)

// fallback to default name and reset error
volumeName = fmt.Sprintf("vol-%v", i)
}
}

volume := cnv.Volume{
Name: volumeName,
VolumeSource: cnv.VolumeSource{
Expand Down Expand Up @@ -1184,3 +1205,24 @@ func (r *Builder) getPVCNameTemplate(vm *model.VM) string {

return ""
}

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

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

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

return ""
}
87 changes: 74 additions & 13 deletions pkg/controller/plan/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ func (r *Reconciler) validate(plan *api.Plan) error {
return err
}

// Validate volume name template
if err := r.validateVolumeNameTemplate(plan); err != nil {
return err
}

return nil
}

Expand All @@ -190,6 +195,22 @@ func (r *Reconciler) validatePVCNameTemplate(plan *api.Plan) error {
return nil
}

func (r *Reconciler) validateVolumeNameTemplate(plan *api.Plan) error {
if err := r.IsValidVolumeNameTemplate(plan.Spec.VolumeNameTemplate); err != nil {
invalidPVCNameTemplate := libcnd.Condition{
Type: NotValid,
Status: True,
Category: Critical,
Message: "Volume name template is invalid.",
Items: []string{},
}

plan.Status.SetCondition(invalidPVCNameTemplate)
}

return nil
}

func (r *Reconciler) validateOpenShiftVersion(plan *api.Plan) error {
source := plan.Referenced.Provider.Source
if source == nil {
Expand Down Expand Up @@ -484,6 +505,13 @@ func (r *Reconciler) validateVM(plan *api.Plan) error {
Message: "VM PVC name template is invalid.",
Items: []string{},
}
volumeNameInvalid := libcnd.Condition{
Type: NotValid,
Status: True,
Category: Critical,
Message: "VM volume name template is invalid.",
Items: []string{},
}

setOf := map[string]bool{}
//
Expand Down Expand Up @@ -625,6 +653,12 @@ func (r *Reconciler) validateVM(plan *api.Plan) error {
pvcNameInvalid.Items = append(pvcNameInvalid.Items, ref.String())
}
}
// is valid vm pvc name template
if plan.Spec.VMs[i].VolumeNameTemplate != "" {
if err := r.IsValidVolumeNameTemplate(plan.Spec.VMs[i].VolumeNameTemplate); err != nil {
volumeNameInvalid.Items = append(volumeNameInvalid.Items, ref.String())
}
}
}
if len(notFound.Items) > 0 {
plan.Status.SetCondition(notFound)
Expand Down Expand Up @@ -665,6 +699,9 @@ func (r *Reconciler) validateVM(plan *api.Plan) error {
if len(pvcNameInvalid.Items) > 0 {
plan.Status.SetCondition(pvcNameInvalid)
}
if len(volumeNameInvalid.Items) > 0 {
plan.Status.SetCondition(volumeNameInvalid)
}

return nil
}
Expand Down Expand Up @@ -1176,6 +1213,28 @@ func (r *Reconciler) checkOCPVersion(clientset kubernetes.Interface) error {
return nil
}

func (r *Reconciler) IsValidTemplate(templateStr string, testData interface{}) (string, error) {
// Validate golang template syntax
tmpl, err := template.New("template").Parse(templateStr)
if err != nil {
return "", liberr.Wrap(err, "Invalid template syntax")
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, testData)
if err != nil {
return "", liberr.Wrap(err, "Template execution failed")
}
result := buf.String()

// Empty output is not valid
if result == "" {
return "", liberr.New("Template output is empty")
}

return result, nil
}

func (r *Reconciler) IsValidPVCNameTemplate(pvcNameTemplate string) error {
if pvcNameTemplate == "" {
return nil
Expand Down Expand Up @@ -1203,24 +1262,26 @@ func (r *Reconciler) IsValidPVCNameTemplate(pvcNameTemplate string) error {
return nil
}

func (r *Reconciler) IsValidTemplate(templateStr string, testData interface{}) (string, error) {
// Validate golang template syntax
tmpl, err := template.New("template").Parse(templateStr)
if err != nil {
return "", liberr.Wrap(err, "Invalid template syntax")
func (r *Reconciler) IsValidVolumeNameTemplate(volumeNameTemplate string) error {
if volumeNameTemplate == "" {
return nil
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, testData)
testData := api.VolumeNameTemplateData{
PVCName: "test-pvc",
VolumeIndex: 0,
}

result, err := r.IsValidTemplate(volumeNameTemplate, testData)
if err != nil {
return "", liberr.Wrap(err, "Template execution failed")
return err
}
result := buf.String()

// Empty output is not valid
if result == "" {
return "", liberr.New("Template output is empty")
// Validate that template output is a valid k8s label
errs := k8svalidation.IsValidLabelValue(result)
if len(errs) > 0 {
return liberr.New("Template output is not a valid k8s label", "errors", errs)
}

return result, nil
return nil
}
Loading