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

[WIP] wait for static ip allocation #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 35 additions & 2 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ func (r vmReconciler) reconcileNormal(ctx *context.VMContext) (reconcile.Result,
// TODO(akutz) Implement selection of VM service based on vSphere version
var vmService services.VirtualMachineService = &govmomi.VMService{}

if r.isWaitingForStaticIPAllocation(ctx) {
ctx.Logger.Info("vm is waiting for static ip to be available")
return reconcile.Result{}, nil
}

// Get or create the VM.
vm, err := vmService.ReconcileVM(ctx)
if err != nil {
Expand Down Expand Up @@ -340,7 +345,14 @@ func (r vmReconciler) reconcileNormal(ctx *context.VMContext) (reconcile.Result,
}

// Update the VSphereVM's network status.
r.reconcileNetwork(ctx, vm)
if ok, err := r.reconcileNetwork(ctx, vm); !ok {
if err != nil {
return reconcile.Result{}, errors.Wrapf(err,
"unexpected error while reconciling network for %s", ctx)
}
ctx.Logger.Info("network is not reconciled")
return reconcile.Result{RequeueAfter: 30 * time.Second}, nil
}

// Once the network is online the VM is considered ready.
ctx.VSphereVM.Status.Ready = true
Expand All @@ -349,13 +361,34 @@ func (r vmReconciler) reconcileNormal(ctx *context.VMContext) (reconcile.Result,
return reconcile.Result{}, nil
}

func (r vmReconciler) reconcileNetwork(ctx *context.VMContext, vm infrav1.VirtualMachine) {
func (r vmReconciler) isWaitingForStaticIPAllocation(ctx *context.VMContext) bool {
waitForIP := false
devices := ctx.VSphereVM.Spec.Network.Devices

for _, dev := range devices {
if !dev.DHCP4 && !dev.DHCP6 && len(dev.IPAddrs) == 0 {
// Static IP is not available yet
waitForIP = true
}
}

return waitForIP
}

func (r vmReconciler) reconcileNetwork(ctx *context.VMContext, vm infrav1.VirtualMachine) (bool, error) {
ctx.VSphereVM.Status.Network = vm.Network
ipAddrs := make([]string, 0, len(vm.Network))
for _, netStatus := range ctx.VSphereVM.Status.Network {
ipAddrs = append(ipAddrs, netStatus.IPAddrs...)
}

if len(ipAddrs) == 0 {
ctx.Logger.Info("waiting on IP addresses")
return false, nil
}

ctx.VSphereVM.Status.Addresses = ipAddrs
return true, nil
}

func (r *vmReconciler) clusterToVSphereVMs(a handler.MapObject) []reconcile.Request {
Expand Down