mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
* refactor(dev)!: remove the deprecated CWE_* and NKE_CONTROLLER_* setting names The controller's development, test and build settings read only their NVKE_CONTROLLER_* names now. The CWE_* and NKE_CONTROLLER_* aliases, deprecated since v0.23.0, are gone from the Makefiles, Tiltfiles, hack and CI scripts, the e2e framework and tests, and the docs. So are CWE_IMAGE (use CONTROLLER_IMAGE), the cwe_image key in tilt_config.json (use nvke_controller_image), --cwe-api in get-kubeconfig.sh (use --controller-api) and the cwe-name input of qualify-e2e.yml (use controller-name). local-backend-e2e.sh reads LOCAL_NVKE_CONTROLLER_IMAGE instead of LOCAL_CWE_IMAGE. BREAKING CHANGE: a value set under an old name, in .env, .env.tilt, tilt_config.json, a shell profile or a repository variable, is now ignored without a warning. Rename CWE_X and NKE_CONTROLLER_X to NVKE_CONTROLLER_X, except CWE_API (use NVKE_CONTROLLER_API_URL) and CWE_IMAGE in make -C cli images (use CONTROLLER_IMAGE). Rename LOCAL_CWE_IMAGE to LOCAL_NVKE_CONTROLLER_IMAGE and the cwe_image key to nvke_controller_image. get-kubeconfig.sh rejects --cwe-api (use --controller-api), and qualify-e2e.yml has no cwe-name input (use controller-name). Signed-off-by: Aaron Patten <apatten@nvidia.com> * ci: fail when a removed controller setting name comes back make name-check, which the merge gate runs, now also runs hack/removed-settings-check.py. It fails on any removed CWE_* or NKE_CONTROLLER_* setting name, LOCAL_CWE_IMAGE, cwe_image, --cwe-api or cwe-name in a tracked file, so a branch cut before the removal cannot bring a reader of an old name back. CHANGELOG.md is not scanned, and two look-alikes that are not controller settings are allowed by file. Signed-off-by: Aaron Patten <apatten@nvidia.com> --------- Signed-off-by: Aaron Patten <apatten@nvidia.com>
323 lines
11 KiB
Go
323 lines
11 KiB
Go
//go:build gofail
|
|
|
|
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package framework
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
"sigs.k8s.io/yaml"
|
|
|
|
"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/controllerconfig"
|
|
"github.com/dsx-ai-factory/nvke/controller/internal/db"
|
|
)
|
|
|
|
const (
|
|
ControllerGofailWorkerBinaryEnv = "NVKE_CONTROLLER_GOFAIL_E2E_WORKER_BINARY"
|
|
ControllerGofailFailpointName = "CWERunbookWorkerCrash"
|
|
ControllerGofailWorkerCrashLine = "cwe-gofail-runbook-worker-crash"
|
|
|
|
// ControllerGofailExecutionErrorFailpoint fails the named runbook immediately
|
|
// instead of hanging it, for scenarios about retry budgets rather than
|
|
// heartbeat loss.
|
|
ControllerGofailExecutionErrorFailpoint = "CWERunbookExecutionError"
|
|
controllerWorkerReadyTimeout = 20 * time.Second
|
|
controllerGofailRequestTimeout = 5 * time.Second
|
|
controllerWorkerReadyLog = "Starting Temporal worker"
|
|
)
|
|
|
|
var (
|
|
ControllerGofailNodeID = uuid.MustParse("11111111-1111-1111-1111-111111111111")
|
|
ControllerGofailClusterID = uuid.MustParse("33333333-3333-3333-3333-333333333333")
|
|
controllerGofailSiteID = uuid.MustParse("22222222-2222-2222-2222-222222222222")
|
|
controllerGofailNodePoolID = uuid.MustParse("44444444-4444-4444-4444-444444444444")
|
|
controllerGofailNodeSetID = uuid.MustParse("55555555-5555-5555-5555-555555555555")
|
|
controllerGofailInstanceID = uuid.MustParse("66666666-6666-6666-6666-666666666666")
|
|
controllerGofailControlNode = "cwe-gofail-cp-0"
|
|
)
|
|
|
|
// ControllerWorkerBinaryConfig describes the production worker and its e2e dependencies.
|
|
type ControllerWorkerBinaryConfig struct {
|
|
BinaryPath string
|
|
DBConnectionString string
|
|
KubeconfigPath string
|
|
NodeAgent config.NodeAgentConfig
|
|
TemporalHostPort string
|
|
Namespace string
|
|
TaskQueue string
|
|
GofailHTTPAddr string
|
|
Env []string
|
|
}
|
|
|
|
// ControllerWorkerBinary returns the production worker binary built by make e2e-gofail.
|
|
func ControllerWorkerBinary(t testing.TB) string {
|
|
t.Helper()
|
|
|
|
binaryPath := os.Getenv(ControllerGofailWorkerBinaryEnv)
|
|
require.NotEmpty(t, binaryPath, "%s must point to the worker built by make e2e-gofail", ControllerGofailWorkerBinaryEnv)
|
|
_, err := os.Stat(binaryPath)
|
|
require.NoError(t, err, "controller worker binary does not exist")
|
|
return binaryPath
|
|
}
|
|
|
|
// RepoRoot returns the module root containing go.mod.
|
|
func RepoRoot(t testing.TB) string {
|
|
t.Helper()
|
|
|
|
dir, err := os.Getwd()
|
|
require.NoError(t, err)
|
|
for {
|
|
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
|
return dir
|
|
}
|
|
parent := filepath.Dir(dir)
|
|
require.NotEqual(t, dir, parent, "could not find go.mod from %s", dir)
|
|
dir = parent
|
|
}
|
|
}
|
|
|
|
// FreeLocalhostAddress returns an available localhost TCP address.
|
|
func FreeLocalhostAddress(t testing.TB) string {
|
|
t.Helper()
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
require.NoError(t, ln.Close())
|
|
}()
|
|
return ln.Addr().String()
|
|
}
|
|
|
|
// ManagementNamespace returns the management-plane namespace the worker started
|
|
// by StartControllerWorkerBinary reads and writes CAPI objects in. It comes from
|
|
// the same local controller config the worker is handed, so a test that seeds a
|
|
// Machine puts it where the worker looks for it.
|
|
func ManagementNamespace(t testing.TB) string {
|
|
t.Helper()
|
|
|
|
settings, err := controllerconfig.Load(filepath.Join(RepoRoot(t), "hack", "local-controller-config.yaml"))
|
|
require.NoError(t, err)
|
|
if settings.ManagementNamespace == "" {
|
|
return constants.LegacyManagementNamespace
|
|
}
|
|
return settings.ManagementNamespace
|
|
}
|
|
|
|
// StartControllerWorkerBinary starts the production controller command against e2e-owned dependencies.
|
|
func StartControllerWorkerBinary(t testing.TB, cfg ControllerWorkerBinaryConfig) *Process {
|
|
t.Helper()
|
|
|
|
require.NotEmpty(t, cfg.DBConnectionString)
|
|
require.NotEmpty(t, cfg.KubeconfigPath)
|
|
require.NotEmpty(t, cfg.NodeAgent.Port)
|
|
// Config profiles and runbooks come from the sanitized fixtures tree.
|
|
profilesDir := filepath.Join(RepoRoot(t), "test", "fixtures", "profiles")
|
|
_, err := os.Stat(filepath.Join(profilesDir, "configProfiles"))
|
|
require.NoError(t, err, "expected fixture config profiles under %s", profilesDir)
|
|
|
|
settings, err := controllerconfig.Load(filepath.Join(RepoRoot(t), "hack", "local-controller-config.yaml"))
|
|
require.NoError(t, err)
|
|
settings.Temporal.HostPort = cfg.TemporalHostPort
|
|
settings.Temporal.Namespace = cfg.Namespace
|
|
settings.Temporal.TaskQueue = cfg.TaskQueue
|
|
settings.ProfilesDir = profilesDir
|
|
settings.RunbooksDir = ""
|
|
settings.Spire.SpiffeEnabled = false
|
|
settings.Teleport.Enabled = false
|
|
encoded, err := yaml.Marshal(settings)
|
|
require.NoError(t, err)
|
|
settingsPath := filepath.Join(t.TempDir(), "controller-config.yaml")
|
|
require.NoError(t, os.WriteFile(settingsPath, encoded, 0o600))
|
|
|
|
env := []string{
|
|
"CONTROLLER_CONFIG_PATH=" + settingsPath,
|
|
"DB_CONNECTION_STRING=" + cfg.DBConnectionString,
|
|
"DB_MAX_OPEN_CONNECTIONS=4",
|
|
"DB_QUERY_LOG_LEVEL=debug",
|
|
"KUBECONFIG=" + cfg.KubeconfigPath,
|
|
"NODE_AGENT_PORT=" + cfg.NodeAgent.Port,
|
|
"NODE_HEALTH_CHECK_DISABLED=true",
|
|
"GOFAIL_FAILPOINTS=",
|
|
"GOFAIL_HTTP=",
|
|
"SERVER_LISTEN_ADDRESS=127.0.0.1:0",
|
|
"LOG_LEVEL=info",
|
|
}
|
|
// Mirror production: supply the DB password via CNPG_SUPERUSER_PASSWORD so config
|
|
// validation accepts the default credentials in DB_CONNECTION_STRING.
|
|
if u, err := url.Parse(cfg.DBConnectionString); err == nil {
|
|
if password, ok := u.User.Password(); ok && password != "" {
|
|
env = append(env, "CNPG_SUPERUSER_PASSWORD="+password)
|
|
}
|
|
}
|
|
if cfg.GofailHTTPAddr != "" {
|
|
env = append(env, "GOFAIL_HTTP="+cfg.GofailHTTPAddr)
|
|
}
|
|
env = append(env, cfg.Env...)
|
|
|
|
return StartProcess(t, ProcessConfig{
|
|
Command: cfg.BinaryPath,
|
|
WorkingDir: RepoRoot(t),
|
|
Env: env,
|
|
ReadyContains: controllerWorkerReadyLog,
|
|
ReadyTimeout: controllerWorkerReadyTimeout,
|
|
})
|
|
}
|
|
|
|
// WriteKubeconfig returns client configuration for the normal worker bootstrap.
|
|
// StartAPIServer explains why a placeholder address no longer works.
|
|
func WriteKubeconfig(t testing.TB) string {
|
|
t.Helper()
|
|
|
|
return StartAPIServer(t)
|
|
}
|
|
|
|
// SetGofail enables a failpoint through gofail's HTTP control plane.
|
|
func SetGofail(t testing.TB, addr, name, term string) {
|
|
t.Helper()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), controllerGofailRequestTimeout)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, "http://"+addr+"/"+name, strings.NewReader(term))
|
|
require.NoError(t, err)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
require.NoError(t, resp.Body.Close())
|
|
}()
|
|
body, err := io.ReadAll(resp.Body)
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusNoContent, resp.StatusCode, "gofail response: %s", body)
|
|
}
|
|
|
|
// SeedControllerGofailDB inserts the production workflow's minimum database state.
|
|
func SeedControllerGofailDB(t testing.TB, dsn string) {
|
|
t.Helper()
|
|
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
bunDB, err := db.NewBunDB(config.Config{
|
|
DBConnectionString: dsn,
|
|
DBMaxOpenConnections: 2,
|
|
DBQueryLogLevel: "info",
|
|
}, logger)
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
require.NoError(t, bunDB.Close())
|
|
}()
|
|
require.NoError(t, seedControllerGofailDB(context.Background(), db.NewDB(bunDB)))
|
|
}
|
|
|
|
// seedControllerGofailDB inserts the seed rows in one transaction. The worker is
|
|
// already running, and its node-pool controller deletes any non-current node
|
|
// set that has no nodes, so a set committed on its own can be gone before the
|
|
// node insert lands. Same reason as seedTeardownScenario.
|
|
func seedControllerGofailDB(ctx context.Context, database db.DB) error {
|
|
return database.RunInTx(ctx, func(ctx context.Context, tx db.Tx) error {
|
|
return seedControllerGofailDBTx(ctx, tx)
|
|
})
|
|
}
|
|
|
|
func seedControllerGofailDBTx(ctx context.Context, database db.Tx) error {
|
|
cluster := db.Cluster{
|
|
ID: ControllerGofailClusterID,
|
|
Name: "cwe-gofail-cluster",
|
|
KubernetesVersion: "1.30.0",
|
|
Conditions: db.ClusterConditions{
|
|
Provisioned: db.NewCondition(true, "seeded"),
|
|
},
|
|
}
|
|
cluster.SetNicoProvider(db.NicoClusterProvider{
|
|
OrgID: "cwe-gofail-org",
|
|
SiteID: controllerGofailSiteID,
|
|
VpcID: uuid.New(),
|
|
})
|
|
if _, err := database.CreateCluster(ctx, &cluster); err != nil {
|
|
return fmt.Errorf("create cluster: %w", err)
|
|
}
|
|
|
|
nodePool := db.NodePool{
|
|
ID: controllerGofailNodePoolID,
|
|
ClusterID: ControllerGofailClusterID,
|
|
Name: "cwe-gofail-cp",
|
|
NodeNamePrefix: "cwe-gofail-cp",
|
|
ControlPlane: true,
|
|
DesiredCount: 1,
|
|
SystemReservedMemory: "16Gi",
|
|
NodeLabels: map[string]string{},
|
|
Conditions: db.NodePoolConditions{
|
|
Provisioned: db.NewCondition(true, "seeded"),
|
|
},
|
|
}
|
|
nodePool.SetNico(db.NicoNodePoolProvider{
|
|
InstanceTypeID: controllerGofailInstanceID,
|
|
Release: "test",
|
|
ConfigProfile: "test",
|
|
SubnetIDs: []uuid.UUID{},
|
|
VpcPrefixIDs: []uuid.UUID{},
|
|
SSHKeyGroupIDs: []uuid.UUID{},
|
|
DiskSelectionStrategy: db.DiskSelectionStrategySmallest,
|
|
})
|
|
if _, err := database.CreateNodePool(ctx, &nodePool); err != nil {
|
|
return fmt.Errorf("create node pool: %w", err)
|
|
}
|
|
|
|
nodeSet := db.NodeSet{
|
|
ID: controllerGofailNodeSetID,
|
|
NodePoolID: controllerGofailNodePoolID,
|
|
DesiredCount: 1,
|
|
KubernetesVersion: "1.30.0",
|
|
SystemReservedMemory: "16Gi",
|
|
}
|
|
nodeSet.SetNico(db.NicoNodePoolProvider{
|
|
Release: "test",
|
|
ConfigProfile: "test",
|
|
SubnetIDs: []uuid.UUID{},
|
|
VpcPrefixIDs: []uuid.UUID{},
|
|
DiskSelectionStrategy: db.DiskSelectionStrategySmallest,
|
|
})
|
|
if _, err := database.CreateNodeSet(ctx, &nodeSet); err != nil {
|
|
return fmt.Errorf("create node set: %w", err)
|
|
}
|
|
|
|
node := db.Node{
|
|
ID: ControllerGofailNodeID,
|
|
ClusterID: ControllerGofailClusterID,
|
|
NodePoolID: controllerGofailNodePoolID,
|
|
NodeSetID: controllerGofailNodeSetID,
|
|
ControlPlane: true,
|
|
Name: controllerGofailControlNode,
|
|
IP: "127.0.0.1",
|
|
SpireAttestationMethod: db.SpireAttestationMethodTPM,
|
|
Conditions: db.NodeConditions{
|
|
Joined: db.NewCondition(true, "seeded"),
|
|
KubernetesReady: db.NewCondition(true, "seeded"),
|
|
NodeAgentReady: db.NewCondition(true, "seeded"),
|
|
},
|
|
}
|
|
node.SetNico(db.NicoNodeProvider{
|
|
InstanceTypeID: controllerGofailInstanceID,
|
|
})
|
|
if _, err := database.CreateNode(ctx, &node); err != nil {
|
|
return fmt.Errorf("create node: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|