mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
* fix(controller): requeue control-plane upgrade while it waits A control-plane Kubernetes upgrade waits on Temporal workflows (kubeadm ConfigMap update, add-on upgrade) and on control-plane node recycling. A workflow finishing writes nothing to the DB, so the next look came from the 15-minute DB source resync: nico-vms-e2e runs sat idle a mean of 4-6 min, up to 12 min, after the add-on workflow finished. Each of those waits now asks for a 15 s requeue. Controller.Reconcile folds it into the node-agent upgrade result, keeping the sooner RequeueAfter, and only after the cluster transaction commits. Refs: NKX-19294 Signed-off-by: Aaron Patten <apatten@nvidia.com> * test(controller): drop control-plane upgrade requeue unit tests Each wait gains a one-line requeue request, and mergeRequeue keeps the sooner of two durations. The table test restated those calls through Temporal and transaction mocks, so the package keeps its existing tests. Signed-off-by: Aaron Patten <apatten@nvidia.com> --------- Signed-off-by: Aaron Patten <apatten@nvidia.com>
139 lines
4.8 KiB
Go
139 lines
4.8 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package controlplane provides controllers for managing Kubernetes control planes.
|
|
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"go.temporal.io/sdk/client"
|
|
"k8s.io/utils/ptr"
|
|
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/manager"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/agent"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/config"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/controllers/dbreconcile"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/controllers/dbreconcile/dbwrapper"
|
|
dbfilters "github.com/dsx-ai-factory/nvke/controller/internal/controllers/dbreconcile/filters"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/db"
|
|
)
|
|
|
|
// Controller manages keyed control plane reconciliation for clusters.
|
|
// It handles scale-up, remediation, Kubernetes upgrades, and deprecated-node cleanup.
|
|
type Controller struct {
|
|
cfg config.Config
|
|
db *dbwrapper.DB
|
|
logger *slog.Logger
|
|
controllerTemporalClient client.Client
|
|
k8sClient ctrlclient.Client
|
|
k8sReader ctrlclient.Reader
|
|
nodeAgentClient agent.Client
|
|
}
|
|
|
|
// NewController creates a new control plane Controller.
|
|
func NewController(
|
|
cfg config.Config,
|
|
logger *slog.Logger,
|
|
dbInstance *dbwrapper.DB,
|
|
controllerTemporalClient client.Client,
|
|
k8sClient ctrlclient.Client,
|
|
k8sReader ctrlclient.Reader,
|
|
nodeAgentClient agent.Client,
|
|
) *Controller {
|
|
return &Controller{
|
|
cfg: cfg,
|
|
db: dbInstance,
|
|
logger: logger.With("controller", "control-plane"),
|
|
controllerTemporalClient: controllerTemporalClient,
|
|
k8sClient: k8sClient,
|
|
k8sReader: k8sReader,
|
|
nodeAgentClient: nodeAgentClient,
|
|
}
|
|
}
|
|
|
|
// SetupWithManager wires the controller into controller-runtime with DB-backed sources.
|
|
func (c *Controller) SetupWithManager(mgr manager.Manager) error {
|
|
return dbreconcile.SetupController(
|
|
mgr,
|
|
"control-plane-controller",
|
|
c,
|
|
dbwrapper.NewClusterSource(c.db, dbfilters.Cluster{}),
|
|
dbwrapper.NewNodePoolSource(c.db, dbfilters.NodePool{ControlPlane: ptr.To(true)}, nodePoolClusterKeys),
|
|
dbwrapper.NewNodeSource(c.db, dbfilters.Node{ControlPlane: ptr.To(true)}, nodeClusterKeys),
|
|
)
|
|
}
|
|
|
|
func nodePoolClusterKeys(_ context.Context, nodePool db.NodePool) ([]dbreconcile.Key, error) {
|
|
if nodePool.ClusterID == uuid.Nil {
|
|
return nil, nil
|
|
}
|
|
return []dbreconcile.Key{dbreconcile.ClusterKey(nodePool.ClusterID)}, nil
|
|
}
|
|
|
|
func nodeClusterKeys(_ context.Context, node db.Node) ([]dbreconcile.Key, error) {
|
|
if node.ClusterID == uuid.Nil {
|
|
return nil, nil
|
|
}
|
|
return []dbreconcile.Key{dbreconcile.ClusterKey(node.ClusterID)}, nil
|
|
}
|
|
|
|
// Reconcile reconciles one cluster's control plane from a ClusterID root key.
|
|
func (c *Controller) Reconcile(ctx context.Context, key dbreconcile.Key) (reconcile.Result, error) {
|
|
var (
|
|
cluster db.Cluster
|
|
requeue time.Duration
|
|
)
|
|
err := c.db.RunInTx(ctx, func(ctx context.Context, tx db.Tx) error {
|
|
requeue = 0
|
|
// Re-read under a row lock: the cluster may have been enqueued from a stale
|
|
// event, so the reconciler could otherwise act on a stale snapshot and race
|
|
// teardown (e.g. re-provision CP nodes for a cluster being deleted).
|
|
lockedCluster, err := tx.GetClusterForUpdate(ctx, key.ID)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
c.logger.Debug("cluster not found, skipping control-plane reconciliation", "clusterID", key.ID.String())
|
|
return nil
|
|
}
|
|
return fmt.Errorf("locking cluster %s for reconcile: %w", key.ID, err)
|
|
}
|
|
|
|
if lockedCluster.Conditions.Deleting.Status == db.ConditionStatusTrue {
|
|
return nil
|
|
}
|
|
|
|
cluster = lockedCluster
|
|
reconciler := newReconciler(c.cfg, c.logger, tx, c.controllerTemporalClient, c.k8sClient, c.k8sReader)
|
|
if err := reconciler.reconcile(ctx, lockedCluster); err != nil {
|
|
return err
|
|
}
|
|
requeue = reconciler.requeue
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return reconcile.Result{}, err
|
|
}
|
|
result, err := c.reconcileNodeAgentUpgrade(ctx, cluster)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
return mergeRequeue(result, requeue), nil
|
|
}
|
|
|
|
// mergeRequeue schedules a control-plane check-back only when the node-agent
|
|
// upgrade scheduled none. A cluster with a node-agent version already requeues
|
|
// within agentUpgradeRetryDelay, so the check-back matters only without one.
|
|
func mergeRequeue(result reconcile.Result, requeue time.Duration) reconcile.Result {
|
|
if requeue > 0 && result.RequeueAfter <= 0 {
|
|
result.RequeueAfter = requeue
|
|
}
|
|
return result
|
|
}
|