Skip to content

feat: add SKE managed cluster and node pool APIs - #36

Open
Rana-Ashar wants to merge 3 commits into
feat/skefrom
feature/stackit-managed-specs
Open

Rana-Ashar wants to merge 3 commits into
feat/skefrom
feature/stackit-managed-specs

Conversation

@Rana-Ashar

Copy link
Copy Markdown

This PR adds the initial SKE managed cluster and node pool APIs to CAPSTK.

It introduces StackitManagedCluster and StackitManagedNodePool specifications, Cluster API v1beta2 contract fields, generated CRDs and RBAC, sample manifests, initial controllers, and tests.

// region is the STACKIT region in which infrastructure is created.
// +required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:Pattern=`^[a-z]{2}[0-9]{2}$`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this an official pattern you got from our stackit api docs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is not coming from the SKE api docs. I reused the existing validation from StackitClusterSpec.Region.

Comment on lines +77 to +85
// providerIDList are the identification IDs of machine instances provided by
// the provider. This field must match the provider IDs as seen on the Node
// objects corresponding to a MachinePool's machine instances.
// +optional
// +listType=atomic
// +kubebuilder:validation:MaxItems=10000
// +kubebuilder:validation:items:MinLength=1
// +kubebuilder:validation:items:MaxLength=512
ProviderIDList []string `json:"providerIDList,omitempty"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not belong into the spec that is something that we retrieve from the stackit api and store inside the status

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or where did you get the idea from putting this inside the spec?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This placement comes directly from the CAPI v1beta2 InfraMachinePool contract.

CAPI requires an InfraMachinePool to expose the instance provider IDs as spec.providerIDList. Core CAPI reads that field to match the MachinePool instances with Kubernetes Nodes and to track deletion.

So although this is controller-populated information rather than user desired state, it intentionally lives in spec for CAPI compatibility. I think we should keep it here for that reason.

https://cluster-api.sigs.k8s.io/developer/providers/contracts/infra-machinepool.html

Comment thread cloud/services/ske/ske.go
Comment on lines +26 to +54
// ManagedClusterDesiredState holds the cluster-level fields needed by the SKE
// service layer.
type ManagedClusterDesiredState struct {
ProjectID string
Region string
ClusterName string
KubernetesVersion string
NetworkID string
Labels map[string]string
}

// ManagedNodePoolDesiredState holds the node-pool-level fields needed by the
// SKE service layer.
type ManagedNodePoolDesiredState struct {
Name string
AvailabilityZones []string
MachineType string
ImageName string
ImageVersion string
Minimum int32
Maximum int32
VolumeSize int32
VolumeType string
AllowSystemComponents *bool
Labels map[string]string
Taints []ManagedNodePoolTaint
MaxSurge *int32
MaxUnavailable *int32
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the desired state should be reflected by the CRs themselves why do we need these "shadow" structs that are essentially just code duplication?

Or was this added as prematurely for the actually API abstraction/implementation?

// TODO(ske): This fallback version is time-sensitive as SKE rotates supported
// Flatcar images; replace with a durable configuration or provider-options
// selection strategy.
defaultNodePoolImageVersion = "4593.2.5-containerd2.1.9"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot be just leave this empty and SKE will automatically select the latest stable image version?

how does the terraform implementation deal with this?

Comment on lines +119 to +144
updateTriggered := false
if needsUpdate && state == skev2.CLUSTERSTATUSSTATE_STATE_HEALTHY {
log.Info("Updating SKE cluster node pools", "clusterName", s.scope.ClusterName(), "desiredPoolCount", len(desiredPools))
payload := s.buildClusterPayload(desiredPools)
if _, updateErr := s.skeClient.
CreateOrUpdateCluster(ctx, s.scope.ProjectID(), s.scope.Region(), s.scope.ClusterName()).
CreateOrUpdateClusterPayload(payload).
Execute(); updateErr != nil {
updateErr = cloud.ClassifySDKError("update SKE cluster", updateErr)
if cloud.IsConflict(updateErr) {
log.V(1).Info("SKE cluster update already in progress", "clusterName", s.scope.ClusterName())
updateTriggered = true
} else {
log.Error(updateErr, "Failed to update SKE cluster", "clusterName", s.scope.ClusterName())
reason := "UpdateFailed"
if cloud.IsInvalidInput(updateErr) {
reason = ReasonInvalidNodePoolConfiguration
}
s.scope.SetNotReady(reason, updateErr.Error())
s.projectNodePoolStatuses(cluster, observedByName, state, stateOK, false, reason, updateErr.Error())
return ctrl.Result{}, updateErr
}
} else {
updateTriggered = true
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is way too much nesting

and why is the updateTriggered set in the else branch?
isn't the triggering happening in the if branch?

I guess the variable isn't precise enough it should probably be something like activeUpdate or isUpgradeActive

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sake of maintainability i would put everything that is not strictly the reconcile and delete method into two separate files: cluster.go and nodepool.go

@tuunit
tuunit changed the base branch from main to feat/ske November 17, 2026 09:28
@tuunit
tuunit marked this pull request as ready for review November 17, 2026 09:29
Comment thread cloud/sdk_client.go
Comment on lines +1121 to +1131
// ClassifySDKError maps STACKIT OpenAPI SDK errors to CAPSTK cloud sentinel
// errors while preserving pre-classified sentinel errors from test fakes.
func ClassifySDKError(op string, err error) error {
if err == nil {
return nil
}
if IsNotFound(err) || IsUnauthorized(err) || IsInvalidInput(err) || IsConflict(err) || IsTransient(err) {
return fmt.Errorf("%s: %w", op, err)
}
return classifySDKError(op, err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplication or at least overlap

Comment thread cmd/manager/main.go
if err := (&controller.StackitManagedClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
SKEClientFactory: cloud.NewSKEClient,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use the general cloud client factory and not have its own factory

Comment thread cloud/sdk_client.go
Comment on lines +88 to +91
// NewSKEClient returns the real STACKIT SKE v2 API client.
func NewSKEClient(_ context.Context, creds Credentials) (skev2.DefaultAPI, error) {
if creds.ProjectID == "" {
return nil, fmt.Errorf("%w: project ID is empty", ErrInvalidInput)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong semantics. should be integrated into the existing cloud client constructor and just be a sub type. this duplicates the whole authentication logic

Comment on lines +192 to +202
// Standalone deletion handshake: remove the finalizer once the parent
// StackitManagedClusterReconciler confirms the external node pool is absent
// from SKE by projecting Ready=False with Reason="Deleted".
readyCond := meta.FindStatusCondition(nodePool.Status.Conditions, infrav1.ManagedNodePoolReadyCondition)
if readyCond != nil &&
readyCond.Status == metav1.ConditionFalse &&
readyCond.Reason == nodePoolDeletedReason {
log.Info("Confirmed external SKE node pool deletion, removing finalizer", "nodePoolName", nodePool.Spec.Name)
controllerutil.RemoveFinalizer(nodePool, infrav1.ManagedNodePoolFinalizer)
return
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the deletion of nodepools is tied to two conditions Ready=False and Reason=Deleted. this could be fragile and should be investigated

Comment on lines +569 to +578
// listManagedNodePoolsForCluster discovers all StackitManagedNodePools
// belonging to the given Cluster / StackitManagedCluster, sorted
// deterministically by Kubernetes resource name.
func listManagedNodePoolsForCluster(
ctx context.Context,
c client.Client,
cluster *clusterv1.Cluster,
managedCluster *infrav1.StackitManagedCluster,
) ([]scope.ManagedNodePoolEntry, error) {
if managedCluster == nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole listManagedNodePoolsForCluster in "stackitmanagednodepool_controller.go" seems convoluted and overengineered

Comment on lines +52 to +55
// minimum is the minimum number of nodes in the pool.
// +required
// +kubebuilder:validation:Maximum=1000
Minimum int32 `json:"minimum"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of the types like this one need more strict validation. like this would currently allow a minimum of 0 or even negative

