Skip to content

Commit

Permalink
chore: Fix hash annotation key resolution for v1beta1/NodeClaim (aw…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Aug 25, 2023
1 parent 9505737 commit 6bca755
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pkg/controllers/machine/disruption/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,14 @@ func (d *Drift) Reconcile(ctx context.Context, nodePool *v1beta1.NodePool, nodeC
return reconcile.Result{RequeueAfter: 5 * time.Minute}, nil
}

// isDrifted will check if a machine is drifted from the fields in the provisioner.Spec and
// the cloudprovider
// isDrifted will check if a NodeClaim is drifted from the fields in the NodePool Spec and the CloudProvider
func (d *Drift) isDrifted(ctx context.Context, nodePool *v1beta1.NodePool, nodeClaim *v1beta1.NodeClaim) (cloudprovider.DriftReason, error) {
// First check for static drift or node requirements have drifted to save on API calls.
if reason := lo.FindOrElse([]cloudprovider.DriftReason{areStaticFieldsDrifted(nodePool, nodeClaim), areRequirementsDrifted(nodePool, nodeClaim)}, "", func(i cloudprovider.DriftReason) bool {
return i != ""
}); reason != "" {
return reason, nil
}

driftedReason, err := d.cloudProvider.IsMachineDrifted(ctx, machineutil.NewFromNodeClaim(nodeClaim))
if err != nil {
return "", err
Expand All @@ -114,24 +112,29 @@ func (d *Drift) isDrifted(ctx context.Context, nodePool *v1beta1.NodePool, nodeC
// Eligible fields for static drift are described in the docs
// https://karpenter.sh/docs/concepts/deprovisioning/#drift
func areStaticFieldsDrifted(nodePool *v1beta1.NodePool, nodeClaim *v1beta1.NodeClaim) cloudprovider.DriftReason {
provisionerHash, foundHashProvisioner := nodePool.Annotations[v1alpha5.ProvisionerHashAnnotationKey]
machineHash, foundHashMachine := nodeClaim.Annotations[v1alpha5.ProvisionerHashAnnotationKey]
if !foundHashProvisioner || !foundHashMachine {
var ownerHashKey string
if nodeClaim.IsMachine {
ownerHashKey = v1alpha5.ProvisionerHashAnnotationKey
} else {
ownerHashKey = v1beta1.NodePoolHashAnnotationKey
}
nodePoolHash, foundHashNodePool := nodePool.Annotations[ownerHashKey]
nodeClaimHash, foundHashNodeClaim := nodeClaim.Annotations[ownerHashKey]
if !foundHashNodePool || !foundHashNodeClaim {
return ""
}
if provisionerHash != machineHash {
if nodePoolHash != nodeClaimHash {
return ProvisionerDrifted
}

return ""
}

func areRequirementsDrifted(nodePool *v1beta1.NodePool, nodeClaim *v1beta1.NodeClaim) cloudprovider.DriftReason {
provisionerReq := scheduling.NewNodeSelectorRequirements(nodePool.Spec.Template.Spec.Requirements...)
machineReq := scheduling.NewLabelRequirements(nodeClaim.Labels)
nodeClaimReq := scheduling.NewLabelRequirements(nodeClaim.Labels)

// Every provisioner requirement is compatible with the Machine label set
if machineReq.StrictlyCompatible(provisionerReq) != nil {
// Every provisioner requirement is compatible with the NodeClaim label set
if nodeClaimReq.StrictlyCompatible(provisionerReq) != nil {
return RequirementsDrifted
}

Expand Down

0 comments on commit 6bca755

Please sign in to comment.