Skip to content

Commit

Permalink
add new NetworkNameTemplate field to plan CR
Browse files Browse the repository at this point in the history
Signed-off-by: yaacov <[email protected]>
  • Loading branch information
yaacov committed Feb 2, 2025
1 parent 0429598 commit 8538f45
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 0 deletions.
16 changes: 16 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ spec:
The VM Namespace
Only relevant for an openshift source.
type: string
networkNameTemplate:
description: |-
NetworkNameTemplate is a template for generating network interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .NetworkName: If target network is multus, name of the Multus network attachment definition, empty otherwise.
- .NetworkNamespace: If target network is multus, namespace where the network attachment definition is located.
- .NetworkType: type of the network ("multus" or "pod")
- .NetworkIndex: sequential index of the network interface (0-based)
The template can be used to customize network interface names based on target network configuration.
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:
'net-{{.NetworkIndex}}'
'{{if eq .NetworkType "pod"}}pod{{else}}multus-{{.NetworkIndex}}{{end}}'
type: string
newName:
description: The new name of the VM after matching DNS1123 requirements.
type: string
Expand Down
48 changes: 48 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_plans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ spec:
- network
- storage
type: object
networkNameTemplate:
description: |-
NetworkNameTemplate is a template for generating network interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .NetworkName: If target network is multus, name of the Multus network attachment definition, empty otherwise.
- .NetworkNamespace: If target network is multus, namespace where the network attachment definition is located.
- .NetworkType: type of the network ("multus" or "pod")
- .NetworkIndex: sequential index of the network interface (0-based)
The template can be used to customize network interface names based on target network configuration.
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:
'net-{{.NetworkIndex}}'
'{{if eq .NetworkType "pod"}}pod{{else}}multus-{{.NetworkIndex}}{{end}}'
type: string
preserveClusterCpuModel:
description: Preserve the CPU model and flags the VM runs with in
its oVirt cluster.
Expand Down Expand Up @@ -432,6 +448,22 @@ spec:
The VM Namespace
Only relevant for an openshift source.
type: string
networkNameTemplate:
description: |-
NetworkNameTemplate is a template for generating network interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .NetworkName: If target network is multus, name of the Multus network attachment definition, empty otherwise.
- .NetworkNamespace: If target network is multus, namespace where the network attachment definition is located.
- .NetworkType: type of the network ("multus" or "pod")
- .NetworkIndex: sequential index of the network interface (0-based)
The template can be used to customize network interface names based on target network configuration.
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:
'net-{{.NetworkIndex}}'
'{{if eq .NetworkType "pod"}}pod{{else}}multus-{{.NetworkIndex}}{{end}}'
type: string
rootDisk:
description: Choose the primary disk the VM boots from
type: string
Expand Down Expand Up @@ -888,6 +920,22 @@ spec:
The VM Namespace
Only relevant for an openshift source.
type: string
networkNameTemplate:
description: |-
NetworkNameTemplate is a template for generating network interface names in the target virtual machine.
It follows Go template syntax and has access to the following variables:
- .NetworkName: If target network is multus, name of the Multus network attachment definition, empty otherwise.
- .NetworkNamespace: If target network is multus, namespace where the network attachment definition is located.
- .NetworkType: type of the network ("multus" or "pod")
- .NetworkIndex: sequential index of the network interface (0-based)
The template can be used to customize network interface names based on target network configuration.
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:
'net-{{.NetworkIndex}}'
'{{if eq .NetworkType "pod"}}pod{{else}}multus-{{.NetworkIndex}}{{end}}'
type: string
newName:
description: The new name of the VM after matching DNS1123
requirements.
Expand Down
27 changes: 27 additions & 0 deletions pkg/apis/forklift/v1beta1/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ type PlanSpec struct {
PreserveClusterCPUModel bool `json:"preserveClusterCpuModel,omitempty"`
// Preserve static IPs of VMs in vSphere
PreserveStaticIPs bool `json:"preserveStaticIPs,omitempty"`
// NetworkNameTemplate is a template for generating network interface names in the target virtual machine.
// It follows Go template syntax and has access to the following variables:
// - .NetworkName: If target network is multus, name of the Multus network attachment definition, empty otherwise.
// - .NetworkNamespace: If target network is multus, namespace where the network attachment definition is located.
// - .NetworkType: type of the network ("multus" or "pod")
// - .NetworkIndex: sequential index of the network interface (0-based)
// The template can be used to customize network interface names based on target network configuration.
// 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:
// 'net-{{.NetworkIndex}}'
// '{{if eq .NetworkType "pod"}}pod{{else}}multus-{{.NetworkIndex}}{{end}}'
// +optional
NetworkNameTemplate string `json:"networkNameTemplate,omitempty"`
}

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

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

// NetworkNameTemplateData contains fields used in naming templates.
type NetworkNameTemplateData struct {
// NetworkName is the name of the Multus network attachment definition if target network is multus, empty otherwise
NetworkName string `json:"networkName,omitempty"`
// NetworkNamespace is the namespace where the network attachment definition is located if target network is multus
NetworkNamespace string `json:"networkNamespace,omitempty"`
// NetworkType is the type of the network ("multus" or "pod")
NetworkType string `json:"networkType,omitempty"`
// NetworkIndex is the sequential index of the network interface (0-based)
NetworkIndex int `json:"networkIndex,omitempty"`
}
15 changes: 15 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,21 @@ type VM struct {
// Selected InstanceType that will override the VM properties.
// +optional
InstanceType string `json:"instanceType,omitempty"`
// NetworkNameTemplate is a template for generating network interface names in the target virtual machine.
// It follows Go template syntax and has access to the following variables:
// - .NetworkName: If target network is multus, name of the Multus network attachment definition, empty otherwise.
// - .NetworkNamespace: If target network is multus, namespace where the network attachment definition is located.
// - .NetworkType: type of the network ("multus" or "pod")
// - .NetworkIndex: sequential index of the network interface (0-based)
// The template can be used to customize network interface names based on target network configuration.
// 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:
// 'net-{{.NetworkIndex}}'
// '{{if eq .NetworkType "pod"}}pod{{else}}multus-{{.NetworkIndex}}{{end}}'
// +optional
NetworkNameTemplate string `json:"networkNameTemplate,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.

72 changes: 72 additions & 0 deletions 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 @@ -592,6 +594,26 @@ func (r *Builder) mapNetworks(vm *model.VM, object *cnv.VirtualMachineSpec) (err
}
for _, nic := range needed {
networkName := fmt.Sprintf("net-%v", numNetworks)

// If the network name template is set, use it to generate the network name.
networkNameTemplate := r.getNetworkNameTemplate(vm)
if networkNameTemplate != "" {
// Create template data
templateData := api.NetworkNameTemplateData{
NetworkName: mapped.Destination.Name,
NetworkNamespace: mapped.Destination.Namespace,
NetworkType: strings.ToLower(string(mapped.Destination.Type)),
NetworkIndex: numNetworks,
}

networkName, err = r.executeTemplate(networkNameTemplate, &templateData)
if err != nil {
// fallback to default name and reset error
networkName = fmt.Sprintf("net-%v", numNetworks)
err = nil
}
}

numNetworks++
kNetwork := cnv.Network{
Name: networkName,
Expand Down Expand Up @@ -981,3 +1003,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
}

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

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

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

return ""
}
Loading

0 comments on commit 8538f45

Please sign in to comment.