Files
nvke/controller/internal/server/node_metadata_validation_test.go
Ganesh Reddy 4c9ed2ffb5 feat(e2e): run the kaps-bench smoke test on the perf stream (#2361)
* feat(e2e): run kaps-bench on the perf stream's tenant

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* feat(e2e): stage kaps-bench and helm on the perf stream's runner

Today a CI perf run stops at the suite's early check: the job leaves E2E_KAPS_BENCH_DIR empty and the runner has neither kaps-bench nor helm.

After this, a stream with kaps-bench run specs logs in to NVCR, sets up helm at the pinned version, copies kaps-bench and its folder out of the image pinned in make/tool-versions.mk, and checks them with list-run-specs. Each run also uploads kaps-bench-results.tar.gz with the other suite files.

Every new step runs only for a stream with kaps-bench run specs, so other streams run as before and their plans do not change. kaps-bench-results.tar.gz holds kaps-bench's output dir alone: its kubeconfig sits in the suite's temp dir outside it, and kaps-bench never sees the jump hosts or the token issuer that the log scrub withholds.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* fix(e2e): check the kaps-bench binary before a machine is taken

requireKapsBenchTools now refuses a kaps-bench folder with no runnable kaps-bench in it, and a folder path that is not absolute, so both fail before the site and tenant are built instead of at the first run spec.

Flag help and docs list the binary among the folder contents, say the results archive is absent when no run spec got as far as writing an output dir, and say helm is needed and checked for a stream with kaps-bench run specs.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* feat(e2e): let kaps-bench install what its run specs need

Perf stream policy now names the run spec smoke/smoke-prometheus.yaml in place of smoke/smoke.yaml. That run spec lists kube-prometheus-stack as something to install first, so kaps-bench installs it with the helm in its own folder and leaves it for the run specs that follow. nvke no longer installs Prometheus: the helm call and its embedded values file are removed, and the perf stream's runner no longer needs helm on PATH.

requireKapsBenchTools also refuses a folder without a runnable bin/helm, so such a folder fails before the site and tenant are built. kapsBenchSetup is the renamed once-per-tenant setup and writes the cluster context.

kaps-bench gets 5 minutes between SIGTERM and SIGKILL when a run spec reaches its limit. In that time it stops its engine, writes its final record and removes a chart it had half installed. 5 seconds was too short for that: a stop during a chart install took 11.2 seconds in a hand test. tenantToolRun takes the 5 minutes as a stop grace per tool, and AICR keeps 5 seconds and its 10 second wait. So that the grace fits inside the spec's limit, kaps-bench is stopped 39m50s into the first run spec and 24m50s into a later one. When a canceled command returns, every process group it listed gets SIGKILL straight away, so a child that ignored SIGTERM does not outlive the spec. AICR runs get the same.

Each run spec keeps its time limit and token lifetime, and the perf stream keeps its suite timeout. In qualify-e2e.yml, the helm steps run only for the GPU runtime again, as on main.

KAPS_BENCH_IMAGE stays at v0.3.1, which has no smoke-prometheus.yaml, so the perf stream stops at its early check until the pin moves to an image that has it.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* feat(e2e): give the perf stream's tenant a monitoring node

Today the perf stream's tenant is one control plane and three workers, and nothing keeps the benchmark's Prometheus off the workers it measures.

A stream can now ask for monitoring nodes with monitoringWorkers in release/policy.yaml, and the perf stream asks for one, so its tenant is one control plane, three workers and one monitoring node. Planning carries the value into the plan, the summary line and the matrix, and the run step passes it to the suite as -e2e.monitoring-workers. With it above 0, the suite asks the site controller for a second node pool, monitoring, whose nodes are labeled node-role.kubernetes.io/monitoring and tainted monitoring=true:NoSchedule, and labels the workers node-role.kubernetes.io/worker. Only Pods that tolerate the taint can land on the monitoring node. Pool labels and taints are applied by the controller while it provisions a node, before it lifts its own provisioning taint, so no node is schedulable without them.

Both label keys sit under kubernetes.io, which the controller refuses unless its nodeMetadata.allowlistedLabelKeys lists them. A site the suite builds allowlists exactly those two keys, and only when the stream asks for a monitoring node. Taint keys under reserved domains are always refused, so the taint key is simply monitoring. Forge and DSX pool specs and the NICo and CAPD request builders now carry a pool's labels and taints where they dropped them, and the capacity check counts the monitoring node with the workers, since it takes the worker shape.

A stream without the parameter, or with 0, runs as before: its plan, its run step's flags, the node pools in its create request and the site's controller values are all unchanged. Plans for every other scope are byte identical.

Not proven before a real run: the tenant's OVN-Kubernetes DaemonSet comes from a bundle outside this repository. Upstream OVN-Kubernetes tolerates every taint, and this bundle was not read. If it does not tolerate monitoring=true:NoSchedule, the monitoring node never becomes Ready and the tenant create times out. kaps-bench does not place Prometheus on the monitoring node yet, so until its values select the node, Prometheus still lands on a worker.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* chore(e2e): follow the latest kaps-bench image

Perf runs stage nvcr.io/j7sbcjl3qgta/kaps-bench:latest and pull it fresh each time, so every run gets the build kaps-bench main published last. A perf result can move with a kaps-bench change and no nvke change. Each run prints the image digest in its Stage kaps-bench step; compare digests before comparing two results.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* fix(e2e): redact the kaps-bench results archive

kaps-bench-results.tar.gz now goes through the same substitution as nvkectl-diagnostics.tar.gz, with each withheld value and its base64 form replaced in every file of the archive. In the site lane the list is the one the site's archives get: the values withheldFromEnvironment returns and every credential the deploy places on the node. In the lane that builds no site it is the values withheldFromEnvironment returns. Perf runs follow the newest kaps-bench image, so what kaps-bench writes into its output folder can change with no change in this repository, and a failed connection's error text names the address it tried; a file with nothing to replace keeps its bytes.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* fix(e2e): refuse monitoring nodes on a tenant that gets the GPU runtime

A run that installs the GPU runtime on its tenant and also asks for monitoring nodes now fails before a machine is taken: in the site lane's first step at stage full, and at the start of the tenant create in the lane that builds no site. GPU checks expect -e2e.tenant-workers GPU nodes, and the monitoring node takes the worker shape, so on a GPU run it would most likely count as one more. No stream runs that combination, so the suite refuses it up front. A run with the GPU runtime switched on and no GPU to install it on, as on the perf stream, still goes ahead and skips the runtime.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

* fix(e2e): check the kaps-bench folder before the nico-forge lane creates a tenant

Against a site the suite did not build, the nico-forge lane now checks the kaps-bench folder and its run specs at the start of the tenant create, as the site lane does in its first step at stage full. Until now that lane found a missing folder only once the tenant was up and its checks had run. A run without kaps-bench run specs passes the check as before. In the e2e README, nico-forge is now the only lane named as running on a site the suite did not build, since nico-dsx runs only on a site it builds.

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>

---------

Signed-off-by: Ganesh Reddy <gputta@nvidia.com>
2026-11-20 16:46:02 -06:00

333 lines
14 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package server
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"github.com/dsx-ai-factory/nvke/controller/api"
"github.com/dsx-ai-factory/nvke/controller/internal/clients/shared"
"github.com/dsx-ai-factory/nvke/controller/internal/db"
"github.com/dsx-ai-factory/nvke/controller/internal/keys"
)
func TestValidateNodeLabels(t *testing.T) {
t.Parallel()
tests := []struct {
name string
labels map[string]string
wantErr string
}{
{name: "empty"},
{name: "bare key", labels: map[string]string{"nodegroup": "workers"}},
{name: "empty value", labels: map[string]string{"nodegroup": ""}},
{name: "vendor key", labels: map[string]string{"nvidia.com/gpu.product": "B300"}},
{name: "lookalike nke domain", labels: map[string]string{"mynke.nvidia.com/rack": "a"}},
{name: "lookalike kubernetes domain", labels: map[string]string{"notkubernetes.io/hostname": "n1"}},
{
name: "kubernetes key without an allowlist entry",
labels: map[string]string{"node.kubernetes.io/instance-type": "h100"},
wantErr: `nodeLabels: key "node.kubernetes.io/instance-type" is under reserved domain "kubernetes.io"; labels in this domain cannot be set`,
},
{
name: "invalid key",
labels: map[string]string{"bad key": "x"},
wantErr: `nodeLabels: key "bad key" is invalid`,
},
{
name: "invalid value",
labels: map[string]string{"role": "not a value"},
wantErr: `nodeLabels: value "not a value" for key "role" is invalid`,
},
{
name: "nke domain",
labels: map[string]string{"nke.nvidia.com/managed": "true"},
wantErr: `nodeLabels: key "nke.nvidia.com/managed" is under reserved domain "nke.nvidia.com"; labels in this domain cannot be set`,
},
{
name: "nvke subdomain",
labels: map[string]string{"site.nvke.nvidia.com/rack": "a"},
wantErr: `nodeLabels: key "site.nvke.nvidia.com/rack" is under reserved domain "nvke.nvidia.com"; labels in this domain cannot be set`,
},
{
name: "control-plane role",
labels: map[string]string{"node-role.kubernetes.io/control-plane": ""},
wantErr: `nodeLabels: key "node-role.kubernetes.io/control-plane" is managed by core bootstrap components and cannot be set`,
},
{
name: "unlisted node role",
labels: map[string]string{"node-role.kubernetes.io/master": ""},
wantErr: `nodeLabels: key "node-role.kubernetes.io/master" is under reserved domain "kubernetes.io"; labels in this domain cannot be set`,
},
{
name: "exact kubernetes.io",
labels: map[string]string{"kubernetes.io/hostname": "n1"},
wantErr: `nodeLabels: key "kubernetes.io/hostname" is under reserved domain "kubernetes.io"; labels in this domain cannot be set`,
},
{
name: "exact k8s.io",
labels: map[string]string{"k8s.io/foo": "bar"},
wantErr: `nodeLabels: key "k8s.io/foo" is under reserved domain "k8s.io"; labels in this domain cannot be set`,
},
{
name: "kubernetes.io subdomain",
labels: map[string]string{"topology.kubernetes.io/zone": "az1"},
wantErr: `nodeLabels: key "topology.kubernetes.io/zone" is under reserved domain "kubernetes.io"; labels in this domain cannot be set`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := validateNodeLabels(tt.labels, nil)
if tt.wantErr == "" {
require.NoError(t, err)
return
}
require.ErrorContains(t, err, tt.wantErr)
})
}
}
func TestValidateNodeLabels_ReportsFirstInvalidKeyInSortedOrder(t *testing.T) {
t.Parallel()
labels := map[string]string{
"nvke.nvidia.com/managed": "true",
"k8s.io/foo": "bar",
"bad key": "x",
}
// Map iteration order varies between ranges, so repeat to catch an unsorted walk.
for range 10 {
require.ErrorContains(t, validateNodeLabels(labels, nil), `nodeLabels: key "bad key" is invalid`)
}
}
func TestValidateNodeLabels_ConfiguredAllowlist(t *testing.T) {
t.Parallel()
allowlistedLabelKeys := []string{"node.kubernetes.io/custom", "k8s.io/custom", "nke.nvidia.com/terminate", "nke.io/k8s-api-encryption-configured"}
require.NoError(t, validateNodeLabels(map[string]string{"node.kubernetes.io/custom": ""}, allowlistedLabelKeys))
require.NoError(t, validateNodeLabels(map[string]string{"k8s.io/custom": ""}, allowlistedLabelKeys))
require.ErrorContains(t,
validateNodeLabels(map[string]string{"node.kubernetes.io/instance-type": "h100"}, allowlistedLabelKeys),
`nodeLabels: key "node.kubernetes.io/instance-type" is under reserved domain "kubernetes.io"; labels in this domain cannot be set`,
)
require.ErrorContains(t,
validateNodeLabels(map[string]string{controlPlaneRoleKey: ""}, allowlistedLabelKeys),
`nodeLabels: key "node-role.kubernetes.io/control-plane" is managed by core bootstrap components and cannot be set`,
)
require.ErrorContains(t,
validateNodeLabels(map[string]string{"nke.nvidia.com/terminate": "true"}, allowlistedLabelKeys),
`nodeLabels: key "nke.nvidia.com/terminate" is under reserved domain "nke.nvidia.com"; labels in this domain cannot be set`,
)
require.ErrorContains(t,
validateNodeLabels(map[string]string{"nke.io/k8s-api-encryption-configured": "true"}, allowlistedLabelKeys),
`nodeLabels: key "nke.io/k8s-api-encryption-configured" is under reserved domain "nke.io"; labels in this domain cannot be set`,
)
}
func TestValidateNodeMetadata_MonitoringPool(t *testing.T) {
t.Parallel()
roleKeys := []string{"node-role.kubernetes.io/worker", "node-role.kubernetes.io/monitoring"}
for _, key := range roleKeys {
require.NoError(t, validateNodeLabels(map[string]string{key: ""}, roleKeys))
require.ErrorContains(t,
validateNodeLabels(map[string]string{key: ""}, nil),
`nodeLabels: key "`+key+`" is under reserved domain "kubernetes.io"; labels in this domain cannot be set`,
)
}
require.NoError(t, validateNodeTaints([]api.NodeTaint{{Key: "monitoring", Value: "true", Effect: api.NodeTaintEffectNoSchedule}}, false))
}
func TestValidateNodeTaints(t *testing.T) {
t.Parallel()
valid := []api.NodeTaint{{
Key: "nvidia.com/dedicated",
Value: "team-b",
Effect: api.NodeTaintEffectNoSchedule,
}}
tests := []struct {
name string
taints []api.NodeTaint
controlPlane bool
wantErr string
}{
{name: "empty"},
{name: "control plane with no taints", controlPlane: true},
{name: "vendor taint", taints: valid},
{name: "empty value", taints: []api.NodeTaint{{Key: "dedicated", Effect: api.NodeTaintEffectNoExecute}}},
{
name: "same key different effects",
taints: []api.NodeTaint{
{Key: "dedicated", Effect: api.NodeTaintEffectNoSchedule},
{Key: "dedicated", Effect: api.NodeTaintEffectNoExecute},
},
},
{
name: "invalid key",
taints: []api.NodeTaint{{
Key: "bad key",
Effect: api.NodeTaintEffectNoSchedule,
}},
wantErr: `nodeTaints: key "bad key" is invalid`,
},
{
name: "control-plane role",
taints: []api.NodeTaint{{
Key: "node-role.kubernetes.io/control-plane",
Effect: api.NodeTaintEffectNoSchedule,
}},
wantErr: `nodeTaints: key "node-role.kubernetes.io/control-plane" is managed by core bootstrap components and cannot be set`,
},
{
name: "kubernetes.io subdomain",
taints: []api.NodeTaint{{
Key: "node.kubernetes.io/instance-type",
Value: "h100",
Effect: api.NodeTaintEffectNoSchedule,
}},
wantErr: `nodeTaints: key "node.kubernetes.io/instance-type" is under reserved domain "kubernetes.io"; taints in this domain cannot be set`,
},
{
name: "nke domain",
taints: []api.NodeTaint{{
Key: "nke.nvidia.com/managed",
Effect: api.NodeTaintEffectNoSchedule,
}},
wantErr: `nodeTaints: key "nke.nvidia.com/managed" is under reserved domain "nke.nvidia.com"; taints in this domain cannot be set`,
},
{
name: "k8s.io",
taints: []api.NodeTaint{{
Key: "k8s.io/foo",
Effect: api.NodeTaintEffectNoSchedule,
}},
wantErr: `nodeTaints: key "k8s.io/foo" is under reserved domain "k8s.io"; taints in this domain cannot be set`,
},
{
name: "control-plane pool",
taints: valid,
controlPlane: true,
wantErr: "nodeTaints: not allowed on a control-plane pool",
},
{
name: "duplicate key and effect",
taints: []api.NodeTaint{
{Key: "dedicated", Effect: api.NodeTaintEffectNoSchedule},
{Key: "dedicated", Value: "other", Effect: api.NodeTaintEffectNoSchedule},
},
wantErr: `nodeTaints: duplicate taint key "dedicated" with effect "NoSchedule"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := validateNodeTaints(tt.taints, tt.controlPlane)
if tt.wantErr == "" {
require.NoError(t, err)
return
}
require.ErrorContains(t, err, tt.wantErr)
})
}
}
func TestValidateMachineMetadata_Inputs(t *testing.T) {
t.Parallel()
const sizeTestKey = "example.com/large"
maxValueBytes := apivalidation.TotalAnnotationSizeLimitB - len(sizeTestKey)
for key, value := range shared.MachineAnnotations(db.Node{
NodeAgentExternalMTLSEnabled: true,
NodeAgentTLSJWTEnabled: true,
SpireAttestationMethod: db.SpireAttestationMethodJoinToken,
}) {
maxValueBytes -= len(key) + len(value)
}
type parameters struct {
annotations map[string]string
labels map[string]string
wantErr string
allowedAnnotations []string
allowedLabels []string
}
tests := map[string]parameters{
"omitted": {},
"free-form annotations": {annotations: map[string]string{"nke.nvidia.com/profile-selection": "[\"gpu\", \"network\"]\n"}, allowedAnnotations: []string{"nke.nvidia.com/profile-selection"}},
"custom labels": {labels: map[string]string{"example.com/pool": "gpu", "empty": ""}},
"invalid annotation key": {annotations: map[string]string{"bad key": "value"}, wantErr: "machineAnnotations"},
"annotation value too large": {annotations: map[string]string{"example.com/large": strings.Repeat("a", 256*1024)}, wantErr: "Too long"},
"annotations below total limit": {annotations: map[string]string{sizeTestKey: strings.Repeat("a", maxValueBytes-1)}},
"annotations at total limit": {annotations: map[string]string{sizeTestKey: strings.Repeat("a", maxValueBytes)}},
"annotations exceed total limit": {annotations: map[string]string{sizeTestKey: strings.Repeat("a", maxValueBytes+1)}, wantErr: "Too long"},
"caller annotations at Kubernetes limit": {annotations: map[string]string{sizeTestKey: strings.Repeat("a", apivalidation.TotalAnnotationSizeLimitB-len(sizeTestKey))}, wantErr: "Too long"},
"invalid label key": {labels: map[string]string{"bad key": "value"}, wantErr: "machineLabels"},
"invalid label value": {labels: map[string]string{"pool": "not a label"}, wantErr: "machineLabels"},
"cluster identity label": {labels: map[string]string{clusterv1.ClusterNameLabel: "other"}, wantErr: "must be explicitly allowlisted"},
"control-plane role label": {labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""}, wantErr: "must be explicitly allowlisted"},
"annotation exception": {annotations: map[string]string{"nke.nvidia.com/profile": "gpu,network"}, allowedAnnotations: []string{"nke.nvidia.com/profile"}},
"label exception": {labels: map[string]string{"cluster.x-k8s.io/custom": "gpu"}, allowedLabels: []string{"cluster.x-k8s.io/custom"}},
"annotation exception cannot permit label": {labels: map[string]string{"nvke.nvidia.com/profile": "gpu"}, allowedAnnotations: []string{"nvke.nvidia.com/profile"}, wantErr: "must be explicitly allowlisted"},
"label exception cannot permit annotation": {annotations: map[string]string{"nvke.nvidia.com/profile": "gpu"}, allowedLabels: []string{"nvke.nvidia.com/profile"}, wantErr: "must be explicitly allowlisted"},
"exception must match exact key": {annotations: map[string]string{"nvke.nvidia.com/other": "gpu"}, allowedAnnotations: []string{"nvke.nvidia.com/profile"}, wantErr: "must be explicitly allowlisted"},
"lookalike domains": {annotations: map[string]string{"mynke.io/key": "value"}, labels: map[string]string{"notk8s.io/key": "value"}},
}
for _, domain := range []string{"nke.nvidia.com", "nke.io", "nvke.nvidia.com", "kubernetes.io", "k8s.io", "cluster.x-k8s.io"} {
for _, prefix := range []string{domain, "child." + domain} {
key := prefix + "/future-key"
tests["reserved annotation "+key] = parameters{annotations: map[string]string{key: "value"}, wantErr: "must be explicitly allowlisted"}
tests["reserved label "+key] = parameters{labels: map[string]string{key: "value"}, wantErr: "must be explicitly allowlisted"}
}
}
// Both domains of each owned annotation: the controller still reads the
// legacy form, so a caller setting that one would be just as damaging as
// setting the current one.
for _, key := range []keys.Key{
keys.MachineHostID, keys.MachineReboot, keys.MachineRebootCompleteTimestamp,
keys.MachineHealthIssue, keys.MachineSpireAgentSpiffeIDPath,
} {
for _, name := range key.All() {
tests["reserved annotation "+name] = parameters{annotations: map[string]string{name: "value"}, wantErr: "must be explicitly allowlisted"}
}
}
for _, key := range []string{
clusterv1.ExcludeNodeDrainingAnnotation,
clusterv1.ExcludeWaitForNodeVolumeDetachAnnotation,
} {
tests["reserved annotation "+key] = parameters{annotations: map[string]string{key: "value"}, wantErr: "must be explicitly allowlisted"}
}
for name, params := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
err := validateMachineMetadata(params.annotations, params.labels, params.allowedAnnotations, params.allowedLabels)
if params.wantErr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, params.wantErr)
}
})
}
}
func TestMachineMetadata_ConnectionContractReserved(t *testing.T) {
for _, owned := range []keys.Key{keys.MachineAgentConnection, keys.MachineSpireAttestationMethod, keys.MachineSpiffeEnabled} {
for _, key := range owned.All() {
err := validateMachineMetadata(map[string]string{key: "forged"}, nil, []string{key}, nil)
require.ErrorContains(t, err, "controller-owned")
}
}
}