Skip to content

Commit

Permalink
set machine addresses (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
lubedacht authored Aug 23, 2023
1 parent 47b8777 commit daf105b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
36 changes: 36 additions & 0 deletions internal/service/vmservice/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions pkg/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit daf105b

Please sign in to comment.