Files
nvke/controller/e2e/framework/postgres.go
Aaron Patten 55b2a0029a refactor(dev)!: remove the deprecated CWE_* and NKE_CONTROLLER_* setting names (#2114)
* 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>
2026-11-04 12:29:02 -05:00

87 lines
2.5 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"
"database/sql"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/uptrace/bun/driver/pgdriver"
)
const (
ControllerGofailPostgresDSNEnv = "NVKE_CONTROLLER_GOFAIL_E2E_DB_CONNECTION_STRING"
postgresReadyTimeout = 15 * time.Second
)
// StartPostgres returns the CI-provided PostgreSQL DSN or starts an ephemeral local server.
func StartPostgres(t testing.TB) string {
t.Helper()
if dsn := os.Getenv(ControllerGofailPostgresDSNEnv); dsn != "" {
waitForPostgres(t, dsn)
return dsn
}
initdb, err := exec.LookPath("initdb")
require.NoError(t, err, "initdb is required locally; set %s to use an existing disposable PostgreSQL database", ControllerGofailPostgresDSNEnv)
postgres, err := exec.LookPath("postgres")
require.NoError(t, err, "postgres is required locally; set %s to use an existing disposable PostgreSQL database", ControllerGofailPostgresDSNEnv)
dataDir := filepath.Join(t.TempDir(), "data")
initCmd := exec.Command(initdb, "-D", dataDir, "-A", "trust", "-U", "postgres", "--no-locale", "--encoding=UTF8", "--no-sync") // #nosec G204 -- test-controlled executable and directory.
output, err := initCmd.CombinedOutput()
require.NoError(t, err, "initdb failed:\n%s", output)
addr := FreeLocalhostAddress(t)
_, port, err := net.SplitHostPort(addr)
require.NoError(t, err)
process := StartProcess(t, ProcessConfig{
Command: postgres,
Args: []string{"-D", dataDir, "-h", "127.0.0.1", "-p", port, "-F"},
ReadyContains: "database system is ready to accept connections",
ReadyTimeout: postgresReadyTimeout,
})
process.WaitReady(t)
t.Cleanup(func() {
process.Stop(t)
})
dsn := fmt.Sprintf("postgres://postgres@%s/postgres?sslmode=disable", addr)
waitForPostgres(t, dsn)
return dsn
}
func waitForPostgres(t testing.TB, dsn string) {
t.Helper()
database := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
t.Cleanup(func() {
require.NoError(t, database.Close())
})
ctx, cancel := context.WithTimeout(context.Background(), postgresReadyTimeout)
defer cancel()
for {
if err := database.PingContext(ctx); err == nil {
return
}
select {
case <-ctx.Done():
require.FailNow(t, "PostgreSQL did not become ready", "dsn=%s", dsn)
case <-time.After(100 * time.Millisecond):
}
}
}