Skip to content
Draft
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
1 change: 1 addition & 0 deletions api/v1alpha1/kubevirtcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const ( //labels
const ( // annotations
VmiDeletionGraceTime = "capk.cluster.x-k8s.io/vmi-deletion-grace-time"
VmiDeletionGraceTimeEscape = "capk.cluster.x-k8s.io~1vmi-deletion-grace-time"
HostnameListAnnotation = "capk.cluster.x-k8s.io/hostname-list"
)

// KubevirtClusterSpec defines the desired state of KubevirtCluster.
Expand Down
14 changes: 13 additions & 1 deletion pkg/kubevirt/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,19 @@
func (m *Machine) Create(ctx gocontext.Context) error {
m.machineContext.Logger.Info(fmt.Sprintf("Creating VM with role '%s'...", nodeRole(m.machineContext)))

virtualMachine := newVirtualMachineFromKubevirtMachine(m.machineContext, m.namespace)
// Get assigned hostname from MachineSet annotation if available
hostnameSelector := NewHostnameSelector(m.client)

Check failure on line 185 in pkg/kubevirt/machine.go

View workflow job for this annotation

GitHub Actions / coverage

undefined: NewHostnameSelector

Check failure on line 185 in pkg/kubevirt/machine.go

View workflow job for this annotation

GitHub Actions / go-linter

undefined: NewHostnameSelector (typecheck)

Check failure on line 185 in pkg/kubevirt/machine.go

View workflow job for this annotation

GitHub Actions / go-linter

undefined: NewHostnameSelector) (typecheck)

Check failure on line 185 in pkg/kubevirt/machine.go

View workflow job for this annotation

GitHub Actions / go-linter

undefined: NewHostnameSelector) (typecheck)

Check failure on line 185 in pkg/kubevirt/machine.go

View workflow job for this annotation

GitHub Actions / go-linter

undefined: NewHostnameSelector) (typecheck)

Check failure on line 185 in pkg/kubevirt/machine.go

View workflow job for this annotation

GitHub Actions / unit_test (ubuntu-latest)

undefined: NewHostnameSelector
assignedHostname, err := hostnameSelector.GetAssignedHostname(ctx, m.machineContext.KubevirtMachine, m.machineContext.Machine)
if err != nil {
m.machineContext.Logger.Error(err, "Failed to get assigned hostname, using original name")
assignedHostname = m.machineContext.KubevirtMachine.Name
}

if assignedHostname != m.machineContext.KubevirtMachine.Name {
m.machineContext.Logger.Info(fmt.Sprintf("Using assigned hostname '%s' instead of generated name '%s'", assignedHostname, m.machineContext.KubevirtMachine.Name))
}

virtualMachine := newVirtualMachineFromKubevirtMachine(m.machineContext, m.namespace, assignedHostname)

