diff --git a/CHANGELOG.md b/CHANGELOG.md index 32f4fd1..5ec2676 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- `nr cluster liqo upgrade` upgrades an existing Liqo release to the pinned Harbor chart. It applies the chart CRDs, then runs Helm with `--reset-then-reuse-values` so the cluster id and provider values stay. It does not ask for a hypervisor token. Run it on the agent together with `nr cluster operator upgrade`. + ## [1.0.20] - 2026-11-29 ### Changed diff --git a/README.md b/README.md index 50c2137..1f52e84 100644 --- a/README.md +++ b/README.md @@ -72,12 +72,15 @@ If registration is interrupted: nr cluster register --resume --name edge-ams-01 --org-id ``` -Upgrade the operator later without re-entering hypervisor tokens: +Upgrade an existing agent without re-entering hypervisor tokens: ```bash +nr cluster liqo upgrade nr cluster operator upgrade ``` +`nr cluster liqo upgrade` moves Liqo to the pinned chart and image and keeps the cluster id. The gateway restarts, so the peer can blip. `nr cluster operator upgrade` moves the hypervisor operator chart and image. + `nr cluster register --resume --reinstall-operator` also reuses existing Secrets unless you pass credentials (that path is for rotating tokens). ## Common commands @@ -87,6 +90,7 @@ nr auth status nr cluster status --name edge-ams-01 nr cluster verify --name edge-ams-01 nr cluster health --name edge-ams-01 +nr cluster liqo upgrade nr cluster operator upgrade nr cluster deregister --name edge-ams-01 ``` diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 865ce5a..90cf4aa 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -107,4 +107,4 @@ This is not a `--hypervisor-driver` value. Register the hypervisor agent first, Registration checkpoints live under `~/.nr/`. Do not copy these directories between machines. -Never commit tokens, kubeconfigs, or instances files to version control. If a hypervisor token leaked, rotate it and re-run `nr cluster register --resume --reinstall-operator` with the new credentials (env or instances file). Chart-only upgrades use `nr cluster operator upgrade` and do not rewrite Secrets. +Never commit tokens, kubeconfigs, or instances files to version control. If a hypervisor token leaked, rotate it and re-run `nr cluster register --resume --reinstall-operator` with the new credentials (env or instances file). Chart-only upgrades use `nr cluster liqo upgrade` and `nr cluster operator upgrade`. Neither rewrites Secrets. diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index 6df909d..0cfad9d 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -47,6 +47,15 @@ nr cluster register --resume --name --org-id Checkpoints live under `~/.nr/`. Use `nr cluster status` and `nr cluster debug` for details. +**Upgrade Liqo and the operator (no hypervisor tokens)** + +```bash +nr cluster liqo upgrade +nr cluster operator upgrade +``` + +`nr cluster liqo upgrade` reuses the Liqo cluster id and Helm values, applies the chart CRDs, and sets the image tag. It does not need `NR_API_TOKEN` or `--org-id`. The gateway restarts and the peer can show Error until it returns to Connected. + **Upgrade operator charts (no hypervisor tokens)** ```bash diff --git a/internal/cli/cluster.go b/internal/cli/cluster.go index 3ce9bf9..241712c 100644 --- a/internal/cli/cluster.go +++ b/internal/cli/cluster.go @@ -919,7 +919,7 @@ func handleResume(cmd *cobra.Command, ctx context.Context, stateManager *state.M if err := runPostRegisterVerify(ctx, apiClient, log, installState.AgentID, registerOpts); err != nil { return err } - log.Info("Tip: upgrade charts with 'nr cluster operator upgrade', or --resume --reinstall-operator") + log.Info("Tip: upgrade with 'nr cluster liqo upgrade' and 'nr cluster operator upgrade', or --resume --reinstall-operator") return nil } diff --git a/internal/cli/cluster_liqo_upgrade.go b/internal/cli/cluster_liqo_upgrade.go new file mode 100644 index 0000000..05e6bbd --- /dev/null +++ b/internal/cli/cluster_liqo_upgrade.go @@ -0,0 +1,99 @@ +package cli + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" + + "github.com/noderings/cli/internal/config" + "github.com/noderings/cli/internal/install" + "github.com/noderings/cli/internal/logger" +) + +const ( + flagLiqoChart = "liqo-chart" + flagLiqoChartVersion = "liqo-chart-version" + flagLiqoTimeout = "timeout" +) + +var clusterLiqoCmd = &cobra.Command{ + Use: "liqo", + Short: "Manage the Liqo Helm release on this agent", +} + +var clusterLiqoUpgradeCmd = &cobra.Command{ + Use: "upgrade", + Short: "Upgrade the Liqo chart and images without re-peering", + Long: `Upgrade the existing Liqo Helm release on this agent to the CLI-pinned build. + +Reuses the cluster id, network CIDRs, and custom-resource list already stored +on the release. Applies the chart CRDs first, then runs helm upgrade with +--reset-then-reuse-values so new chart keys render. Does not uninstall Liqo, +change the cluster id, or ask for a hypervisor token. + +Does not require NR_API_TOKEN or --org-id. Run on the agent VM. + +The WireGuard gateway restarts. The peer can show Error for a short time and +then return to Connected. ` + fmt.Sprintf("This CLI pins chart %s and image %s.", config.DefaultLiqoChartVersion, config.DefaultLiqoVersion), + Args: cobra.NoArgs, + RunE: runClusterLiqoUpgrade, +} + +func init() { + clusterCmd.AddCommand(clusterLiqoCmd) + clusterLiqoCmd.AddCommand(clusterLiqoUpgradeCmd) + clusterLiqoUpgradeCmd.Flags().String(flagLiqoChart, "", "Local path or OCI ref for the Liqo chart (default: Harbor OCI pin)") + clusterLiqoUpgradeCmd.Flags().String(flagLiqoChartVersion, "", "Helm chart version (image tag is the same value with a leading v)") + clusterLiqoUpgradeCmd.Flags().String(flagLiqoTimeout, "", "Helm --wait timeout (default 10m)") + clusterLiqoUpgradeCmd.Flags().Bool(flagDryRun, false, "Print the Helm upgrade and exit") +} + +func runClusterLiqoUpgrade(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + if ctx == nil { + ctx = context.Background() + } + + cfgLoader := config.NewLoader() + cfg, err := cfgLoader.Load() + if err != nil { + return fmt.Errorf("load config: %w", err) + } + log, err := logger.NewLogger(cfg.Logging.Level, cfg.Logging.File) + if err != nil { + return err + } + + chart, err := cmd.Flags().GetString(flagLiqoChart) + if err != nil { + return err + } + chartVersion, err := cmd.Flags().GetString(flagLiqoChartVersion) + if err != nil { + return err + } + timeout, err := cmd.Flags().GetString(flagLiqoTimeout) + if err != nil { + return err + } + dryRun, err := cmd.Flags().GetBool(flagDryRun) + if err != nil { + return err + } + + if _, err := install.EnsureCLIBinDir(); err != nil { + log.Warnf("Could not add ~/.nr/bin to PATH: %v", err) + } + kubeconfig := install.EnsureReadableKubeconfig(ctx, "", log) + u, err := install.NewLiqoUpgrade(chart, chartVersion, kubeconfig, timeout) + if err != nil { + return err + } + if dryRun { + _, err := fmt.Fprint(cmd.OutOrStdout(), install.LiqoUpgradeDryRun(u)) + return err + } + log.Infof("Liqo upgrade target chart %s tag %s", u.ChartVersion, u.ImageTag) + return install.UpgradeLiqoRelease(ctx, u, log) +} diff --git a/internal/install/liqo_upgrade.go b/internal/install/liqo_upgrade.go new file mode 100644 index 0000000..c1395fb --- /dev/null +++ b/internal/install/liqo_upgrade.go @@ -0,0 +1,221 @@ +package install + +import ( + "context" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/noderings/cli/internal/config" +) + +const ( + liqoHelmRelease = "liqo" + liqoHelmValueTag = "tag" + liqoCRDRelDir = "charts/liqo-crds/crds" + liqoPulledChartPlaceholder = "" + defaultLiqoHelmTimeout = "10m" + helmFlagWait = "--wait" + helmFlagTimeout = "--timeout" +) + +// LiqoUpgrade is an in-place Helm upgrade of an existing provider Liqo release. +// It reuses the cluster id, CIDRs, and custom-resource list already stored on the +// release. It does not install Liqo, rotate peering, or ask for a hypervisor token. +type LiqoUpgrade struct { + Chart string + ChartVersion string + ImageTag string + Release string + Namespace string + Kubeconfig string + Timeout string +} + +// NewLiqoUpgrade builds an upgrade from the CLI pin. An empty chart uses the Harbor +// OCI chart. chartVersion is the Helm chart version (leading v is stripped). The +// image tag always keeps the v prefix. +func NewLiqoUpgrade(chart, chartVersion, kubeconfig, timeout string) (LiqoUpgrade, error) { + chart = strings.TrimSpace(chart) + if chart == "" { + chart = config.DefaultLiqoChartOCI + } + if strings.TrimSpace(timeout) == "" { + timeout = defaultLiqoHelmTimeout + } + u := LiqoUpgrade{ + Chart: chart, + ChartVersion: liqoChartVersion(chartVersion), + ImageTag: liqoImageTag(chartVersion), + Release: liqoHelmRelease, + Namespace: config.DefaultLiqoNamespace, + Kubeconfig: strings.TrimSpace(kubeconfig), + Timeout: strings.TrimSpace(timeout), + } + return u, u.Validate() +} + +func (u LiqoUpgrade) Validate() error { + if strings.TrimSpace(u.Chart) == "" { + return fmt.Errorf("liqo chart is required") + } + if strings.TrimSpace(u.ImageTag) == "" { + return fmt.Errorf("liqo image tag is required") + } + if strings.TrimSpace(u.Release) == "" || strings.TrimSpace(u.Namespace) == "" { + return fmt.Errorf("liqo release and namespace are required") + } + if strings.TrimSpace(u.Timeout) == "" { + return fmt.Errorf("liqo upgrade timeout is required") + } + if isOCIRef(u.Chart) && strings.TrimSpace(u.ChartVersion) == "" { + return fmt.Errorf("liqo chart version is required for an OCI chart") + } + // Helm splits one --set-string value on commas, so a comma in the tag can + // inject another key=value pair. Reject the separators before argv is built. + if err := rejectHelmSetSeparators("liqo chart version", u.ChartVersion); err != nil { + return err + } + if err := rejectHelmSetSeparators("liqo image tag", u.ImageTag); err != nil { + return err + } + return nil +} + +func rejectHelmSetSeparators(field, value string) error { + if strings.ContainsAny(value, ",=") { + return fmt.Errorf("%s %q must not contain ',' or '='", field, value) + } + return nil +} + +// LiqoUpgradeArgs is the helm argv for the in-place upgrade. +// --reset-then-reuse-values keeps the cluster id and provider values while +// picking up chart keys added since the previous release. --reuse-values alone +// leaves those keys nil and the new chart fails to render. +// This is helm upgrade without --install: a missing release must fail closed. +func LiqoUpgradeArgs(u LiqoUpgrade) []string { + args := []string{ + helmCmdUpgrade, u.Release, u.Chart, + helmFlagNamespace, u.Namespace, + helmFlagResetThenReuseValues, + helmFlagSetString, liqoHelmValueTag + "=" + u.ImageTag, + helmFlagWait, + helmFlagTimeout, u.Timeout, + } + args = appendOCIChartVersion(args, u.Chart, u.ChartVersion) + return withKubeconfig(u.Kubeconfig, args) +} + +// LiqoCRDDir is the chart directory of Liqo CRD manifests. Helm does not upgrade +// CRDs from a subchart on upgrade, and the new chart writes statusSync. +func LiqoCRDDir(chartPath string) (string, error) { + dir := filepath.Join(chartPath, liqoCRDRelDir) + st, err := os.Stat(dir) + if err != nil { + return "", fmt.Errorf("liqo chart %s CRD directory %s: %w", chartPath, dir, err) + } + if !st.IsDir() { + return "", fmt.Errorf("liqo chart %s CRD path %s is not a directory", chartPath, dir) + } + return dir, nil +} + +// LiqoUpgradeDryRun is the command sequence UpgradeLiqoRelease runs. +// An OCI chart is pulled first. Helm then upgrades that extracted chart, so the +// upgrade argv has no --version. +func LiqoUpgradeDryRun(u LiqoUpgrade) string { + chartPath := u.Chart + var b strings.Builder + if isOCIRef(u.Chart) { + fmt.Fprintf(&b, "helm pull %s --version %s\n", u.Chart, u.ChartVersion) + chartPath = liqoPulledChartPlaceholder + } + fmt.Fprintf(&b, "kubectl apply --server-side --force-conflicts -f %s/%s\n", chartPath, liqoCRDRelDir) + fmt.Fprintf(&b, "helm %s\n", strings.Join(liqoExecutedUpgradeArgs(u, chartPath), " ")) + return b.String() +} + +func liqoExecutedUpgradeArgs(u LiqoUpgrade, chartPath string) []string { + run := u + run.Chart = chartPath + return LiqoUpgradeArgs(run) +} + +// UpgradeLiqoRelease applies the chart CRDs, then upgrades the existing release. +func UpgradeLiqoRelease(ctx context.Context, u LiqoUpgrade, logger Logger) error { + if logger == nil { + logger = noopLogger{} + } + if err := u.Validate(); err != nil { + return err + } + ok, err := helmReleaseExists(ctx, u.Kubeconfig, u.Release, u.Namespace) + if err != nil { + return err + } + if !ok { + return fmt.Errorf("no Liqo Helm release %s in namespace %s. Install with nr cluster register", u.Release, u.Namespace) + } + + chartPath, cleanup, err := resolveLiqoChart(ctx, u, logger) + if err != nil { + return err + } + if cleanup != nil { + defer cleanup() + } + crdDir, err := LiqoCRDDir(chartPath) + if err != nil { + return err + } + logger.Infof("Applying Liqo CRDs from %s", crdDir) + if err := runCmd(ctx, binKubectl, withKubeconfig(u.Kubeconfig, []string{ + kubectlCmdApply, kubectlFlagServerSide, kubectlFlagForceConflicts, + kubectlFlagFilename, crdDir, + })...); err != nil { + return fmt.Errorf("apply liqo CRDs: %w", err) + } + + args := liqoExecutedUpgradeArgs(u, chartPath) + logger.Infof("Upgrading Liqo chart %s tag=%s (reusing the existing cluster id)...", u.ChartVersion, u.ImageTag) + if err := runCmd(ctx, binHelm, args...); err != nil { + return fmt.Errorf("helm upgrade: %w", err) + } + logger.Infof("✓ Liqo chart %s / image %s", u.ChartVersion, u.ImageTag) + return nil +} + +func resolveLiqoChart(ctx context.Context, u LiqoUpgrade, logger Logger) (string, func(), error) { + if !isOCIRef(u.Chart) { + if _, err := os.Stat(filepath.Join(u.Chart, helmChartFile)); err != nil { + return "", nil, fmt.Errorf("liqo chart %s: %w", u.Chart, err) + } + return u.Chart, nil, nil + } + mgr := NewLiqoManager(nil, nil, &LiqoConfig{ + ChartOCI: u.Chart, + ChartVersion: u.ChartVersion, + }, logger) + return mgr.prepareLocalChart(ctx) +} + +func liqoChartVersion(version string) string { + version = strings.TrimSpace(version) + if version == "" { + return config.DefaultLiqoChartVersion + } + return strings.TrimPrefix(version, "v") +} + +func liqoImageTag(version string) string { + version = strings.TrimSpace(version) + if version == "" { + return config.DefaultLiqoVersion + } + if strings.HasPrefix(version, "v") { + return version + } + return "v" + version +} diff --git a/internal/install/liqo_upgrade_test.go b/internal/install/liqo_upgrade_test.go new file mode 100644 index 0000000..e4e0f83 --- /dev/null +++ b/internal/install/liqo_upgrade_test.go @@ -0,0 +1,232 @@ +package install + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/noderings/cli/internal/config" +) + +func TestLiqoUpgradeArgsResetsThenReusesAndSetsImageTag(t *testing.T) { + t.Parallel() + u, err := NewLiqoUpgrade("", "", "/tmp/kubeconfig", "") + if err != nil { + t.Fatal(err) + } + if u.Chart != config.DefaultLiqoChartOCI { + t.Fatalf("chart=%q", u.Chart) + } + if u.ChartVersion != config.DefaultLiqoChartVersion { + t.Fatalf("chart version=%q", u.ChartVersion) + } + if u.ImageTag != config.DefaultLiqoVersion { + t.Fatalf("image tag=%q", u.ImageTag) + } + if u.Timeout != defaultLiqoHelmTimeout { + t.Fatalf("timeout=%q", u.Timeout) + } + args := LiqoUpgradeArgs(u) + joined := strings.Join(args, " ") + for _, want := range []string{ + helmCmdUpgrade + " " + liqoHelmRelease + " " + config.DefaultLiqoChartOCI, + helmFlagNamespace + " " + config.DefaultLiqoNamespace, + helmFlagResetThenReuseValues, + helmFlagSetString + " " + liqoHelmValueTag + "=" + config.DefaultLiqoVersion, + helmFlagWait, + helmFlagTimeout + " " + defaultLiqoHelmTimeout, + helmFlagVersion + " " + config.DefaultLiqoChartVersion, + helmFlagKubeconfig + " /tmp/kubeconfig", + } { + if !strings.Contains(joined, want) { + t.Fatalf("missing %q in %s", want, joined) + } + } + for _, forbidden := range []string{ + helmFlagInstall, + helmFlagReuseValues, + "--cluster-id", + "statusSync", + "TOKEN", + "existingSecret", + } { + if strings.Contains(joined, forbidden) { + t.Fatalf("argv must not contain %q: %s", forbidden, joined) + } + } + if strings.Contains(joined, liqoHelmValueTag+"="+config.DefaultLiqoChartVersion) { + t.Fatalf("bare chart version must not be the image tag: %s", joined) + } +} + +func TestNewLiqoUpgradeRejectsHelmSetSeparators(t *testing.T) { + t.Parallel() + for _, version := range []string{ + "0.0.0-foo,clusterId=injected", + "v0.0.0-foo,clusterId=injected", + "0.0.0-foo=bar", + } { + if _, err := NewLiqoUpgrade("", version, "", ""); err == nil { + t.Fatalf("version %q accepted", version) + } + } + + u := LiqoUpgrade{ + Chart: "/tmp/liqo", + ChartVersion: "0.0.0-ok", + ImageTag: "v0.0.0-ok,clusterId=injected", + Release: liqoHelmRelease, + Namespace: config.DefaultLiqoNamespace, + Timeout: defaultLiqoHelmTimeout, + } + if err := u.Validate(); err == nil { + t.Fatal("image tag with a comma accepted") + } +} + +func TestLiqoUpgradeArgsLocalChartOmitsVersion(t *testing.T) { + t.Parallel() + u, err := NewLiqoUpgrade("/tmp/liqo", "v0.0.0-deadbeef", "", "5m") + if err != nil { + t.Fatal(err) + } + if u.ChartVersion != "0.0.0-deadbeef" || u.ImageTag != "v0.0.0-deadbeef" { + t.Fatalf("version=%q tag=%q", u.ChartVersion, u.ImageTag) + } + joined := strings.Join(LiqoUpgradeArgs(u), " ") + if strings.Contains(joined, helmFlagVersion) { + t.Fatalf("local chart must not pass %s: %s", helmFlagVersion, joined) + } + if !strings.Contains(joined, liqoHelmValueTag+"=v0.0.0-deadbeef") { + t.Fatalf("missing image tag: %s", joined) + } + if u.Timeout != "5m" { + t.Fatalf("timeout=%q", u.Timeout) + } +} + +func TestLiqoCRDDir(t *testing.T) { + t.Parallel() + root := t.TempDir() + dir := filepath.Join(root, liqoCRDRelDir) + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + got, err := LiqoCRDDir(root) + if err != nil || got != dir { + t.Fatalf("dir=%q err=%v", got, err) + } + missing, err := LiqoCRDDir(t.TempDir()) + if err == nil || missing != "" || !strings.Contains(err.Error(), "CRD directory") { + t.Fatalf("missing dir=%q err=%v", missing, err) + } + + fileRoot := t.TempDir() + crdFile := filepath.Join(fileRoot, liqoCRDRelDir) + if err := os.MkdirAll(filepath.Dir(crdFile), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(crdFile, []byte("not-a-dir"), 0o644); err != nil { + t.Fatal(err) + } + if _, err := LiqoCRDDir(fileRoot); err == nil || !strings.Contains(err.Error(), "not a directory") { + t.Fatalf("file path err=%v", err) + } +} + +func TestLiqoUpgradeDryRunUsesExtractedChart(t *testing.T) { + t.Parallel() + u, err := NewLiqoUpgrade("", "", "/tmp/kubeconfig", "") + if err != nil { + t.Fatal(err) + } + out := LiqoUpgradeDryRun(u) + if !strings.Contains(out, "helm pull "+config.DefaultLiqoChartOCI+" --version "+config.DefaultLiqoChartVersion+"\n") { + t.Fatalf("missing pull: %s", out) + } + upgrade := out[strings.LastIndex(out, "helm upgrade "):] + if strings.Contains(upgrade, helmFlagVersion) { + t.Fatalf("extracted chart upgrade must not pass %s: %s", helmFlagVersion, upgrade) + } + if !strings.Contains(upgrade, liqoPulledChartPlaceholder) { + t.Fatalf("upgrade must use the pulled chart: %s", upgrade) + } + if !strings.Contains(out, liqoPulledChartPlaceholder+"/"+liqoCRDRelDir) { + t.Fatalf("CRDs must come from the pulled chart: %s", out) + } +} + +func TestUpgradeLiqoReleaseMissingReleaseDoesNotUpgrade(t *testing.T) { + calls := filepath.Join(t.TempDir(), "calls") + installFakeHelm(t, calls, "status") + u, err := NewLiqoUpgrade("", "", "/tmp/kubeconfig", "1m") + if err != nil { + t.Fatal(err) + } + err = UpgradeLiqoRelease(context.Background(), u, nil) + if err == nil || !strings.Contains(err.Error(), "no Liqo Helm release") { + t.Fatalf("err=%v", err) + } + got, err := os.ReadFile(calls) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(got), helmCmdUpgrade) { + t.Fatalf("upgrade ran without a release: %s", got) + } +} + +func TestUpgradeLiqoReleaseUpgradesExtractedChart(t *testing.T) { + chart := t.TempDir() + if err := os.WriteFile(filepath.Join(chart, helmChartFile), []byte("apiVersion: v2\nname: liqo\nversion: 0.0.0\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(chart, liqoCRDRelDir), 0o755); err != nil { + t.Fatal(err) + } + calls := filepath.Join(t.TempDir(), "calls") + installFakeHelm(t, calls, "") + u, err := NewLiqoUpgrade(chart, "", "/tmp/kubeconfig", "1m") + if err != nil { + t.Fatal(err) + } + if err := UpgradeLiqoRelease(context.Background(), u, nil); err != nil { + t.Fatal(err) + } + got, err := os.ReadFile(calls) + if err != nil { + t.Fatal(err) + } + text := string(got) + if !strings.Contains(text, helmCmdUpgrade+" "+liqoHelmRelease+" "+chart) { + t.Fatalf("upgrade did not use the extracted chart: %s", text) + } + if strings.Contains(text, helmFlagVersion) { + t.Fatalf("local upgrade passed %s: %s", helmFlagVersion, text) + } + if !strings.Contains(text, "kubectl apply") || !strings.Contains(text, filepath.Join(chart, liqoCRDRelDir)) { + t.Fatalf("CRDs were not applied from the chart: %s", text) + } +} + +// installFakeHelm puts helm and kubectl on PATH. statusFail of "status" makes +// helm status report a missing release. An empty statusFail makes both succeed. +func installFakeHelm(t *testing.T, calls, statusFail string) { + t.Helper() + bin := t.TempDir() + helm := "#!/bin/sh\nprintf '%s\\n' \"helm $*\" >> " + calls + "\n" + if statusFail == "status" { + helm += "if [ \"$1\" = status ]; then echo \"Error: release: not found\" >&2; exit 1; fi\n" + } + helm += "exit 0\n" + kubectl := "#!/bin/sh\nprintf '%s\\n' \"kubectl $*\" >> " + calls + "\nexit 0\n" + if err := os.WriteFile(filepath.Join(bin, "helm"), []byte(helm), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(bin, "kubectl"), []byte(kubectl), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) +}