From daf105b7af7a9fbb84118d01a3232288353a6c20 Mon Sep 17 00:00:00 2001 From: lubedacht <132355999+lubedacht@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:14:38 +0200 Subject: [PATCH] set machine addresses (#32) --- internal/service/vmservice/vm.go | 36 ++++++++++++++++++++++++++++++++ pkg/scope/machine.go | 5 +++++ 2 files changed, 41 insertions(+) diff --git a/internal/service/vmservice/vm.go b/internal/service/vmservice/vm.go index 5e71c46f..a375468a 100644 --- a/internal/service/vmservice/vm.go +++ b/internal/service/vmservice/vm.go @@ -117,6 +117,10 @@ func ReconcileVM(ctx context.Context, scope *scope.MachineScope) (infrav1alpha1. return vm, err } + if err := reconcileMachineAddresses(scope); err != nil { + return vm, err + } + vm.State = infrav1alpha1.VirtualMachineStateReady return vm, nil } @@ -208,6 +212,38 @@ func reconcileIPAddresses(ctx context.Context, machineScope *scope.MachineScope) return true, nil } +func reconcileMachineAddresses(scope *scope.MachineScope) error { + addr, err := getMachineAddresses(scope) + if err != nil { + scope.Error(err, "failed to retrieve machine addresses") + return err + } + + scope.SetAddresses(addr) + return nil +} + +func getMachineAddresses(scope *scope.MachineScope) ([]clusterv1.MachineAddress, error) { + if scope.ProxmoxMachine.Status.IPAddr == nil { + return nil, errors.New("machine does not yet have an ip address") + } + + if !scope.VirtualMachine.IsRunning() { + return nil, errors.New("unable to apply configuration as long as the virtual machine is not running") + } + + return []clusterv1.MachineAddress{ + { + Type: clusterv1.MachineHostName, + Address: scope.Name(), + }, + { + Type: clusterv1.MachineInternalIP, + Address: *scope.ProxmoxMachine.Status.IPAddr, + }, + }, nil +} + func createVM(scope *scope.MachineScope) (proxmox.VMCloneResponse, error) { options := proxmox.VMCloneRequest{ Node: scope.ProxmoxMachine.GetNode(), diff --git a/pkg/scope/machine.go b/pkg/scope/machine.go index 0498e712..80c964ed 100644 --- a/pkg/scope/machine.go +++ b/pkg/scope/machine.go @@ -192,6 +192,11 @@ func (m *MachineScope) PatchObject() error { }}) } +// SetAddresses sets the addresses in the status. +func (m *MachineScope) SetAddresses(addr []clusterv1.MachineAddress) { + m.ProxmoxMachine.Status.Addresses = addr +} + // Close the MachineScope by updating the machine spec, machine status. func (m *MachineScope) Close() error { return m.PatchObject()