mutateFn := func() (err error) {
if virtualMachine.Labels == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubevirt/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ var _ = Describe("util functions", func() {
machineContext.KubevirtMachine.Spec.VirtualMachineTemplate.Spec.DataVolumeTemplates = dataVolumeTemplates
machineContext.KubevirtMachine.Spec.VirtualMachineTemplate.Spec.Template.Spec.Volumes = volumes

newVM := newVirtualMachineFromKubevirtMachine(machineContext, "default")
newVM := newVirtualMachineFromKubevirtMachine(machineContext, "default", "")

Expect(newVM.Spec.DataVolumeTemplates[0].ObjectMeta.Name).To(Equal(kubevirtMachineName + "-dv1"))
Expect(newVM.Spec.Template.Spec.Volumes[0].VolumeSource.DataVolume.Name).To(Equal(kubevirtMachineName + "-dv1"))
Expand Down Expand Up @@ -771,7 +771,7 @@ var _ = Describe("util functions", func() {
machineContext.KubevirtMachine.Spec.VirtualMachineTemplate.Spec.DataVolumeTemplates = dataVolumeTemplates
machineContext.KubevirtMachine.Spec.VirtualMachineTemplate.Spec.Template.Spec.Volumes = volumes

newVM := newVirtualMachineFromKubevirtMachine(machineContext, "default")
newVM := newVirtualMachineFromKubevirtMachine(machineContext, "default", "")

Expect(newVM.Spec.DataVolumeTemplates[0].ObjectMeta.Name).To(Equal(kubevirtMachineName + "-dv1"))
Expect(newVM.Spec.Template.Spec.Volumes[0].VolumeSource.DataVolume.Name).To(Equal(kubevirtMachineName + "-dv1"))
Expand Down
30 changes: 21 additions & 9 deletions pkg/kubevirt/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func prefixDataVolumeTemplates(vm *kubevirtv1.VirtualMachine, prefix string) *ku
}

// newVirtualMachineFromKubevirtMachine creates VirtualMachine instance.
func newVirtualMachineFromKubevirtMachine(ctx *context.MachineContext, namespace string) *kubevirtv1.VirtualMachine {
vmiTemplate := buildVirtualMachineInstanceTemplate(ctx)
func newVirtualMachineFromKubevirtMachine(ctx *context.MachineContext, namespace string, assignedHostname string) *kubevirtv1.VirtualMachine {
vmiTemplate := buildVirtualMachineInstanceTemplate(ctx, assignedHostname)

virtualMachine := &kubevirtv1.VirtualMachine{
Spec: *ctx.KubevirtMachine.Spec.VirtualMachineTemplate.Spec.DeepCopy(),
Expand All @@ -80,8 +80,14 @@ func newVirtualMachineFromKubevirtMachine(ctx *context.MachineContext, namespace
virtualMachine.APIVersion = "kubevirt.io/v1"
virtualMachine.Kind = "VirtualMachine"

// Use assigned hostname if provided, otherwise fall back to original KubevirtMachine name
vmName := assignedHostname
if vmName == "" {
vmName = ctx.KubevirtMachine.Name
}

virtualMachine.ObjectMeta = metav1.ObjectMeta{
Name: ctx.KubevirtMachine.Name,
Name: vmName,
Namespace: namespace,
Labels: map[string]string{},
}
Expand All @@ -94,13 +100,13 @@ func newVirtualMachineFromKubevirtMachine(ctx *context.MachineContext, namespace
virtualMachine.ObjectMeta.Annotations = mapCopy(ctx.KubevirtMachine.Spec.VirtualMachineTemplate.ObjectMeta.Annotations)
}

virtualMachine.ObjectMeta.Labels["kubevirt.io/vm"] = ctx.KubevirtMachine.Name
virtualMachine.ObjectMeta.Labels["name"] = ctx.KubevirtMachine.Name
virtualMachine.ObjectMeta.Labels["kubevirt.io/vm"] = vmName
virtualMachine.ObjectMeta.Labels["name"] = vmName
virtualMachine.ObjectMeta.Labels["cluster.x-k8s.io/role"] = nodeRole(ctx)
virtualMachine.ObjectMeta.Labels["cluster.x-k8s.io/cluster-name"] = ctx.Cluster.Name

// make each datavolume unique by appending machine name as a prefix
virtualMachine = prefixDataVolumeTemplates(virtualMachine, ctx.KubevirtMachine.Name)
virtualMachine = prefixDataVolumeTemplates(virtualMachine, vmName)

return virtualMachine
}
Expand All @@ -115,7 +121,7 @@ func mapCopy(src map[string]string) map[string]string {
}

// buildVirtualMachineInstanceTemplate creates VirtualMachineInstanceTemplateSpec.
func buildVirtualMachineInstanceTemplate(ctx *context.MachineContext) *kubevirtv1.VirtualMachineInstanceTemplateSpec {
func buildVirtualMachineInstanceTemplate(ctx *context.MachineContext, assignedHostname string) *kubevirtv1.VirtualMachineInstanceTemplateSpec {
template := &kubevirtv1.VirtualMachineInstanceTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{},
Expand All @@ -131,8 +137,14 @@ func buildVirtualMachineInstanceTemplate(ctx *context.MachineContext) *kubevirtv
template.ObjectMeta.Annotations = mapCopy(ctx.KubevirtMachine.Spec.VirtualMachineTemplate.Spec.Template.ObjectMeta.Annotations)
}

template.ObjectMeta.Labels["kubevirt.io/vm"] = ctx.KubevirtMachine.Name
template.ObjectMeta.Labels["name"] = ctx.KubevirtMachine.Name
// Use assigned hostname if provided, otherwise fall back to original KubevirtMachine name
vmName := assignedHostname
if vmName == "" {
vmName = ctx.KubevirtMachine.Name
}

template.ObjectMeta.Labels["kubevirt.io/vm"] = vmName
template.ObjectMeta.Labels["name"] = vmName
template.ObjectMeta.Labels["cluster.x-k8s.io/role"] = nodeRole(ctx)
template.ObjectMeta.Labels["cluster.x-k8s.io/cluster-name"] = ctx.Cluster.Name

Expand Down
Loading