From b415f1a20a953c505ac67d132e9239f491fc78c4 Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:54:41 +0100 Subject: [PATCH 1/8] cr: CommittedResourceTransaction CRD and reconciler happy path Introduces the `CommittedResourceTransaction` CRD Implements the CRT reconciler happy path Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- .../committed_resource_transaction_types.go | 164 +++ api/v1alpha1/zz_generated.deepcopy.go | 175 ++- ...itted-resource-transaction-architecture.md | 127 ++ .../cortex-nova/templates/kpis_kvm.yaml | 1 + helm/bundles/cortex-nova/values.yaml | 3 + ...x.cloud_committedresourcetransactions.yaml | 419 +++++++ .../commitments/transactions/field_index.go | 48 + .../commitments/transactions/reconciler.go | 343 ++++++ .../transactions/reconciler_test.go | 1082 +++++++++++++++++ 9 files changed, 2341 insertions(+), 21 deletions(-) create mode 100644 api/v1alpha1/committed_resource_transaction_types.go create mode 100644 docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md create mode 100644 helm/library/cortex/files/crds/cortex.cloud_committedresourcetransactions.yaml create mode 100644 internal/scheduling/reservations/commitments/transactions/field_index.go create mode 100644 internal/scheduling/reservations/commitments/transactions/reconciler.go create mode 100644 internal/scheduling/reservations/commitments/transactions/reconciler_test.go diff --git a/api/v1alpha1/committed_resource_transaction_types.go b/api/v1alpha1/committed_resource_transaction_types.go new file mode 100644 index 000000000..84fe23f9d --- /dev/null +++ b/api/v1alpha1/committed_resource_transaction_types.go @@ -0,0 +1,164 @@ +// Copyright SAP SE +// SPDX-License-Identifier: Apache-2.0 + +package v1alpha1 + +import ( + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// TxPhase represents the FSM phase of a CommittedResourceTransaction. +type TxPhase string + +const ( + // TxPhasePreparing: transaction created; sanity checks and optional snapshot in progress. + TxPhasePreparing TxPhase = "Preparing" + // TxPhaseAllocating: CR specs are being written. + TxPhaseAllocating TxPhase = "Allocating" + // TxPhaseAwaitingScheduling: CR specs written; waiting for placement success. + TxPhaseAwaitingScheduling TxPhase = "AwaitingScheduling" + // TxPhaseRollingBack: a CR was rejected or timed out; restoring pre-change state. + TxPhaseRollingBack TxPhase = "RollingBack" + // TxPhaseDryRunCleanup: dry-run would-accept; restoring pre-change state. + TxPhaseDryRunCleanup TxPhase = "DryRunCleanup" + // TxPhaseAccepted: terminal; transaction committed successfully. + TxPhaseAccepted TxPhase = "Accepted" + // TxPhaseRejected: terminal; transaction rolled back. + TxPhaseRejected TxPhase = "Rejected" + // TxPhaseSuperseded: terminal; abandoned because a newer transaction for the same AZ took over. + TxPhaseSuperseded TxPhase = "Superseded" +) + +// TxSource identifies the originator of a CommittedResourceTransaction. +type TxSource string + +const ( + // TxSourceAPI: transaction created by the change-commitments HTTP API. + TxSourceAPI TxSource = "api" + // TxSourceSyncer: transaction created by the Limes syncer. + TxSourceSyncer TxSource = "syncer" +) + +const ( + // TxConditionProgressing is True while the transaction is advancing normally. + // False + reason indicates a problem while still non-terminal. + TxConditionProgressing = "Progressing" + + // TxConditionFinished is True once the transaction has reached a terminal phase. + TxConditionFinished = "Finished" + + // Reasons for TxConditionProgressing=False (non-terminal problems). + TxReasonStalled = "Stalled" + TxReasonTimedOut = "TimedOut" + TxReasonError = "Error" + TxReasonRetryExhausted = "RetryExhausted" + + // Reasons for TxConditionFinished=True. + TxReasonAccepted = "Accepted" + TxReasonRejected = "Rejected" + TxReasonSuperseded = "Superseded" +) + +// CommittedResourceTransactionSpec defines the desired change for a CommittedResourceTransaction. +type CommittedResourceTransactionSpec struct { + // CorrelationID is the request/correlation ID. + // +kubebuilder:validation:Optional + CorrelationID string `json:"correlationID,omitempty"` + + // AvailabilityZone is the availability zone this transaction is scoped to. + // +kubebuilder:validation:Required + // +kubebuilder:validation:MinLength=1 + AvailabilityZone string `json:"availabilityZone"` + + // Source identifies who created this transaction. + // +kubebuilder:validation:Enum=api;syncer + // +kubebuilder:validation:Required + Source TxSource `json:"source"` + + // AllowRejection controls whether the transaction may be rejected and rolled back + // on placement failure. When false, the reconciler retries until Accepted or Superseded. + // +kubebuilder:validation:Optional + AllowRejection bool `json:"allowRejection,omitempty"` + + // DryRun controls whether the transaction commits or only probes schedulability. + // +kubebuilder:validation:Optional + DryRun bool `json:"dryRun,omitempty"` + + // NewState maps commitmentUUID to the desired CommittedResourceSpec. + // A nil value means the corresponding CommittedResource should be deleted. + // +kubebuilder:validation:Optional + NewState map[string]*CommittedResourceSpec `json:"newState,omitempty"` +} + +// CommittedResourceTransactionStatus defines the observed state of a CommittedResourceTransaction. +type CommittedResourceTransactionStatus struct { + // Phase is the current FSM phase of the transaction. + // +kubebuilder:validation:Optional + // +kubebuilder:validation:XValidation:rule="oldSelf == '' || !['Accepted','Rejected','Superseded'].exists(t, t == oldSelf) || self == oldSelf",message="phase cannot transition out of a terminal state" + Phase TxPhase `json:"phase,omitempty"` + + // SnapshotBefore records the CommittedResource spec state before this transaction. + // Key is commitmentUUID; nil value means the CR did not exist before (will be deleted on rollback). + // Populated in Preparing when AllowRejection=true. + // +kubebuilder:validation:Optional + SnapshotBefore map[string]*CommittedResourceSpec `json:"snapshotBefore,omitempty"` + + // UpsertedCommitments holds references to CommittedResource CRDs written by this transaction. + // Watched in AwaitingScheduling to gate on placement success. + // These are plain object references, not owner references. + // +kubebuilder:validation:Optional + UpsertedCommitments []corev1.ObjectReference `json:"upsertedCommitments,omitempty"` + + // Conditions holds Progressing and Finished conditions. + // +kubebuilder:validation:Optional + Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"` + + // Message is a human-readable description of the current state or terminal outcome. + // +kubebuilder:validation:Optional + Message string `json:"message,omitempty"` +} + +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:resource:scope=Cluster +// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase" +// +kubebuilder:printcolumn:name="AZ",type="string",JSONPath=".spec.availabilityZone" +// +kubebuilder:printcolumn:name="Source",type="string",JSONPath=".spec.source" +// +kubebuilder:printcolumn:name="DryRun",type="boolean",JSONPath=".spec.dryRun",priority=1 +// +kubebuilder:printcolumn:name="Progressing",type="string",JSONPath=".status.conditions[?(@.type=='Progressing')].status",priority=1 +// +kubebuilder:printcolumn:name="Finished",type="string",JSONPath=".status.conditions[?(@.type=='Finished')].status",priority=1 + +// CommittedResourceTransaction is the Schema for the committedresourcetransactions API +type CommittedResourceTransaction struct { + metav1.TypeMeta `json:",inline"` + + // +optional + metav1.ObjectMeta `json:"metadata,omitempty,omitzero"` + + // +required + Spec CommittedResourceTransactionSpec `json:"spec"` + + // +optional + Status CommittedResourceTransactionStatus `json:"status,omitempty,omitzero"` +} + +// IsTerminal reports whether the transaction has reached a terminal phase. +func (tx *CommittedResourceTransaction) IsTerminal() bool { + return tx.Status.Phase == TxPhaseAccepted || + tx.Status.Phase == TxPhaseRejected || + tx.Status.Phase == TxPhaseSuperseded +} + +// +kubebuilder:object:root=true + +// CommittedResourceTransactionList contains a list of CommittedResourceTransaction +type CommittedResourceTransactionList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []CommittedResourceTransaction `json:"items"` +} + +func init() { + SchemeBuilder.Register(&CommittedResourceTransaction{}, &CommittedResourceTransactionList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index c4f4115ff..6f9db9cd1 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -9,9 +9,9 @@ package v1alpha1 import ( apiv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1" - corev1 "k8s.io/api/core/v1" + "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" - "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ) @@ -223,7 +223,7 @@ func (in *CommittedResourceStatus) DeepCopyInto(out *CommittedResourceStatus) { } if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -240,6 +240,139 @@ func (in *CommittedResourceStatus) DeepCopy() *CommittedResourceStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceTransaction) DeepCopyInto(out *CommittedResourceTransaction) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransaction. +func (in *CommittedResourceTransaction) DeepCopy() *CommittedResourceTransaction { + if in == nil { + return nil + } + out := new(CommittedResourceTransaction) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CommittedResourceTransaction) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceTransactionList) DeepCopyInto(out *CommittedResourceTransactionList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]CommittedResourceTransaction, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransactionList. +func (in *CommittedResourceTransactionList) DeepCopy() *CommittedResourceTransactionList { + if in == nil { + return nil + } + out := new(CommittedResourceTransactionList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CommittedResourceTransactionList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceTransactionSpec) DeepCopyInto(out *CommittedResourceTransactionSpec) { + *out = *in + if in.NewState != nil { + in, out := &in.NewState, &out.NewState + *out = make(map[string]*CommittedResourceSpec, len(*in)) + for key, val := range *in { + var outVal *CommittedResourceSpec + if val == nil { + (*out)[key] = nil + } else { + inVal := (*in)[key] + in, out := &inVal, &outVal + *out = new(CommittedResourceSpec) + (*in).DeepCopyInto(*out) + } + (*out)[key] = outVal + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransactionSpec. +func (in *CommittedResourceTransactionSpec) DeepCopy() *CommittedResourceTransactionSpec { + if in == nil { + return nil + } + out := new(CommittedResourceTransactionSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceTransactionStatus) DeepCopyInto(out *CommittedResourceTransactionStatus) { + *out = *in + if in.SnapshotBefore != nil { + in, out := &in.SnapshotBefore, &out.SnapshotBefore + *out = make(map[string]*CommittedResourceSpec, len(*in)) + for key, val := range *in { + var outVal *CommittedResourceSpec + if val == nil { + (*out)[key] = nil + } else { + inVal := (*in)[key] + in, out := &inVal, &outVal + *out = new(CommittedResourceSpec) + (*in).DeepCopyInto(*out) + } + (*out)[key] = outVal + } + } + if in.UpsertedCommitments != nil { + in, out := &in.UpsertedCommitments, &out.UpsertedCommitments + *out = make([]v1.ObjectReference, len(*in)) + copy(*out, *in) + } + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]metav1.Condition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransactionStatus. +func (in *CommittedResourceTransactionStatus) DeepCopy() *CommittedResourceTransactionStatus { + if in == nil { + return nil + } + out := new(CommittedResourceTransactionStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Datasource) DeepCopyInto(out *Datasource) { *out = *in @@ -307,7 +440,7 @@ func (in *DatasourceSpec) DeepCopyInto(out *DatasourceSpec) { out.DatabaseSecretRef = in.DatabaseSecretRef if in.SSOSecretRef != nil { in, out := &in.SSOSecretRef, &out.SSOSecretRef - *out = new(corev1.SecretReference) + *out = new(v1.SecretReference) **out = **in } } @@ -334,7 +467,7 @@ func (in *DatasourceStatus) DeepCopyInto(out *DatasourceStatus) { in.NextSyncTime.DeepCopyInto(&out.NextSyncTime) if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -484,12 +617,12 @@ func (in *DecisionSpec) DeepCopyInto(out *DecisionSpec) { } if in.MachineRef != nil { in, out := &in.MachineRef, &out.MachineRef - *out = new(corev1.ObjectReference) + *out = new(v1.ObjectReference) **out = **in } if in.PodRef != nil { in, out := &in.PodRef, &out.PodRef - *out = new(corev1.ObjectReference) + *out = new(v1.ObjectReference) **out = **in } } @@ -514,10 +647,10 @@ func (in *DecisionStatus) DeepCopyInto(out *DecisionStatus) { } if in.History != nil { in, out := &in.History, &out.History - *out = new([]corev1.ObjectReference) + *out = new([]v1.ObjectReference) if **in != nil { in, out := *in, *out - *out = make([]corev1.ObjectReference, len(*in)) + *out = make([]v1.ObjectReference, len(*in)) copy(*out, *in) } } @@ -528,7 +661,7 @@ func (in *DecisionStatus) DeepCopyInto(out *DecisionStatus) { } if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -624,7 +757,7 @@ func (in *DeschedulingStatus) DeepCopyInto(out *DeschedulingStatus) { *out = *in if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -914,7 +1047,7 @@ func (in *FlavorGroupCapacityStatus) DeepCopyInto(out *FlavorGroupCapacityStatus in.LastReconcileAt.DeepCopyInto(&out.LastReconcileAt) if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -1008,12 +1141,12 @@ func (in *KPIDependenciesSpec) DeepCopyInto(out *KPIDependenciesSpec) { *out = *in if in.Datasources != nil { in, out := &in.Datasources, &out.Datasources - *out = make([]corev1.ObjectReference, len(*in)) + *out = make([]v1.ObjectReference, len(*in)) copy(*out, *in) } if in.Knowledges != nil { in, out := &in.Knowledges, &out.Knowledges - *out = make([]corev1.ObjectReference, len(*in)) + *out = make([]v1.ObjectReference, len(*in)) copy(*out, *in) } } @@ -1082,7 +1215,7 @@ func (in *KPIStatus) DeepCopyInto(out *KPIStatus) { *out = *in if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -1131,12 +1264,12 @@ func (in *KnowledgeDependenciesSpec) DeepCopyInto(out *KnowledgeDependenciesSpec *out = *in if in.Datasources != nil { in, out := &in.Datasources, &out.Datasources - *out = make([]corev1.ObjectReference, len(*in)) + *out = make([]v1.ObjectReference, len(*in)) copy(*out, *in) } if in.Knowledges != nil { in, out := &in.Knowledges, &out.Knowledges - *out = make([]corev1.ObjectReference, len(*in)) + *out = make([]v1.ObjectReference, len(*in)) copy(*out, *in) } } @@ -1225,7 +1358,7 @@ func (in *KnowledgeStatus) DeepCopyInto(out *KnowledgeStatus) { in.Raw.DeepCopyInto(&out.Raw) if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -1491,7 +1624,7 @@ func (in *PipelineStatus) DeepCopyInto(out *PipelineStatus) { *out = *in if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -1631,7 +1764,7 @@ func (in *ProjectQuotaStatus) DeepCopyInto(out *ProjectQuotaStatus) { } if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -1781,7 +1914,7 @@ func (in *ReservationStatus) DeepCopyInto(out *ReservationStatus) { *out = *in if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) + *out = make([]metav1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } diff --git a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md new file mode 100644 index 000000000..7280a3219 --- /dev/null +++ b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md @@ -0,0 +1,127 @@ + + +# Committed-resource transaction architecture (target) + +## Components + +| Component | Kind | Owned by | +|-----------|------|----------| +| `CommittedResourceTransaction` (CRT) | CRD | Spec: created by API handler or syncer; Status: CRT reconciler | +| `CommittedResource` (CR) | CRD | Spec: CRT reconciler (sole writer); Status: CR controller | +| `Reservation` | CRD | Spec: CR controller (sole writer); Status: Reservation controller | +| Change-commitments API | HTTP handler | Creates CRT with `source=api` | +| Limes syncer | Background poller | Creates CRT with `source=syncer` on each diff | +| CRT reconciler | Controller | Drives CRT FSM; writes CR specs; rolls back on failure | +| CR controller | Controller | Derives Reservation slots from CR spec; reports CR readiness | +| Reservation controller | Controller | Attempts host placement; reports slot readiness | + +## Ownership + +```mermaid +flowchart TD + API["API handler\nsource=api"] -->|creates| CRT["CommittedResourceTransaction"] + Syncer["Limes syncer\nsource=syncer"] -->|creates on diff| CRT + + CRT -->|"CRT reconciler — writes spec"| CR["CommittedResource"] + CR -->|"CR controller — writes spec"| Res["Reservation"] + Res -->|"Reservation controller — tracks placement"| VM["VM / scheduler\nlive-migration"] + + CR -.->|"CR controller reports readiness\nCRT reconciler gates on this"| CRT +``` + +No controller writes its own spec. Spec flows down; status flows up. + +## Component interaction + +The three reconcilers operate independently and communicate only through CRD status conditions. + +```mermaid +sequenceDiagram + participant W as API handler or Syncer + participant CRT as CRT reconciler + participant CRCRD as CommittedResource CRD + participant CRCtl as CR controller + participant ResCRD as Reservation CRD + participant ResCtl as Reservation controller + + W->>CRCRD: create CommittedResourceTransaction + CRT->>CRCRD: write CR.spec (Allocating phase) + Note over CRT,CRCRD: CRT reconciler is sole writer of CR.spec + CRCtl->>ResCRD: reconcile Reservation slots from CR.spec + Note over CRCtl,ResCRD: CR controller is sole writer of Reservation.spec + ResCtl->>ResCRD: attempt placement, write Reservation.status + Note over ResCtl: reacts to VM moves, live-migration, in-flight actions + CRCtl->>CRCRD: aggregate slot readiness, write CR.status + CRT->>CRCRD: read CR.status.Ready + observedGeneration, advance to Accepted or RollingBack +``` + +## CRT state machine + +```mermaid +stateDiagram-v2 + [*] --> Preparing : CRT created by API handler or syncer + + Preparing --> Allocating : validation ok;\nAPI mode: snapshot taken if rejectable;\nSyncer mode: no snapshot + Preparing --> Rejected : validation failed + + Allocating --> AwaitingScheduling : API mode — CR specs written,\nupserted refs recorded + Allocating --> Accepted : Syncer mode — apply-and-done,\nno scheduling gate + Allocating --> Superseded : non-rejectable, newer CRT\nfor same AZ took over + + AwaitingScheduling --> Accepted : all CRs report Ready\n(observedGeneration matches) + AwaitingScheduling --> DryRunCleanup : all CRs Ready, but DryRun=true + AwaitingScheduling --> RollingBack : any CR rejected\nor timeout + + DryRunCleanup --> Accepted : pre-state restored\n(would-accept result) + + RollingBack --> Rejected : pre-state restored + RollingBack --> Rejected : cleanup failed permanently\n(reason: RollbackFailed) + + Accepted --> [*] + Rejected --> [*] + Superseded --> [*] +``` + +Terminal phases (`Accepted`, `Rejected`, `Superseded`) set `Finished=True`. Non-terminal problems set +`Progressing=False` with a reason (`Stalled`, `TimedOut`, `Error`, `RetryExhausted`) without advancing +the phase. A stuck non-terminal CRT triggers an alert; after a threshold it is forcibly moved to +`Superseded` so its AZ is unblocked and the syncer repairs state on the next poll. + +## Request flows + +### API-mode flow + +1. **API handler** receives a change-commitments request from Limes; creates a CRT (`source=api`, `allowRejection=true`, optional `dryRun=true`). +2. **CRT reconciler — Preparing**: validates the request; snapshots current CR specs as the rollback journal. +3. **CRT reconciler — Allocating**: writes the desired CR specs atomically; records which CRs were touched. +4. **CRT reconciler — AwaitingScheduling**: watches the touched CRs; waits until each reports its ready condition against the generation just written. +5. **CR controller**: for each CR, derives and reconciles Reservation slots; reports `Ready` once all slots are placed. +6. **Reservation controller**: places each slot on a host; reports slot readiness. +7. **CRT reconciler**: all CRs ready → `Accepted`; any CR rejected or timeout → rolls all CR specs back to the snapshot → `Rejected`. +8. **API handler** polls the CRT by name; maps terminal phase to HTTP response. + +### Syncer-mode flow + +1. **Limes syncer** polls Limes per project; diffs desired state against current CRs; on any delta creates a CRT (`source=syncer`, `allowRejection=false`). +2. **CRT reconciler — Preparing**: no snapshot (non-rejectable; syncer repairs on next poll if anything drifts). +3. **CRT reconciler — Allocating**: recency check — skips changes for any project another CRT touched in the same AZ in the last ~10 minutes (prevents clobbering an in-flight API change); writes CR specs; advances directly to `Accepted`. +4. **CR controller**: reconciles Reservation slots async; CRT is already terminal. +5. Slots converge in the background; the next syncer poll repairs any remaining drift. + +## Key invariants + +- **Single writer per spec layer.** The CRT reconciler is the only component that writes `CommittedResource.spec`. The CR controller is the only component that writes `Reservation.spec`. No controller writes its own spec. +- **AwaitingScheduling gates on generation.** The CRT reconciler advances from `AwaitingScheduling` only when a CR's ready condition carries an `observedGeneration` that matches the generation the CRT reconciler wrote — not an older reconcile cycle's result. +- **Rollback is durable.** The pre-change snapshot is stored in `CRT.status` before any CR spec is touched. A process restart resumes from the persisted phase and snapshot. +- **Syncer-mode never rolls back.** Syncer CRTs are non-rejectable: on any error they retry until `Accepted` or are superseded by a newer CRT. The syncer repairs drift on its next poll. +- **At most one active CRT per AZ.** The CRT reconciler serialises with `MaxConcurrentReconciles=1` plus an in-memory per-AZ active-transaction map (rebuilt from CRDs on startup). Younger CRTs requeue until the active one reaches a terminal phase. +- **Syncer recency guard prevents clobbering.** A syncer CRT skips changes to any project that an API CRT touched in the same AZ within the last ~10 minutes — the API change is authoritative; the syncer will reconcile remaining drift on its next poll. +- **Terminal CRTs are retained for auditability.** `Accepted` CRTs are kept for ~1 hour; `Rejected` CRTs for ~5 days. Non-terminal CRTs (including stuck `RollingBack`) are never GC'd — deleting one loses the rollback journal and the AZ active-marker. + +--- + +[Prev: CR lifecycle internals](03-a-cr-lifecycle-internals.md) · [Next: Failover reservations for KVM HA »](03-failover-reservations.md) diff --git a/helm/bundles/cortex-nova/templates/kpis_kvm.yaml b/helm/bundles/cortex-nova/templates/kpis_kvm.yaml index 7fc4f0223..664c39a4b 100644 --- a/helm/bundles/cortex-nova/templates/kpis_kvm.yaml +++ b/helm/bundles/cortex-nova/templates/kpis_kvm.yaml @@ -52,6 +52,7 @@ spec: gvks: - cortex.cloud/v1alpha1/ReservationList - cortex.cloud/v1alpha1/CommittedResourceList + - cortex.cloud/v1alpha1/CommittedResourceTransactionList - kvm.cloud.sap/v1/HypervisorList - cobaltcore.cloud/v1alpha1/VirtualMachineList description: | diff --git a/helm/bundles/cortex-nova/values.yaml b/helm/bundles/cortex-nova/values.yaml index 28b643566..6edf499da 100644 --- a/helm/bundles/cortex-nova/values.yaml +++ b/helm/bundles/cortex-nova/values.yaml @@ -137,6 +137,8 @@ cortex: &cortex - cortex.cloud/v1alpha1/ReservationList - cortex.cloud/v1alpha1/CommittedResource - cortex.cloud/v1alpha1/CommittedResourceList + - cortex.cloud/v1alpha1/CommittedResourceTransaction + - cortex.cloud/v1alpha1/CommittedResourceTransactionList - cortex.cloud/v1alpha1/ProjectQuota - cortex.cloud/v1alpha1/ProjectQuotaList - cortex.cloud/v1alpha1/FlavorGroupCapacity @@ -159,6 +161,7 @@ cortex: &cortex gvks: - cortex.cloud/v1alpha1/Reservation - cortex.cloud/v1alpha1/CommittedResource + - cortex.cloud/v1alpha1/CommittedResourceTransaction monitoring: labels: &cortexMonitoringLabels github_org: cobaltcore-dev diff --git a/helm/library/cortex/files/crds/cortex.cloud_committedresourcetransactions.yaml b/helm/library/cortex/files/crds/cortex.cloud_committedresourcetransactions.yaml new file mode 100644 index 000000000..b19b4662c --- /dev/null +++ b/helm/library/cortex/files/crds/cortex.cloud_committedresourcetransactions.yaml @@ -0,0 +1,419 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.23.0 + name: committedresourcetransactions.cortex.cloud +spec: + group: cortex.cloud + names: + kind: CommittedResourceTransaction + listKind: CommittedResourceTransactionList + plural: committedresourcetransactions + singular: committedresourcetransaction + scope: Cluster + versions: + - additionalPrinterColumns: + - jsonPath: .status.phase + name: Phase + type: string + - jsonPath: .spec.availabilityZone + name: AZ + type: string + - jsonPath: .spec.source + name: Source + type: string + - jsonPath: .spec.dryRun + name: DryRun + priority: 1 + type: boolean + - jsonPath: .status.conditions[?(@.type=='Progressing')].status + name: Progressing + priority: 1 + type: string + - jsonPath: .status.conditions[?(@.type=='Finished')].status + name: Finished + priority: 1 + type: string + name: v1alpha1 + schema: + openAPIV3Schema: + description: CommittedResourceTransaction is the Schema for the committedresourcetransactions + API + properties: + apiVersion: + description: |- + APIVersion defines the versioned schema of this representation of an object. + Servers should convert recognized schemas to the latest internal value, and + may reject unrecognized values. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + type: string + kind: + description: |- + Kind is a string value representing the REST resource this object represents. + Servers may infer this from the endpoint the client submits requests to. + Cannot be updated. + In CamelCase. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + type: string + metadata: + type: object + spec: + description: CommittedResourceTransactionSpec defines the desired change + for a CommittedResourceTransaction. + properties: + allowRejection: + description: |- + AllowRejection controls whether the transaction may be rejected and rolled back + on placement failure. When false, the reconciler retries until Accepted or Superseded. + type: boolean + availabilityZone: + description: AvailabilityZone is the availability zone this transaction + is scoped to. + minLength: 1 + type: string + correlationID: + description: CorrelationID is the request/correlation ID. + type: string + dryRun: + description: DryRun controls whether the transaction commits or only + probes schedulability. + type: boolean + newState: + additionalProperties: + description: CommittedResourceSpec defines the desired state of + CommittedResource, + properties: + allowRejection: + description: |- + AllowRejection controls what the CommittedResource controller does when placement fails + for a guaranteed or confirmed commitment. + true — controller may reject: on failure, child Reservations are rolled back and the CR + is marked Rejected. Use this when the caller is making a first-time placement + decision and a "no" answer is acceptable (e.g. the change-commitments API). + false — controller must retry: on failure, existing child Reservations are kept and the + CR is set to Reserving so the controller retries later. Use this when the caller + is restoring already-committed state that Cortex must honour (e.g. the syncer). + Only meaningful for state=guaranteed or state=confirmed; ignored for all other states. + type: boolean + amount: + anyOf: + - type: integer + - type: string + description: |- + Amount is the total committed quantity. + memory: MiB expressed in K8s binary SI notation (e.g. "1280Gi", "640Mi"). + cores: integer core count (e.g. "40"). + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + availabilityZone: + description: AvailabilityZone specifies the availability zone + for this commitment. + type: string + commitmentUUID: + description: UUID of the commitment this resource corresponds + to. + type: string + confirmedAt: + description: ConfirmedAt is when the commitment was confirmed. + format: date-time + type: string + domainID: + description: DomainID of the OpenStack domain this commitment + belongs to. + type: string + endTime: + description: EndTime is when Reservation slots expire. Nil for + unbounded commitments with no expiry. + format: date-time + type: string + flavorGroupName: + description: FlavorGroupName identifies the flavor group this + commitment targets, e.g. "kvm_v2_hana_s". + type: string + projectID: + description: ProjectID of the OpenStack project this commitment + belongs to. + type: string + resourceType: + description: 'ResourceType identifies the kind of resource committed: + memory drives Reservation slots; cores uses an arithmetic + check only.' + enum: + - memory + - cores + type: string + schedulingDomain: + description: SchedulingDomain specifies the scheduling domain + for this committed resource (e.g., "nova", "ironcore"). + type: string + startTime: + description: |- + StartTime is the activation time for Reservation slots. + Nil for guaranteed commitments (slots are active from creation); set to ConfirmedAt for confirmed ones. + format: date-time + type: string + state: + description: State is the lifecycle state of the commitment. + enum: + - planned + - pending + - guaranteed + - confirmed + - superseded + - expired + type: string + required: + - amount + - availabilityZone + - commitmentUUID + - domainID + - flavorGroupName + - projectID + - resourceType + - state + type: object + description: |- + NewState maps commitmentUUID to the desired CommittedResourceSpec. + A nil value means the corresponding CommittedResource should be deleted. + type: object + source: + description: Source identifies who created this transaction. + enum: + - api + - syncer + type: string + required: + - availabilityZone + - source + type: object + status: + description: CommittedResourceTransactionStatus defines the observed state + of a CommittedResourceTransaction. + properties: + conditions: + description: Conditions holds Progressing and Finished conditions. + items: + description: Condition contains details for one aspect of the current + state of this API Resource. + properties: + lastTransitionTime: + description: |- + lastTransitionTime is the last time the condition transitioned from one status to another. + This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: |- + message is a human readable message indicating details about the transition. + This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: |- + observedGeneration represents the .metadata.generation that the condition was set based upon. + For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date + with respect to the current state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: |- + reason contains a programmatic identifier indicating the reason for the condition's last transition. + Producers of specific condition types may define expected values and meanings for this field, + and whether the values are considered a guaranteed API. + The value should be a CamelCase string. + This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + message: + description: Message is a human-readable description of the current + state or terminal outcome. + type: string + phase: + description: Phase is the current FSM phase of the transaction. + type: string + x-kubernetes-validations: + - message: phase cannot transition out of a terminal state + rule: oldSelf == '' || !['Accepted','Rejected','Superseded'].exists(t, + t == oldSelf) || self == oldSelf + snapshotBefore: + additionalProperties: + description: CommittedResourceSpec defines the desired state of + CommittedResource, + properties: + allowRejection: + description: |- + AllowRejection controls what the CommittedResource controller does when placement fails + for a guaranteed or confirmed commitment. + true — controller may reject: on failure, child Reservations are rolled back and the CR + is marked Rejected. Use this when the caller is making a first-time placement + decision and a "no" answer is acceptable (e.g. the change-commitments API). + false — controller must retry: on failure, existing child Reservations are kept and the + CR is set to Reserving so the controller retries later. Use this when the caller + is restoring already-committed state that Cortex must honour (e.g. the syncer). + Only meaningful for state=guaranteed or state=confirmed; ignored for all other states. + type: boolean + amount: + anyOf: + - type: integer + - type: string + description: |- + Amount is the total committed quantity. + memory: MiB expressed in K8s binary SI notation (e.g. "1280Gi", "640Mi"). + cores: integer core count (e.g. "40"). + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + availabilityZone: + description: AvailabilityZone specifies the availability zone + for this commitment. + type: string + commitmentUUID: + description: UUID of the commitment this resource corresponds + to. + type: string + confirmedAt: + description: ConfirmedAt is when the commitment was confirmed. + format: date-time + type: string + domainID: + description: DomainID of the OpenStack domain this commitment + belongs to. + type: string + endTime: + description: EndTime is when Reservation slots expire. Nil for + unbounded commitments with no expiry. + format: date-time + type: string + flavorGroupName: + description: FlavorGroupName identifies the flavor group this + commitment targets, e.g. "kvm_v2_hana_s". + type: string + projectID: + description: ProjectID of the OpenStack project this commitment + belongs to. + type: string + resourceType: + description: 'ResourceType identifies the kind of resource committed: + memory drives Reservation slots; cores uses an arithmetic + check only.' + enum: + - memory + - cores + type: string + schedulingDomain: + description: SchedulingDomain specifies the scheduling domain + for this committed resource (e.g., "nova", "ironcore"). + type: string + startTime: + description: |- + StartTime is the activation time for Reservation slots. + Nil for guaranteed commitments (slots are active from creation); set to ConfirmedAt for confirmed ones. + format: date-time + type: string + state: + description: State is the lifecycle state of the commitment. + enum: + - planned + - pending + - guaranteed + - confirmed + - superseded + - expired + type: string + required: + - amount + - availabilityZone + - commitmentUUID + - domainID + - flavorGroupName + - projectID + - resourceType + - state + type: object + description: |- + SnapshotBefore records the CommittedResource spec state before this transaction. + Key is commitmentUUID; nil value means the CR did not exist before (will be deleted on rollback). + Populated in Preparing when AllowRejection=true. + type: object + upsertedCommitments: + description: |- + UpsertedCommitments holds references to CommittedResource CRDs written by this transaction. + Watched in AwaitingScheduling to gate on placement success. + These are plain object references, not owner references. + items: + description: ObjectReference contains enough information to let + you inspect or modify the referred object. + properties: + apiVersion: + description: API version of the referent. + type: string + fieldPath: + description: |- + If referring to a piece of an object instead of an entire object, this string + should contain a valid JSON/Go field access statement, such as desiredState.manifest.containers[2]. + For example, if the object reference is to a container within a pod, this would take on a value like: + "spec.containers{name}" (where "name" refers to the name of the container that triggered + the event) or if no container name is specified "spec.containers[2]" (container with + index 2 in this pod). This syntax is chosen only to have some well-defined way of + referencing a part of an object. + type: string + kind: + description: |- + Kind of the referent. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + type: string + name: + description: |- + Name of the referent. + More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + type: string + namespace: + description: |- + Namespace of the referent. + More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/ + type: string + resourceVersion: + description: |- + Specific resourceVersion to which this reference is made, if any. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency + type: string + uid: + description: |- + UID of the referent. + More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids + type: string + type: object + x-kubernetes-map-type: atomic + type: array + type: object + required: + - spec + type: object + served: true + storage: true + subresources: + status: {} diff --git a/internal/scheduling/reservations/commitments/transactions/field_index.go b/internal/scheduling/reservations/commitments/transactions/field_index.go new file mode 100644 index 000000000..bfc516250 --- /dev/null +++ b/internal/scheduling/reservations/commitments/transactions/field_index.go @@ -0,0 +1,48 @@ +// Copyright SAP SE +// SPDX-License-Identifier: Apache-2.0 + +package transactions + +import ( + "context" + "errors" + "sync" + + "github.com/cobaltcore-dev/cortex/api/v1alpha1" + "github.com/cobaltcore-dev/cortex/pkg/multicluster" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +// idxCRTByUpsertedCR indexes CommittedResourceTransactions by each CR name in UpsertedCommitments. +// Used by crToTransaction to map a CR watch event back to its owning transaction. +const idxCRTByUpsertedCR = "status.upsertedCommitments.name" + +var onceIndexCRTByUpsertedCR sync.Once + +// indexCRTByUpsertedCR registers the field index on all clusters that serve CRT objects. +func indexCRTByUpsertedCR(ctx context.Context, mcl *multicluster.Client) (err error) { + onceIndexCRTByUpsertedCR.Do(func() { + log := ctrl.LoggerFrom(ctx) + err = mcl.IndexField(ctx, + &v1alpha1.CommittedResourceTransaction{}, + &v1alpha1.CommittedResourceTransactionList{}, + idxCRTByUpsertedCR, + func(obj client.Object) []string { + tx, ok := obj.(*v1alpha1.CommittedResourceTransaction) + if !ok { + log.Error(errors.New("unexpected type"), "expected CommittedResourceTransaction", "object", obj) + return nil + } + names := make([]string, 0, len(tx.Status.UpsertedCommitments)) + for _, ref := range tx.Status.UpsertedCommitments { + if ref.Name != "" { + names = append(names, ref.Name) + } + } + return names + }, + ) + }) + return err +} diff --git a/internal/scheduling/reservations/commitments/transactions/reconciler.go b/internal/scheduling/reservations/commitments/transactions/reconciler.go new file mode 100644 index 000000000..1e8ab0218 --- /dev/null +++ b/internal/scheduling/reservations/commitments/transactions/reconciler.go @@ -0,0 +1,343 @@ +// Copyright SAP SE +// SPDX-License-Identifier: Apache-2.0 + +package transactions + +import ( + "context" + "fmt" + "time" + + "github.com/go-logr/logr" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/handler" + + "github.com/cobaltcore-dev/cortex/api/v1alpha1" + "github.com/cobaltcore-dev/cortex/internal/scheduling/reservations/commitments" + "github.com/cobaltcore-dev/cortex/pkg/multicluster" +) + +// CommittedResourceTransactionReconciler reconciles CommittedResourceTransaction CRDs. +// It is the sole writer of CommittedResource.Spec; it reads CommittedResource.Status +// to drive its own FSM. It never writes CommittedResource.Status. +// +// TODO (#783): add perAZActive map[string]types.UID + mu sync.Mutex for per-AZ serialization. +type CommittedResourceTransactionReconciler struct { + client.Client + Scheme *runtime.Scheme + // AwaitingSchedulingTimeout is how long to wait in AwaitingScheduling before rolling back. + // Zero (default) means no timeout. + AwaitingSchedulingTimeout time.Duration +} + +func (r *CommittedResourceTransactionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + ctx = commitments.WithNewGlobalRequestID(ctx) + logger := commitments.LoggerFromContext(ctx).WithValues("committedResourceTransaction", req.Name) + + var tx v1alpha1.CommittedResourceTransaction + if err := r.Get(ctx, req.NamespacedName, &tx); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + if !tx.DeletionTimestamp.IsZero() { + return ctrl.Result{}, nil + } + + logger.V(1).Info("reconcile entry", "rv", tx.ResourceVersion, "gen", tx.Generation, "phase", tx.Status.Phase) + + switch tx.Status.Phase { + case "", v1alpha1.TxPhasePreparing: + return r.reconcilePreparing(ctx, logger, &tx) + case v1alpha1.TxPhaseAllocating: + return r.reconcileAllocating(ctx, logger, &tx) + case v1alpha1.TxPhaseAwaitingScheduling: + return r.reconcileAwaitingScheduling(ctx, logger, &tx) + case v1alpha1.TxPhaseRollingBack: + return r.reconcileRollingBack(ctx, logger, &tx) + case v1alpha1.TxPhaseDryRunCleanup: + return r.reconcileDryRunCleanup(ctx, logger, &tx) + case v1alpha1.TxPhaseAccepted, v1alpha1.TxPhaseRejected, v1alpha1.TxPhaseSuperseded: + return ctrl.Result{}, nil + default: + return ctrl.Result{}, fmt.Errorf("unknown phase %q", tx.Status.Phase) + } +} + +// commitmentName returns the CommittedResource name for a given commitment UUID. +func commitmentName(uuid string) string { return "commitment-" + uuid } + +// reconcilePreparing snapshots pre-change CR specs and advances to Allocating. +// Always snapshots regardless of AllowRejection — syncer-mode CRTs never roll back but the +// snapshot is cheap and keeps this method free of branching. +func (r *CommittedResourceTransactionReconciler) reconcilePreparing(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { + // Snapshot current CR specs for all keys in NewState so rollback can restore them. + // nil entry = CR did not exist before = delete it on rollback. + snapshot := make(map[string]*v1alpha1.CommittedResourceSpec, len(tx.Spec.NewState)) + for uuid := range tx.Spec.NewState { + var cr v1alpha1.CommittedResource + if err := r.Get(ctx, client.ObjectKey{Name: commitmentName(uuid)}, &cr); err != nil { + if client.IgnoreNotFound(err) == nil { + snapshot[uuid] = nil + continue + } + logger.Error(err, "failed to snapshot CR", "commitmentUUID", uuid, "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID) + r.markError(ctx, tx, fmt.Sprintf("failed to snapshot commitment-%s: %v", uuid, err)) + return ctrl.Result{}, err + } + spec := cr.Spec + snapshot[uuid] = &spec + } + + logger.Info("starting transaction", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "newStateSize", len(tx.Spec.NewState), "allowRejection", tx.Spec.AllowRejection) + return r.advanceTo(ctx, tx, v1alpha1.TxPhaseAllocating, func(s *v1alpha1.CommittedResourceTransactionStatus) { + s.SnapshotBefore = snapshot + }) +} + +// reconcileAllocating writes CommittedResource specs from NewState and records upserted refs. +// API-mode: advances to AwaitingScheduling. +// Syncer-mode: advances directly to Accepted (apply-and-done, no scheduling gate). +// +// UpsertedCommitments and the phase advance must happen in one patch (via the mutate function) +// so that a crash between CR spec writes and status persistence does not leave AwaitingScheduling +// with an empty UpsertedCommitments list — which would incorrectly advance to Accepted. +func (r *CommittedResourceTransactionReconciler) reconcileAllocating(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { + var upserted []corev1.ObjectReference + + for uuid, desiredSpec := range tx.Spec.NewState { + name := commitmentName(uuid) + if desiredSpec == nil { + toDelete := &v1alpha1.CommittedResource{ObjectMeta: metav1.ObjectMeta{Name: name}} + if err := r.Delete(ctx, toDelete); client.IgnoreNotFound(err) != nil { + logger.Error(err, "failed to delete CR", "commitmentUUID", uuid, "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID) + r.markError(ctx, tx, fmt.Sprintf("failed to delete commitment-%s: %v", uuid, err)) + return ctrl.Result{}, err + } + continue + } + + cr := &v1alpha1.CommittedResource{ObjectMeta: metav1.ObjectMeta{Name: name}} + if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, cr, func() error { + cr.Spec = *desiredSpec + return nil + }); err != nil { + logger.Error(err, "failed to upsert CR spec", "commitmentUUID", uuid, "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID) + r.markError(ctx, tx, fmt.Sprintf("failed to upsert commitment-%s: %v", uuid, err)) + return ctrl.Result{}, err + } + upserted = append(upserted, corev1.ObjectReference{ + APIVersion: v1alpha1.GroupVersion.String(), + Kind: "CommittedResource", + Name: name, + }) + } + + // TODO (#782): syncer-mode recency check before advancing + nextPhase := v1alpha1.TxPhaseAwaitingScheduling + if !tx.Spec.AllowRejection { + nextPhase = v1alpha1.TxPhaseAccepted + } + return r.advanceTo(ctx, tx, nextPhase, func(s *v1alpha1.CommittedResourceTransactionStatus) { + s.UpsertedCommitments = upserted + }) +} + +// reconcileAwaitingScheduling reads UpsertedCommitments and waits for placement settlement. +// Gates on each CR's ready-condition observedGeneration matching the generation written in Allocating. +// All CRs accepted → Accepted (or DryRunCleanup when DryRun=true). +// Any CR rejected → RollingBack. +// Not yet settled → wait for the CR watch to re-enqueue. +func (r *CommittedResourceTransactionReconciler) reconcileAwaitingScheduling(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { + allAccepted := true + acceptedCount := 0 + for _, ref := range tx.Status.UpsertedCommitments { + var cr v1alpha1.CommittedResource + if err := r.Get(ctx, client.ObjectKey{Name: ref.Name}, &cr); err != nil { + if client.IgnoreNotFound(err) == nil { + // CR briefly unavailable — wait for the next watch event. + allAccepted = false + continue + } + logger.Error(err, "failed to get upserted CR", "cr", ref.Name, "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID) + r.markError(ctx, tx, fmt.Sprintf("failed to get %s: %v", ref.Name, err)) + return ctrl.Result{}, err + } + + cond := meta.FindStatusCondition(cr.Status.Conditions, v1alpha1.CommittedResourceConditionReady) + if cond == nil || cond.ObservedGeneration != cr.Generation { + // CR controller hasn't caught up to the spec we wrote — wait. + allAccepted = false + continue + } + + switch { + case cond.Status == metav1.ConditionTrue && cond.Reason == v1alpha1.CommittedResourceReasonAccepted: + // CR accepted — placement succeeded; keep iterating. + acceptedCount++ + case cond.Status == metav1.ConditionFalse && cond.Reason == v1alpha1.CommittedResourceReasonRejected: + // Fail-fast: start rollback immediately on first rejection rather than waiting for all CRs + // to settle — this unblocks the AZ sooner and the first rejection message is sufficient for diagnosis. + logger.Info("CR rejected, rolling back", "cr", ref.Name, "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "message", cond.Message) + return r.advanceTo(ctx, tx, v1alpha1.TxPhaseRollingBack, func(s *v1alpha1.CommittedResourceTransactionStatus) { + s.Message = fmt.Sprintf("CR %s rejected: %s", ref.Name, cond.Message) + }) + default: + // Still transitioning — wait. + allAccepted = false + } + } + + if !allAccepted { + logger.V(1).Info("awaiting scheduling", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "ready", acceptedCount, "total", len(tx.Status.UpsertedCommitments)) + if r.AwaitingSchedulingTimeout > 0 { + progressingCond := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.TxConditionProgressing) + if progressingCond != nil && time.Since(progressingCond.LastTransitionTime.Time) > r.AwaitingSchedulingTimeout { + logger.Info("timed out waiting for scheduling, rolling back", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "timeout", r.AwaitingSchedulingTimeout) + return r.advanceTo(ctx, tx, v1alpha1.TxPhaseRollingBack, func(s *v1alpha1.CommittedResourceTransactionStatus) { + s.Message = "timed out waiting for scheduling" + meta.SetStatusCondition(&s.Conditions, metav1.Condition{ + Type: v1alpha1.TxConditionProgressing, + Status: metav1.ConditionFalse, + Reason: v1alpha1.TxReasonTimedOut, + Message: "timed out waiting for scheduling", + ObservedGeneration: tx.Generation, + }) + }) + } + } + // Primary requeue is event-driven via the CR watch (crToTransaction). + // RequeueAfter is a safety-net for missed watch events (reconnects, startup races). + return ctrl.Result{RequeueAfter: 5 * time.Second}, nil + } + + if tx.Spec.DryRun { + return r.advanceTo(ctx, tx, v1alpha1.TxPhaseDryRunCleanup, nil) + } + return r.advanceTo(ctx, tx, v1alpha1.TxPhaseAccepted, nil) +} + +// reconcileRollingBack converges affected CR specs back to the pre-change SnapshotBefore state. +// On restore complete: advances to Rejected. +// On permanent cleanup failure: advances to Rejected with reason RollbackFailed. +func (r *CommittedResourceTransactionReconciler) reconcileRollingBack(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { + // TODO (#780): for each entry in SnapshotBefore, converge CR spec back to snapshot value + // using SSA with fieldManager=txFieldManager. + // nil snapshot entry → CR did not exist before → delete it. + // Gate terminal transition on each CR confirming its spec reverted (check observedGeneration). + return ctrl.Result{}, nil +} + +// reconcileDryRunCleanup restores pre-change state after a dry-run would-accept outcome. +// Re-uses snapshot rollback logic; terminal phase is Accepted (not Rejected). +func (r *CommittedResourceTransactionReconciler) reconcileDryRunCleanup(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { + // TODO (#780): same convergence as reconcileRollingBack; terminal → advanceTo(Accepted, nil). + return ctrl.Result{}, nil +} + +// advanceTo updates the transaction status to the given phase. +// mutate is called (when non-nil) to allow phase advance and other status mutations +// (e.g. UpsertedCommitments, SnapshotBefore) to be persisted atomically in one Update call. +// Progressing is set to False on terminal phases to reflect that no further work is expected. +// +// Status().Update is used (not Patch) so that nil map values in SnapshotBefore and similar +// fields are preserved — JSON merge patch semantics treat null map values as deletions. +func (r *CommittedResourceTransactionReconciler) advanceTo( + ctx context.Context, + tx *v1alpha1.CommittedResourceTransaction, + phase v1alpha1.TxPhase, + mutate func(*v1alpha1.CommittedResourceTransactionStatus), +) (ctrl.Result, error) { + + tx.Status.Phase = phase + if mutate != nil { + mutate(&tx.Status) + } + progressingStatus, progressingReason := metav1.ConditionTrue, "Advancing" + if tx.IsTerminal() { + progressingStatus, progressingReason = metav1.ConditionFalse, string(phase) + meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ + Type: v1alpha1.TxConditionFinished, + Status: metav1.ConditionTrue, + Reason: string(phase), + ObservedGeneration: tx.Generation, + }) + } + // Preserve a Progressing=False condition already stamped by the mutate func (e.g. TimedOut). + // Terminal phases always overwrite to ensure Progressing=False/. + existingProgressing := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.TxConditionProgressing) + if tx.IsTerminal() || existingProgressing == nil || existingProgressing.Status != metav1.ConditionFalse { + meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ + Type: v1alpha1.TxConditionProgressing, + Status: progressingStatus, + Reason: progressingReason, + ObservedGeneration: tx.Generation, + }) + } + if err := r.Status().Update(ctx, tx); err != nil { + return ctrl.Result{}, err + } + return ctrl.Result{}, nil +} + +// markError stamps Progressing=False/Error on the transaction as a best-effort status update. +// The update error is logged but not returned — callers return the original error for requeue. +func (r *CommittedResourceTransactionReconciler) markError(ctx context.Context, tx *v1alpha1.CommittedResourceTransaction, msg string) { + meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ + Type: v1alpha1.TxConditionProgressing, + Status: metav1.ConditionFalse, + Reason: v1alpha1.TxReasonError, + Message: msg, + ObservedGeneration: tx.Generation, + }) + if err := r.Status().Update(ctx, tx); err != nil { + ctrl.LoggerFrom(ctx).Error(err, "failed to mark error condition") + } +} + +// SetupWithManager registers the controller and configures watches. +// TODO (#783): set MaxConcurrentReconciles: 1 via WithOptions(controller.Options{MaxConcurrentReconciles: 1}). +// TODO (#783): add predicate on WatchesMulticluster() to re-trigger same-AZ non-terminal siblings on terminal transition. +func (r *CommittedResourceTransactionReconciler) SetupWithManager(mgr ctrl.Manager, mcl *multicluster.Client) error { + ctx := context.Background() + if err := indexCRTByUpsertedCR(ctx, mcl); err != nil { + return fmt.Errorf("failed to set up CRT-by-upserted-CR field index: %w", err) + } + bldr := multicluster.BuildController(mcl, mgr) + var err error + bldr, err = bldr.WatchesMulticluster( + &v1alpha1.CommittedResourceTransaction{}, + &handler.EnqueueRequestForObject{}, + ) + if err != nil { + return err + } + // Re-enqueues this transaction when a CR it upserted settles placement. + bldr, err = bldr.WatchesMulticluster( + &v1alpha1.CommittedResource{}, + handler.EnqueueRequestsFromMapFunc(r.crToTransaction), + ) + if err != nil { + return err + } + return bldr.Named("committed-resource-transaction").Complete(r) +} + +// crToTransaction maps a CommittedResource watch event back to the transaction that upserted it. +func (r *CommittedResourceTransactionReconciler) crToTransaction(ctx context.Context, obj client.Object) []ctrl.Request { + var txList v1alpha1.CommittedResourceTransactionList + if err := r.List(ctx, &txList, client.MatchingFields{idxCRTByUpsertedCR: obj.GetName()}); err != nil { + commitments.LoggerFromContext(ctx).Error(err, "failed to list CRTs by upserted CR", "cr", obj.GetName()) + return nil + } + reqs := make([]ctrl.Request, 0, len(txList.Items)) + for i := range txList.Items { + reqs = append(reqs, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(&txList.Items[i])}) + } + return reqs +} diff --git a/internal/scheduling/reservations/commitments/transactions/reconciler_test.go b/internal/scheduling/reservations/commitments/transactions/reconciler_test.go new file mode 100644 index 000000000..b459f270b --- /dev/null +++ b/internal/scheduling/reservations/commitments/transactions/reconciler_test.go @@ -0,0 +1,1082 @@ +// Copyright SAP SE +// SPDX-License-Identifier: Apache-2.0 + +package transactions + +import ( + "context" + "strings" + "testing" + "time" + + apimeta "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + "github.com/cobaltcore-dev/cortex/api/v1alpha1" +) + +// ============================================================================ +// Helpers +// ============================================================================ + +var testCtx = context.Background() + +func testScheme(t *testing.T) *runtime.Scheme { + t.Helper() + s := runtime.NewScheme() + if err := v1alpha1.AddToScheme(s); err != nil { + t.Fatalf("add scheme: %v", err) + } + return s +} + +func testClient(t *testing.T, scheme *runtime.Scheme, objects ...client.Object) client.Client { + t.Helper() + return fake.NewClientBuilder(). + WithScheme(scheme). + WithObjects(objects...). + WithStatusSubresource( + &v1alpha1.CommittedResourceTransaction{}, + &v1alpha1.CommittedResource{}, + ). + WithIndex(&v1alpha1.CommittedResourceTransaction{}, idxCRTByUpsertedCR, func(obj client.Object) []string { + tx, ok := obj.(*v1alpha1.CommittedResourceTransaction) + if !ok { + return nil + } + names := make([]string, 0, len(tx.Status.UpsertedCommitments)) + for _, ref := range tx.Status.UpsertedCommitments { + if ref.Name != "" { + names = append(names, ref.Name) + } + } + return names + }). + Build() +} + +func testReconciler(c client.Client, scheme *runtime.Scheme) *CommittedResourceTransactionReconciler { + return &CommittedResourceTransactionReconciler{Client: c, Scheme: scheme} +} + +func doReconcile(t *testing.T, r *CommittedResourceTransactionReconciler, name string) ctrl.Result { + t.Helper() + res, err := r.Reconcile(testCtx, ctrl.Request{NamespacedName: types.NamespacedName{Name: name}}) + if err != nil { + t.Fatalf("Reconcile(%q): %v", name, err) + } + return res +} + +func getTx(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResourceTransaction { + t.Helper() + var tx v1alpha1.CommittedResourceTransaction + if err := c.Get(testCtx, types.NamespacedName{Name: name}, &tx); err != nil { + t.Fatalf("get CRT %q: %v", name, err) + } + return &tx +} + +func getCR(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResource { + t.Helper() + var cr v1alpha1.CommittedResource + if err := c.Get(testCtx, types.NamespacedName{Name: name}, &cr); err != nil { + t.Fatalf("get CR %q: %v", name, err) + } + return &cr +} + +// driveToPhase calls Reconcile until the CRT reaches target, up to 5 attempts. +func driveToPhase(t *testing.T, r *CommittedResourceTransactionReconciler, c client.Client, txName string, target v1alpha1.TxPhase) { + t.Helper() + const maxSteps = 5 + for range maxSteps { + if getTx(t, c, txName).Status.Phase == target { + return + } + doReconcile(t, r, txName) + } + if got := getTx(t, c, txName).Status.Phase; got != target { + t.Fatalf("driveToPhase: wanted %q, got %q after %d steps", target, got, maxSteps) + } +} + +// setCRReady simulates the CR controller writing a Ready condition using the CR's current Generation. +// + +func setCRReady(t *testing.T, c client.Client, crName string, status metav1.ConditionStatus, reason, message string) { + t.Helper() + cr := getCR(t, c, crName) + apimeta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{ + Type: v1alpha1.CommittedResourceConditionReady, + Status: status, + Reason: reason, + Message: message, + ObservedGeneration: cr.Generation, + LastTransitionTime: metav1.Now(), + }) + if err := c.Status().Update(testCtx, cr); err != nil { + t.Fatalf("update CR %q status: %v", crName, err) + } +} + +// setCRReadyStaleGen sets Ready=True/Accepted but with a stale ObservedGeneration (generation-1), +// so the reconciler's generation gate treats the condition as not yet caught up. +func setCRReadyStaleGen(t *testing.T, c client.Client, crName string) { + t.Helper() + cr := getCR(t, c, crName) + apimeta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{ + Type: v1alpha1.CommittedResourceConditionReady, + Status: metav1.ConditionTrue, + Reason: v1alpha1.CommittedResourceReasonAccepted, + ObservedGeneration: cr.Generation - 1, + LastTransitionTime: metav1.Now(), + }) + if err := c.Status().Update(testCtx, cr); err != nil { + t.Fatalf("update CR %q status (stale gen): %v", crName, err) + } +} + +// backdateProgressingCondition sets the Progressing condition's LastTransitionTime to the past. +func backdateProgressingCondition(t *testing.T, c client.Client, txName string, age time.Duration) { + t.Helper() + tx := getTx(t, c, txName) + for i, cond := range tx.Status.Conditions { + if cond.Type == v1alpha1.TxConditionProgressing { + tx.Status.Conditions[i].LastTransitionTime = metav1.NewTime(time.Now().Add(-age)) + break + } + } + if err := c.Status().Update(testCtx, tx); err != nil { + t.Fatalf("backdate Progressing condition on %q: %v", txName, err) + } +} + +// assertCondition verifies a condition in the given list by type, status, and optional reason. +// Pass an empty wantReason to skip the reason check. +func assertCondition(t *testing.T, conditions []metav1.Condition, condType string, wantStatus metav1.ConditionStatus, wantReason string) { + t.Helper() + cond := apimeta.FindStatusCondition(conditions, condType) + if cond == nil { + t.Errorf("condition %q not found in %v", condType, conditions) + return + } + if cond.Status != wantStatus { + t.Errorf("condition %q status: want %v, got %v", condType, wantStatus, cond.Status) + } + if wantReason != "" && cond.Reason != wantReason { + t.Errorf("condition %q reason: want %q, got %q", condType, wantReason, cond.Reason) + } +} + +// minimalCRSpec returns a minimal valid CommittedResourceSpec for testing. +func minimalCRSpec(az string) *v1alpha1.CommittedResourceSpec { + return &v1alpha1.CommittedResourceSpec{ + CommitmentUUID: "test-uuid", + FlavorGroupName: "kvm_v2", + ResourceType: v1alpha1.CommittedResourceTypeMemory, + Amount: resource.MustParse("1Gi"), + AvailabilityZone: az, + ProjectID: "project-1", + DomainID: "domain-1", + State: v1alpha1.CommitmentStatusConfirmed, + } +} + +// ============================================================================ +// TestCRTReconciler_Preparing +// ============================================================================ + +func TestCRTReconciler_Preparing(t *testing.T) { + tests := []struct { + name string + preObjects []client.Object + tx *v1alpha1.CommittedResourceTransaction + wantPhase v1alpha1.TxPhase + wantSnapshotNilFor []string // UUIDs that must have nil snapshot entry (CR did not exist) + wantSnapshotSetFor []string // UUIDs that must have non-nil snapshot entry (CR existed) + wantProgressingStatus metav1.ConditionStatus + wantProgressingReason string + }{ + { + name: "no pre-existing CR: snapshot entry is nil, phase advances to Allocating", + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-1"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-a": minimalCRSpec("az-1")}, + }, + }, + wantPhase: v1alpha1.TxPhaseAllocating, + wantSnapshotNilFor: []string{"uuid-a"}, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "pre-existing CR spec is captured in SnapshotBefore", + preObjects: []client.Object{ + &v1alpha1.CommittedResource{ + ObjectMeta: metav1.ObjectMeta{Name: "commitment-uuid-b"}, + Spec: *minimalCRSpec("az-1"), + }, + }, + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-2"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-b": minimalCRSpec("az-1")}, + }, + }, + wantPhase: v1alpha1.TxPhaseAllocating, + wantSnapshotSetFor: []string{"uuid-b"}, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "Preparing idempotent: running reconcile twice does not error", + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-3"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-c": minimalCRSpec("az-1")}, + }, + }, + wantPhase: v1alpha1.TxPhaseAllocating, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + scheme := testScheme(t) + objs := append([]client.Object{tt.tx}, tt.preObjects...) + c := testClient(t, scheme, objs...) + r := testReconciler(c, scheme) + + doReconcile(t, r, tt.tx.Name) + if tt.name == "Preparing idempotent: running reconcile twice does not error" { + // Reset to Preparing to verify a second run is also error-free. + stored := getTx(t, c, tt.tx.Name) + stored.Status.Phase = "" + if err := c.Status().Update(testCtx, stored); err != nil { + t.Fatalf("reset phase: %v", err) + } + doReconcile(t, r, tt.tx.Name) + } + + tx := getTx(t, c, tt.tx.Name) + if tx.Status.Phase != tt.wantPhase { + t.Errorf("phase: want %q, got %q", tt.wantPhase, tx.Status.Phase) + } + if tt.wantProgressingStatus != "" { + assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + } + for _, uuid := range tt.wantSnapshotNilFor { + snap, ok := tx.Status.SnapshotBefore[uuid] + if !ok { + t.Errorf("SnapshotBefore: missing key %q", uuid) + continue + } + if snap != nil { + t.Errorf("SnapshotBefore[%q]: want nil, got non-nil", uuid) + } + } + for _, uuid := range tt.wantSnapshotSetFor { + snap, ok := tx.Status.SnapshotBefore[uuid] + if !ok { + t.Errorf("SnapshotBefore: missing key %q", uuid) + continue + } + if snap == nil { + t.Errorf("SnapshotBefore[%q]: want non-nil snapshot, got nil", uuid) + } + } + }) + } +} + +// ============================================================================ +// TestCRTReconciler_Allocating +// ============================================================================ + +func TestCRTReconciler_Allocating(t *testing.T) { + tests := []struct { + name string + preObjects []client.Object + tx *v1alpha1.CommittedResourceTransaction + wantPhase v1alpha1.TxPhase + wantCRExists []string + wantCRAbsent []string + wantUpsertedCommitments int + wantProgressingStatus metav1.ConditionStatus + wantProgressingReason string + wantFinished bool + wantFinishedReason string + }{ + { + name: "API-mode: creates CR and advances to AwaitingScheduling", + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-1"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-1": minimalCRSpec("az-1")}, + }, + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantCRExists: []string{"commitment-uuid-1"}, + wantUpsertedCommitments: 1, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "syncer-mode: applies CR and advances directly to Accepted (no scheduling gate)", + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-2"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceSyncer, + AllowRejection: false, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-2": minimalCRSpec("az-1")}, + }, + }, + wantPhase: v1alpha1.TxPhaseAccepted, + wantCRExists: []string{"commitment-uuid-2"}, + wantUpsertedCommitments: 1, + wantProgressingStatus: metav1.ConditionFalse, + wantProgressingReason: string(v1alpha1.TxPhaseAccepted), + wantFinished: true, + wantFinishedReason: string(v1alpha1.TxPhaseAccepted), + }, + { + name: "nil spec deletes CR and it is absent from UpsertedCommitments", + preObjects: []client.Object{ + &v1alpha1.CommittedResource{ + ObjectMeta: metav1.ObjectMeta{Name: "commitment-uuid-3"}, + Spec: *minimalCRSpec("az-1"), + }, + }, + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-3"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-3": nil}, + }, + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantCRAbsent: []string{"commitment-uuid-3"}, + wantUpsertedCommitments: 0, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "pre-existing CR with different spec: spec is overwritten by CreateOrUpdate", + preObjects: []client.Object{ + &v1alpha1.CommittedResource{ + ObjectMeta: metav1.ObjectMeta{Name: "commitment-uuid-4"}, + Spec: *minimalCRSpec("az-old"), + }, + }, + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-4"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-4": minimalCRSpec("az-new")}, + }, + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantCRExists: []string{"commitment-uuid-4"}, + wantUpsertedCommitments: 1, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + scheme := testScheme(t) + objs := append([]client.Object{tt.tx}, tt.preObjects...) + c := testClient(t, scheme, objs...) + r := testReconciler(c, scheme) + + driveToPhase(t, r, c, tt.tx.Name, tt.wantPhase) + + tx := getTx(t, c, tt.tx.Name) + if tx.Status.Phase != tt.wantPhase { + t.Errorf("phase: want %q, got %q", tt.wantPhase, tx.Status.Phase) + } + if got := len(tx.Status.UpsertedCommitments); got != tt.wantUpsertedCommitments { + t.Errorf("UpsertedCommitments len: want %d, got %d", tt.wantUpsertedCommitments, got) + } + if tt.wantProgressingStatus != "" { + assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + } + if tt.wantFinished { + assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) + } + for _, name := range tt.wantCRExists { + getCR(t, c, name) // fatals if absent + } + for _, name := range tt.wantCRAbsent { + var cr v1alpha1.CommittedResource + if err := c.Get(testCtx, types.NamespacedName{Name: name}, &cr); err == nil { + t.Errorf("CR %q should be absent but exists", name) + } + } + }) + } +} + +// TestCRTReconciler_Allocating_Idempotent verifies that re-running Allocating with the same +// desired spec is idempotent: no error, phase advances correctly again. +func TestCRTReconciler_Allocating_Idempotent(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-idem"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem": minimalCRSpec("az-1")}, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + + // Reset to Allocating to re-run it. + stored := getTx(t, c, tx.Name) + stored.Status.Phase = v1alpha1.TxPhaseAllocating + if err := c.Status().Update(testCtx, stored); err != nil { + t.Fatalf("reset phase to Allocating: %v", err) + } + + doReconcile(t, r, tx.Name) // second run of Allocating + + final := getTx(t, c, tx.Name) + if final.Status.Phase != v1alpha1.TxPhaseAwaitingScheduling { + t.Errorf("idempotent Allocating: want AwaitingScheduling, got %q", final.Status.Phase) + } +} + +// ============================================================================ +// TestCRTReconciler_AwaitingScheduling +// ============================================================================ + +func TestCRTReconciler_AwaitingScheduling(t *testing.T) { + const txName = "tx-await" + const crName = "commitment-uuid-await" + + // setup drives a CRT to AwaitingScheduling and returns the client and reconciler. + setup := func(t *testing.T, txOpts ...func(*v1alpha1.CommittedResourceTransaction)) (client.Client, *CommittedResourceTransactionReconciler) { + t.Helper() + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: txName}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-await": minimalCRSpec("az-1")}, + }, + } + for _, opt := range txOpts { + opt(tx) + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + driveToPhase(t, r, c, txName, v1alpha1.TxPhaseAwaitingScheduling) + return c, r + } + + tests := []struct { + name string + txOpts []func(*v1alpha1.CommittedResourceTransaction) + setupCR func(t *testing.T, c client.Client) + setupTx func(t *testing.T, c client.Client) + reconcilerOpt func(r *CommittedResourceTransactionReconciler) + wantPhase v1alpha1.TxPhase + wantMessage string // substring that must appear in tx.Status.Message; empty = no check + wantProgressingStatus metav1.ConditionStatus + wantProgressingReason string + wantFinished bool + wantFinishedReason string + }{ + { + name: "all CRs accepted → Accepted", + setupCR: func(t *testing.T, c client.Client) { + setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") + }, + wantPhase: v1alpha1.TxPhaseAccepted, + wantProgressingStatus: metav1.ConditionFalse, + wantProgressingReason: string(v1alpha1.TxPhaseAccepted), + wantFinished: true, + wantFinishedReason: string(v1alpha1.TxPhaseAccepted), + }, + { + name: "DryRun=true and all CRs accepted → DryRunCleanup", + txOpts: []func(*v1alpha1.CommittedResourceTransaction){ + func(tx *v1alpha1.CommittedResourceTransaction) { tx.Spec.DryRun = true }, + }, + setupCR: func(t *testing.T, c client.Client) { + setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") + }, + wantPhase: v1alpha1.TxPhaseDryRunCleanup, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "CR rejected → RollingBack with rejection message", + setupCR: func(t *testing.T, c client.Client) { + setCRReady(t, c, crName, metav1.ConditionFalse, v1alpha1.CommittedResourceReasonRejected, "no capacity on host") + }, + wantPhase: v1alpha1.TxPhaseRollingBack, + wantMessage: "no capacity on host", + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "stale observedGeneration → stays in AwaitingScheduling", + setupCR: func(t *testing.T, c client.Client) { + setCRReadyStaleGen(t, c, crName) + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "ConditionTrue with non-Accepted reason → stays in AwaitingScheduling", + setupCR: func(t *testing.T, c client.Client) { + setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonReserving, "still placing") + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + { + name: "timeout exceeded → RollingBack with timed out message", + reconcilerOpt: func(r *CommittedResourceTransactionReconciler) { + r.AwaitingSchedulingTimeout = 50 * time.Millisecond + }, + setupTx: func(t *testing.T, c client.Client) { + backdateProgressingCondition(t, c, txName, 1*time.Second) + }, + wantPhase: v1alpha1.TxPhaseRollingBack, + wantMessage: "timed out", + wantProgressingStatus: metav1.ConditionFalse, + wantProgressingReason: v1alpha1.TxReasonTimedOut, + }, + { + name: "timeout=0 means no timeout: CRT stays in AwaitingScheduling indefinitely", + // No reconcilerOpt, so AwaitingSchedulingTimeout=0 (disabled). + // CR ready condition is never set → should stay in AwaitingScheduling. + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantProgressingStatus: metav1.ConditionTrue, + wantProgressingReason: "Advancing", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c, r := setup(t, tt.txOpts...) + + if tt.reconcilerOpt != nil { + tt.reconcilerOpt(r) + } + if tt.setupCR != nil { + tt.setupCR(t, c) + } + if tt.setupTx != nil { + tt.setupTx(t, c) + } + + doReconcile(t, r, txName) + + tx := getTx(t, c, txName) + if tx.Status.Phase != tt.wantPhase { + t.Errorf("phase: want %q, got %q", tt.wantPhase, tx.Status.Phase) + } + if tt.wantMessage != "" && !strings.Contains(tx.Status.Message, tt.wantMessage) { + t.Errorf("message: want substring %q in %q", tt.wantMessage, tx.Status.Message) + } + if tt.wantProgressingStatus != "" { + assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + } + if tt.wantFinished { + assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) + } + }) + } +} + +// setPhase directly sets a CRT's phase via Status().Update for test setup. +func setPhase(t *testing.T, c client.Client, txName string, phase v1alpha1.TxPhase) { + t.Helper() + tx := getTx(t, c, txName) + tx.Status.Phase = phase + if err := c.Status().Update(testCtx, tx); err != nil { + t.Fatalf("setPhase %q on %q: %v", phase, txName, err) + } +} + +// reconcileN calls doReconcile exactly n times without inspecting the phase between calls. +func reconcileN(t *testing.T, r *CommittedResourceTransactionReconciler, name string, n int) { + t.Helper() + for range n { + doReconcile(t, r, name) + } +} + +// ============================================================================ +// TestCRTReconciler_Idempotency +// ============================================================================ + +// TestCRTReconciler_Idempotency verifies that phase handlers are re-entrant: running the same +// handler multiple times (simulating crash-then-requeue) produces the same end state. +// The fake client never returns transient errors; these tests confirm re-entrancy — the property +// that makes controller-runtime's error-then-requeue pattern safe. +func TestCRTReconciler_Idempotency(t *testing.T) { + tests := []struct { + name string + run func(t *testing.T) + }{ + { + name: "Preparing: snapshot is consistent across re-runs", + run: func(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-1"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-1": minimalCRSpec("az-1")}, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + doReconcile(t, r, tx.Name) // Preparing → Allocating + snap1 := getTx(t, c, tx.Name).Status.SnapshotBefore + + setPhase(t, c, tx.Name, "") // reset to Preparing + doReconcile(t, r, tx.Name) // Preparing → Allocating (re-run) + snap2 := getTx(t, c, tx.Name).Status.SnapshotBefore + + for i, snap := range []map[string]*v1alpha1.CommittedResourceSpec{snap1, snap2} { + v, ok := snap["uuid-idem-1"] + if !ok { + t.Errorf("run %d: snapshot missing uuid-idem-1", i+1) + } else if v != nil { + t.Errorf("run %d: snapshot[uuid-idem-1]: want nil, got non-nil", i+1) + } + } + }, + }, + { + name: "Allocating: UpsertedCommitments and CR spec stable across re-runs", + run: func(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-2"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-2": minimalCRSpec("az-1")}, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + upserted1 := getTx(t, c, tx.Name).Status.UpsertedCommitments + + setPhase(t, c, tx.Name, v1alpha1.TxPhaseAllocating) // reset to Allocating + doReconcile(t, r, tx.Name) // Allocating → AwaitingScheduling (re-run) + upserted2 := getTx(t, c, tx.Name).Status.UpsertedCommitments + + if len(upserted1) != 1 || len(upserted2) != 1 { + t.Errorf("UpsertedCommitments: want len 1 on each run, got %d, %d", len(upserted1), len(upserted2)) + } + if len(upserted1) > 0 && len(upserted2) > 0 && upserted1[0].Name != upserted2[0].Name { + t.Errorf("UpsertedCommitments name changed: %q → %q", upserted1[0].Name, upserted2[0].Name) + } + getCR(t, c, "commitment-uuid-idem-2") // CR must still exist + }, + }, + { + name: "AwaitingScheduling: multiple calls without CR ready stay waiting", + run: func(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-3"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-3": minimalCRSpec("az-1")}, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + reconcileN(t, r, tx.Name, 3) // CR never set ready — must stay in AwaitingScheduling + + if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.TxPhaseAwaitingScheduling { + t.Errorf("phase: want AwaitingScheduling, got %q", got) + } + }, + }, + { + name: "AwaitingScheduling → Accepted stays Accepted on repeated reconciles", + run: func(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-4"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-4": minimalCRSpec("az-1")}, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + setCRReady(t, c, "commitment-uuid-idem-4", metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") + reconcileN(t, r, tx.Name, 3) // first call → Accepted; subsequent calls → terminal no-op + + if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.TxPhaseAccepted { + t.Errorf("phase: want Accepted, got %q", got) + } + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, tt.run) + } +} + +// ============================================================================ +// TestCRTReconciler_APIScenarios +// ============================================================================ + +// crExists returns true if a CommittedResource with the given name exists. +func crExists(t *testing.T, c client.Client, name string) bool { + t.Helper() + var cr v1alpha1.CommittedResource + err := c.Get(testCtx, types.NamespacedName{Name: name}, &cr) + if err == nil { + return true + } + if client.IgnoreNotFound(err) == nil { + return false + } + t.Fatalf("crExists(%q): %v", name, err) + return false +} + +// TestCRTReconciler_APIScenarios covers realistic multi-CR change patterns as seen from the +// CR acceptance API: mixed creates, updates, and deletes in a single transaction. +func TestCRTReconciler_APIScenarios(t *testing.T) { + tests := []struct { + name string + // pre-existing CRs before the transaction is created + preObjects []client.Object + // the CRT to create + tx *v1alpha1.CommittedResourceTransaction + // expected phase after driving the FSM to a stable point + wantPhase v1alpha1.TxPhase + // CR names that must exist after Allocating + wantCRExists []string + // CR names that must be absent after Allocating + wantCRAbsent []string + // exact UpsertedCommitments count (nils are not upserted) + wantUpsertedCount int + // UUIDs that must have nil snapshot (CR did not exist before) + wantSnapshotNilFor []string + // UUIDs that must have non-nil snapshot (CR existed before) + wantSnapshotSetFor []string + }{ + { + name: "multiple new CRs: all created, all upserted", + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-api-1"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{ + "uuid-a1": minimalCRSpec("az-1"), + "uuid-a2": minimalCRSpec("az-1"), + "uuid-a3": minimalCRSpec("az-1"), + }, + }, + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantCRExists: []string{"commitment-uuid-a1", "commitment-uuid-a2", "commitment-uuid-a3"}, + wantUpsertedCount: 3, + wantSnapshotNilFor: []string{"uuid-a1", "uuid-a2", "uuid-a3"}, + }, + { + name: "one new CR + one existing CR updated: both upserted, snapshot captures existing", + preObjects: []client.Object{ + &v1alpha1.CommittedResource{ + ObjectMeta: metav1.ObjectMeta{Name: "commitment-uuid-b2"}, + Spec: *minimalCRSpec("az-old"), + }, + }, + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-api-2"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{ + "uuid-b1": minimalCRSpec("az-1"), // new + "uuid-b2": minimalCRSpec("az-new"), // existing, spec updated + }, + }, + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantCRExists: []string{"commitment-uuid-b1", "commitment-uuid-b2"}, + wantUpsertedCount: 2, + wantSnapshotNilFor: []string{"uuid-b1"}, + wantSnapshotSetFor: []string{"uuid-b2"}, + }, + { + name: "one CR deleted + one CR updated: deleted absent, only updated upserted", + preObjects: []client.Object{ + &v1alpha1.CommittedResource{ + ObjectMeta: metav1.ObjectMeta{Name: "commitment-uuid-c1"}, + Spec: *minimalCRSpec("az-1"), + }, + &v1alpha1.CommittedResource{ + ObjectMeta: metav1.ObjectMeta{Name: "commitment-uuid-c2"}, + Spec: *minimalCRSpec("az-1"), + }, + }, + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-api-3"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{ + "uuid-c1": nil, // delete + "uuid-c2": minimalCRSpec("az-1"), // update + }, + }, + }, + wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantCRExists: []string{"commitment-uuid-c2"}, + wantCRAbsent: []string{"commitment-uuid-c1"}, + wantUpsertedCount: 1, + wantSnapshotSetFor: []string{"uuid-c1", "uuid-c2"}, + }, + { + name: "empty NewState: no CRs touched, advances all the way to Accepted", + tx: &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-api-4"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{}, + }, + }, + // AwaitingScheduling with empty UpsertedCommitments → allAccepted=true immediately → Accepted + wantPhase: v1alpha1.TxPhaseAccepted, + wantUpsertedCount: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + scheme := testScheme(t) + objs := append([]client.Object{tt.tx}, tt.preObjects...) + c := testClient(t, scheme, objs...) + r := testReconciler(c, scheme) + + driveToPhase(t, r, c, tt.tx.Name, tt.wantPhase) + + tx := getTx(t, c, tt.tx.Name) + if tx.Status.Phase != tt.wantPhase { + t.Errorf("phase: want %q, got %q", tt.wantPhase, tx.Status.Phase) + } + if got := len(tx.Status.UpsertedCommitments); got != tt.wantUpsertedCount { + t.Errorf("UpsertedCommitments count: want %d, got %d", tt.wantUpsertedCount, got) + } + for _, name := range tt.wantCRExists { + if !crExists(t, c, name) { + t.Errorf("CR %q should exist but is absent", name) + } + } + for _, name := range tt.wantCRAbsent { + if crExists(t, c, name) { + t.Errorf("CR %q should be absent but exists", name) + } + } + for _, uuid := range tt.wantSnapshotNilFor { + snap, ok := tx.Status.SnapshotBefore[uuid] + if !ok { + t.Errorf("SnapshotBefore: missing key %q", uuid) + } else if snap != nil { + t.Errorf("SnapshotBefore[%q]: want nil, got non-nil", uuid) + } + } + for _, uuid := range tt.wantSnapshotSetFor { + snap, ok := tx.Status.SnapshotBefore[uuid] + if !ok { + t.Errorf("SnapshotBefore: missing key %q", uuid) + } else if snap == nil { + t.Errorf("SnapshotBefore[%q]: want non-nil, got nil", uuid) + } + } + }) + } +} + +// ============================================================================ +// TestCRTReconciler_EdgeCases +// ============================================================================ + +// TestCRTReconciler_EdgeCases covers paths that don't fit the main FSM tables. +func TestCRTReconciler_EdgeCases(t *testing.T) { + t.Run("DeletionTimestamp: reconcile is a no-op", func(t *testing.T) { + scheme := testScheme(t) + now := metav1.Now() + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{ + Name: "tx-del", + DeletionTimestamp: &now, + // Finalizers required for the fake client to allow a non-zero DeletionTimestamp. + Finalizers: []string{"test.io/hold"}, + }, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + doReconcile(t, r, tx.Name) // must not error, must not advance phase + + got := getTx(t, c, tx.Name).Status.Phase + if got != "" { + t.Errorf("phase: want empty (no-op), got %q", got) + } + }) + + t.Run("unknown phase returns error", func(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-unknown"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + // Seed an unknown phase. + tx.Status.Phase = "Bogus" + if err := c.Status().Update(testCtx, tx); err != nil { + t.Fatalf("seed phase: %v", err) + } + + _, err := r.Reconcile(testCtx, ctrl.Request{NamespacedName: types.NamespacedName{Name: tx.Name}}) + if err == nil { + t.Error("expected error for unknown phase, got nil") + } + }) + + t.Run("crToTransaction maps CR name to owning CRT via field index", func(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-map"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + AllowRejection: true, + NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-map": minimalCRSpec("az-1")}, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + // Drive to AwaitingScheduling so UpsertedCommitments is populated. + driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + + // crToTransaction should return a request for the owning CRT. + cr := getCR(t, c, "commitment-uuid-map") + reqs := r.crToTransaction(testCtx, cr) + if len(reqs) != 1 { + t.Fatalf("crToTransaction: want 1 request, got %d", len(reqs)) + } + if reqs[0].Name != tx.Name { + t.Errorf("crToTransaction: want request for %q, got %q", tx.Name, reqs[0].Name) + } + }) +} + +// ============================================================================ +// TestCRTReconciler_Terminal +// ============================================================================ + +func TestCRTReconciler_Terminal(t *testing.T) { + tests := []struct { + name string + phase v1alpha1.TxPhase + }{ + {"Accepted is a no-op", v1alpha1.TxPhaseAccepted}, + {"Rejected is a no-op", v1alpha1.TxPhaseRejected}, + {"Superseded is a no-op", v1alpha1.TxPhaseSuperseded}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + scheme := testScheme(t) + tx := &v1alpha1.CommittedResourceTransaction{ + ObjectMeta: metav1.ObjectMeta{Name: "tx-term"}, + Spec: v1alpha1.CommittedResourceTransactionSpec{ + AvailabilityZone: "az-1", + Source: v1alpha1.TxSourceAPI, + }, + } + c := testClient(t, scheme, tx) + r := testReconciler(c, scheme) + + // Seed the terminal phase directly via Status().Update. + tx.Status.Phase = tt.phase + if err := c.Status().Update(testCtx, tx); err != nil { + t.Fatalf("seed phase %q: %v", tt.phase, err) + } + + doReconcile(t, r, tx.Name) // must not error + + // Phase must remain unchanged. + got := getTx(t, c, tx.Name).Status.Phase + if got != tt.phase { + t.Errorf("terminal phase changed: want %q, got %q", tt.phase, got) + } + }) + } +} From e6da72156947c9d994401306772368177e005850 Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:56:28 +0100 Subject: [PATCH 2/8] remove docs for now Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- ...itted-resource-transaction-architecture.md | 127 ------------------ 1 file changed, 127 deletions(-) delete mode 100644 docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md diff --git a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md deleted file mode 100644 index 7280a3219..000000000 --- a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md +++ /dev/null @@ -1,127 +0,0 @@ - - -# Committed-resource transaction architecture (target) - -## Components - -| Component | Kind | Owned by | -|-----------|------|----------| -| `CommittedResourceTransaction` (CRT) | CRD | Spec: created by API handler or syncer; Status: CRT reconciler | -| `CommittedResource` (CR) | CRD | Spec: CRT reconciler (sole writer); Status: CR controller | -| `Reservation` | CRD | Spec: CR controller (sole writer); Status: Reservation controller | -| Change-commitments API | HTTP handler | Creates CRT with `source=api` | -| Limes syncer | Background poller | Creates CRT with `source=syncer` on each diff | -| CRT reconciler | Controller | Drives CRT FSM; writes CR specs; rolls back on failure | -| CR controller | Controller | Derives Reservation slots from CR spec; reports CR readiness | -| Reservation controller | Controller | Attempts host placement; reports slot readiness | - -## Ownership - -```mermaid -flowchart TD - API["API handler\nsource=api"] -->|creates| CRT["CommittedResourceTransaction"] - Syncer["Limes syncer\nsource=syncer"] -->|creates on diff| CRT - - CRT -->|"CRT reconciler — writes spec"| CR["CommittedResource"] - CR -->|"CR controller — writes spec"| Res["Reservation"] - Res -->|"Reservation controller — tracks placement"| VM["VM / scheduler\nlive-migration"] - - CR -.->|"CR controller reports readiness\nCRT reconciler gates on this"| CRT -``` - -No controller writes its own spec. Spec flows down; status flows up. - -## Component interaction - -The three reconcilers operate independently and communicate only through CRD status conditions. - -```mermaid -sequenceDiagram - participant W as API handler or Syncer - participant CRT as CRT reconciler - participant CRCRD as CommittedResource CRD - participant CRCtl as CR controller - participant ResCRD as Reservation CRD - participant ResCtl as Reservation controller - - W->>CRCRD: create CommittedResourceTransaction - CRT->>CRCRD: write CR.spec (Allocating phase) - Note over CRT,CRCRD: CRT reconciler is sole writer of CR.spec - CRCtl->>ResCRD: reconcile Reservation slots from CR.spec - Note over CRCtl,ResCRD: CR controller is sole writer of Reservation.spec - ResCtl->>ResCRD: attempt placement, write Reservation.status - Note over ResCtl: reacts to VM moves, live-migration, in-flight actions - CRCtl->>CRCRD: aggregate slot readiness, write CR.status - CRT->>CRCRD: read CR.status.Ready + observedGeneration, advance to Accepted or RollingBack -``` - -## CRT state machine - -```mermaid -stateDiagram-v2 - [*] --> Preparing : CRT created by API handler or syncer - - Preparing --> Allocating : validation ok;\nAPI mode: snapshot taken if rejectable;\nSyncer mode: no snapshot - Preparing --> Rejected : validation failed - - Allocating --> AwaitingScheduling : API mode — CR specs written,\nupserted refs recorded - Allocating --> Accepted : Syncer mode — apply-and-done,\nno scheduling gate - Allocating --> Superseded : non-rejectable, newer CRT\nfor same AZ took over - - AwaitingScheduling --> Accepted : all CRs report Ready\n(observedGeneration matches) - AwaitingScheduling --> DryRunCleanup : all CRs Ready, but DryRun=true - AwaitingScheduling --> RollingBack : any CR rejected\nor timeout - - DryRunCleanup --> Accepted : pre-state restored\n(would-accept result) - - RollingBack --> Rejected : pre-state restored - RollingBack --> Rejected : cleanup failed permanently\n(reason: RollbackFailed) - - Accepted --> [*] - Rejected --> [*] - Superseded --> [*] -``` - -Terminal phases (`Accepted`, `Rejected`, `Superseded`) set `Finished=True`. Non-terminal problems set -`Progressing=False` with a reason (`Stalled`, `TimedOut`, `Error`, `RetryExhausted`) without advancing -the phase. A stuck non-terminal CRT triggers an alert; after a threshold it is forcibly moved to -`Superseded` so its AZ is unblocked and the syncer repairs state on the next poll. - -## Request flows - -### API-mode flow - -1. **API handler** receives a change-commitments request from Limes; creates a CRT (`source=api`, `allowRejection=true`, optional `dryRun=true`). -2. **CRT reconciler — Preparing**: validates the request; snapshots current CR specs as the rollback journal. -3. **CRT reconciler — Allocating**: writes the desired CR specs atomically; records which CRs were touched. -4. **CRT reconciler — AwaitingScheduling**: watches the touched CRs; waits until each reports its ready condition against the generation just written. -5. **CR controller**: for each CR, derives and reconciles Reservation slots; reports `Ready` once all slots are placed. -6. **Reservation controller**: places each slot on a host; reports slot readiness. -7. **CRT reconciler**: all CRs ready → `Accepted`; any CR rejected or timeout → rolls all CR specs back to the snapshot → `Rejected`. -8. **API handler** polls the CRT by name; maps terminal phase to HTTP response. - -### Syncer-mode flow - -1. **Limes syncer** polls Limes per project; diffs desired state against current CRs; on any delta creates a CRT (`source=syncer`, `allowRejection=false`). -2. **CRT reconciler — Preparing**: no snapshot (non-rejectable; syncer repairs on next poll if anything drifts). -3. **CRT reconciler — Allocating**: recency check — skips changes for any project another CRT touched in the same AZ in the last ~10 minutes (prevents clobbering an in-flight API change); writes CR specs; advances directly to `Accepted`. -4. **CR controller**: reconciles Reservation slots async; CRT is already terminal. -5. Slots converge in the background; the next syncer poll repairs any remaining drift. - -## Key invariants - -- **Single writer per spec layer.** The CRT reconciler is the only component that writes `CommittedResource.spec`. The CR controller is the only component that writes `Reservation.spec`. No controller writes its own spec. -- **AwaitingScheduling gates on generation.** The CRT reconciler advances from `AwaitingScheduling` only when a CR's ready condition carries an `observedGeneration` that matches the generation the CRT reconciler wrote — not an older reconcile cycle's result. -- **Rollback is durable.** The pre-change snapshot is stored in `CRT.status` before any CR spec is touched. A process restart resumes from the persisted phase and snapshot. -- **Syncer-mode never rolls back.** Syncer CRTs are non-rejectable: on any error they retry until `Accepted` or are superseded by a newer CRT. The syncer repairs drift on its next poll. -- **At most one active CRT per AZ.** The CRT reconciler serialises with `MaxConcurrentReconciles=1` plus an in-memory per-AZ active-transaction map (rebuilt from CRDs on startup). Younger CRTs requeue until the active one reaches a terminal phase. -- **Syncer recency guard prevents clobbering.** A syncer CRT skips changes to any project that an API CRT touched in the same AZ within the last ~10 minutes — the API change is authoritative; the syncer will reconcile remaining drift on its next poll. -- **Terminal CRTs are retained for auditability.** `Accepted` CRTs are kept for ~1 hour; `Rejected` CRTs for ~5 days. Non-terminal CRTs (including stuck `RollingBack`) are never GC'd — deleting one loses the rollback journal and the AZ active-marker. - ---- - -[Prev: CR lifecycle internals](03-a-cr-lifecycle-internals.md) · [Next: Failover reservations for KVM HA »](03-failover-reservations.md) From 65c6b74a6a180909a84af6c248635ef57b4b46cb Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Tue, 27 Oct 2026 10:27:39 +0100 Subject: [PATCH 3/8] fix docs Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- ...itted-resource-transaction-architecture.md | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md diff --git a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md new file mode 100644 index 000000000..640ab366b --- /dev/null +++ b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md @@ -0,0 +1,125 @@ + + +# Committed-resource transaction architecture (target) + +## Components + +| Component | Kind | Owned by | +|-----------|------|----------| +| `CommittedResourceTransaction` (CRT) | CRD | Spec: created by API handler or syncer; Status: CRT reconciler | +| `CommittedResource` (CR) | CRD | Spec: CRT reconciler (sole writer); Status: CR controller | +| `Reservation` | CRD | Spec: CR controller (sole writer); Status: Reservation controller | +| Change-commitments API | HTTP handler | Creates CRT with `source=api` | +| Limes syncer | Background poller | Creates CRT with `source=syncer` on each diff | +| CRT reconciler | Controller | Drives CRT FSM; writes CR specs; rolls back on failure | +| CR controller | Controller | Derives Reservation slots from CR spec; reports CR readiness | +| Reservation controller | Controller | Attempts host placement; reports slot readiness | + +## Ownership + +```mermaid +flowchart TD + API["API handler\nsource=api"] -->|creates| CRT["CommittedResourceTransaction"] + Syncer["Limes syncer\nsource=syncer"] -->|creates on diff| CRT + + CRT -->|"CRT reconciler — writes spec"| CR["CommittedResource"] + CR -->|"CR controller — writes spec"| Res["Reservation"] + Res -->|"Reservation controller — tracks placement"| VM["VM / scheduler\nlive-migration"] + + CR -.->|"CR controller reports readiness\nCRT reconciler gates on this"| CRT +``` + +No controller writes its own spec. Spec flows down; status flows up. + +## Component interaction + +The three reconcilers operate independently and communicate only through CRD status conditions. + +```mermaid +sequenceDiagram + participant W as API handler or Syncer + participant CRT as CRT reconciler + participant CRCRD as CommittedResource CRD + participant CRCtl as CR controller + participant ResCRD as Reservation CRD + participant ResCtl as Reservation controller + + W->>CRCRD: create CommittedResourceTransaction + CRT->>CRCRD: write CR.spec (Allocating phase) + Note over CRT,CRCRD: CRT reconciler is sole writer of CR.spec + CRCtl->>ResCRD: reconcile Reservation slots from CR.spec + Note over CRCtl,ResCRD: CR controller is sole writer of Reservation.spec + ResCtl->>ResCRD: attempt placement, write Reservation.status + Note over ResCtl: reacts to VM moves, live-migration, in-flight actions + CRCtl->>CRCRD: aggregate slot readiness, write CR.status + CRT->>CRCRD: read CR.status.Ready + observedGeneration, advance to Accepted or RollingBack +``` + +## CRT state machine + +```mermaid +stateDiagram-v2 + [*] --> Preparing : CRT created by API handler or syncer + + Preparing --> Allocating : snapshot taken;\nadvance to Allocating + + Allocating --> AwaitingScheduling : API mode — CR specs written,\nupserted refs recorded + Allocating --> Accepted : Syncer mode — apply-and-done,\nno scheduling gate + Allocating --> Superseded : non-rejectable, newer CRT\nfor same AZ took over + + AwaitingScheduling --> Accepted : all CRs report Ready\n(observedGeneration matches) + AwaitingScheduling --> DryRunCleanup : all CRs Ready, but DryRun=true + AwaitingScheduling --> RollingBack : any CR rejected\nor timeout + + DryRunCleanup --> Accepted : pre-state restored\n(would-accept result) + + RollingBack --> Rejected : pre-state restored\n(reason: RollbackFailed on permanent failure) + + Accepted --> [*] + Rejected --> [*] + Superseded --> [*] +``` + +Terminal phases (`Accepted`, `Rejected`, `Superseded`) set `Finished=True`. Non-terminal problems set +`Progressing=False` with a reason (`Stalled`, `TimedOut`, `Error`, `RetryExhausted`) without advancing +the phase. A stuck non-terminal CRT triggers an alert; after a threshold it is forcibly moved to +`Superseded` so its AZ is unblocked and the syncer repairs state on the next poll. + +## Request flows + +### API-mode flow + +1. **API handler** receives a change-commitments request from Limes; creates a CRT (`source=api`, `allowRejection=true`, optional `dryRun=true`). +2. **CRT reconciler — Preparing**: validates the request; snapshots current CR specs as the rollback journal. +3. **CRT reconciler — Allocating**: writes the desired CR specs atomically; records which CRs were touched. +4. **CRT reconciler — AwaitingScheduling**: watches the touched CRs; waits until each reports its ready condition against the generation just written. +5. **CR controller**: for each CR, derives and reconciles Reservation slots; reports `Ready` once all slots are placed. +6. **Reservation controller**: places each slot on a host; reports slot readiness. +7. **CRT reconciler**: all CRs ready → `Accepted`; any CR rejected or timeout → rolls all CR specs back to the snapshot → `Rejected`. +8. **API handler** polls the CRT by name; maps terminal phase to HTTP response. + +### Syncer-mode flow + +1. **Limes syncer** polls Limes per project; diffs desired state against current CRs; on any delta creates a CRT (`source=syncer`, `allowRejection=false`). +2. **CRT reconciler — Preparing**: no snapshot (non-rejectable; syncer repairs on next poll if anything drifts). +3. **CRT reconciler — Allocating**: recency check — skips changes for any project another CRT touched in the same AZ in the last ~10 minutes (prevents clobbering an in-flight API change); writes CR specs; advances directly to `Accepted`. +4. **CR controller**: reconciles Reservation slots async; CRT is already terminal. +5. Slots converge in the background; the next syncer poll repairs any remaining drift. + +## Key invariants + +- **Single writer per spec layer.** The CRT reconciler is the only component that writes `CommittedResource.spec`. The CR controller is the only component that writes `Reservation.spec`. No controller writes its own spec. +- **AwaitingScheduling gates on generation.** The CRT reconciler advances from `AwaitingScheduling` only when a CR's ready condition carries an `observedGeneration` that matches the generation the CRT reconciler wrote — not an older reconcile cycle's result. +- **Rollback is durable.** The pre-change snapshot is stored in `CRT.status` before any CR spec is touched. A process restart resumes from the persisted phase and snapshot. +- **Syncer-mode never rolls back.** Syncer CRTs are non-rejectable: on any error they retry until `Accepted` or are superseded by a newer CRT. The syncer repairs drift on its next poll. +- **At most one active CRT per AZ.** The CRT reconciler serialises with `MaxConcurrentReconciles=1` plus an in-memory per-AZ active-transaction map (rebuilt from CRDs on startup). Younger CRTs requeue until the active one reaches a terminal phase. +- **Syncer recency guard prevents clobbering.** A syncer CRT skips changes to any project that an API CRT touched in the same AZ within the last ~10 minutes — the API change is authoritative; the syncer will reconcile remaining drift on its next poll. +- **Terminal CRTs are retained for auditability.** `Accepted` CRTs are kept for ~1 hour; `Rejected` CRTs for ~5 days. Non-terminal CRTs (including stuck `RollingBack`) are never GC'd — deleting one loses the rollback journal and the AZ active-marker. + +--- + +[Prev: CR lifecycle internals](03-a-cr-lifecycle-internals.md) · [Next: Failover reservations for KVM HA »](03-failover-reservations.md) From 2aaa4d81165a5ce571b857232718bc3e8bfd30de Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Tue, 27 Oct 2026 13:54:43 +0100 Subject: [PATCH 4/8] fix bug Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- .../commitments/transactions/field_index.go | 47 ++++++++----------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/internal/scheduling/reservations/commitments/transactions/field_index.go b/internal/scheduling/reservations/commitments/transactions/field_index.go index bfc516250..d6f375f61 100644 --- a/internal/scheduling/reservations/commitments/transactions/field_index.go +++ b/internal/scheduling/reservations/commitments/transactions/field_index.go @@ -5,12 +5,9 @@ package transactions import ( "context" - "errors" - "sync" "github.com/cobaltcore-dev/cortex/api/v1alpha1" "github.com/cobaltcore-dev/cortex/pkg/multicluster" - ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -18,31 +15,25 @@ import ( // Used by crToTransaction to map a CR watch event back to its owning transaction. const idxCRTByUpsertedCR = "status.upsertedCommitments.name" -var onceIndexCRTByUpsertedCR sync.Once - // indexCRTByUpsertedCR registers the field index on all clusters that serve CRT objects. -func indexCRTByUpsertedCR(ctx context.Context, mcl *multicluster.Client) (err error) { - onceIndexCRTByUpsertedCR.Do(func() { - log := ctrl.LoggerFrom(ctx) - err = mcl.IndexField(ctx, - &v1alpha1.CommittedResourceTransaction{}, - &v1alpha1.CommittedResourceTransactionList{}, - idxCRTByUpsertedCR, - func(obj client.Object) []string { - tx, ok := obj.(*v1alpha1.CommittedResourceTransaction) - if !ok { - log.Error(errors.New("unexpected type"), "expected CommittedResourceTransaction", "object", obj) - return nil - } - names := make([]string, 0, len(tx.Status.UpsertedCommitments)) - for _, ref := range tx.Status.UpsertedCommitments { - if ref.Name != "" { - names = append(names, ref.Name) - } +// Called once from SetupWithManager; no deduplication guard needed. +func indexCRTByUpsertedCR(ctx context.Context, mcl *multicluster.Client) error { + return mcl.IndexField(ctx, + &v1alpha1.CommittedResourceTransaction{}, + &v1alpha1.CommittedResourceTransactionList{}, + idxCRTByUpsertedCR, + func(obj client.Object) []string { + tx, ok := obj.(*v1alpha1.CommittedResourceTransaction) + if !ok { + return nil + } + names := make([]string, 0, len(tx.Status.UpsertedCommitments)) + for _, ref := range tx.Status.UpsertedCommitments { + if ref.Name != "" { + names = append(names, ref.Name) } - return names - }, - ) - }) - return err + } + return names + }, + ) } From 7381f099d8660a7cba83d5d55398f408e272d055 Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:04:42 +0100 Subject: [PATCH 5/8] small fix Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- .../reservations/commitments/transactions/reconciler.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/scheduling/reservations/commitments/transactions/reconciler.go b/internal/scheduling/reservations/commitments/transactions/reconciler.go index 1e8ab0218..bf2ddb9ae 100644 --- a/internal/scheduling/reservations/commitments/transactions/reconciler.go +++ b/internal/scheduling/reservations/commitments/transactions/reconciler.go @@ -90,8 +90,7 @@ func (r *CommittedResourceTransactionReconciler) reconcilePreparing(ctx context. r.markError(ctx, tx, fmt.Sprintf("failed to snapshot commitment-%s: %v", uuid, err)) return ctrl.Result{}, err } - spec := cr.Spec - snapshot[uuid] = &spec + snapshot[uuid] = cr.Spec.DeepCopy() } logger.Info("starting transaction", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "newStateSize", len(tx.Spec.NewState), "allowRejection", tx.Spec.AllowRejection) From e5fb44c9ac214669cfdeb17153fc78b111649891 Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:38:27 +0100 Subject: [PATCH 6/8] . Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- ...ommitted-resource-transaction-architecture.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md index 640ab366b..b30a7d7b0 100644 --- a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md +++ b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md @@ -65,19 +65,19 @@ sequenceDiagram stateDiagram-v2 [*] --> Preparing : CRT created by API handler or syncer - Preparing --> Allocating : snapshot taken;\nadvance to Allocating + Preparing --> Allocating : snapshot taken - Allocating --> AwaitingScheduling : API mode — CR specs written,\nupserted refs recorded - Allocating --> Accepted : Syncer mode — apply-and-done,\nno scheduling gate - Allocating --> Superseded : non-rejectable, newer CRT\nfor same AZ took over + Allocating --> AwaitingScheduling : API mode — CR specs written + Allocating --> Accepted : Syncer mode — apply-and-done + Allocating --> Superseded : newer CRT for same AZ took over - AwaitingScheduling --> Accepted : all CRs report Ready\n(observedGeneration matches) + AwaitingScheduling --> Accepted : all CRs Ready (observedGeneration matches) AwaitingScheduling --> DryRunCleanup : all CRs Ready, but DryRun=true - AwaitingScheduling --> RollingBack : any CR rejected\nor timeout + AwaitingScheduling --> RollingBack : CR rejected or timeout - DryRunCleanup --> Accepted : pre-state restored\n(would-accept result) + DryRunCleanup --> Accepted : pre-state restored (would-accept) - RollingBack --> Rejected : pre-state restored\n(reason: RollbackFailed on permanent failure) + RollingBack --> Rejected : pre-state restored (RollbackFailed on permanent failure) Accepted --> [*] Rejected --> [*] From 36399179f9d194d05ce41fc2636480c741edae7f Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:55:58 +0100 Subject: [PATCH 7/8] rename CRD Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- ...committed_resource_change_request_types.go | 164 +++++++++++ .../committed_resource_transaction_types.go | 164 ----------- api/v1alpha1/zz_generated.deepcopy.go | 266 +++++++++--------- ...itted-resource-transaction-architecture.md | 70 ++--- .../cortex-nova/templates/kpis_kvm.yaml | 2 +- helm/bundles/cortex-nova/values.yaml | 6 +- ...loud_committedresourcechangerequests.yaml} | 40 +-- .../commitments/changerequests/field_index.go | 39 +++ .../reconciler.go | 100 +++---- .../reconciler_test.go | 228 +++++++-------- .../commitments/transactions/field_index.go | 39 --- 11 files changed, 560 insertions(+), 558 deletions(-) create mode 100644 api/v1alpha1/committed_resource_change_request_types.go delete mode 100644 api/v1alpha1/committed_resource_transaction_types.go rename helm/library/cortex/files/crds/{cortex.cloud_committedresourcetransactions.yaml => cortex.cloud_committedresourcechangerequests.yaml} (94%) create mode 100644 internal/scheduling/reservations/commitments/changerequests/field_index.go rename internal/scheduling/reservations/commitments/{transactions => changerequests}/reconciler.go (75%) rename internal/scheduling/reservations/commitments/{transactions => changerequests}/reconciler_test.go (81%) delete mode 100644 internal/scheduling/reservations/commitments/transactions/field_index.go diff --git a/api/v1alpha1/committed_resource_change_request_types.go b/api/v1alpha1/committed_resource_change_request_types.go new file mode 100644 index 000000000..2531a1a92 --- /dev/null +++ b/api/v1alpha1/committed_resource_change_request_types.go @@ -0,0 +1,164 @@ +// Copyright SAP SE +// SPDX-License-Identifier: Apache-2.0 + +package v1alpha1 + +import ( + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// ChangeRequestPhase represents the FSM phase of a CommittedResourceChangeRequest. +type ChangeRequestPhase string + +const ( + // ChangeRequestPhasePreparing: change request created; sanity checks and optional snapshot in progress. + ChangeRequestPhasePreparing ChangeRequestPhase = "Preparing" + // ChangeRequestPhaseAllocating: CR specs are being written. + ChangeRequestPhaseAllocating ChangeRequestPhase = "Allocating" + // ChangeRequestPhaseAwaitingScheduling: CR specs written; waiting for placement success. + ChangeRequestPhaseAwaitingScheduling ChangeRequestPhase = "AwaitingScheduling" + // ChangeRequestPhaseRollingBack: a CR was rejected or timed out; restoring pre-change state. + ChangeRequestPhaseRollingBack ChangeRequestPhase = "RollingBack" + // ChangeRequestPhaseDryRunCleanup: dry-run would-accept; restoring pre-change state. + ChangeRequestPhaseDryRunCleanup ChangeRequestPhase = "DryRunCleanup" + // ChangeRequestPhaseAccepted: terminal; change request committed successfully. + ChangeRequestPhaseAccepted ChangeRequestPhase = "Accepted" + // ChangeRequestPhaseRejected: terminal; change request rolled back. + ChangeRequestPhaseRejected ChangeRequestPhase = "Rejected" + // ChangeRequestPhaseSuperseded: terminal; abandoned because a newer change request for the same AZ took over. + ChangeRequestPhaseSuperseded ChangeRequestPhase = "Superseded" +) + +// TxSource identifies the originator of a CommittedResourceChangeRequest. +type TxSource string + +const ( + // TxSourceAPI: change request created by the change-commitments HTTP API. + TxSourceAPI TxSource = "api" + // TxSourceSyncer: change request created by the Limes syncer. + TxSourceSyncer TxSource = "syncer" +) + +const ( + // ChangeRequestConditionProgressing is True while the change request is advancing normally. + // False + reason indicates a problem while still non-terminal. + ChangeRequestConditionProgressing = "Progressing" + + // ChangeRequestConditionFinished is True once the change request has reached a terminal phase. + ChangeRequestConditionFinished = "Finished" + + // Reasons for ChangeRequestConditionProgressing=False (non-terminal problems). + ChangeRequestReasonStalled = "Stalled" + ChangeRequestReasonTimedOut = "TimedOut" + ChangeRequestReasonError = "Error" + ChangeRequestReasonRetryExhausted = "RetryExhausted" + + // Reasons for ChangeRequestConditionFinished=True. + ChangeRequestReasonAccepted = "Accepted" + ChangeRequestReasonRejected = "Rejected" + ChangeRequestReasonSuperseded = "Superseded" +) + +// CommittedResourceChangeRequestSpec defines the desired change for a CommittedResourceChangeRequest. +type CommittedResourceChangeRequestSpec struct { + // CorrelationID is the request/correlation ID. + // +kubebuilder:validation:Optional + CorrelationID string `json:"correlationID,omitempty"` + + // AvailabilityZone is the availability zone this change request is scoped to. + // +kubebuilder:validation:Required + // +kubebuilder:validation:MinLength=1 + AvailabilityZone string `json:"availabilityZone"` + + // Source identifies who created this change request. + // +kubebuilder:validation:Enum=api;syncer + // +kubebuilder:validation:Required + Source TxSource `json:"source"` + + // AllowRejection controls whether the change request may be rejected and rolled back + // on placement failure. When false, the reconciler retries until Accepted or Superseded. + // +kubebuilder:validation:Optional + AllowRejection bool `json:"allowRejection,omitempty"` + + // DryRun controls whether the change request commits or only probes schedulability. + // +kubebuilder:validation:Optional + DryRun bool `json:"dryRun,omitempty"` + + // NewState maps commitmentUUID to the desired CommittedResourceSpec. + // A nil value means the corresponding CommittedResource should be deleted. + // +kubebuilder:validation:Optional + NewState map[string]*CommittedResourceSpec `json:"newState,omitempty"` +} + +// CommittedResourceChangeRequestStatus defines the observed state of a CommittedResourceChangeRequest. +type CommittedResourceChangeRequestStatus struct { + // Phase is the current FSM phase of the change request. + // +kubebuilder:validation:Optional + // +kubebuilder:validation:XValidation:rule="oldSelf == '' || !['Accepted','Rejected','Superseded'].exists(t, t == oldSelf) || self == oldSelf",message="phase cannot transition out of a terminal state" + Phase ChangeRequestPhase `json:"phase,omitempty"` + + // SnapshotBefore records the CommittedResource spec state before this change request. + // Key is commitmentUUID; nil value means the CR did not exist before (will be deleted on rollback). + // Populated in Preparing when AllowRejection=true. + // +kubebuilder:validation:Optional + SnapshotBefore map[string]*CommittedResourceSpec `json:"snapshotBefore,omitempty"` + + // UpsertedCommitments holds references to CommittedResource CRDs written by this change request. + // Watched in AwaitingScheduling to gate on placement success. + // These are plain object references, not owner references. + // +kubebuilder:validation:Optional + UpsertedCommitments []corev1.ObjectReference `json:"upsertedCommitments,omitempty"` + + // Conditions holds Progressing and Finished conditions. + // +kubebuilder:validation:Optional + Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"` + + // Message is a human-readable description of the current state or terminal outcome. + // +kubebuilder:validation:Optional + Message string `json:"message,omitempty"` +} + +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:resource:scope=Cluster,shortName=crcr +// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase" +// +kubebuilder:printcolumn:name="AZ",type="string",JSONPath=".spec.availabilityZone" +// +kubebuilder:printcolumn:name="Source",type="string",JSONPath=".spec.source" +// +kubebuilder:printcolumn:name="DryRun",type="boolean",JSONPath=".spec.dryRun",priority=1 +// +kubebuilder:printcolumn:name="Progressing",type="string",JSONPath=".status.conditions[?(@.type=='Progressing')].status",priority=1 +// +kubebuilder:printcolumn:name="Finished",type="string",JSONPath=".status.conditions[?(@.type=='Finished')].status",priority=1 + +// CommittedResourceChangeRequest is the Schema for the committedresourcechangerequests API +type CommittedResourceChangeRequest struct { + metav1.TypeMeta `json:",inline"` + + // +optional + metav1.ObjectMeta `json:"metadata,omitempty,omitzero"` + + // +required + Spec CommittedResourceChangeRequestSpec `json:"spec"` + + // +optional + Status CommittedResourceChangeRequestStatus `json:"status,omitempty,omitzero"` +} + +// IsTerminal reports whether the change request has reached a terminal phase. +func (tx *CommittedResourceChangeRequest) IsTerminal() bool { + return tx.Status.Phase == ChangeRequestPhaseAccepted || + tx.Status.Phase == ChangeRequestPhaseRejected || + tx.Status.Phase == ChangeRequestPhaseSuperseded +} + +// +kubebuilder:object:root=true + +// CommittedResourceChangeRequestList contains a list of CommittedResourceChangeRequest +type CommittedResourceChangeRequestList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []CommittedResourceChangeRequest `json:"items"` +} + +func init() { + SchemeBuilder.Register(&CommittedResourceChangeRequest{}, &CommittedResourceChangeRequestList{}) +} diff --git a/api/v1alpha1/committed_resource_transaction_types.go b/api/v1alpha1/committed_resource_transaction_types.go deleted file mode 100644 index 84fe23f9d..000000000 --- a/api/v1alpha1/committed_resource_transaction_types.go +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright SAP SE -// SPDX-License-Identifier: Apache-2.0 - -package v1alpha1 - -import ( - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// TxPhase represents the FSM phase of a CommittedResourceTransaction. -type TxPhase string - -const ( - // TxPhasePreparing: transaction created; sanity checks and optional snapshot in progress. - TxPhasePreparing TxPhase = "Preparing" - // TxPhaseAllocating: CR specs are being written. - TxPhaseAllocating TxPhase = "Allocating" - // TxPhaseAwaitingScheduling: CR specs written; waiting for placement success. - TxPhaseAwaitingScheduling TxPhase = "AwaitingScheduling" - // TxPhaseRollingBack: a CR was rejected or timed out; restoring pre-change state. - TxPhaseRollingBack TxPhase = "RollingBack" - // TxPhaseDryRunCleanup: dry-run would-accept; restoring pre-change state. - TxPhaseDryRunCleanup TxPhase = "DryRunCleanup" - // TxPhaseAccepted: terminal; transaction committed successfully. - TxPhaseAccepted TxPhase = "Accepted" - // TxPhaseRejected: terminal; transaction rolled back. - TxPhaseRejected TxPhase = "Rejected" - // TxPhaseSuperseded: terminal; abandoned because a newer transaction for the same AZ took over. - TxPhaseSuperseded TxPhase = "Superseded" -) - -// TxSource identifies the originator of a CommittedResourceTransaction. -type TxSource string - -const ( - // TxSourceAPI: transaction created by the change-commitments HTTP API. - TxSourceAPI TxSource = "api" - // TxSourceSyncer: transaction created by the Limes syncer. - TxSourceSyncer TxSource = "syncer" -) - -const ( - // TxConditionProgressing is True while the transaction is advancing normally. - // False + reason indicates a problem while still non-terminal. - TxConditionProgressing = "Progressing" - - // TxConditionFinished is True once the transaction has reached a terminal phase. - TxConditionFinished = "Finished" - - // Reasons for TxConditionProgressing=False (non-terminal problems). - TxReasonStalled = "Stalled" - TxReasonTimedOut = "TimedOut" - TxReasonError = "Error" - TxReasonRetryExhausted = "RetryExhausted" - - // Reasons for TxConditionFinished=True. - TxReasonAccepted = "Accepted" - TxReasonRejected = "Rejected" - TxReasonSuperseded = "Superseded" -) - -// CommittedResourceTransactionSpec defines the desired change for a CommittedResourceTransaction. -type CommittedResourceTransactionSpec struct { - // CorrelationID is the request/correlation ID. - // +kubebuilder:validation:Optional - CorrelationID string `json:"correlationID,omitempty"` - - // AvailabilityZone is the availability zone this transaction is scoped to. - // +kubebuilder:validation:Required - // +kubebuilder:validation:MinLength=1 - AvailabilityZone string `json:"availabilityZone"` - - // Source identifies who created this transaction. - // +kubebuilder:validation:Enum=api;syncer - // +kubebuilder:validation:Required - Source TxSource `json:"source"` - - // AllowRejection controls whether the transaction may be rejected and rolled back - // on placement failure. When false, the reconciler retries until Accepted or Superseded. - // +kubebuilder:validation:Optional - AllowRejection bool `json:"allowRejection,omitempty"` - - // DryRun controls whether the transaction commits or only probes schedulability. - // +kubebuilder:validation:Optional - DryRun bool `json:"dryRun,omitempty"` - - // NewState maps commitmentUUID to the desired CommittedResourceSpec. - // A nil value means the corresponding CommittedResource should be deleted. - // +kubebuilder:validation:Optional - NewState map[string]*CommittedResourceSpec `json:"newState,omitempty"` -} - -// CommittedResourceTransactionStatus defines the observed state of a CommittedResourceTransaction. -type CommittedResourceTransactionStatus struct { - // Phase is the current FSM phase of the transaction. - // +kubebuilder:validation:Optional - // +kubebuilder:validation:XValidation:rule="oldSelf == '' || !['Accepted','Rejected','Superseded'].exists(t, t == oldSelf) || self == oldSelf",message="phase cannot transition out of a terminal state" - Phase TxPhase `json:"phase,omitempty"` - - // SnapshotBefore records the CommittedResource spec state before this transaction. - // Key is commitmentUUID; nil value means the CR did not exist before (will be deleted on rollback). - // Populated in Preparing when AllowRejection=true. - // +kubebuilder:validation:Optional - SnapshotBefore map[string]*CommittedResourceSpec `json:"snapshotBefore,omitempty"` - - // UpsertedCommitments holds references to CommittedResource CRDs written by this transaction. - // Watched in AwaitingScheduling to gate on placement success. - // These are plain object references, not owner references. - // +kubebuilder:validation:Optional - UpsertedCommitments []corev1.ObjectReference `json:"upsertedCommitments,omitempty"` - - // Conditions holds Progressing and Finished conditions. - // +kubebuilder:validation:Optional - Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"` - - // Message is a human-readable description of the current state or terminal outcome. - // +kubebuilder:validation:Optional - Message string `json:"message,omitempty"` -} - -// +kubebuilder:object:root=true -// +kubebuilder:subresource:status -// +kubebuilder:resource:scope=Cluster -// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase" -// +kubebuilder:printcolumn:name="AZ",type="string",JSONPath=".spec.availabilityZone" -// +kubebuilder:printcolumn:name="Source",type="string",JSONPath=".spec.source" -// +kubebuilder:printcolumn:name="DryRun",type="boolean",JSONPath=".spec.dryRun",priority=1 -// +kubebuilder:printcolumn:name="Progressing",type="string",JSONPath=".status.conditions[?(@.type=='Progressing')].status",priority=1 -// +kubebuilder:printcolumn:name="Finished",type="string",JSONPath=".status.conditions[?(@.type=='Finished')].status",priority=1 - -// CommittedResourceTransaction is the Schema for the committedresourcetransactions API -type CommittedResourceTransaction struct { - metav1.TypeMeta `json:",inline"` - - // +optional - metav1.ObjectMeta `json:"metadata,omitempty,omitzero"` - - // +required - Spec CommittedResourceTransactionSpec `json:"spec"` - - // +optional - Status CommittedResourceTransactionStatus `json:"status,omitempty,omitzero"` -} - -// IsTerminal reports whether the transaction has reached a terminal phase. -func (tx *CommittedResourceTransaction) IsTerminal() bool { - return tx.Status.Phase == TxPhaseAccepted || - tx.Status.Phase == TxPhaseRejected || - tx.Status.Phase == TxPhaseSuperseded -} - -// +kubebuilder:object:root=true - -// CommittedResourceTransactionList contains a list of CommittedResourceTransaction -type CommittedResourceTransactionList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` - Items []CommittedResourceTransaction `json:"items"` -} - -func init() { - SchemeBuilder.Register(&CommittedResourceTransaction{}, &CommittedResourceTransactionList{}) -} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 6f9db9cd1..67f05fc8c 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -80,6 +80,139 @@ func (in *CommittedResourceAllocation) DeepCopy() *CommittedResourceAllocation { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceChangeRequest) DeepCopyInto(out *CommittedResourceChangeRequest) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequest. +func (in *CommittedResourceChangeRequest) DeepCopy() *CommittedResourceChangeRequest { + if in == nil { + return nil + } + out := new(CommittedResourceChangeRequest) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CommittedResourceChangeRequest) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceChangeRequestList) DeepCopyInto(out *CommittedResourceChangeRequestList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]CommittedResourceChangeRequest, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequestList. +func (in *CommittedResourceChangeRequestList) DeepCopy() *CommittedResourceChangeRequestList { + if in == nil { + return nil + } + out := new(CommittedResourceChangeRequestList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CommittedResourceChangeRequestList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceChangeRequestSpec) DeepCopyInto(out *CommittedResourceChangeRequestSpec) { + *out = *in + if in.NewState != nil { + in, out := &in.NewState, &out.NewState + *out = make(map[string]*CommittedResourceSpec, len(*in)) + for key, val := range *in { + var outVal *CommittedResourceSpec + if val == nil { + (*out)[key] = nil + } else { + inVal := (*in)[key] + in, out := &inVal, &outVal + *out = new(CommittedResourceSpec) + (*in).DeepCopyInto(*out) + } + (*out)[key] = outVal + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequestSpec. +func (in *CommittedResourceChangeRequestSpec) DeepCopy() *CommittedResourceChangeRequestSpec { + if in == nil { + return nil + } + out := new(CommittedResourceChangeRequestSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommittedResourceChangeRequestStatus) DeepCopyInto(out *CommittedResourceChangeRequestStatus) { + *out = *in + if in.SnapshotBefore != nil { + in, out := &in.SnapshotBefore, &out.SnapshotBefore + *out = make(map[string]*CommittedResourceSpec, len(*in)) + for key, val := range *in { + var outVal *CommittedResourceSpec + if val == nil { + (*out)[key] = nil + } else { + inVal := (*in)[key] + in, out := &inVal, &outVal + *out = new(CommittedResourceSpec) + (*in).DeepCopyInto(*out) + } + (*out)[key] = outVal + } + } + if in.UpsertedCommitments != nil { + in, out := &in.UpsertedCommitments, &out.UpsertedCommitments + *out = make([]v1.ObjectReference, len(*in)) + copy(*out, *in) + } + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]metav1.Condition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequestStatus. +func (in *CommittedResourceChangeRequestStatus) DeepCopy() *CommittedResourceChangeRequestStatus { + if in == nil { + return nil + } + out := new(CommittedResourceChangeRequestStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *CommittedResourceList) DeepCopyInto(out *CommittedResourceList) { *out = *in @@ -240,139 +373,6 @@ func (in *CommittedResourceStatus) DeepCopy() *CommittedResourceStatus { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceTransaction) DeepCopyInto(out *CommittedResourceTransaction) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) - in.Status.DeepCopyInto(&out.Status) -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransaction. -func (in *CommittedResourceTransaction) DeepCopy() *CommittedResourceTransaction { - if in == nil { - return nil - } - out := new(CommittedResourceTransaction) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *CommittedResourceTransaction) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceTransactionList) DeepCopyInto(out *CommittedResourceTransactionList) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ListMeta.DeepCopyInto(&out.ListMeta) - if in.Items != nil { - in, out := &in.Items, &out.Items - *out = make([]CommittedResourceTransaction, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransactionList. -func (in *CommittedResourceTransactionList) DeepCopy() *CommittedResourceTransactionList { - if in == nil { - return nil - } - out := new(CommittedResourceTransactionList) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *CommittedResourceTransactionList) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceTransactionSpec) DeepCopyInto(out *CommittedResourceTransactionSpec) { - *out = *in - if in.NewState != nil { - in, out := &in.NewState, &out.NewState - *out = make(map[string]*CommittedResourceSpec, len(*in)) - for key, val := range *in { - var outVal *CommittedResourceSpec - if val == nil { - (*out)[key] = nil - } else { - inVal := (*in)[key] - in, out := &inVal, &outVal - *out = new(CommittedResourceSpec) - (*in).DeepCopyInto(*out) - } - (*out)[key] = outVal - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransactionSpec. -func (in *CommittedResourceTransactionSpec) DeepCopy() *CommittedResourceTransactionSpec { - if in == nil { - return nil - } - out := new(CommittedResourceTransactionSpec) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceTransactionStatus) DeepCopyInto(out *CommittedResourceTransactionStatus) { - *out = *in - if in.SnapshotBefore != nil { - in, out := &in.SnapshotBefore, &out.SnapshotBefore - *out = make(map[string]*CommittedResourceSpec, len(*in)) - for key, val := range *in { - var outVal *CommittedResourceSpec - if val == nil { - (*out)[key] = nil - } else { - inVal := (*in)[key] - in, out := &inVal, &outVal - *out = new(CommittedResourceSpec) - (*in).DeepCopyInto(*out) - } - (*out)[key] = outVal - } - } - if in.UpsertedCommitments != nil { - in, out := &in.UpsertedCommitments, &out.UpsertedCommitments - *out = make([]v1.ObjectReference, len(*in)) - copy(*out, *in) - } - if in.Conditions != nil { - in, out := &in.Conditions, &out.Conditions - *out = make([]metav1.Condition, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceTransactionStatus. -func (in *CommittedResourceTransactionStatus) DeepCopy() *CommittedResourceTransactionStatus { - if in == nil { - return nil - } - out := new(CommittedResourceTransactionStatus) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Datasource) DeepCopyInto(out *Datasource) { *out = *in diff --git a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md index b30a7d7b0..b544e248e 100644 --- a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md +++ b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md @@ -10,12 +10,12 @@ | Component | Kind | Owned by | |-----------|------|----------| -| `CommittedResourceTransaction` (CRT) | CRD | Spec: created by API handler or syncer; Status: CRT reconciler | -| `CommittedResource` (CR) | CRD | Spec: CRT reconciler (sole writer); Status: CR controller | +| `CommittedResourceChangeRequest` (CRCR) | CRD | Spec: created by API handler or syncer; Status: CRCR reconciler | +| `CommittedResource` (CR) | CRD | Spec: CRCR reconciler (sole writer); Status: CR controller | | `Reservation` | CRD | Spec: CR controller (sole writer); Status: Reservation controller | -| Change-commitments API | HTTP handler | Creates CRT with `source=api` | -| Limes syncer | Background poller | Creates CRT with `source=syncer` on each diff | -| CRT reconciler | Controller | Drives CRT FSM; writes CR specs; rolls back on failure | +| Change-commitments API | HTTP handler | Creates CRCR with `source=api` | +| Limes syncer | Background poller | Creates CRCR with `source=syncer` on each diff | +| CRCR reconciler | Controller | Drives CRCR FSM; writes CR specs; rolls back on failure | | CR controller | Controller | Derives Reservation slots from CR spec; reports CR readiness | | Reservation controller | Controller | Attempts host placement; reports slot readiness | @@ -23,14 +23,14 @@ ```mermaid flowchart TD - API["API handler\nsource=api"] -->|creates| CRT["CommittedResourceTransaction"] - Syncer["Limes syncer\nsource=syncer"] -->|creates on diff| CRT + API["API handler\nsource=api"] -->|creates| CRCR["CommittedResourceChangeRequest"] + Syncer["Limes syncer\nsource=syncer"] -->|creates on diff| CRCR - CRT -->|"CRT reconciler — writes spec"| CR["CommittedResource"] + CRCR -->|"CRCR reconciler — writes spec"| CR["CommittedResource"] CR -->|"CR controller — writes spec"| Res["Reservation"] Res -->|"Reservation controller — tracks placement"| VM["VM / scheduler\nlive-migration"] - CR -.->|"CR controller reports readiness\nCRT reconciler gates on this"| CRT + CR -.->|"CR controller reports readiness\nCRCR reconciler gates on this"| CRCR ``` No controller writes its own spec. Spec flows down; status flows up. @@ -42,34 +42,34 @@ The three reconcilers operate independently and communicate only through CRD sta ```mermaid sequenceDiagram participant W as API handler or Syncer - participant CRT as CRT reconciler + participant CRCR as CRCR reconciler participant CRCRD as CommittedResource CRD participant CRCtl as CR controller participant ResCRD as Reservation CRD participant ResCtl as Reservation controller - W->>CRCRD: create CommittedResourceTransaction - CRT->>CRCRD: write CR.spec (Allocating phase) - Note over CRT,CRCRD: CRT reconciler is sole writer of CR.spec + W->>CRCRD: create CommittedResourceChangeRequest + CRCR->>CRCRD: write CR.spec (Allocating phase) + Note over CRCR,CRCRD: CRCR reconciler is sole writer of CR.spec CRCtl->>ResCRD: reconcile Reservation slots from CR.spec Note over CRCtl,ResCRD: CR controller is sole writer of Reservation.spec ResCtl->>ResCRD: attempt placement, write Reservation.status Note over ResCtl: reacts to VM moves, live-migration, in-flight actions CRCtl->>CRCRD: aggregate slot readiness, write CR.status - CRT->>CRCRD: read CR.status.Ready + observedGeneration, advance to Accepted or RollingBack + CRCR->>CRCRD: read CR.status.Ready + observedGeneration, advance to Accepted or RollingBack ``` -## CRT state machine +## CRCR state machine ```mermaid stateDiagram-v2 - [*] --> Preparing : CRT created by API handler or syncer + [*] --> Preparing : CRCR created by API handler or syncer Preparing --> Allocating : snapshot taken Allocating --> AwaitingScheduling : API mode — CR specs written Allocating --> Accepted : Syncer mode — apply-and-done - Allocating --> Superseded : newer CRT for same AZ took over + Allocating --> Superseded : newer CRCR for same AZ took over AwaitingScheduling --> Accepted : all CRs Ready (observedGeneration matches) AwaitingScheduling --> DryRunCleanup : all CRs Ready, but DryRun=true @@ -86,39 +86,39 @@ stateDiagram-v2 Terminal phases (`Accepted`, `Rejected`, `Superseded`) set `Finished=True`. Non-terminal problems set `Progressing=False` with a reason (`Stalled`, `TimedOut`, `Error`, `RetryExhausted`) without advancing -the phase. A stuck non-terminal CRT triggers an alert; after a threshold it is forcibly moved to +the phase. A stuck non-terminal CRCR triggers an alert; after a threshold it is forcibly moved to `Superseded` so its AZ is unblocked and the syncer repairs state on the next poll. ## Request flows ### API-mode flow -1. **API handler** receives a change-commitments request from Limes; creates a CRT (`source=api`, `allowRejection=true`, optional `dryRun=true`). -2. **CRT reconciler — Preparing**: validates the request; snapshots current CR specs as the rollback journal. -3. **CRT reconciler — Allocating**: writes the desired CR specs atomically; records which CRs were touched. -4. **CRT reconciler — AwaitingScheduling**: watches the touched CRs; waits until each reports its ready condition against the generation just written. +1. **API handler** receives a change-commitments request from Limes; creates a CRCR (`source=api`, `allowRejection=true`, optional `dryRun=true`). +2. **CRCR reconciler — Preparing**: validates the request; snapshots current CR specs as the rollback journal. +3. **CRCR reconciler — Allocating**: writes the desired CR specs atomically; records which CRs were touched. +4. **CRCR reconciler — AwaitingScheduling**: watches the touched CRs; waits until each reports its ready condition against the generation just written. 5. **CR controller**: for each CR, derives and reconciles Reservation slots; reports `Ready` once all slots are placed. 6. **Reservation controller**: places each slot on a host; reports slot readiness. -7. **CRT reconciler**: all CRs ready → `Accepted`; any CR rejected or timeout → rolls all CR specs back to the snapshot → `Rejected`. -8. **API handler** polls the CRT by name; maps terminal phase to HTTP response. +7. **CRCR reconciler**: all CRs ready → `Accepted`; any CR rejected or timeout → rolls all CR specs back to the snapshot → `Rejected`. +8. **API handler** polls the CRCR by name; maps terminal phase to HTTP response. ### Syncer-mode flow -1. **Limes syncer** polls Limes per project; diffs desired state against current CRs; on any delta creates a CRT (`source=syncer`, `allowRejection=false`). -2. **CRT reconciler — Preparing**: no snapshot (non-rejectable; syncer repairs on next poll if anything drifts). -3. **CRT reconciler — Allocating**: recency check — skips changes for any project another CRT touched in the same AZ in the last ~10 minutes (prevents clobbering an in-flight API change); writes CR specs; advances directly to `Accepted`. -4. **CR controller**: reconciles Reservation slots async; CRT is already terminal. +1. **Limes syncer** polls Limes per project; diffs desired state against current CRs; on any delta creates a CRCR (`source=syncer`, `allowRejection=false`). +2. **CRCR reconciler — Preparing**: no snapshot (non-rejectable; syncer repairs on next poll if anything drifts). +3. **CRCR reconciler — Allocating**: recency check — skips changes for any project another CRCR touched in the same AZ in the last ~10 minutes (prevents clobbering an in-flight API change); writes CR specs; advances directly to `Accepted`. +4. **CR controller**: reconciles Reservation slots async; CRCR is already terminal. 5. Slots converge in the background; the next syncer poll repairs any remaining drift. ## Key invariants -- **Single writer per spec layer.** The CRT reconciler is the only component that writes `CommittedResource.spec`. The CR controller is the only component that writes `Reservation.spec`. No controller writes its own spec. -- **AwaitingScheduling gates on generation.** The CRT reconciler advances from `AwaitingScheduling` only when a CR's ready condition carries an `observedGeneration` that matches the generation the CRT reconciler wrote — not an older reconcile cycle's result. -- **Rollback is durable.** The pre-change snapshot is stored in `CRT.status` before any CR spec is touched. A process restart resumes from the persisted phase and snapshot. -- **Syncer-mode never rolls back.** Syncer CRTs are non-rejectable: on any error they retry until `Accepted` or are superseded by a newer CRT. The syncer repairs drift on its next poll. -- **At most one active CRT per AZ.** The CRT reconciler serialises with `MaxConcurrentReconciles=1` plus an in-memory per-AZ active-transaction map (rebuilt from CRDs on startup). Younger CRTs requeue until the active one reaches a terminal phase. -- **Syncer recency guard prevents clobbering.** A syncer CRT skips changes to any project that an API CRT touched in the same AZ within the last ~10 minutes — the API change is authoritative; the syncer will reconcile remaining drift on its next poll. -- **Terminal CRTs are retained for auditability.** `Accepted` CRTs are kept for ~1 hour; `Rejected` CRTs for ~5 days. Non-terminal CRTs (including stuck `RollingBack`) are never GC'd — deleting one loses the rollback journal and the AZ active-marker. +- **Single writer per spec layer.** The CRCR reconciler is the only component that writes `CommittedResource.spec`. The CR controller is the only component that writes `Reservation.spec`. No controller writes its own spec. +- **AwaitingScheduling gates on generation.** The CRCR reconciler advances from `AwaitingScheduling` only when a CR's ready condition carries an `observedGeneration` that matches the generation the CRCR reconciler wrote — not an older reconcile cycle's result. +- **Rollback is durable.** The pre-change snapshot is stored in `CRCR.status` before any CR spec is touched. A process restart resumes from the persisted phase and snapshot. +- **Syncer-mode never rolls back.** Syncer CRTs are non-rejectable: on any error they retry until `Accepted` or are superseded by a newer CRCR. The syncer repairs drift on its next poll. +- **At most one active CRCR per AZ.** The CRCR reconciler serialises with `MaxConcurrentReconciles=1` plus an in-memory per-AZ active-transaction map (rebuilt from CRDs on startup). Younger CRTs requeue until the active one reaches a terminal phase. +- **Syncer recency guard prevents clobbering.** A syncer CRCR skips changes to any project that an API CRCR touched in the same AZ within the last ~10 minutes — the API change is authoritative; the syncer will reconcile remaining drift on its next poll. +- **Terminal CRTs are retained for auditability.** `Accepted` CRTs are kept for ~1 hour; `Rejected` CRTs for ~5 days. Non-terminal CRCRs (including stuck `RollingBack`) are never GC'd — deleting one loses the rollback journal and the AZ active-marker. --- diff --git a/helm/bundles/cortex-nova/templates/kpis_kvm.yaml b/helm/bundles/cortex-nova/templates/kpis_kvm.yaml index 664c39a4b..9f86eb747 100644 --- a/helm/bundles/cortex-nova/templates/kpis_kvm.yaml +++ b/helm/bundles/cortex-nova/templates/kpis_kvm.yaml @@ -52,7 +52,7 @@ spec: gvks: - cortex.cloud/v1alpha1/ReservationList - cortex.cloud/v1alpha1/CommittedResourceList - - cortex.cloud/v1alpha1/CommittedResourceTransactionList + - cortex.cloud/v1alpha1/CommittedResourceChangeRequestList - kvm.cloud.sap/v1/HypervisorList - cobaltcore.cloud/v1alpha1/VirtualMachineList description: | diff --git a/helm/bundles/cortex-nova/values.yaml b/helm/bundles/cortex-nova/values.yaml index 6edf499da..94f925552 100644 --- a/helm/bundles/cortex-nova/values.yaml +++ b/helm/bundles/cortex-nova/values.yaml @@ -137,8 +137,8 @@ cortex: &cortex - cortex.cloud/v1alpha1/ReservationList - cortex.cloud/v1alpha1/CommittedResource - cortex.cloud/v1alpha1/CommittedResourceList - - cortex.cloud/v1alpha1/CommittedResourceTransaction - - cortex.cloud/v1alpha1/CommittedResourceTransactionList + - cortex.cloud/v1alpha1/CommittedResourceChangeRequest + - cortex.cloud/v1alpha1/CommittedResourceChangeRequestList - cortex.cloud/v1alpha1/ProjectQuota - cortex.cloud/v1alpha1/ProjectQuotaList - cortex.cloud/v1alpha1/FlavorGroupCapacity @@ -161,7 +161,7 @@ cortex: &cortex gvks: - cortex.cloud/v1alpha1/Reservation - cortex.cloud/v1alpha1/CommittedResource - - cortex.cloud/v1alpha1/CommittedResourceTransaction + - cortex.cloud/v1alpha1/CommittedResourceChangeRequest monitoring: labels: &cortexMonitoringLabels github_org: cobaltcore-dev diff --git a/helm/library/cortex/files/crds/cortex.cloud_committedresourcetransactions.yaml b/helm/library/cortex/files/crds/cortex.cloud_committedresourcechangerequests.yaml similarity index 94% rename from helm/library/cortex/files/crds/cortex.cloud_committedresourcetransactions.yaml rename to helm/library/cortex/files/crds/cortex.cloud_committedresourcechangerequests.yaml index b19b4662c..94b918302 100644 --- a/helm/library/cortex/files/crds/cortex.cloud_committedresourcetransactions.yaml +++ b/helm/library/cortex/files/crds/cortex.cloud_committedresourcechangerequests.yaml @@ -4,14 +4,16 @@ kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: v0.23.0 - name: committedresourcetransactions.cortex.cloud + name: committedresourcechangerequests.cortex.cloud spec: group: cortex.cloud names: - kind: CommittedResourceTransaction - listKind: CommittedResourceTransactionList - plural: committedresourcetransactions - singular: committedresourcetransaction + kind: CommittedResourceChangeRequest + listKind: CommittedResourceChangeRequestList + plural: committedresourcechangerequests + shortNames: + - crcr + singular: committedresourcechangerequest scope: Cluster versions: - additionalPrinterColumns: @@ -39,7 +41,7 @@ spec: name: v1alpha1 schema: openAPIV3Schema: - description: CommittedResourceTransaction is the Schema for the committedresourcetransactions + description: CommittedResourceChangeRequest is the Schema for the committedresourcechangerequests API properties: apiVersion: @@ -60,25 +62,25 @@ spec: metadata: type: object spec: - description: CommittedResourceTransactionSpec defines the desired change - for a CommittedResourceTransaction. + description: CommittedResourceChangeRequestSpec defines the desired change + for a CommittedResourceChangeRequest. properties: allowRejection: description: |- - AllowRejection controls whether the transaction may be rejected and rolled back + AllowRejection controls whether the change request may be rejected and rolled back on placement failure. When false, the reconciler retries until Accepted or Superseded. type: boolean availabilityZone: - description: AvailabilityZone is the availability zone this transaction - is scoped to. + description: AvailabilityZone is the availability zone this change + request is scoped to. minLength: 1 type: string correlationID: description: CorrelationID is the request/correlation ID. type: string dryRun: - description: DryRun controls whether the transaction commits or only - probes schedulability. + description: DryRun controls whether the change request commits or + only probes schedulability. type: boolean newState: additionalProperties: @@ -179,7 +181,7 @@ spec: A nil value means the corresponding CommittedResource should be deleted. type: object source: - description: Source identifies who created this transaction. + description: Source identifies who created this change request. enum: - api - syncer @@ -189,8 +191,8 @@ spec: - source type: object status: - description: CommittedResourceTransactionStatus defines the observed state - of a CommittedResourceTransaction. + description: CommittedResourceChangeRequestStatus defines the observed + state of a CommittedResourceChangeRequest. properties: conditions: description: Conditions holds Progressing and Finished conditions. @@ -254,7 +256,7 @@ spec: state or terminal outcome. type: string phase: - description: Phase is the current FSM phase of the transaction. + description: Phase is the current FSM phase of the change request. type: string x-kubernetes-validations: - message: phase cannot transition out of a terminal state @@ -355,13 +357,13 @@ spec: - state type: object description: |- - SnapshotBefore records the CommittedResource spec state before this transaction. + SnapshotBefore records the CommittedResource spec state before this change request. Key is commitmentUUID; nil value means the CR did not exist before (will be deleted on rollback). Populated in Preparing when AllowRejection=true. type: object upsertedCommitments: description: |- - UpsertedCommitments holds references to CommittedResource CRDs written by this transaction. + UpsertedCommitments holds references to CommittedResource CRDs written by this change request. Watched in AwaitingScheduling to gate on placement success. These are plain object references, not owner references. items: diff --git a/internal/scheduling/reservations/commitments/changerequests/field_index.go b/internal/scheduling/reservations/commitments/changerequests/field_index.go new file mode 100644 index 000000000..d9db8f76f --- /dev/null +++ b/internal/scheduling/reservations/commitments/changerequests/field_index.go @@ -0,0 +1,39 @@ +// Copyright SAP SE +// SPDX-License-Identifier: Apache-2.0 + +package changerequests + +import ( + "context" + + "github.com/cobaltcore-dev/cortex/api/v1alpha1" + "github.com/cobaltcore-dev/cortex/pkg/multicluster" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +// idxCRCRByUpsertedCR indexes CommittedResourceChangeRequests by each CR name in UpsertedCommitments. +// Used by crToChangeRequest to map a CR watch event back to its owning change request. +const idxCRCRByUpsertedCR = "status.upsertedCommitments.name" + +// indexCRCRByUpsertedCR registers the field index on all clusters that serve CRCR objects. +// Called once from SetupWithManager; no deduplication guard needed. +func indexCRCRByUpsertedCR(ctx context.Context, mcl *multicluster.Client) error { + return mcl.IndexField(ctx, + &v1alpha1.CommittedResourceChangeRequest{}, + &v1alpha1.CommittedResourceChangeRequestList{}, + idxCRCRByUpsertedCR, + func(obj client.Object) []string { + tx, ok := obj.(*v1alpha1.CommittedResourceChangeRequest) + if !ok { + return nil + } + names := make([]string, 0, len(tx.Status.UpsertedCommitments)) + for _, ref := range tx.Status.UpsertedCommitments { + if ref.Name != "" { + names = append(names, ref.Name) + } + } + return names + }, + ) +} diff --git a/internal/scheduling/reservations/commitments/transactions/reconciler.go b/internal/scheduling/reservations/commitments/changerequests/reconciler.go similarity index 75% rename from internal/scheduling/reservations/commitments/transactions/reconciler.go rename to internal/scheduling/reservations/commitments/changerequests/reconciler.go index bf2ddb9ae..9e84afe9e 100644 --- a/internal/scheduling/reservations/commitments/transactions/reconciler.go +++ b/internal/scheduling/reservations/commitments/changerequests/reconciler.go @@ -1,7 +1,7 @@ // Copyright SAP SE // SPDX-License-Identifier: Apache-2.0 -package transactions +package changerequests import ( "context" @@ -23,12 +23,12 @@ import ( "github.com/cobaltcore-dev/cortex/pkg/multicluster" ) -// CommittedResourceTransactionReconciler reconciles CommittedResourceTransaction CRDs. +// CommittedResourceChangeRequestReconciler reconciles CommittedResourceChangeRequest CRDs. // It is the sole writer of CommittedResource.Spec; it reads CommittedResource.Status // to drive its own FSM. It never writes CommittedResource.Status. // // TODO (#783): add perAZActive map[string]types.UID + mu sync.Mutex for per-AZ serialization. -type CommittedResourceTransactionReconciler struct { +type CommittedResourceChangeRequestReconciler struct { client.Client Scheme *runtime.Scheme // AwaitingSchedulingTimeout is how long to wait in AwaitingScheduling before rolling back. @@ -36,11 +36,11 @@ type CommittedResourceTransactionReconciler struct { AwaitingSchedulingTimeout time.Duration } -func (r *CommittedResourceTransactionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *CommittedResourceChangeRequestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { ctx = commitments.WithNewGlobalRequestID(ctx) - logger := commitments.LoggerFromContext(ctx).WithValues("committedResourceTransaction", req.Name) + logger := commitments.LoggerFromContext(ctx).WithValues("committedResourceChangeRequest", req.Name) - var tx v1alpha1.CommittedResourceTransaction + var tx v1alpha1.CommittedResourceChangeRequest if err := r.Get(ctx, req.NamespacedName, &tx); err != nil { return ctrl.Result{}, client.IgnoreNotFound(err) } @@ -52,17 +52,17 @@ func (r *CommittedResourceTransactionReconciler) Reconcile(ctx context.Context, logger.V(1).Info("reconcile entry", "rv", tx.ResourceVersion, "gen", tx.Generation, "phase", tx.Status.Phase) switch tx.Status.Phase { - case "", v1alpha1.TxPhasePreparing: + case "", v1alpha1.ChangeRequestPhasePreparing: return r.reconcilePreparing(ctx, logger, &tx) - case v1alpha1.TxPhaseAllocating: + case v1alpha1.ChangeRequestPhaseAllocating: return r.reconcileAllocating(ctx, logger, &tx) - case v1alpha1.TxPhaseAwaitingScheduling: + case v1alpha1.ChangeRequestPhaseAwaitingScheduling: return r.reconcileAwaitingScheduling(ctx, logger, &tx) - case v1alpha1.TxPhaseRollingBack: + case v1alpha1.ChangeRequestPhaseRollingBack: return r.reconcileRollingBack(ctx, logger, &tx) - case v1alpha1.TxPhaseDryRunCleanup: + case v1alpha1.ChangeRequestPhaseDryRunCleanup: return r.reconcileDryRunCleanup(ctx, logger, &tx) - case v1alpha1.TxPhaseAccepted, v1alpha1.TxPhaseRejected, v1alpha1.TxPhaseSuperseded: + case v1alpha1.ChangeRequestPhaseAccepted, v1alpha1.ChangeRequestPhaseRejected, v1alpha1.ChangeRequestPhaseSuperseded: return ctrl.Result{}, nil default: return ctrl.Result{}, fmt.Errorf("unknown phase %q", tx.Status.Phase) @@ -75,7 +75,7 @@ func commitmentName(uuid string) string { return "commitment-" + uuid } // reconcilePreparing snapshots pre-change CR specs and advances to Allocating. // Always snapshots regardless of AllowRejection — syncer-mode CRTs never roll back but the // snapshot is cheap and keeps this method free of branching. -func (r *CommittedResourceTransactionReconciler) reconcilePreparing(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { +func (r *CommittedResourceChangeRequestReconciler) reconcilePreparing(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { // Snapshot current CR specs for all keys in NewState so rollback can restore them. // nil entry = CR did not exist before = delete it on rollback. snapshot := make(map[string]*v1alpha1.CommittedResourceSpec, len(tx.Spec.NewState)) @@ -94,7 +94,7 @@ func (r *CommittedResourceTransactionReconciler) reconcilePreparing(ctx context. } logger.Info("starting transaction", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "newStateSize", len(tx.Spec.NewState), "allowRejection", tx.Spec.AllowRejection) - return r.advanceTo(ctx, tx, v1alpha1.TxPhaseAllocating, func(s *v1alpha1.CommittedResourceTransactionStatus) { + return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseAllocating, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { s.SnapshotBefore = snapshot }) } @@ -106,7 +106,7 @@ func (r *CommittedResourceTransactionReconciler) reconcilePreparing(ctx context. // UpsertedCommitments and the phase advance must happen in one patch (via the mutate function) // so that a crash between CR spec writes and status persistence does not leave AwaitingScheduling // with an empty UpsertedCommitments list — which would incorrectly advance to Accepted. -func (r *CommittedResourceTransactionReconciler) reconcileAllocating(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { +func (r *CommittedResourceChangeRequestReconciler) reconcileAllocating(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { var upserted []corev1.ObjectReference for uuid, desiredSpec := range tx.Spec.NewState { @@ -138,11 +138,11 @@ func (r *CommittedResourceTransactionReconciler) reconcileAllocating(ctx context } // TODO (#782): syncer-mode recency check before advancing - nextPhase := v1alpha1.TxPhaseAwaitingScheduling + nextPhase := v1alpha1.ChangeRequestPhaseAwaitingScheduling if !tx.Spec.AllowRejection { - nextPhase = v1alpha1.TxPhaseAccepted + nextPhase = v1alpha1.ChangeRequestPhaseAccepted } - return r.advanceTo(ctx, tx, nextPhase, func(s *v1alpha1.CommittedResourceTransactionStatus) { + return r.advanceTo(ctx, tx, nextPhase, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { s.UpsertedCommitments = upserted }) } @@ -152,7 +152,7 @@ func (r *CommittedResourceTransactionReconciler) reconcileAllocating(ctx context // All CRs accepted → Accepted (or DryRunCleanup when DryRun=true). // Any CR rejected → RollingBack. // Not yet settled → wait for the CR watch to re-enqueue. -func (r *CommittedResourceTransactionReconciler) reconcileAwaitingScheduling(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { +func (r *CommittedResourceChangeRequestReconciler) reconcileAwaitingScheduling(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { allAccepted := true acceptedCount := 0 for _, ref := range tx.Status.UpsertedCommitments { @@ -183,7 +183,7 @@ func (r *CommittedResourceTransactionReconciler) reconcileAwaitingScheduling(ctx // Fail-fast: start rollback immediately on first rejection rather than waiting for all CRs // to settle — this unblocks the AZ sooner and the first rejection message is sufficient for diagnosis. logger.Info("CR rejected, rolling back", "cr", ref.Name, "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "message", cond.Message) - return r.advanceTo(ctx, tx, v1alpha1.TxPhaseRollingBack, func(s *v1alpha1.CommittedResourceTransactionStatus) { + return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseRollingBack, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { s.Message = fmt.Sprintf("CR %s rejected: %s", ref.Name, cond.Message) }) default: @@ -195,36 +195,36 @@ func (r *CommittedResourceTransactionReconciler) reconcileAwaitingScheduling(ctx if !allAccepted { logger.V(1).Info("awaiting scheduling", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "ready", acceptedCount, "total", len(tx.Status.UpsertedCommitments)) if r.AwaitingSchedulingTimeout > 0 { - progressingCond := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.TxConditionProgressing) + progressingCond := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing) if progressingCond != nil && time.Since(progressingCond.LastTransitionTime.Time) > r.AwaitingSchedulingTimeout { logger.Info("timed out waiting for scheduling, rolling back", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "timeout", r.AwaitingSchedulingTimeout) - return r.advanceTo(ctx, tx, v1alpha1.TxPhaseRollingBack, func(s *v1alpha1.CommittedResourceTransactionStatus) { + return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseRollingBack, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { s.Message = "timed out waiting for scheduling" meta.SetStatusCondition(&s.Conditions, metav1.Condition{ - Type: v1alpha1.TxConditionProgressing, + Type: v1alpha1.ChangeRequestConditionProgressing, Status: metav1.ConditionFalse, - Reason: v1alpha1.TxReasonTimedOut, + Reason: v1alpha1.ChangeRequestReasonTimedOut, Message: "timed out waiting for scheduling", ObservedGeneration: tx.Generation, }) }) } } - // Primary requeue is event-driven via the CR watch (crToTransaction). + // Primary requeue is event-driven via the CR watch (crToChangeRequest). // RequeueAfter is a safety-net for missed watch events (reconnects, startup races). return ctrl.Result{RequeueAfter: 5 * time.Second}, nil } if tx.Spec.DryRun { - return r.advanceTo(ctx, tx, v1alpha1.TxPhaseDryRunCleanup, nil) + return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseDryRunCleanup, nil) } - return r.advanceTo(ctx, tx, v1alpha1.TxPhaseAccepted, nil) + return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseAccepted, nil) } // reconcileRollingBack converges affected CR specs back to the pre-change SnapshotBefore state. // On restore complete: advances to Rejected. // On permanent cleanup failure: advances to Rejected with reason RollbackFailed. -func (r *CommittedResourceTransactionReconciler) reconcileRollingBack(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { +func (r *CommittedResourceChangeRequestReconciler) reconcileRollingBack(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { // TODO (#780): for each entry in SnapshotBefore, converge CR spec back to snapshot value // using SSA with fieldManager=txFieldManager. // nil snapshot entry → CR did not exist before → delete it. @@ -234,7 +234,7 @@ func (r *CommittedResourceTransactionReconciler) reconcileRollingBack(ctx contex // reconcileDryRunCleanup restores pre-change state after a dry-run would-accept outcome. // Re-uses snapshot rollback logic; terminal phase is Accepted (not Rejected). -func (r *CommittedResourceTransactionReconciler) reconcileDryRunCleanup(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceTransaction) (ctrl.Result, error) { +func (r *CommittedResourceChangeRequestReconciler) reconcileDryRunCleanup(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { // TODO (#780): same convergence as reconcileRollingBack; terminal → advanceTo(Accepted, nil). return ctrl.Result{}, nil } @@ -246,11 +246,11 @@ func (r *CommittedResourceTransactionReconciler) reconcileDryRunCleanup(ctx cont // // Status().Update is used (not Patch) so that nil map values in SnapshotBefore and similar // fields are preserved — JSON merge patch semantics treat null map values as deletions. -func (r *CommittedResourceTransactionReconciler) advanceTo( +func (r *CommittedResourceChangeRequestReconciler) advanceTo( ctx context.Context, - tx *v1alpha1.CommittedResourceTransaction, - phase v1alpha1.TxPhase, - mutate func(*v1alpha1.CommittedResourceTransactionStatus), + tx *v1alpha1.CommittedResourceChangeRequest, + phase v1alpha1.ChangeRequestPhase, + mutate func(*v1alpha1.CommittedResourceChangeRequestStatus), ) (ctrl.Result, error) { tx.Status.Phase = phase @@ -261,7 +261,7 @@ func (r *CommittedResourceTransactionReconciler) advanceTo( if tx.IsTerminal() { progressingStatus, progressingReason = metav1.ConditionFalse, string(phase) meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ - Type: v1alpha1.TxConditionFinished, + Type: v1alpha1.ChangeRequestConditionFinished, Status: metav1.ConditionTrue, Reason: string(phase), ObservedGeneration: tx.Generation, @@ -269,10 +269,10 @@ func (r *CommittedResourceTransactionReconciler) advanceTo( } // Preserve a Progressing=False condition already stamped by the mutate func (e.g. TimedOut). // Terminal phases always overwrite to ensure Progressing=False/. - existingProgressing := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.TxConditionProgressing) + existingProgressing := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing) if tx.IsTerminal() || existingProgressing == nil || existingProgressing.Status != metav1.ConditionFalse { meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ - Type: v1alpha1.TxConditionProgressing, + Type: v1alpha1.ChangeRequestConditionProgressing, Status: progressingStatus, Reason: progressingReason, ObservedGeneration: tx.Generation, @@ -286,11 +286,11 @@ func (r *CommittedResourceTransactionReconciler) advanceTo( // markError stamps Progressing=False/Error on the transaction as a best-effort status update. // The update error is logged but not returned — callers return the original error for requeue. -func (r *CommittedResourceTransactionReconciler) markError(ctx context.Context, tx *v1alpha1.CommittedResourceTransaction, msg string) { +func (r *CommittedResourceChangeRequestReconciler) markError(ctx context.Context, tx *v1alpha1.CommittedResourceChangeRequest, msg string) { meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ - Type: v1alpha1.TxConditionProgressing, + Type: v1alpha1.ChangeRequestConditionProgressing, Status: metav1.ConditionFalse, - Reason: v1alpha1.TxReasonError, + Reason: v1alpha1.ChangeRequestReasonError, Message: msg, ObservedGeneration: tx.Generation, }) @@ -302,15 +302,15 @@ func (r *CommittedResourceTransactionReconciler) markError(ctx context.Context, // SetupWithManager registers the controller and configures watches. // TODO (#783): set MaxConcurrentReconciles: 1 via WithOptions(controller.Options{MaxConcurrentReconciles: 1}). // TODO (#783): add predicate on WatchesMulticluster() to re-trigger same-AZ non-terminal siblings on terminal transition. -func (r *CommittedResourceTransactionReconciler) SetupWithManager(mgr ctrl.Manager, mcl *multicluster.Client) error { +func (r *CommittedResourceChangeRequestReconciler) SetupWithManager(mgr ctrl.Manager, mcl *multicluster.Client) error { ctx := context.Background() - if err := indexCRTByUpsertedCR(ctx, mcl); err != nil { - return fmt.Errorf("failed to set up CRT-by-upserted-CR field index: %w", err) + if err := indexCRCRByUpsertedCR(ctx, mcl); err != nil { + return fmt.Errorf("failed to set up CRCR-by-upserted-CR field index: %w", err) } bldr := multicluster.BuildController(mcl, mgr) var err error bldr, err = bldr.WatchesMulticluster( - &v1alpha1.CommittedResourceTransaction{}, + &v1alpha1.CommittedResourceChangeRequest{}, &handler.EnqueueRequestForObject{}, ) if err != nil { @@ -319,19 +319,19 @@ func (r *CommittedResourceTransactionReconciler) SetupWithManager(mgr ctrl.Manag // Re-enqueues this transaction when a CR it upserted settles placement. bldr, err = bldr.WatchesMulticluster( &v1alpha1.CommittedResource{}, - handler.EnqueueRequestsFromMapFunc(r.crToTransaction), + handler.EnqueueRequestsFromMapFunc(r.crToChangeRequest), ) if err != nil { return err } - return bldr.Named("committed-resource-transaction").Complete(r) + return bldr.Named("committed-resource-change-request").Complete(r) } -// crToTransaction maps a CommittedResource watch event back to the transaction that upserted it. -func (r *CommittedResourceTransactionReconciler) crToTransaction(ctx context.Context, obj client.Object) []ctrl.Request { - var txList v1alpha1.CommittedResourceTransactionList - if err := r.List(ctx, &txList, client.MatchingFields{idxCRTByUpsertedCR: obj.GetName()}); err != nil { - commitments.LoggerFromContext(ctx).Error(err, "failed to list CRTs by upserted CR", "cr", obj.GetName()) +// crToChangeRequest maps a CommittedResource watch event back to the transaction that upserted it. +func (r *CommittedResourceChangeRequestReconciler) crToChangeRequest(ctx context.Context, obj client.Object) []ctrl.Request { + var txList v1alpha1.CommittedResourceChangeRequestList + if err := r.List(ctx, &txList, client.MatchingFields{idxCRCRByUpsertedCR: obj.GetName()}); err != nil { + commitments.LoggerFromContext(ctx).Error(err, "failed to list CRCRs by upserted CR", "cr", obj.GetName()) return nil } reqs := make([]ctrl.Request, 0, len(txList.Items)) diff --git a/internal/scheduling/reservations/commitments/transactions/reconciler_test.go b/internal/scheduling/reservations/commitments/changerequests/reconciler_test.go similarity index 81% rename from internal/scheduling/reservations/commitments/transactions/reconciler_test.go rename to internal/scheduling/reservations/commitments/changerequests/reconciler_test.go index b459f270b..d4f9ae236 100644 --- a/internal/scheduling/reservations/commitments/transactions/reconciler_test.go +++ b/internal/scheduling/reservations/commitments/changerequests/reconciler_test.go @@ -1,7 +1,7 @@ // Copyright SAP SE // SPDX-License-Identifier: Apache-2.0 -package transactions +package changerequests import ( "context" @@ -42,11 +42,11 @@ func testClient(t *testing.T, scheme *runtime.Scheme, objects ...client.Object) WithScheme(scheme). WithObjects(objects...). WithStatusSubresource( - &v1alpha1.CommittedResourceTransaction{}, + &v1alpha1.CommittedResourceChangeRequest{}, &v1alpha1.CommittedResource{}, ). - WithIndex(&v1alpha1.CommittedResourceTransaction{}, idxCRTByUpsertedCR, func(obj client.Object) []string { - tx, ok := obj.(*v1alpha1.CommittedResourceTransaction) + WithIndex(&v1alpha1.CommittedResourceChangeRequest{}, idxCRCRByUpsertedCR, func(obj client.Object) []string { + tx, ok := obj.(*v1alpha1.CommittedResourceChangeRequest) if !ok { return nil } @@ -61,11 +61,11 @@ func testClient(t *testing.T, scheme *runtime.Scheme, objects ...client.Object) Build() } -func testReconciler(c client.Client, scheme *runtime.Scheme) *CommittedResourceTransactionReconciler { - return &CommittedResourceTransactionReconciler{Client: c, Scheme: scheme} +func testReconciler(c client.Client, scheme *runtime.Scheme) *CommittedResourceChangeRequestReconciler { + return &CommittedResourceChangeRequestReconciler{Client: c, Scheme: scheme} } -func doReconcile(t *testing.T, r *CommittedResourceTransactionReconciler, name string) ctrl.Result { +func doReconcile(t *testing.T, r *CommittedResourceChangeRequestReconciler, name string) ctrl.Result { t.Helper() res, err := r.Reconcile(testCtx, ctrl.Request{NamespacedName: types.NamespacedName{Name: name}}) if err != nil { @@ -74,11 +74,11 @@ func doReconcile(t *testing.T, r *CommittedResourceTransactionReconciler, name s return res } -func getTx(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResourceTransaction { +func getTx(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResourceChangeRequest { t.Helper() - var tx v1alpha1.CommittedResourceTransaction + var tx v1alpha1.CommittedResourceChangeRequest if err := c.Get(testCtx, types.NamespacedName{Name: name}, &tx); err != nil { - t.Fatalf("get CRT %q: %v", name, err) + t.Fatalf("get CRCR %q: %v", name, err) } return &tx } @@ -93,7 +93,7 @@ func getCR(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResour } // driveToPhase calls Reconcile until the CRT reaches target, up to 5 attempts. -func driveToPhase(t *testing.T, r *CommittedResourceTransactionReconciler, c client.Client, txName string, target v1alpha1.TxPhase) { +func driveToPhase(t *testing.T, r *CommittedResourceChangeRequestReconciler, c client.Client, txName string, target v1alpha1.ChangeRequestPhase) { t.Helper() const maxSteps = 5 for range maxSteps { @@ -148,7 +148,7 @@ func backdateProgressingCondition(t *testing.T, c client.Client, txName string, t.Helper() tx := getTx(t, c, txName) for i, cond := range tx.Status.Conditions { - if cond.Type == v1alpha1.TxConditionProgressing { + if cond.Type == v1alpha1.ChangeRequestConditionProgressing { tx.Status.Conditions[i].LastTransitionTime = metav1.NewTime(time.Now().Add(-age)) break } @@ -197,8 +197,8 @@ func TestCRTReconciler_Preparing(t *testing.T) { tests := []struct { name string preObjects []client.Object - tx *v1alpha1.CommittedResourceTransaction - wantPhase v1alpha1.TxPhase + tx *v1alpha1.CommittedResourceChangeRequest + wantPhase v1alpha1.ChangeRequestPhase wantSnapshotNilFor []string // UUIDs that must have nil snapshot entry (CR did not exist) wantSnapshotSetFor []string // UUIDs that must have non-nil snapshot entry (CR existed) wantProgressingStatus metav1.ConditionStatus @@ -206,16 +206,16 @@ func TestCRTReconciler_Preparing(t *testing.T) { }{ { name: "no pre-existing CR: snapshot entry is nil, phase advances to Allocating", - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-1"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-a": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.TxPhaseAllocating, + wantPhase: v1alpha1.ChangeRequestPhaseAllocating, wantSnapshotNilFor: []string{"uuid-a"}, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", @@ -228,32 +228,32 @@ func TestCRTReconciler_Preparing(t *testing.T) { Spec: *minimalCRSpec("az-1"), }, }, - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-2"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-b": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.TxPhaseAllocating, + wantPhase: v1alpha1.ChangeRequestPhaseAllocating, wantSnapshotSetFor: []string{"uuid-b"}, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, { name: "Preparing idempotent: running reconcile twice does not error", - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-3"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-c": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.TxPhaseAllocating, + wantPhase: v1alpha1.ChangeRequestPhaseAllocating, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -282,7 +282,7 @@ func TestCRTReconciler_Preparing(t *testing.T) { t.Errorf("phase: want %q, got %q", tt.wantPhase, tx.Status.Phase) } if tt.wantProgressingStatus != "" { - assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) } for _, uuid := range tt.wantSnapshotNilFor { snap, ok := tx.Status.SnapshotBefore[uuid] @@ -316,8 +316,8 @@ func TestCRTReconciler_Allocating(t *testing.T) { tests := []struct { name string preObjects []client.Object - tx *v1alpha1.CommittedResourceTransaction - wantPhase v1alpha1.TxPhase + tx *v1alpha1.CommittedResourceChangeRequest + wantPhase v1alpha1.ChangeRequestPhase wantCRExists []string wantCRAbsent []string wantUpsertedCommitments int @@ -328,16 +328,16 @@ func TestCRTReconciler_Allocating(t *testing.T) { }{ { name: "API-mode: creates CR and advances to AwaitingScheduling", - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-1"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-1": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-1"}, wantUpsertedCommitments: 1, wantProgressingStatus: metav1.ConditionTrue, @@ -345,22 +345,22 @@ func TestCRTReconciler_Allocating(t *testing.T) { }, { name: "syncer-mode: applies CR and advances directly to Accepted (no scheduling gate)", - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-2"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceSyncer, AllowRejection: false, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-2": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.TxPhaseAccepted, + wantPhase: v1alpha1.ChangeRequestPhaseAccepted, wantCRExists: []string{"commitment-uuid-2"}, wantUpsertedCommitments: 1, wantProgressingStatus: metav1.ConditionFalse, - wantProgressingReason: string(v1alpha1.TxPhaseAccepted), + wantProgressingReason: string(v1alpha1.ChangeRequestPhaseAccepted), wantFinished: true, - wantFinishedReason: string(v1alpha1.TxPhaseAccepted), + wantFinishedReason: string(v1alpha1.ChangeRequestPhaseAccepted), }, { name: "nil spec deletes CR and it is absent from UpsertedCommitments", @@ -370,16 +370,16 @@ func TestCRTReconciler_Allocating(t *testing.T) { Spec: *minimalCRSpec("az-1"), }, }, - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-3"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-3": nil}, }, }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantCRAbsent: []string{"commitment-uuid-3"}, wantUpsertedCommitments: 0, wantProgressingStatus: metav1.ConditionTrue, @@ -393,16 +393,16 @@ func TestCRTReconciler_Allocating(t *testing.T) { Spec: *minimalCRSpec("az-old"), }, }, - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-4"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-4": minimalCRSpec("az-new")}, }, }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-4"}, wantUpsertedCommitments: 1, wantProgressingStatus: metav1.ConditionTrue, @@ -427,10 +427,10 @@ func TestCRTReconciler_Allocating(t *testing.T) { t.Errorf("UpsertedCommitments len: want %d, got %d", tt.wantUpsertedCommitments, got) } if tt.wantProgressingStatus != "" { - assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) } if tt.wantFinished { - assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) } for _, name := range tt.wantCRExists { getCR(t, c, name) // fatals if absent @@ -449,9 +449,9 @@ func TestCRTReconciler_Allocating(t *testing.T) { // desired spec is idempotent: no error, phase advances correctly again. func TestCRTReconciler_Allocating_Idempotent(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -461,11 +461,11 @@ func TestCRTReconciler_Allocating_Idempotent(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) // Reset to Allocating to re-run it. stored := getTx(t, c, tx.Name) - stored.Status.Phase = v1alpha1.TxPhaseAllocating + stored.Status.Phase = v1alpha1.ChangeRequestPhaseAllocating if err := c.Status().Update(testCtx, stored); err != nil { t.Fatalf("reset phase to Allocating: %v", err) } @@ -473,7 +473,7 @@ func TestCRTReconciler_Allocating_Idempotent(t *testing.T) { doReconcile(t, r, tx.Name) // second run of Allocating final := getTx(t, c, tx.Name) - if final.Status.Phase != v1alpha1.TxPhaseAwaitingScheduling { + if final.Status.Phase != v1alpha1.ChangeRequestPhaseAwaitingScheduling { t.Errorf("idempotent Allocating: want AwaitingScheduling, got %q", final.Status.Phase) } } @@ -487,12 +487,12 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { const crName = "commitment-uuid-await" // setup drives a CRT to AwaitingScheduling and returns the client and reconciler. - setup := func(t *testing.T, txOpts ...func(*v1alpha1.CommittedResourceTransaction)) (client.Client, *CommittedResourceTransactionReconciler) { + setup := func(t *testing.T, txOpts ...func(*v1alpha1.CommittedResourceChangeRequest)) (client.Client, *CommittedResourceChangeRequestReconciler) { t.Helper() scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: txName}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -504,17 +504,17 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { } c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, txName, v1alpha1.TxPhaseAwaitingScheduling) + driveToPhase(t, r, c, txName, v1alpha1.ChangeRequestPhaseAwaitingScheduling) return c, r } tests := []struct { name string - txOpts []func(*v1alpha1.CommittedResourceTransaction) + txOpts []func(*v1alpha1.CommittedResourceChangeRequest) setupCR func(t *testing.T, c client.Client) setupTx func(t *testing.T, c client.Client) - reconcilerOpt func(r *CommittedResourceTransactionReconciler) - wantPhase v1alpha1.TxPhase + reconcilerOpt func(r *CommittedResourceChangeRequestReconciler) + wantPhase v1alpha1.ChangeRequestPhase wantMessage string // substring that must appear in tx.Status.Message; empty = no check wantProgressingStatus metav1.ConditionStatus wantProgressingReason string @@ -526,21 +526,21 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") }, - wantPhase: v1alpha1.TxPhaseAccepted, + wantPhase: v1alpha1.ChangeRequestPhaseAccepted, wantProgressingStatus: metav1.ConditionFalse, - wantProgressingReason: string(v1alpha1.TxPhaseAccepted), + wantProgressingReason: string(v1alpha1.ChangeRequestPhaseAccepted), wantFinished: true, - wantFinishedReason: string(v1alpha1.TxPhaseAccepted), + wantFinishedReason: string(v1alpha1.ChangeRequestPhaseAccepted), }, { name: "DryRun=true and all CRs accepted → DryRunCleanup", - txOpts: []func(*v1alpha1.CommittedResourceTransaction){ - func(tx *v1alpha1.CommittedResourceTransaction) { tx.Spec.DryRun = true }, + txOpts: []func(*v1alpha1.CommittedResourceChangeRequest){ + func(tx *v1alpha1.CommittedResourceChangeRequest) { tx.Spec.DryRun = true }, }, setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") }, - wantPhase: v1alpha1.TxPhaseDryRunCleanup, + wantPhase: v1alpha1.ChangeRequestPhaseDryRunCleanup, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -549,7 +549,7 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionFalse, v1alpha1.CommittedResourceReasonRejected, "no capacity on host") }, - wantPhase: v1alpha1.TxPhaseRollingBack, + wantPhase: v1alpha1.ChangeRequestPhaseRollingBack, wantMessage: "no capacity on host", wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", @@ -559,7 +559,7 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReadyStaleGen(t, c, crName) }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -568,28 +568,28 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonReserving, "still placing") }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, { name: "timeout exceeded → RollingBack with timed out message", - reconcilerOpt: func(r *CommittedResourceTransactionReconciler) { + reconcilerOpt: func(r *CommittedResourceChangeRequestReconciler) { r.AwaitingSchedulingTimeout = 50 * time.Millisecond }, setupTx: func(t *testing.T, c client.Client) { backdateProgressingCondition(t, c, txName, 1*time.Second) }, - wantPhase: v1alpha1.TxPhaseRollingBack, + wantPhase: v1alpha1.ChangeRequestPhaseRollingBack, wantMessage: "timed out", wantProgressingStatus: metav1.ConditionFalse, - wantProgressingReason: v1alpha1.TxReasonTimedOut, + wantProgressingReason: v1alpha1.ChangeRequestReasonTimedOut, }, { name: "timeout=0 means no timeout: CRT stays in AwaitingScheduling indefinitely", // No reconcilerOpt, so AwaitingSchedulingTimeout=0 (disabled). // CR ready condition is never set → should stay in AwaitingScheduling. - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -619,17 +619,17 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { t.Errorf("message: want substring %q in %q", tt.wantMessage, tx.Status.Message) } if tt.wantProgressingStatus != "" { - assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) } if tt.wantFinished { - assertCondition(t, tx.Status.Conditions, v1alpha1.TxConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) } }) } } // setPhase directly sets a CRT's phase via Status().Update for test setup. -func setPhase(t *testing.T, c client.Client, txName string, phase v1alpha1.TxPhase) { +func setPhase(t *testing.T, c client.Client, txName string, phase v1alpha1.ChangeRequestPhase) { t.Helper() tx := getTx(t, c, txName) tx.Status.Phase = phase @@ -639,7 +639,7 @@ func setPhase(t *testing.T, c client.Client, txName string, phase v1alpha1.TxPha } // reconcileN calls doReconcile exactly n times without inspecting the phase between calls. -func reconcileN(t *testing.T, r *CommittedResourceTransactionReconciler, name string, n int) { +func reconcileN(t *testing.T, r *CommittedResourceChangeRequestReconciler, name string, n int) { t.Helper() for range n { doReconcile(t, r, name) @@ -663,9 +663,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "Preparing: snapshot is consistent across re-runs", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-1"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-1": minimalCRSpec("az-1")}, }, @@ -694,9 +694,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "Allocating: UpsertedCommitments and CR spec stable across re-runs", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-2"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-2": minimalCRSpec("az-1")}, }, @@ -704,11 +704,11 @@ func TestCRTReconciler_Idempotency(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) upserted1 := getTx(t, c, tx.Name).Status.UpsertedCommitments - setPhase(t, c, tx.Name, v1alpha1.TxPhaseAllocating) // reset to Allocating - doReconcile(t, r, tx.Name) // Allocating → AwaitingScheduling (re-run) + setPhase(t, c, tx.Name, v1alpha1.ChangeRequestPhaseAllocating) // reset to Allocating + doReconcile(t, r, tx.Name) // Allocating → AwaitingScheduling (re-run) upserted2 := getTx(t, c, tx.Name).Status.UpsertedCommitments if len(upserted1) != 1 || len(upserted2) != 1 { @@ -724,9 +724,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "AwaitingScheduling: multiple calls without CR ready stay waiting", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-3"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-3": minimalCRSpec("az-1")}, }, @@ -734,10 +734,10 @@ func TestCRTReconciler_Idempotency(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) reconcileN(t, r, tx.Name, 3) // CR never set ready — must stay in AwaitingScheduling - if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.TxPhaseAwaitingScheduling { + if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.ChangeRequestPhaseAwaitingScheduling { t.Errorf("phase: want AwaitingScheduling, got %q", got) } }, @@ -746,9 +746,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "AwaitingScheduling → Accepted stays Accepted on repeated reconciles", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-4"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-4": minimalCRSpec("az-1")}, }, @@ -756,11 +756,11 @@ func TestCRTReconciler_Idempotency(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) setCRReady(t, c, "commitment-uuid-idem-4", metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") reconcileN(t, r, tx.Name, 3) // first call → Accepted; subsequent calls → terminal no-op - if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.TxPhaseAccepted { + if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.ChangeRequestPhaseAccepted { t.Errorf("phase: want Accepted, got %q", got) } }, @@ -799,9 +799,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { // pre-existing CRs before the transaction is created preObjects []client.Object // the CRT to create - tx *v1alpha1.CommittedResourceTransaction + tx *v1alpha1.CommittedResourceChangeRequest // expected phase after driving the FSM to a stable point - wantPhase v1alpha1.TxPhase + wantPhase v1alpha1.ChangeRequestPhase // CR names that must exist after Allocating wantCRExists []string // CR names that must be absent after Allocating @@ -815,9 +815,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }{ { name: "multiple new CRs: all created, all upserted", - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-1"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -828,7 +828,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-a1", "commitment-uuid-a2", "commitment-uuid-a3"}, wantUpsertedCount: 3, wantSnapshotNilFor: []string{"uuid-a1", "uuid-a2", "uuid-a3"}, @@ -841,9 +841,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { Spec: *minimalCRSpec("az-old"), }, }, - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-2"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -853,7 +853,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-b1", "commitment-uuid-b2"}, wantUpsertedCount: 2, wantSnapshotNilFor: []string{"uuid-b1"}, @@ -871,9 +871,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { Spec: *minimalCRSpec("az-1"), }, }, - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-3"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -883,7 +883,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, }, - wantPhase: v1alpha1.TxPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-c2"}, wantCRAbsent: []string{"commitment-uuid-c1"}, wantUpsertedCount: 1, @@ -891,9 +891,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, { name: "empty NewState: no CRs touched, advances all the way to Accepted", - tx: &v1alpha1.CommittedResourceTransaction{ + tx: &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-4"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -901,7 +901,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, // AwaitingScheduling with empty UpsertedCommitments → allAccepted=true immediately → Accepted - wantPhase: v1alpha1.TxPhaseAccepted, + wantPhase: v1alpha1.ChangeRequestPhaseAccepted, wantUpsertedCount: 0, }, } @@ -961,14 +961,14 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { t.Run("DeletionTimestamp: reconcile is a no-op", func(t *testing.T) { scheme := testScheme(t) now := metav1.Now() - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{ Name: "tx-del", DeletionTimestamp: &now, // Finalizers required for the fake client to allow a non-zero DeletionTimestamp. Finalizers: []string{"test.io/hold"}, }, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, }, @@ -986,9 +986,9 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { t.Run("unknown phase returns error", func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-unknown"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, }, @@ -1010,9 +1010,9 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { t.Run("crToTransaction maps CR name to owning CRT via field index", func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-map"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -1023,11 +1023,11 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { r := testReconciler(c, scheme) // Drive to AwaitingScheduling so UpsertedCommitments is populated. - driveToPhase(t, r, c, tx.Name, v1alpha1.TxPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) // crToTransaction should return a request for the owning CRT. cr := getCR(t, c, "commitment-uuid-map") - reqs := r.crToTransaction(testCtx, cr) + reqs := r.crToChangeRequest(testCtx, cr) if len(reqs) != 1 { t.Fatalf("crToTransaction: want 1 request, got %d", len(reqs)) } @@ -1044,19 +1044,19 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { func TestCRTReconciler_Terminal(t *testing.T) { tests := []struct { name string - phase v1alpha1.TxPhase + phase v1alpha1.ChangeRequestPhase }{ - {"Accepted is a no-op", v1alpha1.TxPhaseAccepted}, - {"Rejected is a no-op", v1alpha1.TxPhaseRejected}, - {"Superseded is a no-op", v1alpha1.TxPhaseSuperseded}, + {"Accepted is a no-op", v1alpha1.ChangeRequestPhaseAccepted}, + {"Rejected is a no-op", v1alpha1.ChangeRequestPhaseRejected}, + {"Superseded is a no-op", v1alpha1.ChangeRequestPhaseSuperseded}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceTransaction{ + tx := &v1alpha1.CommittedResourceChangeRequest{ ObjectMeta: metav1.ObjectMeta{Name: "tx-term"}, - Spec: v1alpha1.CommittedResourceTransactionSpec{ + Spec: v1alpha1.CommittedResourceChangeRequestSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, }, diff --git a/internal/scheduling/reservations/commitments/transactions/field_index.go b/internal/scheduling/reservations/commitments/transactions/field_index.go deleted file mode 100644 index d6f375f61..000000000 --- a/internal/scheduling/reservations/commitments/transactions/field_index.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright SAP SE -// SPDX-License-Identifier: Apache-2.0 - -package transactions - -import ( - "context" - - "github.com/cobaltcore-dev/cortex/api/v1alpha1" - "github.com/cobaltcore-dev/cortex/pkg/multicluster" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -// idxCRTByUpsertedCR indexes CommittedResourceTransactions by each CR name in UpsertedCommitments. -// Used by crToTransaction to map a CR watch event back to its owning transaction. -const idxCRTByUpsertedCR = "status.upsertedCommitments.name" - -// indexCRTByUpsertedCR registers the field index on all clusters that serve CRT objects. -// Called once from SetupWithManager; no deduplication guard needed. -func indexCRTByUpsertedCR(ctx context.Context, mcl *multicluster.Client) error { - return mcl.IndexField(ctx, - &v1alpha1.CommittedResourceTransaction{}, - &v1alpha1.CommittedResourceTransactionList{}, - idxCRTByUpsertedCR, - func(obj client.Object) []string { - tx, ok := obj.(*v1alpha1.CommittedResourceTransaction) - if !ok { - return nil - } - names := make([]string, 0, len(tx.Status.UpsertedCommitments)) - for _, ref := range tx.Status.UpsertedCommitments { - if ref.Name != "" { - names = append(names, ref.Name) - } - } - return names - }, - ) -} From c5393033e8a62f3d75fcb6897805304016aa4419 Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Thu, 29 Oct 2026 16:20:09 +0100 Subject: [PATCH 8/8] change naming again Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- ....go => committed_resource_change_types.go} | 94 ++++---- api/v1alpha1/zz_generated.deepcopy.go | 38 +-- ...itted-resource-transaction-architecture.md | 74 +++--- .../cortex-nova/templates/kpis_kvm.yaml | 2 +- helm/bundles/cortex-nova/values.yaml | 6 +- ...ortex.cloud_committedresourcechanges.yaml} | 22 +- .../field_index.go | 18 +- .../{changerequests => changes}/reconciler.go | 90 +++---- .../reconciler_test.go | 226 +++++++++--------- 9 files changed, 285 insertions(+), 285 deletions(-) rename api/v1alpha1/{committed_resource_change_request_types.go => committed_resource_change_types.go} (57%) rename helm/library/cortex/files/crds/{cortex.cloud_committedresourcechangerequests.yaml => cortex.cloud_committedresourcechanges.yaml} (97%) rename internal/scheduling/reservations/commitments/{changerequests => changes}/field_index.go (56%) rename internal/scheduling/reservations/commitments/{changerequests => changes}/reconciler.go (77%) rename internal/scheduling/reservations/commitments/{changerequests => changes}/reconciler_test.go (81%) diff --git a/api/v1alpha1/committed_resource_change_request_types.go b/api/v1alpha1/committed_resource_change_types.go similarity index 57% rename from api/v1alpha1/committed_resource_change_request_types.go rename to api/v1alpha1/committed_resource_change_types.go index 2531a1a92..29fb90e13 100644 --- a/api/v1alpha1/committed_resource_change_request_types.go +++ b/api/v1alpha1/committed_resource_change_types.go @@ -8,29 +8,29 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// ChangeRequestPhase represents the FSM phase of a CommittedResourceChangeRequest. -type ChangeRequestPhase string +// ChangePhase represents the FSM phase of a CommittedResourceChange. +type ChangePhase string const ( - // ChangeRequestPhasePreparing: change request created; sanity checks and optional snapshot in progress. - ChangeRequestPhasePreparing ChangeRequestPhase = "Preparing" - // ChangeRequestPhaseAllocating: CR specs are being written. - ChangeRequestPhaseAllocating ChangeRequestPhase = "Allocating" - // ChangeRequestPhaseAwaitingScheduling: CR specs written; waiting for placement success. - ChangeRequestPhaseAwaitingScheduling ChangeRequestPhase = "AwaitingScheduling" - // ChangeRequestPhaseRollingBack: a CR was rejected or timed out; restoring pre-change state. - ChangeRequestPhaseRollingBack ChangeRequestPhase = "RollingBack" - // ChangeRequestPhaseDryRunCleanup: dry-run would-accept; restoring pre-change state. - ChangeRequestPhaseDryRunCleanup ChangeRequestPhase = "DryRunCleanup" - // ChangeRequestPhaseAccepted: terminal; change request committed successfully. - ChangeRequestPhaseAccepted ChangeRequestPhase = "Accepted" - // ChangeRequestPhaseRejected: terminal; change request rolled back. - ChangeRequestPhaseRejected ChangeRequestPhase = "Rejected" - // ChangeRequestPhaseSuperseded: terminal; abandoned because a newer change request for the same AZ took over. - ChangeRequestPhaseSuperseded ChangeRequestPhase = "Superseded" + // ChangePhasePreparing: change request created; sanity checks and optional snapshot in progress. + ChangePhasePreparing ChangePhase = "Preparing" + // ChangePhaseAllocating: CR specs are being written. + ChangePhaseAllocating ChangePhase = "Allocating" + // ChangePhaseAwaitingScheduling: CR specs written; waiting for placement success. + ChangePhaseAwaitingScheduling ChangePhase = "AwaitingScheduling" + // ChangePhaseRollingBack: a CR was rejected or timed out; restoring pre-change state. + ChangePhaseRollingBack ChangePhase = "RollingBack" + // ChangePhaseDryRunCleanup: dry-run would-accept; restoring pre-change state. + ChangePhaseDryRunCleanup ChangePhase = "DryRunCleanup" + // ChangePhaseAccepted: terminal; change request committed successfully. + ChangePhaseAccepted ChangePhase = "Accepted" + // ChangePhaseRejected: terminal; change request rolled back. + ChangePhaseRejected ChangePhase = "Rejected" + // ChangePhaseSuperseded: terminal; abandoned because a newer change request for the same AZ took over. + ChangePhaseSuperseded ChangePhase = "Superseded" ) -// TxSource identifies the originator of a CommittedResourceChangeRequest. +// TxSource identifies the originator of a CommittedResourceChange. type TxSource string const ( @@ -41,27 +41,27 @@ const ( ) const ( - // ChangeRequestConditionProgressing is True while the change request is advancing normally. + // ChangeConditionProgressing is True while the change request is advancing normally. // False + reason indicates a problem while still non-terminal. - ChangeRequestConditionProgressing = "Progressing" + ChangeConditionProgressing = "Progressing" - // ChangeRequestConditionFinished is True once the change request has reached a terminal phase. - ChangeRequestConditionFinished = "Finished" + // ChangeConditionFinished is True once the change request has reached a terminal phase. + ChangeConditionFinished = "Finished" - // Reasons for ChangeRequestConditionProgressing=False (non-terminal problems). - ChangeRequestReasonStalled = "Stalled" - ChangeRequestReasonTimedOut = "TimedOut" - ChangeRequestReasonError = "Error" - ChangeRequestReasonRetryExhausted = "RetryExhausted" + // Reasons for ChangeConditionProgressing=False (non-terminal problems). + ChangeReasonStalled = "Stalled" + ChangeReasonTimedOut = "TimedOut" + ChangeReasonError = "Error" + ChangeReasonRetryExhausted = "RetryExhausted" - // Reasons for ChangeRequestConditionFinished=True. + // Reasons for ChangeConditionFinished=True. ChangeRequestReasonAccepted = "Accepted" ChangeRequestReasonRejected = "Rejected" ChangeRequestReasonSuperseded = "Superseded" ) -// CommittedResourceChangeRequestSpec defines the desired change for a CommittedResourceChangeRequest. -type CommittedResourceChangeRequestSpec struct { +// CommittedResourceChangeSpec defines the desired change for a CommittedResourceChange. +type CommittedResourceChangeSpec struct { // CorrelationID is the request/correlation ID. // +kubebuilder:validation:Optional CorrelationID string `json:"correlationID,omitempty"` @@ -91,12 +91,12 @@ type CommittedResourceChangeRequestSpec struct { NewState map[string]*CommittedResourceSpec `json:"newState,omitempty"` } -// CommittedResourceChangeRequestStatus defines the observed state of a CommittedResourceChangeRequest. -type CommittedResourceChangeRequestStatus struct { +// CommittedResourceChangeStatus defines the observed state of a CommittedResourceChange. +type CommittedResourceChangeStatus struct { // Phase is the current FSM phase of the change request. // +kubebuilder:validation:Optional // +kubebuilder:validation:XValidation:rule="oldSelf == '' || !['Accepted','Rejected','Superseded'].exists(t, t == oldSelf) || self == oldSelf",message="phase cannot transition out of a terminal state" - Phase ChangeRequestPhase `json:"phase,omitempty"` + Phase ChangePhase `json:"phase,omitempty"` // SnapshotBefore records the CommittedResource spec state before this change request. // Key is commitmentUUID; nil value means the CR did not exist before (will be deleted on rollback). @@ -121,7 +121,7 @@ type CommittedResourceChangeRequestStatus struct { // +kubebuilder:object:root=true // +kubebuilder:subresource:status -// +kubebuilder:resource:scope=Cluster,shortName=crcr +// +kubebuilder:resource:scope=Cluster,shortName=crc // +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase" // +kubebuilder:printcolumn:name="AZ",type="string",JSONPath=".spec.availabilityZone" // +kubebuilder:printcolumn:name="Source",type="string",JSONPath=".spec.source" @@ -129,36 +129,36 @@ type CommittedResourceChangeRequestStatus struct { // +kubebuilder:printcolumn:name="Progressing",type="string",JSONPath=".status.conditions[?(@.type=='Progressing')].status",priority=1 // +kubebuilder:printcolumn:name="Finished",type="string",JSONPath=".status.conditions[?(@.type=='Finished')].status",priority=1 -// CommittedResourceChangeRequest is the Schema for the committedresourcechangerequests API -type CommittedResourceChangeRequest struct { +// CommittedResourceChange is the Schema for the committedresourcechanges API +type CommittedResourceChange struct { metav1.TypeMeta `json:",inline"` // +optional metav1.ObjectMeta `json:"metadata,omitempty,omitzero"` // +required - Spec CommittedResourceChangeRequestSpec `json:"spec"` + Spec CommittedResourceChangeSpec `json:"spec"` // +optional - Status CommittedResourceChangeRequestStatus `json:"status,omitempty,omitzero"` + Status CommittedResourceChangeStatus `json:"status,omitempty,omitzero"` } // IsTerminal reports whether the change request has reached a terminal phase. -func (tx *CommittedResourceChangeRequest) IsTerminal() bool { - return tx.Status.Phase == ChangeRequestPhaseAccepted || - tx.Status.Phase == ChangeRequestPhaseRejected || - tx.Status.Phase == ChangeRequestPhaseSuperseded +func (tx *CommittedResourceChange) IsTerminal() bool { + return tx.Status.Phase == ChangePhaseAccepted || + tx.Status.Phase == ChangePhaseRejected || + tx.Status.Phase == ChangePhaseSuperseded } // +kubebuilder:object:root=true -// CommittedResourceChangeRequestList contains a list of CommittedResourceChangeRequest -type CommittedResourceChangeRequestList struct { +// CommittedResourceChangeList contains a list of CommittedResourceChange +type CommittedResourceChangeList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` - Items []CommittedResourceChangeRequest `json:"items"` + Items []CommittedResourceChange `json:"items"` } func init() { - SchemeBuilder.Register(&CommittedResourceChangeRequest{}, &CommittedResourceChangeRequestList{}) + SchemeBuilder.Register(&CommittedResourceChange{}, &CommittedResourceChangeList{}) } diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 67f05fc8c..a60d5a1f4 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -81,7 +81,7 @@ func (in *CommittedResourceAllocation) DeepCopy() *CommittedResourceAllocation { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceChangeRequest) DeepCopyInto(out *CommittedResourceChangeRequest) { +func (in *CommittedResourceChange) DeepCopyInto(out *CommittedResourceChange) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) @@ -89,18 +89,18 @@ func (in *CommittedResourceChangeRequest) DeepCopyInto(out *CommittedResourceCha in.Status.DeepCopyInto(&out.Status) } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequest. -func (in *CommittedResourceChangeRequest) DeepCopy() *CommittedResourceChangeRequest { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChange. +func (in *CommittedResourceChange) DeepCopy() *CommittedResourceChange { if in == nil { return nil } - out := new(CommittedResourceChangeRequest) + out := new(CommittedResourceChange) in.DeepCopyInto(out) return out } // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *CommittedResourceChangeRequest) DeepCopyObject() runtime.Object { +func (in *CommittedResourceChange) DeepCopyObject() runtime.Object { if c := in.DeepCopy(); c != nil { return c } @@ -108,31 +108,31 @@ func (in *CommittedResourceChangeRequest) DeepCopyObject() runtime.Object { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceChangeRequestList) DeepCopyInto(out *CommittedResourceChangeRequestList) { +func (in *CommittedResourceChangeList) DeepCopyInto(out *CommittedResourceChangeList) { *out = *in out.TypeMeta = in.TypeMeta in.ListMeta.DeepCopyInto(&out.ListMeta) if in.Items != nil { in, out := &in.Items, &out.Items - *out = make([]CommittedResourceChangeRequest, len(*in)) + *out = make([]CommittedResourceChange, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequestList. -func (in *CommittedResourceChangeRequestList) DeepCopy() *CommittedResourceChangeRequestList { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeList. +func (in *CommittedResourceChangeList) DeepCopy() *CommittedResourceChangeList { if in == nil { return nil } - out := new(CommittedResourceChangeRequestList) + out := new(CommittedResourceChangeList) in.DeepCopyInto(out) return out } // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *CommittedResourceChangeRequestList) DeepCopyObject() runtime.Object { +func (in *CommittedResourceChangeList) DeepCopyObject() runtime.Object { if c := in.DeepCopy(); c != nil { return c } @@ -140,7 +140,7 @@ func (in *CommittedResourceChangeRequestList) DeepCopyObject() runtime.Object { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceChangeRequestSpec) DeepCopyInto(out *CommittedResourceChangeRequestSpec) { +func (in *CommittedResourceChangeSpec) DeepCopyInto(out *CommittedResourceChangeSpec) { *out = *in if in.NewState != nil { in, out := &in.NewState, &out.NewState @@ -160,18 +160,18 @@ func (in *CommittedResourceChangeRequestSpec) DeepCopyInto(out *CommittedResourc } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequestSpec. -func (in *CommittedResourceChangeRequestSpec) DeepCopy() *CommittedResourceChangeRequestSpec { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeSpec. +func (in *CommittedResourceChangeSpec) DeepCopy() *CommittedResourceChangeSpec { if in == nil { return nil } - out := new(CommittedResourceChangeRequestSpec) + out := new(CommittedResourceChangeSpec) in.DeepCopyInto(out) return out } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CommittedResourceChangeRequestStatus) DeepCopyInto(out *CommittedResourceChangeRequestStatus) { +func (in *CommittedResourceChangeStatus) DeepCopyInto(out *CommittedResourceChangeStatus) { *out = *in if in.SnapshotBefore != nil { in, out := &in.SnapshotBefore, &out.SnapshotBefore @@ -203,12 +203,12 @@ func (in *CommittedResourceChangeRequestStatus) DeepCopyInto(out *CommittedResou } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeRequestStatus. -func (in *CommittedResourceChangeRequestStatus) DeepCopy() *CommittedResourceChangeRequestStatus { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommittedResourceChangeStatus. +func (in *CommittedResourceChangeStatus) DeepCopy() *CommittedResourceChangeStatus { if in == nil { return nil } - out := new(CommittedResourceChangeRequestStatus) + out := new(CommittedResourceChangeStatus) in.DeepCopyInto(out) return out } diff --git a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md index b544e248e..c35c5a6ae 100644 --- a/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md +++ b/docs/03-reservations-and-inventory/03-b-committed-resource-transaction-architecture.md @@ -10,12 +10,12 @@ | Component | Kind | Owned by | |-----------|------|----------| -| `CommittedResourceChangeRequest` (CRCR) | CRD | Spec: created by API handler or syncer; Status: CRCR reconciler | -| `CommittedResource` (CR) | CRD | Spec: CRCR reconciler (sole writer); Status: CR controller | +| `CommittedResourceChange` (CRC) | CRD | Spec: created by API handler or syncer; Status: CRC reconciler | +| `CommittedResource` (CR) | CRD | Spec: CRC reconciler (sole writer); Status: CR controller | | `Reservation` | CRD | Spec: CR controller (sole writer); Status: Reservation controller | -| Change-commitments API | HTTP handler | Creates CRCR with `source=api` | -| Limes syncer | Background poller | Creates CRCR with `source=syncer` on each diff | -| CRCR reconciler | Controller | Drives CRCR FSM; writes CR specs; rolls back on failure | +| Change-commitments API | HTTP handler | Creates CRC with `source=api` | +| Limes syncer | Background poller | Creates CRC with `source=syncer` on each diff | +| CRC reconciler | Controller | Drives CRC FSM; writes CR specs; rolls back on failure | | CR controller | Controller | Derives Reservation slots from CR spec; reports CR readiness | | Reservation controller | Controller | Attempts host placement; reports slot readiness | @@ -23,14 +23,14 @@ ```mermaid flowchart TD - API["API handler\nsource=api"] -->|creates| CRCR["CommittedResourceChangeRequest"] - Syncer["Limes syncer\nsource=syncer"] -->|creates on diff| CRCR + API["API handler\nsource=api"] -->|creates| CRC["CommittedResourceChange"] + Syncer["Limes syncer\nsource=syncer"] -->|creates on diff| CRC - CRCR -->|"CRCR reconciler — writes spec"| CR["CommittedResource"] + CRC -->|"CRC reconciler — writes spec"| CR["CommittedResource"] CR -->|"CR controller — writes spec"| Res["Reservation"] Res -->|"Reservation controller — tracks placement"| VM["VM / scheduler\nlive-migration"] - CR -.->|"CR controller reports readiness\nCRCR reconciler gates on this"| CRCR + CR -.->|"CR controller reports readiness\nCRC reconciler gates on this"| CRC ``` No controller writes its own spec. Spec flows down; status flows up. @@ -42,34 +42,34 @@ The three reconcilers operate independently and communicate only through CRD sta ```mermaid sequenceDiagram participant W as API handler or Syncer - participant CRCR as CRCR reconciler - participant CRCRD as CommittedResource CRD + participant CRC as CRC reconciler + participant CRCD as CommittedResource CRD participant CRCtl as CR controller participant ResCRD as Reservation CRD participant ResCtl as Reservation controller - W->>CRCRD: create CommittedResourceChangeRequest - CRCR->>CRCRD: write CR.spec (Allocating phase) - Note over CRCR,CRCRD: CRCR reconciler is sole writer of CR.spec + W->>CRCD: create CommittedResourceChange + CRC->>CRCD: write CR.spec (Allocating phase) + Note over CRC,CRCD: CRC reconciler is sole writer of CR.spec CRCtl->>ResCRD: reconcile Reservation slots from CR.spec Note over CRCtl,ResCRD: CR controller is sole writer of Reservation.spec ResCtl->>ResCRD: attempt placement, write Reservation.status Note over ResCtl: reacts to VM moves, live-migration, in-flight actions - CRCtl->>CRCRD: aggregate slot readiness, write CR.status - CRCR->>CRCRD: read CR.status.Ready + observedGeneration, advance to Accepted or RollingBack + CRCtl->>CRCD: aggregate slot readiness, write CR.status + CRC->>CRCD: read CR.status.Ready + observedGeneration, advance to Accepted or RollingBack ``` -## CRCR state machine +## CRC state machine ```mermaid stateDiagram-v2 - [*] --> Preparing : CRCR created by API handler or syncer + [*] --> Preparing : CRC created by API handler or syncer Preparing --> Allocating : snapshot taken Allocating --> AwaitingScheduling : API mode — CR specs written Allocating --> Accepted : Syncer mode — apply-and-done - Allocating --> Superseded : newer CRCR for same AZ took over + Allocating --> Superseded : newer CRC for same AZ took over AwaitingScheduling --> Accepted : all CRs Ready (observedGeneration matches) AwaitingScheduling --> DryRunCleanup : all CRs Ready, but DryRun=true @@ -86,39 +86,39 @@ stateDiagram-v2 Terminal phases (`Accepted`, `Rejected`, `Superseded`) set `Finished=True`. Non-terminal problems set `Progressing=False` with a reason (`Stalled`, `TimedOut`, `Error`, `RetryExhausted`) without advancing -the phase. A stuck non-terminal CRCR triggers an alert; after a threshold it is forcibly moved to +the phase. A stuck non-terminal CRC triggers an alert; after a threshold it is forcibly moved to `Superseded` so its AZ is unblocked and the syncer repairs state on the next poll. ## Request flows ### API-mode flow -1. **API handler** receives a change-commitments request from Limes; creates a CRCR (`source=api`, `allowRejection=true`, optional `dryRun=true`). -2. **CRCR reconciler — Preparing**: validates the request; snapshots current CR specs as the rollback journal. -3. **CRCR reconciler — Allocating**: writes the desired CR specs atomically; records which CRs were touched. -4. **CRCR reconciler — AwaitingScheduling**: watches the touched CRs; waits until each reports its ready condition against the generation just written. +1. **API handler** receives a change-commitments request from Limes; creates a CRC (`source=api`, `allowRejection=true`, optional `dryRun=true`). +2. **CRC reconciler — Preparing**: validates the request; snapshots current CR specs as the rollback journal. +3. **CRC reconciler — Allocating**: writes the desired CR specs atomically; records which CRs were touched. +4. **CRC reconciler — AwaitingScheduling**: watches the touched CRs; waits until each reports its ready condition against the generation just written. 5. **CR controller**: for each CR, derives and reconciles Reservation slots; reports `Ready` once all slots are placed. 6. **Reservation controller**: places each slot on a host; reports slot readiness. -7. **CRCR reconciler**: all CRs ready → `Accepted`; any CR rejected or timeout → rolls all CR specs back to the snapshot → `Rejected`. -8. **API handler** polls the CRCR by name; maps terminal phase to HTTP response. +7. **CRC reconciler**: all CRs ready → `Accepted`; any CR rejected or timeout → rolls all CR specs back to the snapshot → `Rejected`. +8. **API handler** polls the CRC by name; maps terminal phase to HTTP response. ### Syncer-mode flow -1. **Limes syncer** polls Limes per project; diffs desired state against current CRs; on any delta creates a CRCR (`source=syncer`, `allowRejection=false`). -2. **CRCR reconciler — Preparing**: no snapshot (non-rejectable; syncer repairs on next poll if anything drifts). -3. **CRCR reconciler — Allocating**: recency check — skips changes for any project another CRCR touched in the same AZ in the last ~10 minutes (prevents clobbering an in-flight API change); writes CR specs; advances directly to `Accepted`. -4. **CR controller**: reconciles Reservation slots async; CRCR is already terminal. +1. **Limes syncer** polls Limes per project; diffs desired state against current CRs; on any delta creates a CRC (`source=syncer`, `allowRejection=false`). +2. **CRC reconciler — Preparing**: no snapshot (non-rejectable; syncer repairs on next poll if anything drifts). +3. **CRC reconciler — Allocating**: recency check — skips changes for any project another CRC touched in the same AZ in the last ~10 minutes (prevents clobbering an in-flight API change); writes CR specs; advances directly to `Accepted`. +4. **CR controller**: reconciles Reservation slots async; CRC is already terminal. 5. Slots converge in the background; the next syncer poll repairs any remaining drift. ## Key invariants -- **Single writer per spec layer.** The CRCR reconciler is the only component that writes `CommittedResource.spec`. The CR controller is the only component that writes `Reservation.spec`. No controller writes its own spec. -- **AwaitingScheduling gates on generation.** The CRCR reconciler advances from `AwaitingScheduling` only when a CR's ready condition carries an `observedGeneration` that matches the generation the CRCR reconciler wrote — not an older reconcile cycle's result. -- **Rollback is durable.** The pre-change snapshot is stored in `CRCR.status` before any CR spec is touched. A process restart resumes from the persisted phase and snapshot. -- **Syncer-mode never rolls back.** Syncer CRTs are non-rejectable: on any error they retry until `Accepted` or are superseded by a newer CRCR. The syncer repairs drift on its next poll. -- **At most one active CRCR per AZ.** The CRCR reconciler serialises with `MaxConcurrentReconciles=1` plus an in-memory per-AZ active-transaction map (rebuilt from CRDs on startup). Younger CRTs requeue until the active one reaches a terminal phase. -- **Syncer recency guard prevents clobbering.** A syncer CRCR skips changes to any project that an API CRCR touched in the same AZ within the last ~10 minutes — the API change is authoritative; the syncer will reconcile remaining drift on its next poll. -- **Terminal CRTs are retained for auditability.** `Accepted` CRTs are kept for ~1 hour; `Rejected` CRTs for ~5 days. Non-terminal CRCRs (including stuck `RollingBack`) are never GC'd — deleting one loses the rollback journal and the AZ active-marker. +- **Single writer per spec layer.** The CRC reconciler is the only component that writes `CommittedResource.spec`. The CR controller is the only component that writes `Reservation.spec`. No controller writes its own spec. +- **AwaitingScheduling gates on generation.** The CRC reconciler advances from `AwaitingScheduling` only when a CR's ready condition carries an `observedGeneration` that matches the generation the CRC reconciler wrote — not an older reconcile cycle's result. +- **Rollback is durable.** The pre-change snapshot is stored in `CRC.status` before any CR spec is touched. A process restart resumes from the persisted phase and snapshot. +- **Syncer-mode never rolls back.** Syncer CRTs are non-rejectable: on any error they retry until `Accepted` or are superseded by a newer CRC. The syncer repairs drift on its next poll. +- **At most one active CRC per AZ.** The CRC reconciler serialises with `MaxConcurrentReconciles=1` plus an in-memory per-AZ active-transaction map (rebuilt from CRDs on startup). Younger CRTs requeue until the active one reaches a terminal phase. +- **Syncer recency guard prevents clobbering.** A syncer CRC skips changes to any project that an API CRC touched in the same AZ within the last ~10 minutes — the API change is authoritative; the syncer will reconcile remaining drift on its next poll. +- **Terminal CRTs are retained for auditability.** `Accepted` CRTs are kept for ~1 hour; `Rejected` CRTs for ~5 days. Non-terminal CRCs (including stuck `RollingBack`) are never GC'd — deleting one loses the rollback journal and the AZ active-marker. --- diff --git a/helm/bundles/cortex-nova/templates/kpis_kvm.yaml b/helm/bundles/cortex-nova/templates/kpis_kvm.yaml index 9f86eb747..51d1a6df4 100644 --- a/helm/bundles/cortex-nova/templates/kpis_kvm.yaml +++ b/helm/bundles/cortex-nova/templates/kpis_kvm.yaml @@ -52,7 +52,7 @@ spec: gvks: - cortex.cloud/v1alpha1/ReservationList - cortex.cloud/v1alpha1/CommittedResourceList - - cortex.cloud/v1alpha1/CommittedResourceChangeRequestList + - cortex.cloud/v1alpha1/CommittedResourceChangeList - kvm.cloud.sap/v1/HypervisorList - cobaltcore.cloud/v1alpha1/VirtualMachineList description: | diff --git a/helm/bundles/cortex-nova/values.yaml b/helm/bundles/cortex-nova/values.yaml index 94f925552..5a05833e7 100644 --- a/helm/bundles/cortex-nova/values.yaml +++ b/helm/bundles/cortex-nova/values.yaml @@ -137,8 +137,8 @@ cortex: &cortex - cortex.cloud/v1alpha1/ReservationList - cortex.cloud/v1alpha1/CommittedResource - cortex.cloud/v1alpha1/CommittedResourceList - - cortex.cloud/v1alpha1/CommittedResourceChangeRequest - - cortex.cloud/v1alpha1/CommittedResourceChangeRequestList + - cortex.cloud/v1alpha1/CommittedResourceChange + - cortex.cloud/v1alpha1/CommittedResourceChangeList - cortex.cloud/v1alpha1/ProjectQuota - cortex.cloud/v1alpha1/ProjectQuotaList - cortex.cloud/v1alpha1/FlavorGroupCapacity @@ -161,7 +161,7 @@ cortex: &cortex gvks: - cortex.cloud/v1alpha1/Reservation - cortex.cloud/v1alpha1/CommittedResource - - cortex.cloud/v1alpha1/CommittedResourceChangeRequest + - cortex.cloud/v1alpha1/CommittedResourceChange monitoring: labels: &cortexMonitoringLabels github_org: cobaltcore-dev diff --git a/helm/library/cortex/files/crds/cortex.cloud_committedresourcechangerequests.yaml b/helm/library/cortex/files/crds/cortex.cloud_committedresourcechanges.yaml similarity index 97% rename from helm/library/cortex/files/crds/cortex.cloud_committedresourcechangerequests.yaml rename to helm/library/cortex/files/crds/cortex.cloud_committedresourcechanges.yaml index 94b918302..ecf607f4c 100644 --- a/helm/library/cortex/files/crds/cortex.cloud_committedresourcechangerequests.yaml +++ b/helm/library/cortex/files/crds/cortex.cloud_committedresourcechanges.yaml @@ -4,16 +4,16 @@ kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: v0.23.0 - name: committedresourcechangerequests.cortex.cloud + name: committedresourcechanges.cortex.cloud spec: group: cortex.cloud names: - kind: CommittedResourceChangeRequest - listKind: CommittedResourceChangeRequestList - plural: committedresourcechangerequests + kind: CommittedResourceChange + listKind: CommittedResourceChangeList + plural: committedresourcechanges shortNames: - - crcr - singular: committedresourcechangerequest + - crc + singular: committedresourcechange scope: Cluster versions: - additionalPrinterColumns: @@ -41,7 +41,7 @@ spec: name: v1alpha1 schema: openAPIV3Schema: - description: CommittedResourceChangeRequest is the Schema for the committedresourcechangerequests + description: CommittedResourceChange is the Schema for the committedresourcechanges API properties: apiVersion: @@ -62,8 +62,8 @@ spec: metadata: type: object spec: - description: CommittedResourceChangeRequestSpec defines the desired change - for a CommittedResourceChangeRequest. + description: CommittedResourceChangeSpec defines the desired change for + a CommittedResourceChange. properties: allowRejection: description: |- @@ -191,8 +191,8 @@ spec: - source type: object status: - description: CommittedResourceChangeRequestStatus defines the observed - state of a CommittedResourceChangeRequest. + description: CommittedResourceChangeStatus defines the observed state + of a CommittedResourceChange. properties: conditions: description: Conditions holds Progressing and Finished conditions. diff --git a/internal/scheduling/reservations/commitments/changerequests/field_index.go b/internal/scheduling/reservations/commitments/changes/field_index.go similarity index 56% rename from internal/scheduling/reservations/commitments/changerequests/field_index.go rename to internal/scheduling/reservations/commitments/changes/field_index.go index d9db8f76f..0d3a96fa0 100644 --- a/internal/scheduling/reservations/commitments/changerequests/field_index.go +++ b/internal/scheduling/reservations/commitments/changes/field_index.go @@ -1,7 +1,7 @@ // Copyright SAP SE // SPDX-License-Identifier: Apache-2.0 -package changerequests +package changes import ( "context" @@ -11,19 +11,19 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -// idxCRCRByUpsertedCR indexes CommittedResourceChangeRequests by each CR name in UpsertedCommitments. +// idxCRCByUpsertedCR indexes CommittedResourceChanges by each CR name in UpsertedCommitments. // Used by crToChangeRequest to map a CR watch event back to its owning change request. -const idxCRCRByUpsertedCR = "status.upsertedCommitments.name" +const idxCRCByUpsertedCR = "status.upsertedCommitments.name" -// indexCRCRByUpsertedCR registers the field index on all clusters that serve CRCR objects. +// indexCRCByUpsertedCR registers the field index on all clusters that serve CRC objects. // Called once from SetupWithManager; no deduplication guard needed. -func indexCRCRByUpsertedCR(ctx context.Context, mcl *multicluster.Client) error { +func indexCRCByUpsertedCR(ctx context.Context, mcl *multicluster.Client) error { return mcl.IndexField(ctx, - &v1alpha1.CommittedResourceChangeRequest{}, - &v1alpha1.CommittedResourceChangeRequestList{}, - idxCRCRByUpsertedCR, + &v1alpha1.CommittedResourceChange{}, + &v1alpha1.CommittedResourceChangeList{}, + idxCRCByUpsertedCR, func(obj client.Object) []string { - tx, ok := obj.(*v1alpha1.CommittedResourceChangeRequest) + tx, ok := obj.(*v1alpha1.CommittedResourceChange) if !ok { return nil } diff --git a/internal/scheduling/reservations/commitments/changerequests/reconciler.go b/internal/scheduling/reservations/commitments/changes/reconciler.go similarity index 77% rename from internal/scheduling/reservations/commitments/changerequests/reconciler.go rename to internal/scheduling/reservations/commitments/changes/reconciler.go index 9e84afe9e..73e244a99 100644 --- a/internal/scheduling/reservations/commitments/changerequests/reconciler.go +++ b/internal/scheduling/reservations/commitments/changes/reconciler.go @@ -1,7 +1,7 @@ // Copyright SAP SE // SPDX-License-Identifier: Apache-2.0 -package changerequests +package changes import ( "context" @@ -23,12 +23,12 @@ import ( "github.com/cobaltcore-dev/cortex/pkg/multicluster" ) -// CommittedResourceChangeRequestReconciler reconciles CommittedResourceChangeRequest CRDs. +// CommittedResourceChangeReconciler reconciles CommittedResourceChange CRDs. // It is the sole writer of CommittedResource.Spec; it reads CommittedResource.Status // to drive its own FSM. It never writes CommittedResource.Status. // // TODO (#783): add perAZActive map[string]types.UID + mu sync.Mutex for per-AZ serialization. -type CommittedResourceChangeRequestReconciler struct { +type CommittedResourceChangeReconciler struct { client.Client Scheme *runtime.Scheme // AwaitingSchedulingTimeout is how long to wait in AwaitingScheduling before rolling back. @@ -36,11 +36,11 @@ type CommittedResourceChangeRequestReconciler struct { AwaitingSchedulingTimeout time.Duration } -func (r *CommittedResourceChangeRequestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *CommittedResourceChangeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { ctx = commitments.WithNewGlobalRequestID(ctx) logger := commitments.LoggerFromContext(ctx).WithValues("committedResourceChangeRequest", req.Name) - var tx v1alpha1.CommittedResourceChangeRequest + var tx v1alpha1.CommittedResourceChange if err := r.Get(ctx, req.NamespacedName, &tx); err != nil { return ctrl.Result{}, client.IgnoreNotFound(err) } @@ -52,17 +52,17 @@ func (r *CommittedResourceChangeRequestReconciler) Reconcile(ctx context.Context logger.V(1).Info("reconcile entry", "rv", tx.ResourceVersion, "gen", tx.Generation, "phase", tx.Status.Phase) switch tx.Status.Phase { - case "", v1alpha1.ChangeRequestPhasePreparing: + case "", v1alpha1.ChangePhasePreparing: return r.reconcilePreparing(ctx, logger, &tx) - case v1alpha1.ChangeRequestPhaseAllocating: + case v1alpha1.ChangePhaseAllocating: return r.reconcileAllocating(ctx, logger, &tx) - case v1alpha1.ChangeRequestPhaseAwaitingScheduling: + case v1alpha1.ChangePhaseAwaitingScheduling: return r.reconcileAwaitingScheduling(ctx, logger, &tx) - case v1alpha1.ChangeRequestPhaseRollingBack: + case v1alpha1.ChangePhaseRollingBack: return r.reconcileRollingBack(ctx, logger, &tx) - case v1alpha1.ChangeRequestPhaseDryRunCleanup: + case v1alpha1.ChangePhaseDryRunCleanup: return r.reconcileDryRunCleanup(ctx, logger, &tx) - case v1alpha1.ChangeRequestPhaseAccepted, v1alpha1.ChangeRequestPhaseRejected, v1alpha1.ChangeRequestPhaseSuperseded: + case v1alpha1.ChangePhaseAccepted, v1alpha1.ChangePhaseRejected, v1alpha1.ChangePhaseSuperseded: return ctrl.Result{}, nil default: return ctrl.Result{}, fmt.Errorf("unknown phase %q", tx.Status.Phase) @@ -75,7 +75,7 @@ func commitmentName(uuid string) string { return "commitment-" + uuid } // reconcilePreparing snapshots pre-change CR specs and advances to Allocating. // Always snapshots regardless of AllowRejection — syncer-mode CRTs never roll back but the // snapshot is cheap and keeps this method free of branching. -func (r *CommittedResourceChangeRequestReconciler) reconcilePreparing(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { +func (r *CommittedResourceChangeReconciler) reconcilePreparing(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChange) (ctrl.Result, error) { // Snapshot current CR specs for all keys in NewState so rollback can restore them. // nil entry = CR did not exist before = delete it on rollback. snapshot := make(map[string]*v1alpha1.CommittedResourceSpec, len(tx.Spec.NewState)) @@ -94,7 +94,7 @@ func (r *CommittedResourceChangeRequestReconciler) reconcilePreparing(ctx contex } logger.Info("starting transaction", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "newStateSize", len(tx.Spec.NewState), "allowRejection", tx.Spec.AllowRejection) - return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseAllocating, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { + return r.advanceTo(ctx, tx, v1alpha1.ChangePhaseAllocating, func(s *v1alpha1.CommittedResourceChangeStatus) { s.SnapshotBefore = snapshot }) } @@ -106,7 +106,7 @@ func (r *CommittedResourceChangeRequestReconciler) reconcilePreparing(ctx contex // UpsertedCommitments and the phase advance must happen in one patch (via the mutate function) // so that a crash between CR spec writes and status persistence does not leave AwaitingScheduling // with an empty UpsertedCommitments list — which would incorrectly advance to Accepted. -func (r *CommittedResourceChangeRequestReconciler) reconcileAllocating(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { +func (r *CommittedResourceChangeReconciler) reconcileAllocating(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChange) (ctrl.Result, error) { var upserted []corev1.ObjectReference for uuid, desiredSpec := range tx.Spec.NewState { @@ -138,11 +138,11 @@ func (r *CommittedResourceChangeRequestReconciler) reconcileAllocating(ctx conte } // TODO (#782): syncer-mode recency check before advancing - nextPhase := v1alpha1.ChangeRequestPhaseAwaitingScheduling + nextPhase := v1alpha1.ChangePhaseAwaitingScheduling if !tx.Spec.AllowRejection { - nextPhase = v1alpha1.ChangeRequestPhaseAccepted + nextPhase = v1alpha1.ChangePhaseAccepted } - return r.advanceTo(ctx, tx, nextPhase, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { + return r.advanceTo(ctx, tx, nextPhase, func(s *v1alpha1.CommittedResourceChangeStatus) { s.UpsertedCommitments = upserted }) } @@ -152,7 +152,7 @@ func (r *CommittedResourceChangeRequestReconciler) reconcileAllocating(ctx conte // All CRs accepted → Accepted (or DryRunCleanup when DryRun=true). // Any CR rejected → RollingBack. // Not yet settled → wait for the CR watch to re-enqueue. -func (r *CommittedResourceChangeRequestReconciler) reconcileAwaitingScheduling(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { +func (r *CommittedResourceChangeReconciler) reconcileAwaitingScheduling(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChange) (ctrl.Result, error) { allAccepted := true acceptedCount := 0 for _, ref := range tx.Status.UpsertedCommitments { @@ -183,7 +183,7 @@ func (r *CommittedResourceChangeRequestReconciler) reconcileAwaitingScheduling(c // Fail-fast: start rollback immediately on first rejection rather than waiting for all CRs // to settle — this unblocks the AZ sooner and the first rejection message is sufficient for diagnosis. logger.Info("CR rejected, rolling back", "cr", ref.Name, "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "message", cond.Message) - return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseRollingBack, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { + return r.advanceTo(ctx, tx, v1alpha1.ChangePhaseRollingBack, func(s *v1alpha1.CommittedResourceChangeStatus) { s.Message = fmt.Sprintf("CR %s rejected: %s", ref.Name, cond.Message) }) default: @@ -195,15 +195,15 @@ func (r *CommittedResourceChangeRequestReconciler) reconcileAwaitingScheduling(c if !allAccepted { logger.V(1).Info("awaiting scheduling", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "ready", acceptedCount, "total", len(tx.Status.UpsertedCommitments)) if r.AwaitingSchedulingTimeout > 0 { - progressingCond := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing) + progressingCond := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.ChangeConditionProgressing) if progressingCond != nil && time.Since(progressingCond.LastTransitionTime.Time) > r.AwaitingSchedulingTimeout { logger.Info("timed out waiting for scheduling, rolling back", "az", tx.Spec.AvailabilityZone, "correlationID", tx.Spec.CorrelationID, "timeout", r.AwaitingSchedulingTimeout) - return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseRollingBack, func(s *v1alpha1.CommittedResourceChangeRequestStatus) { + return r.advanceTo(ctx, tx, v1alpha1.ChangePhaseRollingBack, func(s *v1alpha1.CommittedResourceChangeStatus) { s.Message = "timed out waiting for scheduling" meta.SetStatusCondition(&s.Conditions, metav1.Condition{ - Type: v1alpha1.ChangeRequestConditionProgressing, + Type: v1alpha1.ChangeConditionProgressing, Status: metav1.ConditionFalse, - Reason: v1alpha1.ChangeRequestReasonTimedOut, + Reason: v1alpha1.ChangeReasonTimedOut, Message: "timed out waiting for scheduling", ObservedGeneration: tx.Generation, }) @@ -216,15 +216,15 @@ func (r *CommittedResourceChangeRequestReconciler) reconcileAwaitingScheduling(c } if tx.Spec.DryRun { - return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseDryRunCleanup, nil) + return r.advanceTo(ctx, tx, v1alpha1.ChangePhaseDryRunCleanup, nil) } - return r.advanceTo(ctx, tx, v1alpha1.ChangeRequestPhaseAccepted, nil) + return r.advanceTo(ctx, tx, v1alpha1.ChangePhaseAccepted, nil) } // reconcileRollingBack converges affected CR specs back to the pre-change SnapshotBefore state. // On restore complete: advances to Rejected. // On permanent cleanup failure: advances to Rejected with reason RollbackFailed. -func (r *CommittedResourceChangeRequestReconciler) reconcileRollingBack(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { +func (r *CommittedResourceChangeReconciler) reconcileRollingBack(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChange) (ctrl.Result, error) { // TODO (#780): for each entry in SnapshotBefore, converge CR spec back to snapshot value // using SSA with fieldManager=txFieldManager. // nil snapshot entry → CR did not exist before → delete it. @@ -234,7 +234,7 @@ func (r *CommittedResourceChangeRequestReconciler) reconcileRollingBack(ctx cont // reconcileDryRunCleanup restores pre-change state after a dry-run would-accept outcome. // Re-uses snapshot rollback logic; terminal phase is Accepted (not Rejected). -func (r *CommittedResourceChangeRequestReconciler) reconcileDryRunCleanup(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChangeRequest) (ctrl.Result, error) { +func (r *CommittedResourceChangeReconciler) reconcileDryRunCleanup(ctx context.Context, logger logr.Logger, tx *v1alpha1.CommittedResourceChange) (ctrl.Result, error) { // TODO (#780): same convergence as reconcileRollingBack; terminal → advanceTo(Accepted, nil). return ctrl.Result{}, nil } @@ -246,11 +246,11 @@ func (r *CommittedResourceChangeRequestReconciler) reconcileDryRunCleanup(ctx co // // Status().Update is used (not Patch) so that nil map values in SnapshotBefore and similar // fields are preserved — JSON merge patch semantics treat null map values as deletions. -func (r *CommittedResourceChangeRequestReconciler) advanceTo( +func (r *CommittedResourceChangeReconciler) advanceTo( ctx context.Context, - tx *v1alpha1.CommittedResourceChangeRequest, - phase v1alpha1.ChangeRequestPhase, - mutate func(*v1alpha1.CommittedResourceChangeRequestStatus), + tx *v1alpha1.CommittedResourceChange, + phase v1alpha1.ChangePhase, + mutate func(*v1alpha1.CommittedResourceChangeStatus), ) (ctrl.Result, error) { tx.Status.Phase = phase @@ -261,7 +261,7 @@ func (r *CommittedResourceChangeRequestReconciler) advanceTo( if tx.IsTerminal() { progressingStatus, progressingReason = metav1.ConditionFalse, string(phase) meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ - Type: v1alpha1.ChangeRequestConditionFinished, + Type: v1alpha1.ChangeConditionFinished, Status: metav1.ConditionTrue, Reason: string(phase), ObservedGeneration: tx.Generation, @@ -269,10 +269,10 @@ func (r *CommittedResourceChangeRequestReconciler) advanceTo( } // Preserve a Progressing=False condition already stamped by the mutate func (e.g. TimedOut). // Terminal phases always overwrite to ensure Progressing=False/. - existingProgressing := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing) + existingProgressing := meta.FindStatusCondition(tx.Status.Conditions, v1alpha1.ChangeConditionProgressing) if tx.IsTerminal() || existingProgressing == nil || existingProgressing.Status != metav1.ConditionFalse { meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ - Type: v1alpha1.ChangeRequestConditionProgressing, + Type: v1alpha1.ChangeConditionProgressing, Status: progressingStatus, Reason: progressingReason, ObservedGeneration: tx.Generation, @@ -286,11 +286,11 @@ func (r *CommittedResourceChangeRequestReconciler) advanceTo( // markError stamps Progressing=False/Error on the transaction as a best-effort status update. // The update error is logged but not returned — callers return the original error for requeue. -func (r *CommittedResourceChangeRequestReconciler) markError(ctx context.Context, tx *v1alpha1.CommittedResourceChangeRequest, msg string) { +func (r *CommittedResourceChangeReconciler) markError(ctx context.Context, tx *v1alpha1.CommittedResourceChange, msg string) { meta.SetStatusCondition(&tx.Status.Conditions, metav1.Condition{ - Type: v1alpha1.ChangeRequestConditionProgressing, + Type: v1alpha1.ChangeConditionProgressing, Status: metav1.ConditionFalse, - Reason: v1alpha1.ChangeRequestReasonError, + Reason: v1alpha1.ChangeReasonError, Message: msg, ObservedGeneration: tx.Generation, }) @@ -302,15 +302,15 @@ func (r *CommittedResourceChangeRequestReconciler) markError(ctx context.Context // SetupWithManager registers the controller and configures watches. // TODO (#783): set MaxConcurrentReconciles: 1 via WithOptions(controller.Options{MaxConcurrentReconciles: 1}). // TODO (#783): add predicate on WatchesMulticluster() to re-trigger same-AZ non-terminal siblings on terminal transition. -func (r *CommittedResourceChangeRequestReconciler) SetupWithManager(mgr ctrl.Manager, mcl *multicluster.Client) error { +func (r *CommittedResourceChangeReconciler) SetupWithManager(mgr ctrl.Manager, mcl *multicluster.Client) error { ctx := context.Background() - if err := indexCRCRByUpsertedCR(ctx, mcl); err != nil { - return fmt.Errorf("failed to set up CRCR-by-upserted-CR field index: %w", err) + if err := indexCRCByUpsertedCR(ctx, mcl); err != nil { + return fmt.Errorf("failed to set up CRC-by-upserted-CR field index: %w", err) } bldr := multicluster.BuildController(mcl, mgr) var err error bldr, err = bldr.WatchesMulticluster( - &v1alpha1.CommittedResourceChangeRequest{}, + &v1alpha1.CommittedResourceChange{}, &handler.EnqueueRequestForObject{}, ) if err != nil { @@ -328,10 +328,10 @@ func (r *CommittedResourceChangeRequestReconciler) SetupWithManager(mgr ctrl.Man } // crToChangeRequest maps a CommittedResource watch event back to the transaction that upserted it. -func (r *CommittedResourceChangeRequestReconciler) crToChangeRequest(ctx context.Context, obj client.Object) []ctrl.Request { - var txList v1alpha1.CommittedResourceChangeRequestList - if err := r.List(ctx, &txList, client.MatchingFields{idxCRCRByUpsertedCR: obj.GetName()}); err != nil { - commitments.LoggerFromContext(ctx).Error(err, "failed to list CRCRs by upserted CR", "cr", obj.GetName()) +func (r *CommittedResourceChangeReconciler) crToChangeRequest(ctx context.Context, obj client.Object) []ctrl.Request { + var txList v1alpha1.CommittedResourceChangeList + if err := r.List(ctx, &txList, client.MatchingFields{idxCRCByUpsertedCR: obj.GetName()}); err != nil { + commitments.LoggerFromContext(ctx).Error(err, "failed to list CRCs by upserted CR", "cr", obj.GetName()) return nil } reqs := make([]ctrl.Request, 0, len(txList.Items)) diff --git a/internal/scheduling/reservations/commitments/changerequests/reconciler_test.go b/internal/scheduling/reservations/commitments/changes/reconciler_test.go similarity index 81% rename from internal/scheduling/reservations/commitments/changerequests/reconciler_test.go rename to internal/scheduling/reservations/commitments/changes/reconciler_test.go index d4f9ae236..cfbdcebc8 100644 --- a/internal/scheduling/reservations/commitments/changerequests/reconciler_test.go +++ b/internal/scheduling/reservations/commitments/changes/reconciler_test.go @@ -1,7 +1,7 @@ // Copyright SAP SE // SPDX-License-Identifier: Apache-2.0 -package changerequests +package changes import ( "context" @@ -42,11 +42,11 @@ func testClient(t *testing.T, scheme *runtime.Scheme, objects ...client.Object) WithScheme(scheme). WithObjects(objects...). WithStatusSubresource( - &v1alpha1.CommittedResourceChangeRequest{}, + &v1alpha1.CommittedResourceChange{}, &v1alpha1.CommittedResource{}, ). - WithIndex(&v1alpha1.CommittedResourceChangeRequest{}, idxCRCRByUpsertedCR, func(obj client.Object) []string { - tx, ok := obj.(*v1alpha1.CommittedResourceChangeRequest) + WithIndex(&v1alpha1.CommittedResourceChange{}, idxCRCByUpsertedCR, func(obj client.Object) []string { + tx, ok := obj.(*v1alpha1.CommittedResourceChange) if !ok { return nil } @@ -61,11 +61,11 @@ func testClient(t *testing.T, scheme *runtime.Scheme, objects ...client.Object) Build() } -func testReconciler(c client.Client, scheme *runtime.Scheme) *CommittedResourceChangeRequestReconciler { - return &CommittedResourceChangeRequestReconciler{Client: c, Scheme: scheme} +func testReconciler(c client.Client, scheme *runtime.Scheme) *CommittedResourceChangeReconciler { + return &CommittedResourceChangeReconciler{Client: c, Scheme: scheme} } -func doReconcile(t *testing.T, r *CommittedResourceChangeRequestReconciler, name string) ctrl.Result { +func doReconcile(t *testing.T, r *CommittedResourceChangeReconciler, name string) ctrl.Result { t.Helper() res, err := r.Reconcile(testCtx, ctrl.Request{NamespacedName: types.NamespacedName{Name: name}}) if err != nil { @@ -74,11 +74,11 @@ func doReconcile(t *testing.T, r *CommittedResourceChangeRequestReconciler, name return res } -func getTx(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResourceChangeRequest { +func getTx(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResourceChange { t.Helper() - var tx v1alpha1.CommittedResourceChangeRequest + var tx v1alpha1.CommittedResourceChange if err := c.Get(testCtx, types.NamespacedName{Name: name}, &tx); err != nil { - t.Fatalf("get CRCR %q: %v", name, err) + t.Fatalf("get CRC %q: %v", name, err) } return &tx } @@ -93,7 +93,7 @@ func getCR(t *testing.T, c client.Client, name string) *v1alpha1.CommittedResour } // driveToPhase calls Reconcile until the CRT reaches target, up to 5 attempts. -func driveToPhase(t *testing.T, r *CommittedResourceChangeRequestReconciler, c client.Client, txName string, target v1alpha1.ChangeRequestPhase) { +func driveToPhase(t *testing.T, r *CommittedResourceChangeReconciler, c client.Client, txName string, target v1alpha1.ChangePhase) { t.Helper() const maxSteps = 5 for range maxSteps { @@ -148,7 +148,7 @@ func backdateProgressingCondition(t *testing.T, c client.Client, txName string, t.Helper() tx := getTx(t, c, txName) for i, cond := range tx.Status.Conditions { - if cond.Type == v1alpha1.ChangeRequestConditionProgressing { + if cond.Type == v1alpha1.ChangeConditionProgressing { tx.Status.Conditions[i].LastTransitionTime = metav1.NewTime(time.Now().Add(-age)) break } @@ -197,8 +197,8 @@ func TestCRTReconciler_Preparing(t *testing.T) { tests := []struct { name string preObjects []client.Object - tx *v1alpha1.CommittedResourceChangeRequest - wantPhase v1alpha1.ChangeRequestPhase + tx *v1alpha1.CommittedResourceChange + wantPhase v1alpha1.ChangePhase wantSnapshotNilFor []string // UUIDs that must have nil snapshot entry (CR did not exist) wantSnapshotSetFor []string // UUIDs that must have non-nil snapshot entry (CR existed) wantProgressingStatus metav1.ConditionStatus @@ -206,16 +206,16 @@ func TestCRTReconciler_Preparing(t *testing.T) { }{ { name: "no pre-existing CR: snapshot entry is nil, phase advances to Allocating", - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-1"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-a": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAllocating, + wantPhase: v1alpha1.ChangePhaseAllocating, wantSnapshotNilFor: []string{"uuid-a"}, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", @@ -228,32 +228,32 @@ func TestCRTReconciler_Preparing(t *testing.T) { Spec: *minimalCRSpec("az-1"), }, }, - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-2"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-b": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAllocating, + wantPhase: v1alpha1.ChangePhaseAllocating, wantSnapshotSetFor: []string{"uuid-b"}, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, { name: "Preparing idempotent: running reconcile twice does not error", - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-prep-3"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-c": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAllocating, + wantPhase: v1alpha1.ChangePhaseAllocating, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -282,7 +282,7 @@ func TestCRTReconciler_Preparing(t *testing.T) { t.Errorf("phase: want %q, got %q", tt.wantPhase, tx.Status.Phase) } if tt.wantProgressingStatus != "" { - assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) } for _, uuid := range tt.wantSnapshotNilFor { snap, ok := tx.Status.SnapshotBefore[uuid] @@ -316,8 +316,8 @@ func TestCRTReconciler_Allocating(t *testing.T) { tests := []struct { name string preObjects []client.Object - tx *v1alpha1.CommittedResourceChangeRequest - wantPhase v1alpha1.ChangeRequestPhase + tx *v1alpha1.CommittedResourceChange + wantPhase v1alpha1.ChangePhase wantCRExists []string wantCRAbsent []string wantUpsertedCommitments int @@ -328,16 +328,16 @@ func TestCRTReconciler_Allocating(t *testing.T) { }{ { name: "API-mode: creates CR and advances to AwaitingScheduling", - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-1"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-1": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-1"}, wantUpsertedCommitments: 1, wantProgressingStatus: metav1.ConditionTrue, @@ -345,22 +345,22 @@ func TestCRTReconciler_Allocating(t *testing.T) { }, { name: "syncer-mode: applies CR and advances directly to Accepted (no scheduling gate)", - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-2"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceSyncer, AllowRejection: false, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-2": minimalCRSpec("az-1")}, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAccepted, + wantPhase: v1alpha1.ChangePhaseAccepted, wantCRExists: []string{"commitment-uuid-2"}, wantUpsertedCommitments: 1, wantProgressingStatus: metav1.ConditionFalse, - wantProgressingReason: string(v1alpha1.ChangeRequestPhaseAccepted), + wantProgressingReason: string(v1alpha1.ChangePhaseAccepted), wantFinished: true, - wantFinishedReason: string(v1alpha1.ChangeRequestPhaseAccepted), + wantFinishedReason: string(v1alpha1.ChangePhaseAccepted), }, { name: "nil spec deletes CR and it is absent from UpsertedCommitments", @@ -370,16 +370,16 @@ func TestCRTReconciler_Allocating(t *testing.T) { Spec: *minimalCRSpec("az-1"), }, }, - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-3"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-3": nil}, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantCRAbsent: []string{"commitment-uuid-3"}, wantUpsertedCommitments: 0, wantProgressingStatus: metav1.ConditionTrue, @@ -393,16 +393,16 @@ func TestCRTReconciler_Allocating(t *testing.T) { Spec: *minimalCRSpec("az-old"), }, }, - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-alloc-4"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-4": minimalCRSpec("az-new")}, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-4"}, wantUpsertedCommitments: 1, wantProgressingStatus: metav1.ConditionTrue, @@ -427,10 +427,10 @@ func TestCRTReconciler_Allocating(t *testing.T) { t.Errorf("UpsertedCommitments len: want %d, got %d", tt.wantUpsertedCommitments, got) } if tt.wantProgressingStatus != "" { - assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) } if tt.wantFinished { - assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) } for _, name := range tt.wantCRExists { getCR(t, c, name) // fatals if absent @@ -449,9 +449,9 @@ func TestCRTReconciler_Allocating(t *testing.T) { // desired spec is idempotent: no error, phase advances correctly again. func TestCRTReconciler_Allocating_Idempotent(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -461,11 +461,11 @@ func TestCRTReconciler_Allocating_Idempotent(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangePhaseAwaitingScheduling) // Reset to Allocating to re-run it. stored := getTx(t, c, tx.Name) - stored.Status.Phase = v1alpha1.ChangeRequestPhaseAllocating + stored.Status.Phase = v1alpha1.ChangePhaseAllocating if err := c.Status().Update(testCtx, stored); err != nil { t.Fatalf("reset phase to Allocating: %v", err) } @@ -473,7 +473,7 @@ func TestCRTReconciler_Allocating_Idempotent(t *testing.T) { doReconcile(t, r, tx.Name) // second run of Allocating final := getTx(t, c, tx.Name) - if final.Status.Phase != v1alpha1.ChangeRequestPhaseAwaitingScheduling { + if final.Status.Phase != v1alpha1.ChangePhaseAwaitingScheduling { t.Errorf("idempotent Allocating: want AwaitingScheduling, got %q", final.Status.Phase) } } @@ -487,12 +487,12 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { const crName = "commitment-uuid-await" // setup drives a CRT to AwaitingScheduling and returns the client and reconciler. - setup := func(t *testing.T, txOpts ...func(*v1alpha1.CommittedResourceChangeRequest)) (client.Client, *CommittedResourceChangeRequestReconciler) { + setup := func(t *testing.T, txOpts ...func(*v1alpha1.CommittedResourceChange)) (client.Client, *CommittedResourceChangeReconciler) { t.Helper() scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: txName}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -504,17 +504,17 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { } c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, txName, v1alpha1.ChangeRequestPhaseAwaitingScheduling) + driveToPhase(t, r, c, txName, v1alpha1.ChangePhaseAwaitingScheduling) return c, r } tests := []struct { name string - txOpts []func(*v1alpha1.CommittedResourceChangeRequest) + txOpts []func(*v1alpha1.CommittedResourceChange) setupCR func(t *testing.T, c client.Client) setupTx func(t *testing.T, c client.Client) - reconcilerOpt func(r *CommittedResourceChangeRequestReconciler) - wantPhase v1alpha1.ChangeRequestPhase + reconcilerOpt func(r *CommittedResourceChangeReconciler) + wantPhase v1alpha1.ChangePhase wantMessage string // substring that must appear in tx.Status.Message; empty = no check wantProgressingStatus metav1.ConditionStatus wantProgressingReason string @@ -526,21 +526,21 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") }, - wantPhase: v1alpha1.ChangeRequestPhaseAccepted, + wantPhase: v1alpha1.ChangePhaseAccepted, wantProgressingStatus: metav1.ConditionFalse, - wantProgressingReason: string(v1alpha1.ChangeRequestPhaseAccepted), + wantProgressingReason: string(v1alpha1.ChangePhaseAccepted), wantFinished: true, - wantFinishedReason: string(v1alpha1.ChangeRequestPhaseAccepted), + wantFinishedReason: string(v1alpha1.ChangePhaseAccepted), }, { name: "DryRun=true and all CRs accepted → DryRunCleanup", - txOpts: []func(*v1alpha1.CommittedResourceChangeRequest){ - func(tx *v1alpha1.CommittedResourceChangeRequest) { tx.Spec.DryRun = true }, + txOpts: []func(*v1alpha1.CommittedResourceChange){ + func(tx *v1alpha1.CommittedResourceChange) { tx.Spec.DryRun = true }, }, setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") }, - wantPhase: v1alpha1.ChangeRequestPhaseDryRunCleanup, + wantPhase: v1alpha1.ChangePhaseDryRunCleanup, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -549,7 +549,7 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionFalse, v1alpha1.CommittedResourceReasonRejected, "no capacity on host") }, - wantPhase: v1alpha1.ChangeRequestPhaseRollingBack, + wantPhase: v1alpha1.ChangePhaseRollingBack, wantMessage: "no capacity on host", wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", @@ -559,7 +559,7 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReadyStaleGen(t, c, crName) }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -568,28 +568,28 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { setupCR: func(t *testing.T, c client.Client) { setCRReady(t, c, crName, metav1.ConditionTrue, v1alpha1.CommittedResourceReasonReserving, "still placing") }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, { name: "timeout exceeded → RollingBack with timed out message", - reconcilerOpt: func(r *CommittedResourceChangeRequestReconciler) { + reconcilerOpt: func(r *CommittedResourceChangeReconciler) { r.AwaitingSchedulingTimeout = 50 * time.Millisecond }, setupTx: func(t *testing.T, c client.Client) { backdateProgressingCondition(t, c, txName, 1*time.Second) }, - wantPhase: v1alpha1.ChangeRequestPhaseRollingBack, + wantPhase: v1alpha1.ChangePhaseRollingBack, wantMessage: "timed out", wantProgressingStatus: metav1.ConditionFalse, - wantProgressingReason: v1alpha1.ChangeRequestReasonTimedOut, + wantProgressingReason: v1alpha1.ChangeReasonTimedOut, }, { name: "timeout=0 means no timeout: CRT stays in AwaitingScheduling indefinitely", // No reconcilerOpt, so AwaitingSchedulingTimeout=0 (disabled). // CR ready condition is never set → should stay in AwaitingScheduling. - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantProgressingStatus: metav1.ConditionTrue, wantProgressingReason: "Advancing", }, @@ -619,17 +619,17 @@ func TestCRTReconciler_AwaitingScheduling(t *testing.T) { t.Errorf("message: want substring %q in %q", tt.wantMessage, tx.Status.Message) } if tt.wantProgressingStatus != "" { - assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeConditionProgressing, tt.wantProgressingStatus, tt.wantProgressingReason) } if tt.wantFinished { - assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeRequestConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) + assertCondition(t, tx.Status.Conditions, v1alpha1.ChangeConditionFinished, metav1.ConditionTrue, tt.wantFinishedReason) } }) } } // setPhase directly sets a CRT's phase via Status().Update for test setup. -func setPhase(t *testing.T, c client.Client, txName string, phase v1alpha1.ChangeRequestPhase) { +func setPhase(t *testing.T, c client.Client, txName string, phase v1alpha1.ChangePhase) { t.Helper() tx := getTx(t, c, txName) tx.Status.Phase = phase @@ -639,7 +639,7 @@ func setPhase(t *testing.T, c client.Client, txName string, phase v1alpha1.Chang } // reconcileN calls doReconcile exactly n times without inspecting the phase between calls. -func reconcileN(t *testing.T, r *CommittedResourceChangeRequestReconciler, name string, n int) { +func reconcileN(t *testing.T, r *CommittedResourceChangeReconciler, name string, n int) { t.Helper() for range n { doReconcile(t, r, name) @@ -663,9 +663,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "Preparing: snapshot is consistent across re-runs", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-1"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-1": minimalCRSpec("az-1")}, }, @@ -694,9 +694,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "Allocating: UpsertedCommitments and CR spec stable across re-runs", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-2"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-2": minimalCRSpec("az-1")}, }, @@ -704,11 +704,11 @@ func TestCRTReconciler_Idempotency(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangePhaseAwaitingScheduling) upserted1 := getTx(t, c, tx.Name).Status.UpsertedCommitments - setPhase(t, c, tx.Name, v1alpha1.ChangeRequestPhaseAllocating) // reset to Allocating - doReconcile(t, r, tx.Name) // Allocating → AwaitingScheduling (re-run) + setPhase(t, c, tx.Name, v1alpha1.ChangePhaseAllocating) // reset to Allocating + doReconcile(t, r, tx.Name) // Allocating → AwaitingScheduling (re-run) upserted2 := getTx(t, c, tx.Name).Status.UpsertedCommitments if len(upserted1) != 1 || len(upserted2) != 1 { @@ -724,9 +724,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "AwaitingScheduling: multiple calls without CR ready stay waiting", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-3"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-3": minimalCRSpec("az-1")}, }, @@ -734,10 +734,10 @@ func TestCRTReconciler_Idempotency(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangePhaseAwaitingScheduling) reconcileN(t, r, tx.Name, 3) // CR never set ready — must stay in AwaitingScheduling - if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.ChangeRequestPhaseAwaitingScheduling { + if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.ChangePhaseAwaitingScheduling { t.Errorf("phase: want AwaitingScheduling, got %q", got) } }, @@ -746,9 +746,9 @@ func TestCRTReconciler_Idempotency(t *testing.T) { name: "AwaitingScheduling → Accepted stays Accepted on repeated reconciles", run: func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-idem-4"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, NewState: map[string]*v1alpha1.CommittedResourceSpec{"uuid-idem-4": minimalCRSpec("az-1")}, }, @@ -756,11 +756,11 @@ func TestCRTReconciler_Idempotency(t *testing.T) { c := testClient(t, scheme, tx) r := testReconciler(c, scheme) - driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangePhaseAwaitingScheduling) setCRReady(t, c, "commitment-uuid-idem-4", metav1.ConditionTrue, v1alpha1.CommittedResourceReasonAccepted, "") reconcileN(t, r, tx.Name, 3) // first call → Accepted; subsequent calls → terminal no-op - if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.ChangeRequestPhaseAccepted { + if got := getTx(t, c, tx.Name).Status.Phase; got != v1alpha1.ChangePhaseAccepted { t.Errorf("phase: want Accepted, got %q", got) } }, @@ -799,9 +799,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { // pre-existing CRs before the transaction is created preObjects []client.Object // the CRT to create - tx *v1alpha1.CommittedResourceChangeRequest + tx *v1alpha1.CommittedResourceChange // expected phase after driving the FSM to a stable point - wantPhase v1alpha1.ChangeRequestPhase + wantPhase v1alpha1.ChangePhase // CR names that must exist after Allocating wantCRExists []string // CR names that must be absent after Allocating @@ -815,9 +815,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }{ { name: "multiple new CRs: all created, all upserted", - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-1"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -828,7 +828,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-a1", "commitment-uuid-a2", "commitment-uuid-a3"}, wantUpsertedCount: 3, wantSnapshotNilFor: []string{"uuid-a1", "uuid-a2", "uuid-a3"}, @@ -841,9 +841,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { Spec: *minimalCRSpec("az-old"), }, }, - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-2"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -853,7 +853,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-b1", "commitment-uuid-b2"}, wantUpsertedCount: 2, wantSnapshotNilFor: []string{"uuid-b1"}, @@ -871,9 +871,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { Spec: *minimalCRSpec("az-1"), }, }, - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-3"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -883,7 +883,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, }, - wantPhase: v1alpha1.ChangeRequestPhaseAwaitingScheduling, + wantPhase: v1alpha1.ChangePhaseAwaitingScheduling, wantCRExists: []string{"commitment-uuid-c2"}, wantCRAbsent: []string{"commitment-uuid-c1"}, wantUpsertedCount: 1, @@ -891,9 +891,9 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, { name: "empty NewState: no CRs touched, advances all the way to Accepted", - tx: &v1alpha1.CommittedResourceChangeRequest{ + tx: &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-api-4"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -901,7 +901,7 @@ func TestCRTReconciler_APIScenarios(t *testing.T) { }, }, // AwaitingScheduling with empty UpsertedCommitments → allAccepted=true immediately → Accepted - wantPhase: v1alpha1.ChangeRequestPhaseAccepted, + wantPhase: v1alpha1.ChangePhaseAccepted, wantUpsertedCount: 0, }, } @@ -961,14 +961,14 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { t.Run("DeletionTimestamp: reconcile is a no-op", func(t *testing.T) { scheme := testScheme(t) now := metav1.Now() - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{ Name: "tx-del", DeletionTimestamp: &now, // Finalizers required for the fake client to allow a non-zero DeletionTimestamp. Finalizers: []string{"test.io/hold"}, }, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, }, @@ -986,9 +986,9 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { t.Run("unknown phase returns error", func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-unknown"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, }, @@ -1010,9 +1010,9 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { t.Run("crToTransaction maps CR name to owning CRT via field index", func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-map"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, AllowRejection: true, @@ -1023,7 +1023,7 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { r := testReconciler(c, scheme) // Drive to AwaitingScheduling so UpsertedCommitments is populated. - driveToPhase(t, r, c, tx.Name, v1alpha1.ChangeRequestPhaseAwaitingScheduling) + driveToPhase(t, r, c, tx.Name, v1alpha1.ChangePhaseAwaitingScheduling) // crToTransaction should return a request for the owning CRT. cr := getCR(t, c, "commitment-uuid-map") @@ -1044,19 +1044,19 @@ func TestCRTReconciler_EdgeCases(t *testing.T) { func TestCRTReconciler_Terminal(t *testing.T) { tests := []struct { name string - phase v1alpha1.ChangeRequestPhase + phase v1alpha1.ChangePhase }{ - {"Accepted is a no-op", v1alpha1.ChangeRequestPhaseAccepted}, - {"Rejected is a no-op", v1alpha1.ChangeRequestPhaseRejected}, - {"Superseded is a no-op", v1alpha1.ChangeRequestPhaseSuperseded}, + {"Accepted is a no-op", v1alpha1.ChangePhaseAccepted}, + {"Rejected is a no-op", v1alpha1.ChangePhaseRejected}, + {"Superseded is a no-op", v1alpha1.ChangePhaseSuperseded}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { scheme := testScheme(t) - tx := &v1alpha1.CommittedResourceChangeRequest{ + tx := &v1alpha1.CommittedResourceChange{ ObjectMeta: metav1.ObjectMeta{Name: "tx-term"}, - Spec: v1alpha1.CommittedResourceChangeRequestSpec{ + Spec: v1alpha1.CommittedResourceChangeSpec{ AvailabilityZone: "az-1", Source: v1alpha1.TxSourceAPI, },