mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
* feat(controller): add the keys package for the nvke.nvidia.com label domain The labels, annotations, taints and node conditions this controller owns move from nke.nvidia.com to nvke.nvidia.com. The rename is not atomic: objects labelled by an earlier build keep the old keys until something rewrites them, and a reboot or terminate request applied by hand arrives under whichever domain the operator knows. keys.Key holds the bare name and renders it under either domain. Callers write Key.String(), always the new one, and read with Get/Value/Has/ Matches, which accept both. Key.All() is the delete form, because removing only the new key leaves a legacy copy the next read would find. Both domains are compiled in, as cluster-api-provider-nico did for its own rename: there is no third domain anything could read, so there is nothing to configure. Signed-off-by: Terry Howe <thowe@nvidia.com> * feat(controller): write object keys under nvke.nvidia.com and read both domains Routes every label, annotation, taint and node-condition key the controller owns through internal/keys, and deletes the string constants that held them. Writes land on nvke.nvidia.com; reads accept either domain, preferring the new one. Three places need more than a mechanical swap: - Node topology labels are rewritten on reconcile, which drops the legacy key rather than leaving a node carrying both. - Taint removal strips both domains. A node joined before the rename carries the provisioning taint under the legacy key, and a workflow untainting it may now be asking for the new one; removing only the requested form would leave the node NoSchedule for good. - Reboot and terminate completion clears both forms of the request label, which an operator or an external automation may have applied under either. Secret ownership labels converge the same way: a rewrite drops the legacy key so a Secret carries one domain, not two that could drift. Signed-off-by: Terry Howe <thowe@nvidia.com> * feat(nico): point the capnico reboot and repair annotations at nvke.nvidia.com cluster-api-provider-nico watches exactly one Machine annotation for a reboot and one for a repair, named by --reboot-annotation and --repair-annotation, and silently ignores a request under any other key. A mismatch is not an error anywhere: reboot and repair requests are simply never actioned. So the controller writes both annotations under both domains until every deployment has moved, and these manifests move to the new one. Both keys are owned by the same field manager, so clearing the annotation still clears both. The dev capd-reboot-controller reads either form. Sites that deploy capnico from their own manifests can move the flags on their own schedule; see docs/operations/label-domain-migration.md. Signed-off-by: Terry Howe <thowe@nvidia.com> * feat(cli): write the etcd key cluster-name annotation under nvke.nvidia.com nvkectl writes this annotation onto the etcd encryption key Secret it saves to disk, and reads it back to match a file to a cluster. The legacy key stays exported so a file written by an earlier nvkectl is still readable. Signed-off-by: Terry Howe <thowe@nvidia.com> * feat(capp): write the SPIRE agent path annotation under nvke.nvidia.com The provider owns this Machine annotation, so it is the thing that moves a Machine over: the write drops the legacy key rather than leaving two for the controller to choose between. Signed-off-by: Terry Howe <thowe@nvidia.com> * docs: describe the nvke.nvidia.com label and annotation domain What an existing site has to do (check the capnico flags, and anything outside NVKE that matches on these keys), what it does not (relabel objects, coordinate the upgrade, change how reboots are requested), and which nke.nvidia.com names are deliberately not part of this change. Signed-off-by: Terry Howe <thowe@nvidia.com> * chore: link the capnico dual-write to the issue that removes it The two Machine annotations written in both domains are scaffolding for a rollout, not a design. FIXME them against #1718 so the next reader knows what has to be true before they go, and say in the migration guide that a stale --reboot-annotation or --repair-annotation becomes a silent failure once the dual write is gone. Signed-off-by: Terry Howe <thowe@nvidia.com> * fix: address review feedback - nodestatus: try the current domain before the legacy one when reading NeedsRecycle and NeedsRepair. Status.Conditions has no ordering guarantee, so matching either form took whichever came first: a stale legacy False left by an earlier build could mask a current True and drop the recycle or repair request it records. - nico_spiffe_controller_test: assert absence against the rendered key. testify compares map keys without converting types, so a keys.Key needle made the NotContains pass even with the annotation present. - capd-reboot-controller: honour a legacy reboot-complete timestamp. It already read a legacy reboot request but checked only the current completion key, so it replayed the simulated reboot and overwrote the completion it had already recorded. - management-engine: reserve nvke.nvidia.com in the userParams.labels CEL rule, not just in the doc comment. The rule excluded the legacy domain only, so a label under the domain this controller now writes could pass CRD admission and collide with one NVKE owns. The REST API validator already rejected both domains; this aligns the two. Signed-off-by: Terry Howe <thowe@nvidia.com> * fix: address review feedback Signed-off-by: Terry Howe <thowe@nvidia.com> --------- Signed-off-by: Terry Howe <thowe@nvidia.com>
253 lines
9.0 KiB
Go
253 lines
9.0 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package automode
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/require"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/config"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/constants"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/controllers/dbreconcile"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/controllers/dbreconcile/dbwrapper"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/db"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/keys"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/temporal/workflows"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/testutil"
|
|
)
|
|
|
|
func TestUnit_ReconcileSkipsNodePools(t *testing.T) {
|
|
ctx := context.Background()
|
|
nodePoolID := uuid.New()
|
|
currentNodeSetID := uuid.New()
|
|
|
|
type parameters struct {
|
|
nodePool db.NodePool
|
|
}
|
|
|
|
tests := map[string]parameters{
|
|
"missing node pool": {
|
|
nodePool: db.NodePool{},
|
|
},
|
|
"auto mode disabled": {
|
|
nodePool: baseNodePool(nodePoolID, currentNodeSetID, func(nodePool *db.NodePool) {
|
|
nodePool.AutoModeDisabled = true
|
|
}),
|
|
},
|
|
"control plane": {
|
|
nodePool: baseNodePool(nodePoolID, currentNodeSetID, func(nodePool *db.NodePool) {
|
|
nodePool.ControlPlane = true
|
|
}),
|
|
},
|
|
"deleting": {
|
|
nodePool: baseNodePool(nodePoolID, currentNodeSetID, func(nodePool *db.NodePool) {
|
|
nodePool.Conditions.Deleting = db.NewCondition(true, "Deleting")
|
|
}),
|
|
},
|
|
"no recycle budget": {
|
|
nodePool: baseNodePool(nodePoolID, currentNodeSetID, func(nodePool *db.NodePool) {
|
|
nodePool.RecycleBudget = 0
|
|
}),
|
|
},
|
|
"no current node set": {
|
|
nodePool: baseNodePool(nodePoolID, currentNodeSetID, func(nodePool *db.NodePool) {
|
|
nodePool.CurrentNodeSetID = nil
|
|
}),
|
|
},
|
|
}
|
|
|
|
for name, params := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
mockDB := &testutil.MockDB{}
|
|
mockTx := &testutil.MockTx{}
|
|
mockDB.MockTx = mockTx
|
|
mockDB.On("RunInTx", mock.Anything, mock.Anything).Return(nil)
|
|
if name == "missing node pool" {
|
|
mockTx.On("GetNodePoolForUpdate", mock.Anything, nodePoolID).Return(db.NodePool{}, db.ErrNotFound)
|
|
} else {
|
|
mockTx.On("GetNodePoolForUpdate", mock.Anything, nodePoolID).Return(params.nodePool, nil)
|
|
}
|
|
|
|
controller := NewController(config.Config{}, slog.Default(), dbwrapper.New(mockDB, slog.Default()))
|
|
|
|
result, err := controller.Reconcile(ctx, dbreconcile.NodePoolKey(nodePoolID))
|
|
|
|
require.NoError(t, err)
|
|
assert.Equal(t, reconcile.Result{}, result)
|
|
mockDB.AssertExpectations(t)
|
|
mockTx.AssertExpectations(t)
|
|
mockTx.AssertNotCalled(t, "ListNodes", mock.Anything, mock.Anything)
|
|
mockTx.AssertNotCalled(t, "ListNodesEligibleForRecycling", mock.Anything, mock.Anything, mock.Anything)
|
|
mockTx.AssertNotCalled(t, "UpdateNode", mock.Anything, mock.Anything)
|
|
mockTx.AssertNotCalled(t, "CreateWorkflowOutboxEntry", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUnit_ReconcileMarksDeprecatedNodesWithinBudget(t *testing.T) {
|
|
ctx := context.Background()
|
|
nodePoolID := uuid.New()
|
|
currentNodeSetID := uuid.New()
|
|
nodePool := baseNodePool(nodePoolID, currentNodeSetID)
|
|
nodePool.DesiredCount = 3
|
|
nodePool.RecycleBudget = 2
|
|
availableNodes := []db.Node{
|
|
{ID: uuid.New(), NodePoolID: nodePoolID, NodeSetID: currentNodeSetID},
|
|
{ID: uuid.New(), NodePoolID: nodePoolID, NodeSetID: currentNodeSetID},
|
|
}
|
|
recyclingNode := db.Node{
|
|
ID: uuid.New(),
|
|
NodePoolID: nodePoolID,
|
|
NodeSetID: uuid.New(),
|
|
Name: "worker-old-0",
|
|
}
|
|
|
|
mockDB := &testutil.MockDB{}
|
|
mockTx := &testutil.MockTx{}
|
|
mockDB.MockTx = mockTx
|
|
mockDB.On("RunInTx", mock.Anything, mock.Anything).Return(nil)
|
|
mockTx.On("GetNodePoolForUpdate", mock.Anything, nodePoolID).Return(nodePool, nil)
|
|
mockTx.On("ListNodes", mock.Anything, availableNodeFilters(nodePoolID)).Return(availableNodes, nil)
|
|
mockTx.On("ListNodesEligibleForRecycling", mock.Anything, nodePoolID, 1).Return([]db.Node{recyclingNode}, nil)
|
|
mockTx.On("GetNodeForUpdate", mock.Anything, recyclingNode.ID).Return(recyclingNode, nil)
|
|
mockTx.On("GetNode", mock.Anything, recyclingNode.ID).Return(recyclingNode, nil)
|
|
mockTx.On("UpdateNode", mock.Anything, mock.MatchedBy(func(node *db.Node) bool {
|
|
return node.ID == recyclingNode.ID &&
|
|
node.Conditions.MarkedForRecycling.Status == db.ConditionStatusTrue &&
|
|
node.Conditions.MarkedForRecycling.Message == "Marked for auto-mode recycling"
|
|
})).Return(recyclingNode, nil)
|
|
mockTx.On(
|
|
"CreateWorkflowOutboxEntry",
|
|
mock.Anything,
|
|
workflows.ApplyK8sNodeConditionWorkflowType,
|
|
constants.ApplyK8sNodeConditionWorkflowPrefix+recyclingNode.ID.String(),
|
|
mock.MatchedBy(func(input workflows.ApplyK8sNodeConditionInput) bool {
|
|
return input.NodeID == recyclingNode.ID &&
|
|
string(input.NodeCondition.Type) == keys.NeedsRecycle.String() &&
|
|
input.NodeCondition.Status == corev1.ConditionTrue &&
|
|
input.NodeCondition.Reason == "Deprecated" &&
|
|
input.NodeCondition.Message == "NVKE auto-mode recycling deprecated node"
|
|
}),
|
|
).Return(db.WorkflowOutbox{}, nil)
|
|
|
|
controller := NewController(config.Config{}, slog.Default(), dbwrapper.New(mockDB, slog.Default()))
|
|
|
|
result, err := controller.Reconcile(ctx, dbreconcile.NodePoolKey(nodePoolID))
|
|
|
|
require.NoError(t, err)
|
|
assert.Equal(t, reconcile.Result{}, result)
|
|
mockDB.AssertExpectations(t)
|
|
mockTx.AssertExpectations(t)
|
|
}
|
|
|
|
func TestUnit_ReconcileSelfEnqueueIsIdempotent(t *testing.T) {
|
|
ctx := context.Background()
|
|
nodePoolID := uuid.New()
|
|
currentNodeSetID := uuid.New()
|
|
nodePool := baseNodePool(nodePoolID, currentNodeSetID)
|
|
nodePool.DesiredCount = 1
|
|
nodePool.RecycleBudget = 1
|
|
recyclingNode := db.Node{
|
|
ID: uuid.New(),
|
|
NodePoolID: nodePoolID,
|
|
NodeSetID: uuid.New(),
|
|
Name: "worker-old-0",
|
|
}
|
|
|
|
mockDB := &testutil.MockDB{}
|
|
mockTx := &testutil.MockTx{}
|
|
mockDB.MockTx = mockTx
|
|
mockDB.On("RunInTx", mock.Anything, mock.Anything).Return(nil).Twice()
|
|
mockTx.On("GetNodePoolForUpdate", mock.Anything, nodePoolID).Return(nodePool, nil).Twice()
|
|
mockTx.On("ListNodes", mock.Anything, availableNodeFilters(nodePoolID)).Return([]db.Node{{ID: uuid.New(), NodePoolID: nodePoolID}}, nil).Twice()
|
|
mockTx.On("ListNodesEligibleForRecycling", mock.Anything, nodePoolID, 1).Return([]db.Node{recyclingNode}, nil).Once()
|
|
mockTx.On("ListNodesEligibleForRecycling", mock.Anything, nodePoolID, 1).Return([]db.Node{}, nil).Once()
|
|
mockTx.On("GetNodeForUpdate", mock.Anything, recyclingNode.ID).Return(recyclingNode, nil).Once()
|
|
mockTx.On("GetNode", mock.Anything, recyclingNode.ID).Return(recyclingNode, nil).Once()
|
|
mockTx.On("UpdateNode", mock.Anything, mock.MatchedBy(func(node *db.Node) bool {
|
|
return node.ID == recyclingNode.ID &&
|
|
node.Conditions.MarkedForRecycling.Status == db.ConditionStatusTrue
|
|
})).Return(recyclingNode, nil).Once()
|
|
mockTx.On("CreateWorkflowOutboxEntry", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
|
Return(db.WorkflowOutbox{}, nil).
|
|
Once()
|
|
|
|
controller := NewController(config.Config{}, slog.Default(), dbwrapper.New(mockDB, slog.Default()))
|
|
|
|
_, err := controller.Reconcile(ctx, dbreconcile.NodePoolKey(nodePoolID))
|
|
require.NoError(t, err)
|
|
_, err = controller.Reconcile(ctx, dbreconcile.NodePoolKey(nodePoolID))
|
|
require.NoError(t, err)
|
|
|
|
mockDB.AssertExpectations(t)
|
|
mockTx.AssertExpectations(t)
|
|
}
|
|
|
|
func TestUnit_NodePoolKeysForNodeSet(t *testing.T) {
|
|
nodePoolID := uuid.New()
|
|
|
|
keys, err := nodePoolKeysForNodeSet(context.Background(), db.NodeSet{NodePoolID: nodePoolID})
|
|
require.NoError(t, err)
|
|
assert.Equal(t,
|
|
[]dbreconcile.Key{dbreconcile.NodePoolKey(nodePoolID)},
|
|
keys,
|
|
)
|
|
keys, err = nodePoolKeysForNodeSet(context.Background(), db.NodeSet{})
|
|
require.NoError(t, err)
|
|
assert.Nil(t, keys)
|
|
}
|
|
|
|
func TestUnit_NodePoolKeysForNode(t *testing.T) {
|
|
nodePoolID := uuid.New()
|
|
|
|
keys, err := nodePoolKeysForNode(context.Background(), db.Node{NodePoolID: nodePoolID})
|
|
require.NoError(t, err)
|
|
assert.Equal(t,
|
|
[]dbreconcile.Key{dbreconcile.NodePoolKey(nodePoolID)},
|
|
keys,
|
|
)
|
|
keys, err = nodePoolKeysForNode(context.Background(), db.Node{})
|
|
require.NoError(t, err)
|
|
assert.Nil(t, keys)
|
|
}
|
|
|
|
func baseNodePool(id, currentNodeSetID uuid.UUID, opts ...func(*db.NodePool)) db.NodePool {
|
|
nodePool := db.NodePool{
|
|
ID: id,
|
|
ClusterID: uuid.New(),
|
|
Name: "workers",
|
|
DesiredCount: 1,
|
|
RecycleBudget: 1,
|
|
CurrentNodeSetID: ¤tNodeSetID,
|
|
Conditions: db.NodePoolConditions{},
|
|
}
|
|
for _, opt := range opts {
|
|
opt(&nodePool)
|
|
}
|
|
return nodePool
|
|
}
|
|
|
|
func availableNodeFilters(nodePoolID uuid.UUID) db.NodeFilters {
|
|
return db.NodeFilters{
|
|
NodePoolID: &nodePoolID,
|
|
KubernetesReady: ptrTo(true),
|
|
KubernetesUnschedulable: ptrTo(false),
|
|
Deleting: ptrTo(false),
|
|
MarkedForRecycling: ptrTo(false),
|
|
MarkedForRepair: ptrTo(false),
|
|
}
|
|
}
|
|
|
|
func ptrTo[T any](v T) *T {
|
|
return &v
|
|
}
|