From 76287e04202f6d590ad731368947cb0ebdaff9aa Mon Sep 17 00:00:00 2001 From: TheiLLeniumStudios <104288623+TheiLLeniumStudios@users.noreply.github.com> Date: Thu, 8 Jan 2026 23:36:02 +0100 Subject: [PATCH] refactor: Cleanup logic for reloader in loadtests --- test/loadtest/internal/cmd/run.go | 25 ++++----------------- test/loadtest/internal/reloader/reloader.go | 21 +++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/test/loadtest/internal/cmd/run.go b/test/loadtest/internal/cmd/run.go index 8c88b54..4a0f731 100644 --- a/test/loadtest/internal/cmd/run.go +++ b/test/loadtest/internal/cmd/run.go @@ -208,8 +208,8 @@ func runSequential(ctx context.Context, cfg RunConfig, scenariosToRun []string, log.Printf("========================================") cleanupTestNamespaces(ctx, "") - cleanupReloader(ctx, "old", "") - cleanupReloader(ctx, "new", "") + reloader.CleanupByVersion(ctx, "old", "") + reloader.CleanupByVersion(ctx, "new", "") if err := promMgr.Reset(ctx); err != nil { log.Printf("Warning: failed to reset Prometheus: %v", err) @@ -357,8 +357,8 @@ func runParallel(ctx context.Context, cfg RunConfig, scenariosToRun []string, ru log.Printf("[Worker %d] Starting scenario %s", w.id, scenarioID) cleanupTestNamespaces(ctx, w.kubeContext) - cleanupReloader(ctx, "old", w.kubeContext) - cleanupReloader(ctx, "new", w.kubeContext) + reloader.CleanupByVersion(ctx, "old", w.kubeContext) + reloader.CleanupByVersion(ctx, "new", w.kubeContext) if err := w.promMgr.Reset(ctx); err != nil { log.Printf("[Worker %d] Warning: failed to reset Prometheus: %v", w.id, err) @@ -629,20 +629,3 @@ func cleanupTestNamespaces(ctx context.Context, kubeContext string) { } } -func cleanupReloader(ctx context.Context, version string, kubeContext string) { - ns := fmt.Sprintf("reloader-%s", version) - - nsArgs := []string{"delete", "namespace", ns, "--wait=false", "--ignore-not-found"} - crArgs := []string{"delete", "clusterrole", fmt.Sprintf("reloader-%s", version), "--ignore-not-found"} - crbArgs := []string{"delete", "clusterrolebinding", fmt.Sprintf("reloader-%s", version), "--ignore-not-found"} - - if kubeContext != "" { - nsArgs = append([]string{"--context", kubeContext}, nsArgs...) - crArgs = append([]string{"--context", kubeContext}, crArgs...) - crbArgs = append([]string{"--context", kubeContext}, crbArgs...) - } - - exec.CommandContext(ctx, "kubectl", nsArgs...).Run() - exec.CommandContext(ctx, "kubectl", crArgs...).Run() - exec.CommandContext(ctx, "kubectl", crbArgs...).Run() -} diff --git a/test/loadtest/internal/reloader/reloader.go b/test/loadtest/internal/reloader/reloader.go index ff3cfdb..2667cd4 100644 --- a/test/loadtest/internal/reloader/reloader.go +++ b/test/loadtest/internal/reloader/reloader.go @@ -216,6 +216,27 @@ func (m *Manager) Cleanup(ctx context.Context) error { return nil } +// CleanupByVersion removes Reloader resources for a specific version without needing a Manager instance. +// This is useful for cleaning up from previous runs before creating a new Manager. +func CleanupByVersion(ctx context.Context, version, kubeContext string) { + ns := fmt.Sprintf("reloader-%s", version) + name := fmt.Sprintf("reloader-%s", version) + + nsArgs := []string{"delete", "namespace", ns, "--wait=false", "--ignore-not-found"} + crArgs := []string{"delete", "clusterrole", name, "--ignore-not-found"} + crbArgs := []string{"delete", "clusterrolebinding", name, "--ignore-not-found"} + + if kubeContext != "" { + nsArgs = append([]string{"--context", kubeContext}, nsArgs...) + crArgs = append([]string{"--context", kubeContext}, crArgs...) + crbArgs = append([]string{"--context", kubeContext}, crbArgs...) + } + + exec.CommandContext(ctx, "kubectl", nsArgs...).Run() + exec.CommandContext(ctx, "kubectl", crArgs...).Run() + exec.CommandContext(ctx, "kubectl", crbArgs...).Run() +} + // CollectLogs collects logs from the Reloader pod and writes them to the specified file. func (m *Manager) CollectLogs(ctx context.Context, logPath string) error { ns := m.namespace()