Comment on lines +111 to +115
if paused, message := util.ReconciliationPaused(cluster, stackitManagedCluster); paused {
util.SetPausedCondition(&stackitManagedCluster.Status.Conditions, stackitManagedCluster.Generation, true, message)
return ctrl.Result{}, nil
}
util.SetPausedCondition(&stackitManagedCluster.Status.Conditions, stackitManagedCluster.Generation, false, "")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the pause handled after deletion? pause should pause everything. needs to be at the very top of the controller. same goes for the nodepool controller

Comment on lines +128 to +131
if !cluster.DeletionTimestamp.IsZero() || !managedCluster.DeletionTimestamp.IsZero() {
log.Info("Parent cluster is deleting, skipping finalizer addition for StackitManagedNodePool")
return ctrl.Result{}, nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to make sense. how would this even be possible. if the parent cluster is in deleting state the nodepool needs to go into deleting state as well

Comment on lines +274 to +290
func parentLifecycleChanged(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}
if e.ObjectOld.GetDeletionTimestamp().IsZero() != e.ObjectNew.GetDeletionTimestamp().IsZero() {
return true
}
if annotations.HasPaused(e.ObjectOld) != annotations.HasPaused(e.ObjectNew) {
return true
}
oldCluster, okOld := e.ObjectOld.(*clusterv1.Cluster)
newCluster, okNew := e.ObjectNew.(*clusterv1.Cluster)
if okOld && okNew {
return ptr.Deref(oldCluster.Spec.Paused, false) != ptr.Deref(newCluster.Spec.Paused, false)
}
return false
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs more comments for the sake of documentation. from a first read it will be hard to understand what this code even tries to accomplish

Comment on lines +649 to +652
// UID-safe deletion fallback: if a MachinePool was already deleted before
// its child StackitManagedNodePool finished releasing its finalizer, match
// only StackitManagedNodePools carrying the exact StackitManagedCluster UID
// label stamped when the child finalizer was established.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is technically possible. i think CAPI requires the child Infra object to be deleted before it allows the deletion

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants