in pkg/controllers/nodeclaim/inplaceupdate/controller.go [61:129]
func (c *Controller) Reconcile(ctx context.Context, nodeClaim *karpv1.NodeClaim) (reconcile.Result, error) {
ctx = injection.WithControllerName(ctx, "nodeclaim.inplaceupdate")
if !nodeClaim.DeletionTimestamp.IsZero() {
return reconcile.Result{}, nil
}
// Node doesn't have provider ID yet
if nodeClaim.Status.ProviderID == "" {
return reconcile.Result{}, nil
}
stored := nodeClaim.DeepCopy()
// TODO: When we have sources of truth coming from NodePool we can do:
// nodePool, err := nodeclaimutil.Owner(ctx, c.kubeClient, nodeClaim)
// TODO: To look it up and use that as input to calculate the goal state as well
// Compare the expected hash with the actual hash
options := options.FromContext(ctx)
goalHash, err := HashFromNodeClaim(options, nodeClaim)
if err != nil {
return reconcile.Result{}, err
}
actualHash := nodeClaim.Annotations[v1alpha2.AnnotationInPlaceUpdateHash]
log.FromContext(ctx).V(1).Info(fmt.Sprintf("goal hash is: %q, actual hash is: %q", goalHash, actualHash))
// If there's no difference from goal state, no need to do anything else
if goalHash == actualHash {
return reconcile.Result{}, nil
}
vmName, err := utils.GetVMName(nodeClaim.Status.ProviderID)
if err != nil {
return reconcile.Result{}, err
}
vm, err := c.instanceProvider.Get(ctx, vmName)
if err != nil {
return reconcile.Result{}, fmt.Errorf("getting azure VM for machine, %w", err)
}
update := CalculateVMPatch(options, vm)
// This is safe only as long as we're not updating fields which we consider secret.
// If we do/are, we need to redact them.
logVMPatch(ctx, update)
// Apply the update, if one is needed
if update != nil {
err = c.instanceProvider.Update(ctx, vmName, *update)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to apply update to VM, %w", err)
}
}
if nodeClaim.Annotations == nil {
nodeClaim.Annotations = make(map[string]string)
}
// Regardless of whether we actually changed anything in Azure, we have confirmed that
// the goal shape is in alignment with our expected shape, so update the annotation to reflect that
nodeClaim.Annotations[v1alpha2.AnnotationInPlaceUpdateHash] = goalHash
err = c.kubeClient.Patch(ctx, nodeClaim, client.MergeFrom(stored))
if err != nil {
return reconcile.Result{}, client.IgnoreNotFound(err)
}
return reconcile.Result{}, nil
}