← Back to Pull requests

AUTOSCALE-944: feat: migrate HostedControlPlane lifecycle controller from HyperShift #52

Closed
unknown closed this pull request without merging on Oct 16, 2026 13:12 UTC
View on GitHub
unknown unknown commented on Oct 07, 2026 13:13 UTC
Author

Related PR: https://github.com/openshift/hypershift/pull/9930
E2E test from the related PR passed, see this comment.

This PR migrates the karpenter_controller.go from the hypershift repo into the standalone karpenter operator.

I split the work in 3 commits:
1. migration with minimal changes (AI comparison against hypershift implementation below)
2. refactor to split controllers and align them with how we setup our controllers here
3. a fix for a very rare bug AI identified
- Reproduction requires slightly out of sync events for NodeClaim and the Node, as the Node seems to receive the NodePool label we use for filtering slightly after the NodeClaim was updated.
- I couldn't reproduce it but the code path seems to be possible, the fix is quite small, just watch Update events that add the label we expect

Summary

Port the remaining HostedControlPlane lifecycle logic from HyperShift's Karpenter controller: the Karpenter finalizer, NodePool and NodeClaim cleanup when the HostedControlPlane or CAPI Cluster is deleted, and the AutoNode node, NodeClaim and vCPU counts in HostedControlPlane status. The finalizer name and status field manager (as we use SSA for this) match HyperShift so clusters can switch from the HyperShift adapter.
The CAPI Cluster is read as metadata only, and the status is written with server-side apply on an unstructured object, avoiding dependencies on cluster-api and the HyperShift client module.

The Taint ConfigMap reconciliation isn't migrated as opposed to the requirement from AUTOSCALE-944, it rather belongs to the ignition handling IMO. See the related PR above how it was integrated.

Comparison against original karpenter_controller.go

AI generated but I read, verified and pruned it.

Scope and ownership

HyperShift’s karpenter_controller.go combines HCP lifecycle work with Karpenter deployment and resource reconciliation. This PR extracts HCP lifecycle and AutoNode status logic into separate controllers:

| Responsibility | This branch |
|---|---|
| Add/remove HCP finalizer; detect HCP or CAPI Cluster deletion; clean up NodePools and NodeClaims | pkg/controllers/hcp/deletion/controller.go |
| Report Node, NodeClaim, and vCPU counts in HCP status | pkg/controllers/hcp/autonodestatus/controller.go |
| Read CAPI Cluster deletion metadata without a full Cluster API dependency | pkg/hypershift/capicluster.go |
| Register lifecycle controllers in management-cluster mode when a hosted-cluster client exists | pkg/controllers/controllers.go |

The PR does not move deployment, CRD, or default NodeClass responsibilities into these lifecycle controllers. Existing controllers remain their owners: pkg/controllers/karpenter, pkg/controllers/crd, and pkg/controllers/nodeclass/default.

Taint ConfigMap reconciliation is intentionally out of scope for this ticket.

Behavior preserved

  • Uses same shared HCP finalizer value: hypershift.openshift.io/karpenter-finalizer.

  • Starts cleanup when either HCP or its CAPI Cluster is deleting.

  • Deletes NodePools first, then waits for NodeClaims; applies Karpenter’s termination annotation to stuck NodeClaims.

  • Removes finalizer only once HCP itself is deleting. CAPI deletion alone starts cleanup but retains finalizer.

  • Counts Nodes with Karpenter’s NodePool label, all NodeClaims (including terminating ones), and vCPUs for NodeClaims whose backing Node exists and has reported CPU capacity.
  • Refactor differences to review

  • Deletion cleanup is polled. HyperShift’s shared controller watched NodeClaim deletes, so those events could enqueue cleanup promptly. Here, NodeClaim events enqueue only autonodestatus; hcp/deletion watches HCP and CAPI Cluster. If cleanup finds NodeClaims remaining, it requeues after 15 seconds. Their removal may therefore be noticed at next poll—roughly one interval later under normal scheduling—unless another HCP/CAPI event triggers reconciliation first.

  • - 🚨🚨🚨 this is IMO OK as we also didn't watch the nodepool deletions, we just watched the nodeclaim deletions before as the same controller also handled autonode status updates.
  • AutoNode status apply changed. HyperShift uses its generated apply client on each reconcile and tolerates a stale-schema error during upgrades. This branch uses unstructured Status().Apply, skips writes when status already matches, and keeps field manager karpenter-operator with forced ownership.

  • Node-count events expanded. HyperShift main enqueues on Node create/delete. This branch also enqueues when the NodePool label is added or removed, preventing stale counts when a Node receives that label after creation. Label-value-only changes do not enqueue.

  • Missing HCP handling is event-driven. HyperShift explicitly requeued after five seconds when HCP was absent. These controllers return successfully and rely on HCP watch events.
  • When reconciliation returns an error, controller-runtime ignores returned Result and uses its rate limiter. So the old RequeueAfter values returned alongside errors did not create a retry-timing difference.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: LucasAndFlores Review duration: 12958m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.75) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #52"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.7% · Safe to merge
    29.7%