Files
Touhidul Alam Seyam eb8ce5efac fix(controller): skip node-agent probe when node has no IP address (#2339)
Pending nodes have no IP address yet, and the readiness probe dialed
localhost on every reconcile, storing the localhost error as the node's
nodeAgentReady message. Transport.Dial now returns ErrNoAddress before
any credential setup so every agent caller is covered, and the readiness
controller marks such nodes unready with "Node has no IP address"
without probing or counting the attempt in the probe metrics.

Closes #2264

Signed-off-by: Seyamalam <seyamalam41@gmail.com>
2026-11-17 16:18:00 -07:00

219 lines
8.3 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package nodeagentreadiness
import (
"context"
"log/slog"
"testing"
"testing/synctest"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/dsx-ai-factory/nvke/controller/internal/agent"
"github.com/dsx-ai-factory/nvke/controller/internal/agent/agenttest"
"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"
"github.com/dsx-ai-factory/nvke/controller/internal/db"
"github.com/dsx-ai-factory/nvke/controller/internal/testutil"
)
func TestUnit_AgentReadinessControllerRequeuesAfterProbe(t *testing.T) {
ctx := context.Background()
nodeID := uuid.New()
node := db.Node{ID: nodeID, Name: "worker-1", IP: "192.0.2.10"}
database := &testutil.MockDB{}
database.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
database.On("RunInTx", mock.Anything, mock.Anything).Return(nil).Once()
tx := &testutil.MockTx{}
database.MockTx = tx
tx.On("GetNodeForUpdate", mock.Anything, nodeID).Return(node, nil).Once()
tx.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
tx.On("UpdateNode", mock.Anything, mock.MatchedBy(func(updated *db.Node) bool {
return updated.Conditions.NodeAgentReady.Status == db.ConditionStatusTrue &&
updated.AgentVersion == "v9.9.9"
})).Return(node, nil).Once()
controller := NewAgentReadinessController(
config.Config{NodeHealthCheckTimeout: time.Second},
slog.New(slog.DiscardHandler),
dbwrapper.New(database, slog.New(slog.DiscardHandler)),
&agenttest.FakeClient{AgentVersion: "v9.9.9"},
)
result, err := controller.Reconcile(ctx, dbreconcile.NodeKey(nodeID))
require.NoError(t, err)
require.Equal(t, 2*time.Minute, result.RequeueAfter)
database.AssertExpectations(t)
tx.AssertExpectations(t)
}
type blockingAgent struct {
agent.Client
started chan struct{}
}
func (a *blockingAgent) GetAgentVersion(ctx context.Context, _ db.Node) (string, error) {
if a.started != nil {
close(a.started)
}
<-ctx.Done()
return "", ctx.Err()
}
func TestUnit_AgentProbeUsesDedicatedDeadline(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
const probeTimeout = 5 * time.Second
nodeID := uuid.New()
node := db.Node{ID: nodeID, IP: "192.0.2.10", Conditions: db.NodeConditions{
NodeAgentReady: db.NewCondition(false, context.DeadlineExceeded.Error()),
}}
database := &testutil.MockDB{}
database.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
c := NewAgentReadinessController(config.Config{
NodeHealthCheckTimeout: time.Minute,
AgentReadiness: config.AgentReadinessConfig{Timeout: probeTimeout},
}, slog.New(slog.DiscardHandler), dbwrapper.New(database, nil), &blockingAgent{})
started := time.Now()
result, err := c.Reconcile(t.Context(), dbreconcile.NodeKey(nodeID))
require.NoError(t, err)
require.Equal(t, probeTimeout, time.Since(started))
require.Equal(t, 2*time.Minute, result.RequeueAfter)
database.AssertExpectations(t)
database.AssertNotCalled(t, "RunInTx", mock.Anything, mock.Anything)
})
}
func TestUnit_AgentProbeShutdownDoesNotChangeReadiness(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
nodeID := uuid.New()
database := &testutil.MockDB{}
database.On("GetNode", mock.Anything, nodeID).Return(db.Node{ID: nodeID, IP: "192.0.2.10"}, nil).Once()
probeStarted := make(chan struct{})
c := NewAgentReadinessController(config.Config{}, slog.New(slog.DiscardHandler), dbwrapper.New(database, nil), &blockingAgent{started: probeStarted})
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
started := time.Now()
done := make(chan error, 1)
go func() {
_, err := c.Reconcile(ctx, dbreconcile.NodeKey(nodeID))
done <- err
}()
<-probeStarted
synctest.Wait()
cancel()
synctest.Wait()
require.ErrorIs(t, <-done, context.Canceled)
require.Zero(t, time.Since(started))
database.AssertExpectations(t)
database.AssertNotCalled(t, "RunInTx", mock.Anything, mock.Anything)
})
}
func TestUnit_AgentProbeJitterBounds(t *testing.T) {
c := NewAgentReadinessController(config.Config{AgentReadiness: config.AgentReadinessConfig{
Interval: 2 * time.Minute, Jitter: 0.1,
}}, nil, nil, nil)
var total time.Duration
for range 1000 {
delay := c.requeueAfter()
require.GreaterOrEqual(t, delay, 108*time.Second)
require.Less(t, delay, 132*time.Second)
total += delay
}
require.InDelta(t, 120, (total / 1000).Seconds(), 2)
}
type countingAgentClient struct {
agent.Client
getAgentVersionCalls int
}
func (a *countingAgentClient) GetAgentVersion(context.Context, db.Node) (string, error) {
a.getAgentVersionCalls++
return "", nil
}
func TestUnit_AgentProbeSkippedForNodeWithNoIP(t *testing.T) {
nodeID := uuid.New()
node := db.Node{ID: nodeID, AgentVersion: "v9.9.9", Conditions: db.NodeConditions{
NodeAgentReady: db.NewCondition(true, "Node agent is reachable"),
}}
database := &testutil.MockDB{}
database.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
database.On("RunInTx", mock.Anything, mock.Anything).Return(nil).Once()
tx := &testutil.MockTx{}
database.MockTx = tx
tx.On("GetNodeForUpdate", mock.Anything, nodeID).Return(node, nil).Once()
tx.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
tx.On("UpdateNode", mock.Anything, mock.MatchedBy(func(updated *db.Node) bool {
return updated.Conditions.NodeAgentReady.Status == db.ConditionStatusFalse &&
updated.Conditions.NodeAgentReady.Message == noAddressMessage &&
updated.AgentVersion == node.AgentVersion
})).Return(node, nil).Once()
client := &countingAgentClient{}
c := NewAgentReadinessController(config.Config{}, slog.New(slog.DiscardHandler), dbwrapper.New(database, nil), client)
result, err := c.Reconcile(t.Context(), dbreconcile.NodeKey(nodeID))
require.NoError(t, err)
require.Equal(t, 2*time.Minute, result.RequeueAfter)
require.Zero(t, client.getAgentVersionCalls)
database.AssertExpectations(t)
tx.AssertExpectations(t)
}
func TestUnit_AgentProbeNoIPConditionUnchangedSkipsWrite(t *testing.T) {
nodeID := uuid.New()
node := db.Node{ID: nodeID, Conditions: db.NodeConditions{
NodeAgentReady: db.NewCondition(false, noAddressMessage),
}}
database := &testutil.MockDB{}
database.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
client := &countingAgentClient{}
c := NewAgentReadinessController(config.Config{}, slog.New(slog.DiscardHandler), dbwrapper.New(database, nil), client)
result, err := c.Reconcile(t.Context(), dbreconcile.NodeKey(nodeID))
require.NoError(t, err)
require.Equal(t, 2*time.Minute, result.RequeueAfter)
require.Zero(t, client.getAgentVersionCalls)
database.AssertNotCalled(t, "RunInTx", mock.Anything, mock.Anything)
database.AssertExpectations(t)
}
func TestUnit_AgentProbeTimeoutMarksReadyNodeUnready(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
nodeID := uuid.New()
node := db.Node{ID: nodeID, IP: "192.0.2.10", AgentVersion: "v9.9.9", Conditions: db.NodeConditions{
NodeAgentReady: db.NewCondition(true, "Node agent is reachable"),
}}
database := &testutil.MockDB{}
database.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
database.On("RunInTx", mock.Anything, mock.Anything).Return(nil).Once()
tx := &testutil.MockTx{}
database.MockTx = tx
tx.On("GetNodeForUpdate", mock.Anything, nodeID).Return(node, nil).Once()
tx.On("GetNode", mock.Anything, nodeID).Return(node, nil).Once()
tx.On("UpdateNode", mock.Anything, mock.MatchedBy(func(updated *db.Node) bool {
return updated.Conditions.NodeAgentReady.Status == db.ConditionStatusFalse &&
updated.Conditions.NodeAgentReady.Message == context.DeadlineExceeded.Error() &&
updated.AgentVersion == node.AgentVersion
})).Return(node, nil).Once()
c := NewAgentReadinessController(config.Config{AgentReadiness: config.AgentReadinessConfig{
Timeout: 5 * time.Second,
}}, slog.New(slog.DiscardHandler), dbwrapper.New(database, nil), &blockingAgent{})
result, err := c.Reconcile(t.Context(), dbreconcile.NodeKey(nodeID))
require.NoError(t, err)
require.Equal(t, 2*time.Minute, result.RequeueAfter)
database.AssertExpectations(t)
tx.AssertExpectations(t)
})
}