mirror of
https://github.com/cozystack/cozystack.git
synced 2026-10-28 23:57:08 +09:00
A cluster whose cozystack-controller predates the GPU binder cannot pick up GPUDevice by starting a second, newer instance: every reconciler is registered unconditionally, so the second instance runs its own copy of the workload, tenant, gateway and secret reconcilers next to the first. --gpu-only (with --enable-gpu) registers only the GPU binder, its webhooks and the probes; nothing else is set up, so no other informer or cache exists in that process. It is not meant to run beside a controller from this tree, which runs a binder of its own. --leader-election-id names the Lease instead of the fixed ID, whose default stays the historical one. A GPU-only instance that elects must name its own Lease: on the default one it would take leadership from the main controller in a shared namespace and stop all of its reconcilers while holding it. The logger is now set right after flag parsing, so this and the other flag refusals reach the pod log instead of exiting silently. Known limit of the pairing: the older controller's WorkloadMonitor still counts the GPUs KubeVirt requests on a virt-launcher pod, while the binder writes a usage record per device, so a VM covered by an application's WorkloadMonitor is counted twice until the main controller is upgraded. The manager wiring is now a list of named steps, so a test can assert which steps each mode runs without starting a manager. The image builds the command as a package, since it no longer fits in main.go. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <f@lex.la>
113 lines
4.1 KiB
Go
113 lines
4.1 KiB
Go
/*
|
|
Copyright 2026 The Cozystack Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cozystack/cozystack/internal/gpu"
|
|
)
|
|
|
|
func stepNames(steps []step) []string {
|
|
out := make([]string, 0, len(steps))
|
|
for _, s := range steps {
|
|
out = append(out, s.name)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// The steps are built with a nil manager and never run: building the list
|
|
// must not touch the manager, or this test would not be able to list them.
|
|
func TestGPUOnlySelectsOnlyTheGPUSteps(t *testing.T) {
|
|
steps := managerSteps(nil, setupConfig{gpu: gpu.Config{Enabled: true}})
|
|
got := stepNames(selectSteps(steps, true))
|
|
want := []string{"gpu-binder", "gpu-webhooks", "healthz", "readyz"}
|
|
if !slices.Equal(got, want) {
|
|
t.Fatalf("--gpu-only runs %v, want exactly %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestDefaultRunsEveryStep(t *testing.T) {
|
|
steps := managerSteps(nil, setupConfig{gpu: gpu.Config{Enabled: true}})
|
|
got := stepNames(selectSteps(steps, false))
|
|
want := []string{
|
|
"WorkloadMonitor", "WorkloadReconciler", "ApplicationDefinitionReconciler",
|
|
"ApplicationDefinitionHelmReconciler", "TenantGateway", "TenantQuota", "TenantLogRouting",
|
|
"WildcardSecret", "CACert", "gpu-binder", "gpu-webhooks", "healthz", "readyz", "telemetry",
|
|
}
|
|
if !slices.Equal(got, want) {
|
|
t.Fatalf("default mode runs %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestValidateGPUOnly(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
gpuOnly, enabled, leaderElect bool
|
|
leaseID string
|
|
wantErr bool
|
|
}{
|
|
{name: "gpu-only without enable-gpu", gpuOnly: true, leaseID: defaultLeaderElectionID, wantErr: true},
|
|
{name: "gpu-only electing on the main controller's Lease", gpuOnly: true, enabled: true, leaderElect: true,
|
|
leaseID: defaultLeaderElectionID, wantErr: true},
|
|
{name: "gpu-only electing on its own Lease", gpuOnly: true, enabled: true, leaderElect: true, leaseID: "gpu-only.example"},
|
|
{name: "gpu-only without election", gpuOnly: true, enabled: true, leaseID: defaultLeaderElectionID},
|
|
{name: "default mode on the default Lease", leaderElect: true, leaseID: defaultLeaderElectionID},
|
|
{name: "default mode with GPU", enabled: true, leaderElect: true, leaseID: defaultLeaderElectionID},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := validateGPUOnly(tc.gpuOnly, tc.enabled, tc.leaderElect, tc.leaseID)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Errorf("got %v, want error %v", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A flag refusal exits before the manager starts, so it has to be logged by
|
|
// then: before the logger is set the message goes nowhere and the pod
|
|
// crash-loops without saying why.
|
|
func TestFlagRefusalIsLogged(t *testing.T) {
|
|
if os.Getenv("TEST_GPU_ONLY_REFUSAL_HELPER") == "1" {
|
|
os.Args = []string{"cozystack-controller", "--gpu-only"}
|
|
main()
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestFlagRefusalIsLogged$")
|
|
// Should the refusal ever stop exiting, main() would go on to start a
|
|
// manager: give it no cluster to reach, neither a kubeconfig nor the
|
|
// in-cluster service account.
|
|
cmd.Env = append(os.Environ(), "TEST_GPU_ONLY_REFUSAL_HELPER=1", "KUBECONFIG=/nonexistent", "KUBERNETES_SERVICE_HOST=")
|
|
out, err := cmd.CombinedOutput()
|
|
var exit *exec.ExitError
|
|
if !errors.As(err, &exit) || exit.ExitCode() != 1 {
|
|
t.Fatalf("want exit code 1, got %v; output:\n%s", err, out)
|
|
}
|
|
if !strings.Contains(string(out), "--gpu-only needs --enable-gpu") {
|
|
t.Fatalf("the refusal is not in the output:\n%s", out)
|
|
}
|
|
}
|