Files
nvke/cli/internal/deploypath/stackconfig/stackconfig_test.go
Meet Vadaria ef31c6efe7 feat(deploy): add a LaunchLayer deploy path that installs CAPLL (#2113)
* feat(deploy): add a LaunchLayer deploy path that installs CAPLL

deploy/nico installs NVKE with CAPNICo. deploy/launchlayer is the same
path with CAPNICo swapped for CAPLL, so a site built with NVKE's own
tooling can run its tenant clusters on LaunchLayer:

- capll: a template item from CAPLL v0.1.0-rc.15's release asset, whose
  manager flags (base URL, per-service overrides, identity Secret) are
  rendered from the stack config, plus a Secret get grant in
  nvke-system, where NVKE writes the bootstrap Secrets CAPLL reads.
- nvke-base-config renders launchlayer-credentials in capll-system in
  place of nico-credentials.
- The controller accepts launchLayer, turns the NICo grant and
  controllers off, and attests nodes with dsxImds.
- The items the two paths share are copies of deploy/nico, held to it
  by a test: a file both hold must be identical unless it is one of the
  four LaunchLayer changes, so a pin bump in deploy/nico that misses
  deploy/launchlayer fails.

The stack config gains an optional launchLayer object, with the client
secret from NVKECTL_LAUNCHLAYER_CLIENT_SECRET. Each provider's values are
required by the path that installs it, so deploy/nico and every existing
stack config are checked and rendered exactly as before.

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>

* refactor(deploy): install CAPLL from its chart on the LaunchLayer path

The capll item becomes a helm item installing CAPLL's published chart, after spire, in place of a copy of its release YAML. The manager's flags reach the chart as site values, and its values add the SPIFFE exec-auth plugin and workload API socket the CAPI controllers get, which CAPLL needs to read a tenant kubeconfig once nvkeOIDCAuth is on. capll-system joins the nvke-service SPIFFE identity, and the Secret get grant in nvke-system moves to nvke-base-config, bound to the chart's ServiceAccount.

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>

* chore(deploy): pin CAPLL's chart to 0.2.0 on the LaunchLayer path

0.2.0 labels the manager app.kubernetes.io/name=cluster-api-provider-launchlayer, as CAPLL's release YAML does, so nvkectl get providers finds a chart install.

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>

* chore(deploy): install NKTP from NGC on the LaunchLayer path

Copies deploy/nico's nktp item, which #2237 moved to NGC's public chart.

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>

* docs(deploy): make the LaunchLayer path's image requirements readable

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>

* docs(deploy): trim comments on the LaunchLayer path

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>

* docs(deploy): keep only the LaunchLayer-specific comments in its controller overlay

The rest repeated deploy/nico's overlay, which the header now points to. Values are unchanged; the rendered chart is byte-identical.

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>

---------

Signed-off-by: Meet Vadaria <mvadaria@nvidia.com>
2026-11-12 22:52:23 +05:30

361 lines
13 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package stackconfig
import (
"os"
"path/filepath"
"strings"
"testing"
)
// example is the operator's starting point, read the way the deploy suite
// reads it: by its path in the checkout.
var example = filepath.Join("..", "..", "..", "..", "deploy", "examples", "stack-config.json")
// filled returns the example with every placeholder replaced by a value.
func filled(t *testing.T) string {
t.Helper()
text, err := os.ReadFile(example)
if err != nil {
t.Fatal(err)
}
return strings.NewReplacer(
"REPLACE_NICO_ENDPOINT", "https://nico.example.com",
"REPLACE_NICO_ORG_ID", "org-1",
"REPLACE_NICO_API_NAME", "forge",
).Replace(string(text))
}
// write puts body at a stack-config.json of its own and returns the path.
func write(t *testing.T, body string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "stack-config.json")
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
return path
}
func TestLoad(t *testing.T) {
t.Run("filled example loads", func(t *testing.T) {
path := write(t, filled(t))
c, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if c.Nico.Endpoint != "https://nico.example.com" || c.Nico.OrgID != "org-1" || c.Nico.APIName != "forge" || c.Images != (Images{}) || c.Mirror != "" {
t.Errorf("loaded: %+v", c)
}
if c.Path() != path {
t.Errorf("Path: %s, want %s", c.Path(), path)
}
})
t.Run("the example lists every placeholder", func(t *testing.T) {
_, err := Load(example)
if err == nil {
t.Fatal("the example loaded with its placeholders")
}
want := []string{
"nico.endpoint: still a REPLACE_* placeholder; fill it in",
"nico.orgID: still a REPLACE_* placeholder; fill it in",
"nico.apiName: still a REPLACE_* placeholder; fill it in",
}
if err.Error() != strings.Join(want, "\n") {
t.Errorf("placeholders:\n got %q\nwant %q", err.Error(), strings.Join(want, "\n"))
}
})
t.Run("optional values are read", func(t *testing.T) {
body := strings.NewReplacer(
`"controller": ""`, `"controller": "registry.example.com/nvke/cwe:v1"`,
`"profiles": ""`, `"profiles": "registry.example.com/nvke/profiles@sha256:`+strings.Repeat("a", 64)+`"`,
`"mirror": ""`, `"mirror": "mirror.example.com:5000"`,
).Replace(filled(t))
c, err := Load(write(t, body))
if err != nil {
t.Fatal(err)
}
if c.Images.Controller != "registry.example.com/nvke/cwe:v1" || !strings.HasPrefix(c.Images.Profiles, "registry.example.com/nvke/profiles@sha256:") || c.Mirror != "mirror.example.com:5000" {
t.Errorf("optional values: %+v", c)
}
})
t.Run("a Config that was not loaded has no path", func(t *testing.T) {
var c Config
if c.Path() != "" {
t.Errorf("Path on a zero Config: %q", c.Path())
}
})
}
func TestLoad_Refusals(t *testing.T) {
long := strings.Repeat("a", 40)
cases := map[string]struct {
// edit rewrites the filled body; body replaces it outright.
edit func(body string) string
body string
// want is a line the error must carry; absent is text it must not.
want string
absent string
}{
"empty required value": {
edit: func(b string) string { return strings.Replace(b, `"orgID": "org-1"`, `"orgID": ""`, 1) },
want: "nico.orgID: must be set",
},
"unknown key at the top level": {
edit: func(b string) string { return strings.Replace(b, `"mirror": ""`, `"mirror": "", "arch": "arm64"`, 1) },
want: "arch: unknown field",
},
"unknown key in an object": {
edit: func(b string) string { return strings.Replace(b, `"endpoint":`, `"endpont":`, 1) },
want: "nico.endpont: unknown field",
},
"duplicate key": {
edit: func(b string) string {
return strings.Replace(b, `"orgID": "org-1",`, `"orgID": "org-1", "orgID": "org-2",`, 1)
},
want: "nico.orgID: duplicate field",
},
"key with the wrong case": {
edit: func(b string) string { return strings.Replace(b, `"nico":`, `"Nico":`, 1) },
want: "Nico: unknown field",
},
"non-object top level": {
body: `["nico"]`,
want: "the file does not hold a JSON object",
},
"empty file": {
body: " \n",
want: "the file is empty",
},
"trailing content": {
edit: func(b string) string { return b + "{}\n" },
want: "after top-level value",
},
"null nico": {
edit: func(b string) string {
return b[:strings.Index(b, `"nico"`)] + `"nico": null,` + b[strings.Index(b, `"images"`):]
},
want: "nico: must be an object, not null",
},
"null images": {
edit: func(b string) string {
return b[:strings.Index(b, `"images"`)] + `"images": null,` + b[strings.Index(b, `"mirror"`):]
},
want: "images: must be an object, not null",
},
"wrong type": {
edit: func(b string) string {
return b[:strings.Index(b, `"images"`)] + `"images": "x",` + b[strings.Index(b, `"mirror"`):]
},
want: "images",
},
"endpoint without a scheme": {
edit: func(b string) string { return strings.Replace(b, "https://nico.example.com", "nico.example.com", 1) },
want: `nico.endpoint: "nico.example.com" must be an http or https URL`,
},
"endpoint with a user and password": {
edit: func(b string) string {
return strings.Replace(b, "https://nico.example.com", "https://nico:"+long+"@nico.example.com", 1)
},
want: "nico.endpoint: must carry no user or password; the token comes from NVKECTL_NICO_TOKEN",
absent: long,
},
"a quote in a template value": {
edit: func(b string) string { return strings.Replace(b, `"org-1"`, `"org\"1"`, 1) },
want: "nico.orgID: the value holds a character the templates cannot carry",
},
"mirror with a scheme": {
edit: func(b string) string {
return strings.Replace(b, `"mirror": ""`, `"mirror": "https://mirror.example.com"`, 1)
},
want: `mirror: "https://mirror.example.com" is a host[:port][/prefix], not a URL`,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
body := tc.body
if tc.edit != nil {
body = tc.edit(filled(t))
}
_, err := Load(write(t, body))
if err == nil {
t.Fatalf("accepted:\n%s", body)
}
if !strings.Contains(err.Error(), tc.want) {
t.Errorf("want %q in:\n%s", tc.want, err)
}
if tc.absent != "" && strings.Contains(err.Error(), tc.absent) {
t.Errorf("the error carries the value:\n%s", err)
}
for _, line := range strings.Split(err.Error(), "\n") {
if strings.HasPrefix(line, "the file ") || strings.Contains(line, "after top-level value") || strings.HasPrefix(line, "cannot unmarshal ") {
continue
}
if _, _, ok := strings.Cut(line, ": "); !ok {
t.Errorf("line is not <json path>: <problem>: %q", line)
}
}
})
}
}
// Every problem is reported at once, so a file is fixed in one pass: the
// value problems, and a stray key beside them.
func TestLoad_ReportsEveryProblem(t *testing.T) {
body := filled(t)
body = strings.Replace(body, `"orgID": "org-1"`, `"orgID": ""`, 1)
body = strings.Replace(body, "https://nico.example.com", "nico.example.com", 1)
body = strings.Replace(body, `"mirror": ""`, `"mirror": "https://m"`, 1)
_, err := Load(write(t, body))
if err == nil {
t.Fatal("accepted")
}
if lines := strings.Split(err.Error(), "\n"); len(lines) != 3 {
t.Errorf("want three lines, got %d:\n%s", len(lines), err)
}
for _, want := range []string{"nico.orgID: must be set", "nico.endpoint:", "mirror:"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("want %q in:\n%s", want, err)
}
}
body = strings.Replace(body, `"mirror": "https://m"`, `"mirror": "", "pullSecret": "x"`, 1)
_, err = Load(write(t, body))
if err == nil {
t.Fatal("accepted")
}
if lines := strings.Split(err.Error(), "\n"); len(lines) != 3 || lines[0] != "pullSecret: unknown field" || !strings.Contains(err.Error(), "nico.orgID: must be set") {
t.Errorf("a stray key and the value problems in one pass:\n%s", err)
}
// A null object is the one problem reported for it: its fields are not
// listed as missing too.
body = filled(t)
body = body[:strings.Index(body, `"nico"`)] + `"nico": null,` + body[strings.Index(body, `"images"`):]
_, err = Load(write(t, body))
if err == nil || !strings.Contains(err.Error(), "nico: must be an object, not null") || strings.Contains(err.Error(), "nico.endpoint") {
t.Errorf("null object:\n%v", err)
}
}
func TestLoad_MissingFile(t *testing.T) {
if _, err := Load(filepath.Join(t.TempDir(), "absent.json")); err == nil || !strings.Contains(err.Error(), "absent.json") {
t.Errorf("missing file: %v", err)
}
}
func TestCredentialsFromEnv(t *testing.T) {
t.Setenv(EnvNicoToken, " token\n")
t.Setenv(EnvNodeUser, "nke")
t.Setenv(EnvNodePassword, "")
t.Setenv(EnvDatabasePassword, "db")
creds, err := (&Config{}).CredentialsFromEnv()
if err == nil || !strings.Contains(err.Error(), EnvNodePassword+" is not set") {
t.Errorf("unset password: %v", err)
}
if creds.NicoToken != "token" || creds.NodeUser != "nke" || creds.DatabasePassword != "db" {
t.Errorf("secrets not read and trimmed: %+v", creds)
}
t.Setenv(EnvNodePassword, "pw")
if _, err := (&Config{}).CredentialsFromEnv(); err != nil {
t.Errorf("all set: %v", err)
}
// The templated credentials refuse what the templates cannot carry; the
// database password, written through YAML, does not.
t.Setenv(EnvDatabasePassword, `d"b\`)
if _, err := (&Config{}).CredentialsFromEnv(); err != nil {
t.Errorf("database password with a quote: %v", err)
}
for _, name := range []string{EnvNicoToken, EnvNodeUser, EnvNodePassword} {
t.Run(name, func(t *testing.T) {
t.Setenv(name, `a"b`)
_, err := (&Config{}).CredentialsFromEnv()
if err == nil || !strings.Contains(err.Error(), name+": the value holds a character") || strings.Contains(err.Error(), `a"b`) {
t.Errorf("quote in %s: %v", name, err)
}
})
}
// Every unset variable is named at once.
for _, name := range []string{EnvNicoToken, EnvNodeUser, EnvNodePassword, EnvDatabasePassword} {
t.Setenv(name, "")
}
_, err = (&Config{}).CredentialsFromEnv()
if err == nil || strings.Count(err.Error(), " is not set") != 4 {
t.Errorf("four unset variables: %v", err)
}
}
// launchLayerExample is the LaunchLayer path's starting point.
var launchLayerExample = filepath.Join("..", "..", "..", "..", "deploy", "examples", "stack-config-launchlayer.json")
// filledLaunchLayer returns the LaunchLayer example with every placeholder replaced.
func filledLaunchLayer(t *testing.T) string {
t.Helper()
text, err := os.ReadFile(launchLayerExample)
if err != nil {
t.Fatal(err)
}
return strings.NewReplacer(
"REPLACE_LAUNCHLAYER_BASE_URL", "https://launchlayer.example.com",
"REPLACE_LAUNCHLAYER_ISSUER_URL", "https://issuer.example.com",
"REPLACE_LAUNCHLAYER_CLIENT_ID", "capll",
).Replace(string(text))
}
func TestLoad_LaunchLayer(t *testing.T) {
c, err := Load(write(t, filledLaunchLayer(t)))
if err != nil {
t.Fatalf("filled LaunchLayer example: %v", err)
}
if c.usesNico() || !c.usesLaunchLayer() || c.LaunchLayer.BaseURL != "https://launchlayer.example.com" || c.LaunchLayer.ClientID != "capll" {
t.Errorf("loaded: %+v", c)
}
if _, err := Load(launchLayerExample); err == nil || !strings.Contains(err.Error(), "launchLayer.baseURL: still a REPLACE_* placeholder") {
t.Errorf("unfilled example: %v", err)
}
cases := map[string]string{
strings.Replace(filledLaunchLayer(t), `"https://launchlayer.example.com"`, `""`, 1): "launchLayer.baseURL: must be set",
strings.Replace(filledLaunchLayer(t), `"computeURL": ""`, `"computeURL": "compute.example.com"`, 1): `launchLayer.computeURL: "compute.example.com" must be an http or https URL`,
strings.Replace(filledLaunchLayer(t), "https://issuer.example.com", "https://u:p@issuer.example.com", 1): "launchLayer.issuerURL: must carry no user or password; the client secret comes from " + EnvLaunchLayerClientSecret,
// Malformed userinfo fails the parse; the value, which may hold a password, is not printed.
strings.Replace(filledLaunchLayer(t), "https://issuer.example.com", "https://u:s3cret%ZZ@issuer.example.com", 1): "launchLayer.issuerURL: must be an http or https URL",
}
for body, want := range cases {
_, err := Load(write(t, body))
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("want %q, got %v", want, err)
}
if err != nil && strings.Contains(err.Error(), "s3cret") {
t.Errorf("the error prints the password: %v", err)
}
}
}
func TestConfig_CredentialsFromEnv(t *testing.T) {
t.Setenv(EnvNodeUser, "nke")
t.Setenv(EnvNodePassword, "pw")
t.Setenv(EnvDatabasePassword, "db")
t.Setenv(EnvNicoToken, "")
t.Setenv(EnvLaunchLayerClientSecret, "")
launchLayer, err := Load(write(t, filledLaunchLayer(t)))
if err != nil {
t.Fatal(err)
}
// A LaunchLayer config needs its client secret and not the NICo token.
if _, err := launchLayer.CredentialsFromEnv(); err == nil || err.Error() != EnvLaunchLayerClientSecret+" is not set" {
t.Errorf("LaunchLayer config, no secret: %v", err)
}
t.Setenv(EnvLaunchLayerClientSecret, " secret\n")
creds, err := launchLayer.CredentialsFromEnv()
if err != nil || creds.LaunchLayerClientSecret != "secret" || creds.NicoToken != "" {
t.Errorf("LaunchLayer config: %+v %v", creds, err)
}
// A NICo config is checked as before: the token, not the LaunchLayer secret.
nico, err := Load(write(t, filled(t)))
if err != nil {
t.Fatal(err)
}
if _, err := nico.CredentialsFromEnv(); err == nil || err.Error() != EnvNicoToken+" is not set" {
t.Errorf("NICo config, no token: %v", err)
}
}