From a257030bdd2706df154541ac83b5aa9bc5d988e9 Mon Sep 17 00:00:00 2001 From: Dmitrii Iutkin Date: Fri, 20 Nov 2026 18:21:42 +0200 Subject: [PATCH 1/2] feat(mk8s): add managed Kubernetes commands (experimental) Add `verda mk8s` (aliases `k8s`, `kubernetes`): - locations - cluster: list, describe, create, upgrade, delete - node-pool: list, describe, create, scale, delete - kubeconfig: list, describe, create, save, delete Commands work on a terminal (pickers, wizards) and in scripts (flags, -o json, --agent). Bump verdacloud-sdk-go to v1.9.1 and stop HTML-escaping <, > and & in JSON output. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 1 + go.mod | 2 +- go.sum | 4 +- internal/verda-cli/cmd/cmd.go | 7 + internal/verda-cli/cmd/mk8s/CLAUDE.md | 67 ++ internal/verda-cli/cmd/mk8s/README.md | 93 +++ internal/verda-cli/cmd/mk8s/cluster.go | 49 ++ internal/verda-cli/cmd/mk8s/cluster_create.go | 271 +++++++ .../verda-cli/cmd/mk8s/cluster_create_test.go | 450 ++++++++++++ internal/verda-cli/cmd/mk8s/cluster_delete.go | 107 +++ .../verda-cli/cmd/mk8s/cluster_delete_test.go | 176 +++++ .../verda-cli/cmd/mk8s/cluster_describe.go | 153 ++++ .../cmd/mk8s/cluster_describe_test.go | 271 +++++++ internal/verda-cli/cmd/mk8s/cluster_list.go | 117 +++ .../verda-cli/cmd/mk8s/cluster_list_test.go | 193 +++++ .../verda-cli/cmd/mk8s/cluster_upgrade.go | 175 +++++ .../cmd/mk8s/cluster_upgrade_test.go | 206 ++++++ internal/verda-cli/cmd/mk8s/confirm.go | 76 ++ internal/verda-cli/cmd/mk8s/errors.go | 126 ++++ internal/verda-cli/cmd/mk8s/errors_test.go | 270 +++++++ internal/verda-cli/cmd/mk8s/fake_test.go | 494 +++++++++++++ internal/verda-cli/cmd/mk8s/kubeconfig.go | 57 ++ .../verda-cli/cmd/mk8s/kubeconfig_create.go | 336 +++++++++ .../cmd/mk8s/kubeconfig_create_test.go | 666 ++++++++++++++++++ .../verda-cli/cmd/mk8s/kubeconfig_delete.go | 126 ++++ .../cmd/mk8s/kubeconfig_delete_test.go | 284 ++++++++ .../verda-cli/cmd/mk8s/kubeconfig_describe.go | 97 +++ .../cmd/mk8s/kubeconfig_describe_test.go | 331 +++++++++ .../verda-cli/cmd/mk8s/kubeconfig_file.go | 239 +++++++ .../cmd/mk8s/kubeconfig_file_test.go | 462 ++++++++++++ .../verda-cli/cmd/mk8s/kubeconfig_list.go | 191 +++++ .../cmd/mk8s/kubeconfig_list_test.go | 262 +++++++ .../verda-cli/cmd/mk8s/kubeconfig_prompts.go | 231 ++++++ .../cmd/mk8s/kubeconfig_prompts_test.go | 439 ++++++++++++ .../verda-cli/cmd/mk8s/kubeconfig_save.go | 271 +++++++ .../cmd/mk8s/kubeconfig_save_test.go | 298 ++++++++ internal/verda-cli/cmd/mk8s/locations.go | 84 +++ internal/verda-cli/cmd/mk8s/locations_test.go | 176 +++++ internal/verda-cli/cmd/mk8s/mk8s.go | 119 ++++ internal/verda-cli/cmd/mk8s/nodepool.go | 171 +++++ .../verda-cli/cmd/mk8s/nodepool_create.go | 274 +++++++ .../cmd/mk8s/nodepool_create_test.go | 367 ++++++++++ .../verda-cli/cmd/mk8s/nodepool_delete.go | 123 ++++ .../cmd/mk8s/nodepool_delete_test.go | 169 +++++ .../verda-cli/cmd/mk8s/nodepool_describe.go | 113 +++ .../cmd/mk8s/nodepool_describe_test.go | 151 ++++ internal/verda-cli/cmd/mk8s/nodepool_list.go | 93 +++ .../verda-cli/cmd/mk8s/nodepool_list_test.go | 178 +++++ internal/verda-cli/cmd/mk8s/nodepool_scale.go | 295 ++++++++ .../verda-cli/cmd/mk8s/nodepool_scale_test.go | 482 +++++++++++++ internal/verda-cli/cmd/mk8s/resolve.go | 182 +++++ internal/verda-cli/cmd/mk8s/resolve_test.go | 210 ++++++ .../verda-cli/cmd/mk8s/testhelpers_test.go | 187 +++++ internal/verda-cli/cmd/mk8s/views.go | 240 +++++++ internal/verda-cli/cmd/mk8s/wait.go | 257 +++++++ internal/verda-cli/cmd/mk8s/wait_test.go | 199 ++++++ internal/verda-cli/cmd/mk8s/wizard_cache.go | 174 +++++ internal/verda-cli/cmd/mk8s/wizard_cluster.go | 214 ++++++ .../verda-cli/cmd/mk8s/wizard_cluster_test.go | 235 ++++++ .../verda-cli/cmd/mk8s/wizard_nodepool.go | 203 ++++++ .../cmd/mk8s/wizard_nodepool_test.go | 272 +++++++ internal/verda-cli/cmd/util/agent_error.go | 9 +- .../verda-cli/cmd/util/agent_error_test.go | 16 +- internal/verda-cli/cmd/util/factory.go | 30 +- internal/verda-cli/cmd/util/output.go | 1 + internal/verda-cli/cmd/util/output_test.go | 5 +- internal/verda-cli/cmd/util/redact_test.go | 91 ++- internal/verda-cli/options/paths.go | 10 +- 68 files changed, 12900 insertions(+), 28 deletions(-) create mode 100644 internal/verda-cli/cmd/mk8s/CLAUDE.md create mode 100644 internal/verda-cli/cmd/mk8s/README.md create mode 100644 internal/verda-cli/cmd/mk8s/cluster.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_create.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_create_test.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_delete.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_delete_test.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_describe.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_describe_test.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_list.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_list_test.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_upgrade.go create mode 100644 internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go create mode 100644 internal/verda-cli/cmd/mk8s/confirm.go create mode 100644 internal/verda-cli/cmd/mk8s/errors.go create mode 100644 internal/verda-cli/cmd/mk8s/errors_test.go create mode 100644 internal/verda-cli/cmd/mk8s/fake_test.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_create.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_delete.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_describe.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_file.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_list.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_save.go create mode 100644 internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go create mode 100644 internal/verda-cli/cmd/mk8s/locations.go create mode 100644 internal/verda-cli/cmd/mk8s/locations_test.go create mode 100644 internal/verda-cli/cmd/mk8s/mk8s.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_create.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_create_test.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_delete.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_delete_test.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_describe.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_describe_test.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_list.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_list_test.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_scale.go create mode 100644 internal/verda-cli/cmd/mk8s/nodepool_scale_test.go create mode 100644 internal/verda-cli/cmd/mk8s/resolve.go create mode 100644 internal/verda-cli/cmd/mk8s/resolve_test.go create mode 100644 internal/verda-cli/cmd/mk8s/testhelpers_test.go create mode 100644 internal/verda-cli/cmd/mk8s/views.go create mode 100644 internal/verda-cli/cmd/mk8s/wait.go create mode 100644 internal/verda-cli/cmd/mk8s/wait_test.go create mode 100644 internal/verda-cli/cmd/mk8s/wizard_cache.go create mode 100644 internal/verda-cli/cmd/mk8s/wizard_cluster.go create mode 100644 internal/verda-cli/cmd/mk8s/wizard_cluster_test.go create mode 100644 internal/verda-cli/cmd/mk8s/wizard_nodepool.go create mode 100644 internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go diff --git a/CLAUDE.md b/CLAUDE.md index 564d3e50..721e81aa 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -52,6 +52,7 @@ Each command directory has its own `CLAUDE.md` (domain knowledge) and `README.md | `cmd/objectstorage/` | CLAUDE.md, README.md | S3-style object storage: configure, mb/rb, cp/mv/sync/ls/rm, uploads, presign | | `cmd/network/` | CLAUDE.md, README.md | Private networks (beta): list, create, delete; subnet list, create, delete | | `cmd/serverless/` | CLAUDE.md, README.md | Serverless containers (`verda container`: create, update, list, describe, scale, replicas, pause/resume/restart, purge-queue, delete, env, secret, file-secret, registry-creds) and batch jobs (`verda batchjob`: create, update, list, describe, delete, pause/resume, purge-queue) | +| `cmd/mk8s/` | CLAUDE.md, README.md | Managed Kubernetes (experimental): locations; cluster list, describe, create, upgrade, delete; node-pool list, describe, create, scale, delete; kubeconfig list, describe, create, save, delete | | `cmd/doctor/` | — | Environment diagnostics | | `cmd/availability/` | — | Instance availability by location | | `cmd/cost/` | — | Balance, running costs, estimates | diff --git a/go.mod b/go.mod index e62b7b8a..241ddd4c 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.10 github.com/spf13/viper v1.21.0 - github.com/verda-cloud/verdacloud-sdk-go v1.9.0 + github.com/verda-cloud/verdacloud-sdk-go v1.9.1 go.yaml.in/yaml/v3 v3.0.5 ) diff --git a/go.sum b/go.sum index 180431ff..545ecb12 100644 --- a/go.sum +++ b/go.sum @@ -186,8 +186,8 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8 github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/vbatts/tar-split v0.12.2 h1:w/Y6tjxpeiFMR47yzZPlPj/FcPLpXbTUi/9H7d3CPa4= github.com/vbatts/tar-split v0.12.2/go.mod h1:eF6B6i6ftWQcDqEn3/iGFRFRo8cBIMSJVOpnNdfTMFA= -github.com/verda-cloud/verdacloud-sdk-go v1.9.0 h1:uKFMwgpHRheKurx0ANhxAsUe21znJn68ndN/MkpH/SQ= -github.com/verda-cloud/verdacloud-sdk-go v1.9.0/go.mod h1:va7s1wnZpFCqY/WVFxuUb66FpJzXgqHBYEWtEJTWx8o= +github.com/verda-cloud/verdacloud-sdk-go v1.9.1 h1:kttXszwkad1mVvpwC9UhkBms2uLzTO52bRI4/sxCT38= +github.com/verda-cloud/verdacloud-sdk-go v1.9.1/go.mod h1:va7s1wnZpFCqY/WVFxuUb66FpJzXgqHBYEWtEJTWx8o= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= diff --git a/internal/verda-cli/cmd/cmd.go b/internal/verda-cli/cmd/cmd.go index 7228b095..173c784e 100644 --- a/internal/verda-cli/cmd/cmd.go +++ b/internal/verda-cli/cmd/cmd.go @@ -34,6 +34,7 @@ import ( "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/instancetypes" "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/locations" mcpcmd "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/mcp" + "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/mk8s" "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/network" "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/objectstorage" "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/registry" @@ -167,6 +168,12 @@ func NewRootCommand(ioStreams cmdutil.IOStreams) (*cobra.Command, *clioptions.Op serverless.NewCmdBatchjob(f, ioStreams), }, }, + cmdutil.CommandGroup{ + Message: "Kubernetes Commands:", + Commands: []*cobra.Command{ + mk8s.NewCmdMK8s(f, ioStreams), + }, + }, cmdutil.CommandGroup{ Message: "Resource Commands:", Commands: resourceCmds, diff --git a/internal/verda-cli/cmd/mk8s/CLAUDE.md b/internal/verda-cli/cmd/mk8s/CLAUDE.md new file mode 100644 index 00000000..12e94bf6 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/CLAUDE.md @@ -0,0 +1,67 @@ +# MK8s Command Knowledge + +> Go house style lives in the root `CLAUDE.md`. This file carries managed-Kubernetes idioms only. User-facing behavior is in `README.md` and in `verda mk8s --help`. + +## Quick Reference + +- Parent: `verda mk8s` (aliases `k8s`, `kubernetes`), tagged `(experimental)` via `cmdutil.TagAnnotation`, in its own "Kubernetes Commands:" group in `cmd/cmd.go`. No env-var gate: the server's per-project flag controls access. +- Verbs: `locations`; `cluster` list/describe/create/upgrade/delete; `node-pool` (`pool`, `np`) list/describe/create/scale/delete; `kubeconfig` list/describe/create/save/delete. One file per verb, plus: + - `resolve.go` -- `clusterTarget`, `resolveCluster`, `apiCall`, pickers' shared constants. + - `nodepool.go` -- `poolScope` (`--cluster` + `-l`, reused by every pool and kubeconfig command), `resolvePool`. + - `confirm.go` -- `checkConfirmed`, `confirmWarning`, `askConfirm`. `errors.go` -- `mapErr`, `createFailed`. + - `wait.go` -- `waitFor` and the done conditions. `views.go` -- every JSON/YAML shape. + - `kubeconfig_file.go` (file rules), `kubeconfig_save.go` (`--file`/`--force`, recovery command), `kubeconfig_prompts.go` (direct prompts of `kubeconfig create`). + - `wizard_cluster.go`, `wizard_nodepool.go`, `wizard_cache.go` -- the two create wizards. +- SDK: `client.ManagedKubernetes` (SDK v1.9.1); the wizards also read `PrivateNetworks`, `InstanceAvailability`, `SSHKeys`. mk8s does not import `cmd/network`. +- Help: every `Long` goes through `longDesc`, which puts `experimentalCaution` first. `TestMK8s_HelpStartsWithCaution` checks only the direct children of `mk8s`, not the leaf commands: do not build a `Long` without `longDesc`. Shared help strings (`yesUsage`, `clusterUsage`, `locationUsage`, `waitTimeUsage`) live in `mk8s.go`; reuse them so one rule reads the same everywhere. + +## Domain-Specific Logic + +- **Names are unique per project.** Cluster and node pool names are unique in the project (pool names too, not only in their cluster). So `resolveCluster` finds a cluster's location with one `List`; with a name and `-l` it makes no call; with no name it shows a picker (filtered by `-l`). No match is a plain `*verda.APIError` 404 (NOT_FOUND via `ClassifyError`); a name in several locations, which cannot happen today, is a `UsageErrorf` asking for `-l`. Every API path needs the location, so all commands go through `clusterTarget`. +- **Network and subnet: IDs only, never created.** `cluster create` sends `--network`/`--subnet` as given; the API checks them. The wizard lists networks, then that network's subnets in the chosen location (`subnetLoader` filters by `Location.Code`), and stops with a `stopError` that names `verda network create` / `verda network subnet create` when a list is empty. +- **Creates are sent exactly once.** The API can save an object and still fail the answer (create saves, then reads back; a kubeconfig gets a server-chosen ID), so a retry would give a 409 or a hidden second kubeconfig. `createFailed` + `createMayHaveSucceeded` print "may have been created anyway" (stderr for people, `details.warning` in agent mode) for a 5xx, a 408, a timeout or a network error after the request left. `neverSent` excludes a failed token fetch and a dial error. Some of these are matched on SDK error text (`sdkAuthFailed`, `sdkResponseUnread`, `sdkResponseUnparsed`): an SDK upgrade can change them; `TestCreateMayHaveSucceeded_WhereTheRequestStopped` guards them. +- **SDK retries.** `factory.go` installs `ExponentialBackoffRetryMiddleware`, but in SDK v1.9.1 the request middleware chain wraps a no-op handler and the HTTP call happens after it, so nothing is retried (the comment in `factory.go` says so). An SDK upgrade that makes retries real must keep mk8s creates at one attempt. Nothing guards that today: the unit tests build the client with `newFakeClient`, without the factory's middleware, and there is no mk8s contract test through the real binary. +- **One confirmation rule.** Deletes, scale-down and `cluster upgrade`: red warning and confirm on a terminal; elsewhere `--yes` is required, else `CONFIRMATION_REQUIRED`. Creates and scale-up: summary and confirm on a terminal only, never `--yes`. `checkConfirmed` never prompts and runs before `f.VerdaClient()` for deletes and upgrade; scale decides in `nodePoolScaleOptions.confirm`, after it reads the pool, because only then is "down" known. `interactiveTTY` = stdin, stdout and stderr are terminals, not agent mode, table output: `-o json` on a terminal is non-interactive. +- **Deletes cascade in the background and have no `--wait`.** The API deletes pools, worker VMs and kubeconfigs after it answers; a cluster or pool GET is 404 at once while VMs are still being deleted, so a wait until 404 would report "completed" while VMs still bill. The CLI does not check for pools before a cluster delete. Named pool and kubeconfig deletes do not read the object first, so one whose read fails stays deletable (`validUntil` then says "until they expire"). +- **A scale to the current count is a no-op.** `node-pool scale` reads the pool first; with `replicas == current` it sends nothing, prints "already has N replicas; nothing to do." and returns `{"status": "unchanged", ...}` (exit 0). With `--wait` and a pool that does not yet meet `poolScaled` (for example after a scale-up timeout: replicas 2, ready 1) it skips the update and the confirmation, waits like a real scale (`poolWaitSettle`: a timeout has no scale-back advice, there is no previous count) and returns the normal `completed`/`accepted` + `wait_error` result. `cluster upgrade` has no such check: it would need an extra read and `kubernetes_version` is the requested version. +- **Timeout advice.** A failed wait prints the advice once, in the error (`poolWaitFailure`); the "Check progress with" line is only for a result without `wait_error`. After a scale-up timeout the advice is to scale back to the previous count (with `--yes`: it is a scale-down, which scripts and agent mode must confirm), after a create it is to delete the pool. +- **Dates.** Every date in mk8s output is UTC and says so (`utcTime`, `timeColumn`). +- **`cluster upgrade` has no `--wait`.** `kubernetes_version` is the requested version, not the running one, so polling it could end before the rollout starts. Add `--wait` only when the API reports the running version. +- **Wait conditions** (`wait.go`): cluster `running` with an endpoint; pool create `active` and `ready_replicas == requested`; scale `active` and `replicas == ready_replicas == requested`; kubeconfig `ready` and YAML present. They compare with the requested count, not the count read, so a stale first read cannot end the wait. A `degraded` pool keeps waiting (the backend keeps retrying and can still bill); the status line shows its error reasons. +- **Wait error handling** (`waitFor`): each read has its own `--timeout`; the whole wait ends after `--wait-timeout` (defaults per command: 15m cluster and pool, 3m kubeconfig; the shared `WaitOptions.AddFlags` 5m is not used). Up to 3 read failures in a row are tolerated (`maxReadFailures`); a 404 is tolerated until the first good read (the read lags the create); other 4xx except 408/429 fail at once (`fatalRead`). A read cut by the overall deadline counts as a timeout. A failed wait still writes the structured result (with `wait_error`) and exits non-zero (`writeResult`). +- **403 `feature_not_enabled`** becomes "Managed Kubernetes is not enabled for this project." (`mapErr`), still AUTH_ERROR. The message carries no instructions on how to get access. +- **Kubeconfig create** asks with direct prompts (`kubeconfig_prompts.go`), not the wizard engine; the others use `pkg/tui/wizard`. `--admin` adds `system:masters`; in scripts at least one of `--admin`/`--group` is required (`NewMissingOneOfFlagsError`; its message names every missing flag, `details` keeps `missing` and `one_of`). `--duration` is left out of the request when unset (API default 90 days). + +## Gotchas & Edge Cases + +- **Kubeconfig path problems are usage errors.** `kubeconfigTarget` returns an `*inputError` for a taken file, a symlink, a directory, a non-regular file, a missing parent directory and an unwritable directory; the commands convert it with `asUsage` (`UsageErrorf`: VALIDATION_ERROR, exit 2). Failures while creating, writing or renaming stay plain errors. +- **Human errors must be plain errors or `cmdutil.UsageErrorf`.** `main` prints any `*AgentError` as a JSON envelope even without `--agent`. Missing flags (`NewMissingFlagsError`) and `CONFIRMATION_REQUIRED` (`confirmationRequired`: one text for scripts, no terminal, agent mode and `-o json|yaml`) are AgentErrors on purpose; everything else returns `*verda.APIError`, `UsageErrorf` or `fmt.Errorf`, which `ClassifyError` still maps for agents. Only `createFailed` and `kubeconfigRecovery` build an AgentError, and only in agent mode. +- **Validate input before `f.VerdaClient()`** (`checkClusterArg`, `checkPoolTarget`, `checkKubeconfigTarget`, `missing()`), so bad input fails the same with or without credentials. A blank `--cluster` or positional is always rejected, even on a terminal. +- **The kubeconfig YAML holds a client private key.** + - `KubeconfigView` has no YAML field; never marshal `verda.ManagedKubernetesKubeconfig` to output or `DebugJSON`. `listKubeconfigViews`/`getKubeconfigView` convert inside the API call so the SDK struct never escapes. The last read of `waitForKubeconfig` holds the YAML: keep it out of every printer. + - `cmdutil` `debugBody` prints `` for any `/kubeconfigs` path, and `sensitiveJSONFieldRe` lists `kubeconfig` (`redact_test.go`). Keep both if you touch `factory.go`. +- **Kubeconfig file rules** (`kubeconfig_file.go`): mode 0600; a new file is created with `O_EXCL` (the open decides, not the earlier check); `--force` writes a temp file in the same directory, syncs and renames; a symlink, a directory or a non-regular file is always refused; `~/.kube` is created 0700 only when missing (`options.MkdirSecure`); the parent of a custom `--file` must exist. The default name is `verda-..yaml`, each part sanitized to `[A-Za-z0-9_-]`: the `.` separator cannot come from either part, so two clusters never share a file (with `-`, cluster `a-b`/cn `c` and cluster `a`/cn `b-c` would collide and `--force` would overwrite another cluster's key). Common names that differ only in replaced characters still share a file. `--file` equal to the default path is treated as the default. Modes are not enforced on Windows; mode tests skip there. +- **Check before `CreateKubeconfig`.** Order: flag validation, then (in scripts) the request (`validRequest`) and `destination()`, which checks the final path and probes that the directory is writable, then the cluster lookup. On a terminal a named `--file` is checked (`checkFree`) before any prompt, and the request and `destination()` after the prompts. A failure costs no certificate. +- **`--force` hint.** When `kubeconfig create --force` replaced an existing file (`kubeconfigDest.replaced`), it prints on stderr that the earlier kubeconfig is still valid and names `kubeconfig list`; nothing in the structured result. `kubeconfig save --force` prints none: it issues no certificate, so the file held the same kubeconfig. +- **`--file -`** writes only the YAML to stdout and is a `UsageErrorf` with `--agent` or `-o json|yaml` (it would share stdout with JSON). The interactive file prompt refuses `-` (it would print the key on the terminal). Recovery commands never use `--file -`. +- **Kubeconfig recovery.** After `CreateKubeconfig` succeeded, a wait timeout, failed reads or a failed write return the kubeconfig ID and the exact `kubeconfig save` command (`kubeconfigSaveCmd`: shell-quoted with `shellQuote`, which treats `:` as unsafe because it splits `KUBECONFIG`; default path and stdout left out; `--force` kept for files) plus "Running create again issues another certificate." In agent mode `kubeconfigRecovery` puts `kubeconfig_id`, `save_command` and `warning` in the details and keeps the recovery text as the message. +- **`node-pool create` wizard.** A cluster given by flag is resolved before the wizard, so a wrong name fails before any prompt. The cluster step sets the location from the picked cluster; its `Resetter` restores the `-l` value (`locationFlag`), so going Back does not keep a stale location. Instance types come from the availability API, which ignores private-network capacity: a listed type can still fail. The API data has no prices, so the picker lists types smallest first (`instanceTypeRank`: CPU-only, then GPU count) and Enter on the first row picks the smallest. The CLI never checks `--instance-type`. +- **Wizard hint bar.** `wizardLayout` has only the progress view: the engine's composite renders the one hint bar (prompt hints plus wizard keys). Adding a `HintBarView` shows a second, contradicting bar. +- **`cluster list` width.** The table drops the ENDPOINT column (after stripping `https://`) when a row is wider than `clusterTableWidth` (120); the endpoint holds the project ID, so it usually does. +- **Cluster wizard defaults.** The CIDR flags have non-empty defaults, so `podCIDRSet`/`serviceCIDRSet` (from `Flags().Changed`) decide whether the step runs; the steps are not `Required`, so their `Default` applies. +- **`cluster describe`** still prints the cluster when `ListNodePools` fails: `NodePools` is a pointer so JSON and YAML both say `null`, with `node_pools_error`. +- **`cluster upgrade`** prints "`describe` may show `degraded` while the rollout runs." on stderr: the API reports the `degraded` phase during a rollout. +- **`make fix.check`** runs `go fix` with the toolchain pinned in the Makefile; use `make fix`, not your local `go fix`. + +## Tests + +- `fake_test.go` is an in-memory mk8s API on `httptest`: pools and kubeconfigs keyed by cluster name, `failures` ("POST /mk8sclusters" → status/body, `Times` to fail only the first N), `disabled` (403 `feature_not_enabled` everywhere), and hooks `onClusterGet`, `onPoolGet`, `onKubeconfigGet` that move objects through states while a command waits. The hooks run under the fake's lock: read `a.requests` directly, never `requestsMatching`/`paths` inside them. +- `TestMain` forces `isInteractiveTerminal` to false, so every test starts off a terminal. `withTerminal`, `fastPoll` (`pollInterval`), `withWizardAnswers` (`newWizardEngine`) and `currentUserName` swap package variables: never call `t.Parallel` in or under a test that uses them. +- Factories: `tableFactory`, `jsonFactory`, `agentFactory`, `yamlFactory`; `runMK8s` runs the command tree. + +## Relationships + +- `cmdutil` -- `WithSpinner`, `RunWithSpinner`, `Poll`, `WaitOptions`, `DebugJSON`, `WriteStructured`, `ClassifyError`, `NewMissingFlagsError`, `NewMissingOneOfFlagsError`, `NewConfirmationRequiredError`, `UsageErrorf`, `TextColumn`; `factory.go` (`debugBody`, `sensitiveJSONFieldRe`, the retry middleware). +- `options.MkdirSecure` -- the 0700 `~/.kube`. +- `pkg/tui/wizard` -- the cluster and node pool create flows; `wizardCache` memoizes their lookups. +- `cmd/network` -- users create the network and subnet there; no code dependency. diff --git a/internal/verda-cli/cmd/mk8s/README.md b/internal/verda-cli/cmd/mk8s/README.md new file mode 100644 index 00000000..c5c233e5 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/README.md @@ -0,0 +1,93 @@ +# verda mk8s -- Managed Kubernetes (experimental) + +> Experimental: the managed Kubernetes API may change in incompatible ways. Commands, flags and output may change without a deprecation period. + +`verda mk8s` (aliases `k8s`, `kubernetes`) manages clusters, their node pools (the worker VMs) and their kubeconfigs. Flags and all options: `verda mk8s --help`. + +| Command | What it does | +|---|---| +| `locations` | Locations where this project can create clusters | +| `cluster list \| describe \| create \| upgrade \| delete` | Clusters | +| `node-pool list \| describe \| create \| scale \| delete` (aliases `pool`, `np`) | Node pools: workers of one instance type | +| `kubeconfig list \| describe \| create \| save \| delete` | Client certificates for `kubectl` | + +## Workflow + +mk8s never creates the private network or the subnet. Create them first with `verda network`, in the cluster's location, and pass their IDs. + +```bash +verda mk8s locations +NETWORK_ID=$(verda network create --name k8s -o json | jq -r .id) +SUBNET_ID=$(verda network subnet create --network "$NETWORK_ID" -l FIN-03 -o json | jq -r .id) + +verda mk8s cluster create --name prod -l FIN-03 --kubernetes-version 1.37.0 \ + --network "$NETWORK_ID" --subnet "$SUBNET_ID" --wait +verda mk8s node-pool create --cluster prod --name workers --instance-type 1V100.6V \ + --ssh-key "$KEY_ID" --replicas 2 --wait +verda mk8s kubeconfig create --cluster prod --common-name alice --admin + +export KUBECONFIG=~/.kube/verda-prod.alice.yaml +kubectl get nodes + +verda mk8s node-pool scale workers --cluster prod --replicas 0 --yes +verda mk8s cluster delete prod --yes # node pools, worker VMs and kubeconfigs go with it +``` + +`cluster list` shows the endpoint without `https://` and drops the ENDPOINT column when a row would be wider than 120 columns; `-o json|yaml` and `cluster describe` always show it in full. + +Cluster and node pool names are unique in the project, so `--cluster prod` is enough: the CLI finds the location with one list call. `-l` gives the location and skips that call. Node pool names must be unique in the project, not only in their cluster. Kubeconfigs are named by an ID that the API chooses (`kubeconfig list`). + +## Prompts and `--yes` + +A command prompts only on a terminal, with table output, without `--agent`. In scripts, agent mode and `-o json|yaml` it never prompts. + +| Command | On a terminal | In scripts, agent mode, `-o json\|yaml` | +|---|---|---| +| `cluster create`, `node-pool create`, `kubeconfig create` | Asks for missing values, shows a summary, asks to confirm | Required flags must be given; no `--yes` needed | +| `node-pool scale` up | Shows a summary, asks to confirm | No `--yes` needed | +| `node-pool scale` down (to 0 too), all deletes, `cluster upgrade` | Red warning, asks to confirm (`--yes` skips it) | `--yes` required, else `CONFIRMATION_REQUIRED` | +| Any command without its cluster, node pool or kubeconfig | Picker | `--cluster` and the positional target are required | + +In the create wizards the instance types are listed smallest first (CPU-only types, then by GPU count), because the availability data has no prices. `kubeconfig create` checks `--duration` (at least `1h`) when you type it. + +## Waiting + +Changes run in the background after the API accepts them. Without `--wait` a command reports `accepted`; with `--wait` it polls and reports `completed`. + +| Command | Done when | `--wait-timeout` default | +|---|---|---| +| `cluster create --wait` | phase `running` and an endpoint is set | 15m | +| `node-pool create --wait` | state `active`, all requested workers ready | 15m | +| `node-pool scale --wait` | state `active`, new count reached and ready | 15m | +| `kubeconfig create` (always waits) | status `ready` | 3m | + +- A timeout exits non-zero; the change keeps going in the background. Ctrl+C stops only the wait. +- A node pool keeps trying to create workers after a timeout, and workers that exist bill even before they join. Stop a create with `node-pool delete`, a scale-up with `node-pool scale` back to the previous count (the printed advice includes `--yes`, because that is a scale-down). +- `node-pool scale` to the current count sends nothing: "already has N replicas; nothing to do." ("1 replica" for one), status `unchanged` in `-o json|yaml`. With `--wait`, a pool that is not yet active with all workers ready is waited for instead (status `completed`, or a timeout), so a retry after a scale-up timeout does not report success early. +- Deletes and `cluster upgrade` have no `--wait`. A deleted cluster or pool leaves the lists at once while its VMs are still being deleted. The API reports the requested Kubernetes version, not the running one: follow an upgrade with `cluster describe`. + +## Kubeconfigs + +The kubeconfig YAML holds a client private key. + +- It is written only to a file with mode 0600: by default `~/.kube/verda-..yaml` (`~/.kube` is created with mode 0700). Never to `-o json|yaml`, `describe` or `--debug`. +- `kubeconfig create` checks the file before it issues a certificate. An existing file stops it unless `--force`; a symlink or a directory is always refused. +- `--file -` writes only the YAML to stdout, for a pipe. It cannot be combined with `--agent` or `-o json|yaml`. A file you redirect it to gets your umask, not 0600. +- In scripts, give `--common-name` and `--admin` (group `system:masters`, full control) and/or `--group` (repeatable). +- Certificates cannot be revoked before they expire (90 days unless `--duration`, at least `1h`): use a short `--duration` for admin credentials. `kubeconfig delete` does not disable copies already downloaded, which keep working until they expire. Deleting the cluster invalidates them. A copy that does not use `system:masters` can also be cut off by removing its group's RBAC binding in the cluster. +- `kubeconfig create --force` replaces the file but not the earlier kubeconfig on the server, which is a different, still valid certificate: the CLI says so on stderr. Run `kubeconfig list` and delete what you no longer need. `kubeconfig save --force` issues no certificate and prints no such hint. +- If `kubeconfig create` stops before the kubeconfig is ready, or cannot write the file, the error gives the kubeconfig ID and the `verda mk8s kubeconfig save ...` command to run. Running `create` again issues another certificate. + +## When a create fails + +A create is sent once and never retried. If `cluster create`, `node-pool create` or `kubeconfig create` fails with a 5xx, a 408, a timeout or a network error, the object may exist anyway: the CLI says so on stderr (agent mode: in `error.details.warning`). Check the matching `list` before you run `create` again. A 4xx means nothing was created. + +## Agent mode + +- Missing flag or target: `MISSING_REQUIRED_FLAGS` (exit 2). `kubeconfig create` without `--admin` or `--group` lists them in `details.one_of`. +- Delete, scale-down or upgrade without `--yes`: `CONFIRMATION_REQUIRED` (exit 2). +- Bad flag values or combinations (`--file -` with `--agent`, `--wait --wait-timeout 0`, negative `--replicas`) and a kubeconfig file that cannot be used (exists without `--force`, symlink, directory, missing or read-only directory): `VALIDATION_ERROR` (exit 2). +- Unknown cluster name: `NOT_FOUND` (exit 5). Other API errors show the API message: 400 and 409 are `API_ERROR` (exit 4). +- Managed Kubernetes not enabled for the project: `AUTH_ERROR` (exit 3) with "Managed Kubernetes is not enabled for this project." +- Results: creates `{"status": "accepted"|"completed", "cluster"|"pool": {...}}`; `node-pool scale` adds `previous_replicas` (and can be `"unchanged"`), `cluster upgrade` adds `previous_version`; deletes `{"resource", "name", "location", "action": "delete", "status": "accepted"}` (`cluster` and, for kubeconfigs, `id` too); kubeconfig `create`/`save` print the metadata with `file`, never the YAML. When a `--wait` fails, stdout still has the result, with `wait_error`, and the command exits non-zero. +- A failed `kubeconfig create` wait or write adds `kubeconfig_id`, `save_command` and `warning` to `error.details`. diff --git a/internal/verda-cli/cmd/mk8s/cluster.go b/internal/verda-cli/cmd/mk8s/cluster.go new file mode 100644 index 00000000..3f66071b --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster.go @@ -0,0 +1,49 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdCluster creates the `verda mk8s cluster` parent command. +func NewCmdCluster(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "cluster", + Short: "Manage clusters", + Long: longDesc(` + List, show, create, upgrade and delete managed Kubernetes clusters. + Cluster names are unique in the project, so commands find a + cluster's location from its name; -l gives the location and skips + that lookup. + `), + Example: cmdutil.Examples(` + verda mk8s cluster list + verda mk8s cluster describe prod + verda mk8s cluster create --name prod -l FIN-03 --kubernetes-version 1.37.0 --network $NETWORK_ID --subnet $SUBNET_ID + `), + Run: cmdutil.DefaultSubCommandRun(ioStreams.Out), + } + cmd.AddCommand( + NewCmdClusterList(f, ioStreams), + NewCmdClusterDescribe(f, ioStreams), + NewCmdClusterCreate(f, ioStreams), + NewCmdClusterUpgrade(f, ioStreams), + NewCmdClusterDelete(f, ioStreams), + ) + return cmd +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_create.go b/internal/verda-cli/cmd/mk8s/cluster_create.go new file mode 100644 index 00000000..1298d445 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_create.go @@ -0,0 +1,271 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "fmt" + "strings" + "time" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + flagName = "name" + flagKubernetesVersion = "kubernetes-version" + flagNetwork = "network" + flagSubnet = "subnet" + flagPodCIDR = "pod-cidr" + flagServiceCIDR = "service-cidr" + flagWait = "wait" + flagWaitTimeout = "wait-timeout" + + defaultPodCIDR = "10.100.0.0/16" + defaultServiceCIDR = "10.200.0.0/16" + defaultClusterWaitTimeout = 15 * time.Minute +) + +type clusterCreateOptions struct { + Name string + Location string + Version string + Network string + Subnet string + PodCIDR string + ServiceCIDR string + Wait cmdutil.WaitOptions + + // The CIDR flags have defaults, so the wizard needs to know if the user set them. + podCIDRSet, serviceCIDRSet bool +} + +// ClusterCreateResult is the structured output of `cluster create`: accepted, +// or completed once --wait saw the cluster running with an endpoint. +type ClusterCreateResult struct { + Status string `json:"status" yaml:"status"` + Cluster ClusterView `json:"cluster" yaml:"cluster"` + WaitError string `json:"wait_error,omitempty" yaml:"wait_error,omitempty"` +} + +// NewCmdClusterCreate creates `verda mk8s cluster create`. +func NewCmdClusterCreate(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + opts := &clusterCreateOptions{} + cmd := &cobra.Command{ + Use: "create", + Short: "Create a cluster", + Long: longDesc(` + Create a managed Kubernetes cluster. The private network and the + subnet must exist already: create them with verda network and pass + their IDs. mk8s never creates them. The CLI sends the IDs as given; + the API checks them. + + On a terminal, the CLI asks for each value that you do not give, + then shows a summary and asks for confirmation. In scripts, agent + mode and -o json|yaml, --name, --location, --kubernetes-version, + --network and --subnet are required and the command never asks. + + The command returns once the API accepts the request (status + "accepted"). With --wait it polls until the cluster is running and + has an endpoint (status "completed"). If the wait times out, or you + stop it with Ctrl+C, the cluster is still being created. + + A create is sent once and is never retried: if it fails with a + server or network error, the cluster may exist anyway, so check + cluster list before you run it again. + `), + Example: cmdutil.Examples(` + # Ask for each value (terminal only) + verda mk8s cluster create + + # Find the IDs of the private network and of its subnet + verda network list + verda network subnet list --network $NETWORK_ID -l FIN-03 + + # Create without prompts + verda mk8s cluster create --name prod -l FIN-03 --kubernetes-version 1.37.0 --network $NETWORK_ID --subnet $SUBNET_ID + + # Other pod and service ranges; wait up to 30 minutes for the cluster to run + verda mk8s cluster create --name prod -l FIN-03 --kubernetes-version 1.37.0 --network $NETWORK_ID --subnet $SUBNET_ID --pod-cidr 10.64.0.0/16 --service-cidr 10.65.0.0/16 --wait --wait-timeout 30m + + # Agent mode: JSON result and structured errors + verda --agent mk8s cluster create --name prod -l FIN-03 --kubernetes-version 1.37.0 --network $NETWORK_ID --subnet $SUBNET_ID --wait + `), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + opts.podCIDRSet = cmd.Flags().Changed(flagPodCIDR) + opts.serviceCIDRSet = cmd.Flags().Changed(flagServiceCIDR) + return runClusterCreate(cmd, f, ioStreams, opts) + }, + } + flags := cmd.Flags() + flags.StringVar(&opts.Name, flagName, "", "Cluster name: a DNS label of at most 30 characters, unique in the project") + flags.StringVarP(&opts.Location, flagLocation, "l", "", "Location code, for example FIN-03 (see verda mk8s locations)") + flags.StringVar(&opts.Version, flagKubernetesVersion, "", "Kubernetes version, for example 1.37.0 (the API checks it)") + flags.StringVar(&opts.Network, flagNetwork, "", "ID of the private network (see verda network list)") + flags.StringVar(&opts.Subnet, flagSubnet, "", "ID of a subnet of that network in the cluster's location (see verda network subnet list)") + flags.StringVar(&opts.PodCIDR, flagPodCIDR, defaultPodCIDR, "IPv4 range for pod IPs, /21 or larger") + flags.StringVar(&opts.ServiceCIDR, flagServiceCIDR, defaultServiceCIDR, "IPv4 range for service IPs; must not overlap the pod range") + flags.BoolVar(&opts.Wait.Wait, flagWait, false, "Wait until the cluster is running and has an endpoint") + flags.DurationVar(&opts.Wait.Timeout, flagWaitTimeout, defaultClusterWaitTimeout, waitTimeUsage) + return cmd +} + +func (o *clusterCreateOptions) trim() { + for _, s := range []*string{&o.Name, &o.Location, &o.Version, &o.Network, &o.Subnet, &o.PodCIDR, &o.ServiceCIDR} { + *s = strings.TrimSpace(*s) + } +} + +// missing lists the required flags that are empty. +func (o *clusterCreateOptions) missing() []string { + var m []string + for _, r := range []struct{ flag, value string }{ + {flagName, o.Name}, {flagLocation, o.Location}, {flagKubernetesVersion, o.Version}, + {flagNetwork, o.Network}, {flagSubnet, o.Subnet}, + } { + if r.value == "" { + m = append(m, "--"+r.flag) + } + } + return m +} + +func (o *clusterCreateOptions) request() verda.CreateManagedKubernetesClusterRequest { + return verda.CreateManagedKubernetesClusterRequest{ + Name: o.Name, + KubernetesVersion: o.Version, + PodCIDR: o.PodCIDR, + ServiceCIDR: o.ServiceCIDR, + Location: o.Location, + NetworkID: o.Network, + SubnetID: o.Subnet, + } +} + +func runClusterCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *clusterCreateOptions) error { + opts.trim() + if opts.Wait.Wait && opts.Wait.Timeout <= 0 { + return cmdutil.UsageErrorf(cmd, "--%s must be greater than 0", flagWaitTimeout) + } + missing := opts.missing() + interactive := interactiveTTY(f) + if len(missing) > 0 && !interactive { + return cmdutil.NewMissingFlagsError(missing) + } + + client, err := f.VerdaClient() + if err != nil { + return err + } + if len(missing) > 0 { + flow := buildClusterCreateFlow(opts, newWizardCache(f, client)) + if err := newWizardEngine(f, ioStreams).Run(cmd.Context(), flow); err != nil { + if stop, ok := errors.AsType[*stopError](err); ok { + return stop + } + return err + } + opts.trim() + } + req := opts.request() + + if interactive { + ok, err := confirmClusterCreate(cmd.Context(), f, ioStreams, &req) + if err != nil || !ok { + return err + } + } + + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + cluster, err := cmdutil.WithSpinner(ctx, f.Status(), "Creating cluster...", func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { + return client.ManagedKubernetes.Create(ctx, req) + }) + if err != nil { + return createFailed(f, ioStreams, kindCluster, "verda mk8s cluster list", err) + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response:", newClusterView(cluster)) + + result := ClusterCreateResult{Status: statusAccepted, Cluster: newClusterView(cluster)} + var waitErr error + if opts.Wait.Wait { + waitErr = waitForCluster(cmd.Context(), f, ioStreams, client, cluster, opts.Wait.Timeout, &result) + } + + if written, err := writeResult(f, ioStreams, result, waitErr); written { + return err + } + printClusterCreated(ioStreams, &result) + return waitErr +} + +func confirmClusterCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, req *verda.CreateManagedKubernetesClusterRequest) (bool, error) { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "\nCreate cluster %s\n Location: %s\n Version: %s\n Network: %s\n Subnet: %s\n Pod CIDR: %s\n Service CIDR: %s\n\n", + req.Name, req.Location, req.KubernetesVersion, req.NetworkID, req.SubnetID, req.PodCIDR, req.ServiceCIDR) + return askConfirm(ctx, ioStreams, f.Prompter(), "Create cluster "+req.Name+"?") +} + +// waitForCluster polls until the cluster is ready and updates result. Its +// error says what was last seen and how to check later. +func waitForCluster(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, created *verda.ManagedKubernetesCluster, timeout time.Duration, result *ClusterCreateResult) error { + name, location := created.Name, created.Location + if f.OutputFormat() == outputTable { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Cluster %s accepted; waiting until it is running (Ctrl+C stops waiting, the cluster stays).\n", name) + } + last, err := waitFor(ctx, f, waitWriter(f, ioStreams), timeout, + func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { + return client.ManagedKubernetes.Get(ctx, location, name) + }, clusterReady) + if last != nil { + result.Cluster = newClusterView(last) + } + if err == nil { + result.Status = statusCompleted + return nil + } + + phase := created.Phase + if last != nil { + phase = last.Phase + } + describe := describeCmd(name, location) + err = waitFailure(err, &waitFailureInfo{ + Subject: fmt.Sprintf("cluster %s in %s", name, location), + Timeout: timeout, + LastSeen: "last phase: " + phase, + TimeoutTail: fmt.Sprintf("It is still being created; check with `%s`", describe), + CheckCmd: describe, + }) + result.WaitError = err.Error() + return err +} + +func printClusterCreated(ioStreams cmdutil.IOStreams, r *ClusterCreateResult) { + c := &r.Cluster + if r.Status == statusCompleted { + _, _ = fmt.Fprintf(ioStreams.Out, "Cluster %s completed (%s): %s, endpoint %s\n", c.Name, c.Location, c.Phase, c.ExternalEndpoint) + return + } + _, _ = fmt.Fprintf(ioStreams.Out, "Cluster %s accepted (%s), phase %s.\n", c.Name, c.Location, c.Phase) + if r.WaitError == "" { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Check progress with: %s\n", describeCmd(c.Name, c.Location)) + } +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_create_test.go b/internal/verda-cli/cmd/mk8s/cluster_create_test.go new file mode 100644 index 00000000..244f4b84 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_create_test.go @@ -0,0 +1,450 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "slices" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +var createArgs = []string{ + "cluster", "create", "--name", "prod", "-l", "FIN-03", "--kubernetes-version", "1.37.0", + "--network", "pn-123", "--subnet", "sn-456", +} + +func withArgs(extra ...string) []string { return append(slices.Clone(createArgs), extra...) } + +// fastPoll shortens the wait loop. Never call t.Parallel in or under a test that +// uses it, withTerminal or withWizardAnswers: they change package variables. +func fastPoll(t *testing.T) { + t.Helper() + orig := pollInterval + pollInterval = 5 * time.Millisecond + t.Cleanup(func() { pollInterval = orig }) +} + +func TestClusterCreate_FlagsSendExactlyTheGivenIDs(t *testing.T) { + t.Parallel() + api := newFakeAPI() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), createArgs...) + if err != nil { + t.Fatal(err) + } + + got := decodeJSON[map[string]any](t, out) + if got["status"] != "accepted" { + t.Errorf("status = %v", got["status"]) + } + if c, _ := got["cluster"].(map[string]any); c["name"] != "prod" || c["phase"] != "provisioning" { + t.Errorf("cluster = %v", got["cluster"]) + } + + reqs := api.requestsMatching("POST") + if len(reqs) != 1 || reqs[0].Path != "/mk8sclusters" { + t.Fatalf("POST requests = %v", reqs) + } + body := decodeJSON[map[string]string](t, reqs[0].Body) + want := map[string]string{ + "name": "prod", "location": "FIN-03", "kubernetes_version": "1.37.0", + "network": "pn-123", "subnet": "sn-456", "pod_cidr": "10.100.0.0/16", "service_cidr": "10.200.0.0/16", + } + for k, v := range want { + if body[k] != v { + t.Errorf("body[%s] = %q, want %q", k, body[k], v) + } + } + // IDs go through unchecked: no network, subnet or cluster read happens. + if all := api.paths("GET"); len(all) != 0 { + t.Errorf("unexpected reads: %v", all) + } +} + +func TestClusterCreate_CustomCIDRsAndTrimming(t *testing.T) { + t.Parallel() + api := newFakeAPI() + args := []string{ + "cluster", "create", "--name", " prod ", "-l", "FIN-03", "--kubernetes-version", "1.37.0", + "--network", " pn-123 ", "--subnet", "sn-456", "--pod-cidr", "10.50.0.0/16", "--service-cidr", "10.60.0.0/16", + } + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); err != nil { + t.Fatal(err) + } + body := decodeJSON[map[string]string](t, api.requestsMatching("POST")[0].Body) + if body["name"] != "prod" || body["network"] != "pn-123" || body["pod_cidr"] != "10.50.0.0/16" || body["service_cidr"] != "10.60.0.0/16" { + t.Errorf("body = %v", body) + } +} + +func TestClusterCreate_TableSaysAccepted(t *testing.T) { + t.Parallel() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, newFakeAPI()), nil), createArgs...) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Cluster prod accepted (FIN-03)") || strings.Contains(out, "completed") { + t.Errorf("stdout = %q", out) + } + if !strings.Contains(errOut, "verda mk8s cluster describe prod -l FIN-03") { + t.Errorf("stderr = %q", errOut) + } +} + +func TestClusterCreate_MissingFlagsOffTerminal(t *testing.T) { + t.Parallel() + for name, f := range map[string]*cmdutil.TestFactory{ + "table": tableFactory(newFakeClient(t, newFakeAPI()), nil), + "agent": agentFactory(newFakeClient(t, newFakeAPI())), + } { + _, _, err := runMK8s(t, f, "cluster", "create") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("%s: err = %v", name, err) + } + missing, _ := ae.Details["missing"].([]string) + want := []string{"--name", "--location", "--kubernetes-version", "--network", "--subnet"} + if !slices.Equal(missing, want) { + t.Errorf("%s: missing = %v, want %v", name, missing, want) + } + } + + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, newFakeAPI())), "cluster", "create", "--name", "prod", "-l", "FIN-03") + missing, _ := cmdutil.ClassifyError(err).Details["missing"].([]string) + if !slices.Equal(missing, []string{"--kubernetes-version", "--network", "--subnet"}) { + t.Errorf("only the missing flags are listed: %v", missing) + } +} + +func TestClusterCreate_SDKValidationHappensBeforeAnyRequest(t *testing.T) { + t.Parallel() + api := newFakeAPI() + args := slices.Clone(createArgs) + args[slices.Index(args, "--name")+1] = "Not_A_Label" + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...) + if err == nil || !strings.Contains(strings.ToLower(err.Error()), "name") { + t.Fatalf("err = %v", err) + } + if n := len(api.requestsMatching("POST")); n != 0 { + t.Errorf("%d POST(s) sent for an invalid name", n) + } + if got := createMayHaveSucceeded(err); got { + t.Error("a validation error must not claim the cluster may exist") + } +} + +func TestClusterCreate_FailureWarnings(t *testing.T) { + t.Parallel() + const route = "POST /mk8sclusters" + + t.Run("502 warns, once, in human mode", func(t *testing.T) { + t.Parallel() + api := newFakeAPI() + api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), createArgs...) + if err == nil || !strings.Contains(err.Error(), "upstream failed") { + t.Fatalf("err = %v", err) + } + if !strings.Contains(errOut, "The cluster may have been created anyway.") || !strings.Contains(errOut, "verda mk8s cluster list") { + t.Errorf("stderr = %q", errOut) + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("a create must be sent once, got %d POSTs", n) + } + }) + + t.Run("502 warns in agent details", func(t *testing.T) { + t.Parallel() + api := newFakeAPI() + api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} + _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), createArgs...) + ae := cmdutil.ClassifyError(err) + if w, _ := ae.Details["warning"].(string); !strings.Contains(w, "may have been created anyway") { + t.Errorf("details = %v", ae.Details) + } + if errOut != "" { + t.Errorf("agent mode reports through the error only: %q", errOut) + } + }) + + t.Run("400 does not warn", func(t *testing.T) { + t.Parallel() + api := newFakeAPI() + api.failures[route] = apiFailure{Status: 400, Body: `{"code":"bad_request","message":"quota","details":"cluster_quota_exceeded"}`} + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), createArgs...) + if err == nil || !strings.Contains(err.Error(), "cluster_quota_exceeded") { + t.Fatalf("the API reason must be shown: %v", err) + } + if errOut != "" { + t.Errorf("unexpected warning: %q", errOut) + } + }) + + t.Run("feature not enabled", func(t *testing.T) { + t.Parallel() + api := newFakeAPI() + api.disabled = true + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), createArgs...) + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Fatalf("err = %v", err) + } + if errOut != "" { + t.Errorf("a 403 never warns: %q", errOut) + } + }) +} + +func TestClusterCreate_WaitCompletesOnRunningWithEndpoint(t *testing.T) { + fastPoll(t) + api := newFakeAPI() + var gets atomic.Int32 + api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { + switch gets.Add(1) { + case 1: // still provisioning + case 2: // running, but the endpoint is not published yet: not done + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + default: + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + c.ExternalEndpoint = "https://prod.example.test:6443" + } + } + + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait", "--wait-timeout", "30s")...) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Cluster prod completed (FIN-03)") || !strings.Contains(out, "https://prod.example.test:6443") { + t.Errorf("stdout = %q", out) + } + if got := gets.Load(); got != 3 { + t.Errorf("reads = %d, want 3: running without an endpoint must not end the wait", got) + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d", n) + } + + api2 := newFakeAPI() + api2.onClusterGet = func(c *verda.ManagedKubernetesCluster) { + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + c.ExternalEndpoint = "https://prod.example.test:6443" + } + out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api2)), withArgs("--wait")...) + if err != nil { + t.Fatal(err) + } + got := decodeJSON[map[string]any](t, out) + if got["status"] != "completed" || got["cluster"].(map[string]any)["external_endpoint"] != "https://prod.example.test:6443" { + t.Errorf("result = %v", got) + } + if _, has := got["wait_error"]; has { + t.Error("no wait_error on success") + } +} + +func TestClusterCreate_WaitTimeout(t *testing.T) { + fastPoll(t) + api := newFakeAPI() // stays provisioning forever + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withArgs("--wait", "--wait-timeout", "300ms")...) + if err == nil { + t.Fatal("a timeout must fail") + } + for _, want := range []string{"timed out after 300ms", "cluster prod in FIN-03", "last phase: provisioning", "verda mk8s cluster describe prod -l FIN-03"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("error misses %q: %v", want, err) + } + } + got := decodeJSON[map[string]any](t, out) + if got["status"] != "accepted" || got["wait_error"] == nil { + t.Errorf("the structured result is still written, with wait_error: %v", got) + } +} + +const clusterGetRoute = "GET /mk8sclusters/{loc}/{name}" + +func TestClusterCreate_WaitReadFailures(t *testing.T) { + fastPoll(t) + + t.Run("reads that keep failing end the wait and say the cluster was accepted", func(t *testing.T) { + api := newFakeAPI() + api.failures[clusterGetRoute] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...) + if err == nil || !strings.Contains(err.Error(), "was accepted, but --wait could not read it") || !strings.Contains(err.Error(), "read failed") { + t.Fatalf("err = %v", err) + } + if n := len(api.paths("GET")); n != maxReadFailures { + t.Errorf("reads = %d, want %d in a row before giving up", n, maxReadFailures) + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d", n) + } + }) + + t.Run("a few failed reads do not fail the wait", func(t *testing.T) { + api := newFakeAPI() + api.failures[clusterGetRoute] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"lb"}`, Times: maxReadFailures - 1} + api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + c.ExternalEndpoint = "https://prod.example.test:6443" + } + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...) + if err != nil || !strings.Contains(out, "completed") { + t.Fatalf("out=%q err=%v", out, err) + } + }) + + t.Run("a read the API refuses ends the wait at once", func(t *testing.T) { + api := newFakeAPI() + api.failures[clusterGetRoute] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access"}`} + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...) + if err == nil || !strings.Contains(err.Error(), "no access") { + t.Fatalf("err = %v", err) + } + if n := len(api.paths("GET")); n != 1 { + t.Errorf("reads = %d, want 1", n) + } + }) + + t.Run("a stale 404 right after the create is tolerated", func(t *testing.T) { + api := newFakeAPI() + api.failures[clusterGetRoute] = apiFailure{Status: 404, Body: `{"code":"not_found","message":"cluster not found"}`, Times: maxReadFailures + 2} + api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + c.ExternalEndpoint = "https://prod.example.test:6443" + } + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...); err != nil { + t.Fatal(err) + } + }) +} + +func TestClusterCreate_WaitTimeoutReportsTheLastPhaseSeen(t *testing.T) { + fastPoll(t) + api := newFakeAPI() + api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { c.Phase = verda.ManagedKubernetesClusterPhaseDegraded } + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait", "--wait-timeout", "300ms")...) + if err == nil || !strings.Contains(err.Error(), "last phase: degraded") { + t.Fatalf("err = %v, want the last phase seen, not the one from the create answer", err) + } +} + +func TestClusterCreate_WaitKeepsStderrQuietOffTerminal(t *testing.T) { + fastPoll(t) + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + } { + api := newFakeAPI() + api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + c.ExternalEndpoint = "https://prod.example.test:6443" + } + _, errOut, err := runMK8s(t, mk(api, t), withArgs("--wait")...) + if err != nil || errOut != "" { + t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + } + } +} + +func TestClusterCreate_FlagValuesAreTrimmed(t *testing.T) { + t.Parallel() + api := newFakeAPI() + args := []string{ + "cluster", "create", "--name", " prod ", "-l", " FIN-03 ", "--kubernetes-version", " 1.37.0 ", + "--network", " pn-123 ", "--subnet", " sn-456 ", "--pod-cidr", " 10.50.0.0/16 ", "--service-cidr", " 10.60.0.0/16 ", + } + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); err != nil { + t.Fatal(err) + } + for k, v := range decodeJSON[map[string]string](t, api.requestsMatching("POST")[0].Body) { + if strings.TrimSpace(v) != v { + t.Errorf("body[%s] = %q was not trimmed", k, v) + } + } +} + +func TestClusterCreate_WaitTimeoutMustBePositive(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, newFakeAPI()), nil), withArgs("--wait", "--wait-timeout", "0s")...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Fatalf("err = %v: want a usage error (an AgentError would print as JSON for a person)", err) + } +} + +func TestClusterCreate_InteractiveSummaryAndConfirm(t *testing.T) { + withTerminal(t) + + t.Run("confirmed", func(t *testing.T) { + api := newFakeAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), createArgs...) + if err != nil { + t.Fatal(err) + } + for _, want := range []string{"Create cluster prod", "pn-123", "sn-456", "10.100.0.0/16"} { + if !strings.Contains(errOut, want) { + t.Errorf("summary misses %q: %s", want, errOut) + } + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d", n) + } + }) + + t.Run("declined, Esc and Ctrl+C create nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "declined": {Prompter: tuitest.New().AddConfirm(false)}, + "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, + } { + api := newFakeAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + _, errOut, err := runMK8s(t, f, createArgs...) + if err != nil || !strings.Contains(errOut, canceledMsg) { + t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + } + if n := len(api.requestsMatching("POST")); n != 0 { + t.Errorf("%s: %d POST(s)", name, n) + } + } + }) + + t.Run("agent mode never prompts", func(t *testing.T) { + api := newFakeAPI() + // No prompter: a prompt would fail the test. + if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), createArgs...); err != nil { + t.Fatal(err) + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d", n) + } + }) + + t.Run("json output never prompts", func(t *testing.T) { + api := newFakeAPI() + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), createArgs...); err != nil { + t.Fatal(err) + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d", n) + } + }) +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_delete.go b/internal/verda-cli/cmd/mk8s/cluster_delete.go new file mode 100644 index 00000000..875f9a56 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_delete.go @@ -0,0 +1,107 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdClusterDelete creates `verda mk8s cluster delete`. +func NewCmdClusterDelete(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + var location string + var yes bool + cmd := &cobra.Command{ + Use: "delete []", + Aliases: []string{"rm"}, + Short: "Delete a cluster with its node pools and kubeconfigs", + Long: longDesc(` + Delete a cluster. After the API accepts the request, it deletes the + node pools, their worker VMs and the kubeconfigs in the background: + the cluster is gone from cluster list at once, but deleting the VMs + takes longer. Kubeconfigs that you downloaded stop working. The + private network and the subnet are kept. The CLI does not check for + node pools first, and has no --wait. + + Without , a terminal session shows a picker. + + Asks for confirmation on a terminal. --yes skips it and is required + in scripts, agent mode and -o json|yaml. + `), + Example: cmdutil.Examples(` + verda mk8s cluster delete prod + + # Without prompts + verda mk8s cluster delete prod -l FIN-03 --yes + verda --agent mk8s cluster delete prod --yes + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runClusterDelete(cmd, f, ioStreams, args, location, yes) + }, + } + addLocationFlag(cmd, &location) + cmd.Flags().BoolVarP(&yes, flagYes, "y", false, yesUsage) + return cmd +} + +func runClusterDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, args []string, location string, yes bool) error { + name := argOrEmpty(args) + if err := checkClusterArg(f, name, len(args) > 0, argCluster); err != nil { + return err + } + if err := checkConfirmed(f, yes, "delete"); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := resolveCluster(cmd, f, ioStreams, client, name, location) + if err != nil || target == nil { + return err + } + + if !yes { + ok, err := confirmWarning(cmd.Context(), ioStreams, f.Prompter(), + fmt.Sprintf("Deletes cluster %s, all its node pools, their worker VMs and its kubeconfigs.", target.Name), + []string{"Downloaded kubeconfigs stop working.", "This action cannot be undone."}, + fmt.Sprintf("Delete cluster %s (%s)?", target.Name, target.Location)) + if err != nil || !ok { + return err + } + } + + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Delete cluster:", map[string]string{"name": target.Name, "location": target.Location}) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + err = cmdutil.RunWithSpinner(ctx, f.Status(), "Deleting cluster...", func(ctx context.Context) error { + return client.ManagedKubernetes.Delete(ctx, target.Location, target.Name) + }) + if err != nil { + return mapErr(err) + } + + result := ActionResult{Resource: kindCluster, Name: target.Name, Location: target.Location, Action: "delete", Status: statusAccepted} + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result); wrote { + return err + } + _, _ = fmt.Fprintln(ioStreams.Out, "Deletion accepted. Node pools and worker VMs are deleted in the background.") + return nil +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_delete_test.go b/internal/verda-cli/cmd/mk8s/cluster_delete_test.go new file mode 100644 index 00000000..22b5855d --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_delete_test.go @@ -0,0 +1,176 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "strings" + "testing" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestClusterDelete_YesMakesOneDeleteCall(t *testing.T) { + t.Parallel() + api := seededClusters() + api.addPool("prod", "workers", "active", 3, 3) + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "delete", "prod", "--yes") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Deletion accepted. Node pools and worker VMs are deleted in the background.") { + t.Errorf("stdout = %q", out) + } + dels := api.requestsMatching("DELETE") + if len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-03/prod" { + t.Fatalf("DELETE requests = %v", dels) + } + // No node pool check, no wait. + for _, p := range api.paths("GET") { + if strings.Contains(p, "/pools") { + t.Errorf("delete must not read node pools: %s", p) + } + } + if len(api.requestsMatching("POST"))+len(api.requestsMatching("PATCH")) != 0 { + t.Error("delete must not write anything else") + } +} + +func TestClusterDelete_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, seededClusters())), "cluster", "rm", "prod", "-l", "FIN-03", "--yes") + if err != nil { + t.Fatal(err) + } + got := decodeJSON[map[string]any](t, out) + want := map[string]any{"resource": "cluster", "name": "prod", "location": "FIN-03", "action": "delete", "status": "accepted"} + for k, v := range want { + if got[k] != v { + t.Errorf("result[%s] = %v, want %v", k, got[k], v) + } + } + for _, k := range []string{"cluster", "id"} { + if _, has := got[k]; has { + t.Errorf("%q must be absent for a cluster: %v", k, got) + } + } +} + +func TestClusterDelete_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { + return tableFactory(newFakeClient(t, api), nil) + }, + } { + api := seededClusters() + _, _, err := runMK8s(t, mk(api, t), "cluster", "delete", "prod") + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { + t.Errorf("%s: err = %v", name, err) + } else if !strings.Contains(ae.Message, "without a terminal, in agent mode or with -o json|yaml") { + t.Errorf("%s: the message must name every cause: %q", name, ae.Message) + } + if n := len(api.requests); n != 0 { + t.Errorf("%s: %d request(s) before the check", name, n) + } + } +} + +func TestClusterDelete_MissingCluster(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, seededClusters())), "cluster", "delete", "--yes") + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } +} + +func TestClusterDelete_Interactive(t *testing.T) { + withTerminal(t) + + t.Run("the warning names pools, worker VMs and kubeconfigs", func(t *testing.T) { + api := seededClusters() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), "cluster", "delete", "prod") + if err != nil { + t.Fatal(err) + } + for _, want := range []string{"node pools", "worker VMs", "kubeconfigs", "Downloaded kubeconfigs stop working", "cannot be undone"} { + if !strings.Contains(errOut, want) { + t.Errorf("warning misses %q: %s", want, errOut) + } + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("DELETEs = %d", n) + } + }) + + t.Run("picker then confirm deletes the chosen cluster", func(t *testing.T) { + api := seededClusters() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddSelect(1).AddConfirm(true)), "cluster", "delete"); err != nil { + t.Fatal(err) + } + dels := api.requestsMatching("DELETE") + if len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-01/staging" { + t.Errorf("DELETE requests = %v", dels) + } + }) + + t.Run("declined, Esc and Ctrl+C delete nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "declined": {Prompter: tuitest.New().AddConfirm(false)}, + "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, + } { + api := seededClusters() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + _, errOut, err := runMK8s(t, f, "cluster", "delete", "prod") + if err != nil || !strings.Contains(errOut, canceledMsg) { + t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + } + if n := len(api.requestsMatching("DELETE")); n != 0 { + t.Errorf("%s: %d DELETE(s)", name, n) + } + } + }) + + t.Run("--yes skips the confirmation", func(t *testing.T) { + api := seededClusters() + // No prompter answers: a confirm would fail the test. + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), "cluster", "delete", "prod", "--yes"); err != nil { + t.Fatal(err) + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("DELETEs = %d", n) + } + }) +} + +func TestClusterDelete_Errors(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "delete", "ghost", "--yes") + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } + + api := newFakeAPI() + api.disabled = true + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "delete", "prod", "-l", "FIN-03", "--yes") + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Errorf("not enabled: %v", err) + } +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_describe.go b/internal/verda-cli/cmd/mk8s/cluster_describe.go new file mode 100644 index 00000000..c8e6b8b8 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_describe.go @@ -0,0 +1,153 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "io" + "text/tabwriter" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdClusterDescribe creates `verda mk8s cluster describe`. +func NewCmdClusterDescribe(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + var location string + cmd := &cobra.Command{ + Use: "describe []", + Aliases: []string{"get", "show"}, + Short: "Show a cluster and its node pools", + Long: longDesc(` + Show a cluster: its version, phase, endpoint, network, conditions + and a summary of its node pools. If the node pools cannot be + loaded, the cluster is still shown, with a warning; in JSON and + YAML, node_pools is then null and node_pools_error says why. + + Without , a terminal session shows a picker. + `), + Example: cmdutil.Examples(` + # Pick a cluster from a list (terminal only) + verda mk8s cluster describe + + verda mk8s cluster describe prod + + # Give the location to skip the lookup; JSON for scripts + verda mk8s cluster describe prod -l FIN-03 -o json + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runClusterDescribe(cmd, f, ioStreams, args, location) + }, + } + addLocationFlag(cmd, &location) + return cmd +} + +func runClusterDescribe(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, args []string, location string) error { + name := argOrEmpty(args) + if err := checkClusterArg(f, name, len(args) > 0, argCluster); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := resolveCluster(cmd, f, ioStreams, client, name, location) + if err != nil || target == nil { + return err + } + + cluster, err := getCluster(cmd.Context(), f, client, target) + if err != nil { + return err + } + view := ClusterDescribeView{ClusterView: newClusterView(cluster)} + + poolCtx, poolCancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer poolCancel() + pools, poolErr := cmdutil.WithSpinner(poolCtx, f.Status(), "Loading node pools...", func(ctx context.Context) ([]verda.ManagedKubernetesNodePool, error) { + return client.ManagedKubernetes.ListNodePools(ctx, target.Location, target.Name) + }) + if poolErr != nil { + view.NodePoolsError = mapErr(poolErr).Error() + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Warning: could not load the node pools of cluster %s: %s\n", target.Name, view.NodePoolsError) + } else { + views := newPoolSummaryViews(pools) + view.NodePools = &views + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response: cluster:", view) + + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), view); wrote { + return err + } + return printClusterDescribe(ioStreams.Out, &view) +} + +func printClusterDescribe(w io.Writer, v *ClusterDescribeView) error { + tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + rows := [][2]string{ + {"Name", v.Name}, + {"Location", v.Location}, + {"Version", v.KubernetesVersion}, + {"Phase", v.Phase}, + {"Endpoint", cmdutil.TextColumn(v.ExternalEndpoint)}, + {"DNS suffix", cmdutil.TextColumn(v.DNSSuffix)}, + {"Network", v.NetworkID}, + {"Subnet", v.SubnetID}, + {"Pod CIDR", v.PodCIDR}, + {"Service CIDR", v.ServiceCIDR}, + {"Created", timeColumn(v.CreatedAt)}, + } + for _, r := range rows { + _, _ = fmt.Fprintf(tw, "%s:\t%s\n", r[0], r[1]) + } + if err := tw.Flush(); err != nil { + return err + } + + if len(v.Conditions) > 0 { + _, _ = fmt.Fprintln(w, "\nConditions:") + ctw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + _, _ = fmt.Fprintln(ctw, " TYPE\tSTATUS\tREASON\tMESSAGE") + for i := range v.Conditions { + c := &v.Conditions[i] + _, _ = fmt.Fprintf(ctw, " %s\t%s\t%s\t%s\n", c.Type, c.Status, cmdutil.TextColumn(c.Reason), cmdutil.TextColumn(c.Message)) + } + if err := ctw.Flush(); err != nil { + return err + } + } + + switch { + case v.NodePools == nil: + _, _ = fmt.Fprintln(w, "\nNode pools: unavailable (see the warning).") + case len(*v.NodePools) == 0: + _, _ = fmt.Fprintln(w, "\nNode pools: none. Add one with: verda mk8s node-pool create --cluster "+v.Name) + default: + _, _ = fmt.Fprintln(w, "\nNode pools:") + ptw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + _, _ = fmt.Fprintln(ptw, " NAME\tINSTANCE TYPE\tREADY/DESIRED\tSTATE") + for i := range *v.NodePools { + p := &(*v.NodePools)[i] + _, _ = fmt.Fprintf(ptw, " %s\t%s\t%d/%d\t%s\n", p.Name, p.InstanceType, p.ReadyReplicas, p.Replicas, p.State) + } + return ptw.Flush() + } + return nil +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_describe_test.go b/internal/verda-cli/cmd/mk8s/cluster_describe_test.go new file mode 100644 index 00000000..fe5cf110 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_describe_test.go @@ -0,0 +1,271 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "reflect" + "regexp" + "slices" + "strings" + "testing" + + "go.yaml.in/yaml/v3" +) + +func describedAPI() *fakeAPI { + api := seededClusters() + api.clusters[0].Conditions = append(api.clusters[0].Conditions, conditionReady()) + api.addPool("prod", "workers", "active", 3, 2) + return api +} + +func TestClusterDescribe_Table(t *testing.T) { + t.Parallel() + api := describedAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod") + if err != nil { + t.Fatal(err) + } + for _, want := range []string{ + "Name:", "prod", "Location:", "FIN-03", "Version:", "1.37.0", "Phase:", "running", + "https://prod.example.test:6443", "pn-1", "sn-1", "10.100.0.0/16", "10.200.0.0/16", + "Conditions:", "TYPE", "Ready", "Node pools:", "INSTANCE TYPE", "READY/DESIRED", "workers", "2/3", "active", + } { + if !strings.Contains(out, want) { + t.Errorf("output misses %q:\n%s", want, out) + } + } +} + +func TestClusterDescribe_OneListThenGetThenPools(t *testing.T) { + t.Parallel() + api := describedAPI() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod"); err != nil { + t.Fatal(err) + } + want := []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod", "/mk8sclusters/FIN-03/prod/pools"} + if got := api.paths("GET"); !slices.Equal(got, want) { + t.Errorf("requests = %v, want %v", got, want) + } +} + +func TestClusterDescribe_Structured(t *testing.T) { + t.Parallel() + for name, f := range map[string]func(t *testing.T, api *fakeAPI) (string, error){ + "json": func(t *testing.T, api *fakeAPI) (string, error) { + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "cluster", "describe", "prod") + return out, err + }, + "agent": func(t *testing.T, api *fakeAPI) (string, error) { + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "cluster", "describe", "prod") + return out, err + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + out, err := f(t, describedAPI()) + if err != nil { + t.Fatal(err) + } + got := decodeJSON[map[string]any](t, out) + if got["name"] != "prod" || got["phase"] != "running" { + t.Errorf("cluster fields = %v", got) + } + pools, ok := got["node_pools"].([]any) + if !ok || len(pools) != 1 { + t.Fatalf("node_pools = %v", got["node_pools"]) + } + if p := pools[0].(map[string]any); p["name"] != "workers" || p["ready_replicas"] != float64(2) || p["replicas"] != float64(3) { + t.Errorf("pool = %v", p) + } + if _, has := got["node_pools_error"]; has { + t.Error("node_pools_error must be absent when the pool call worked") + } + if conds, _ := got["conditions"].([]any); len(conds) != 1 { + t.Errorf("conditions = %v", got["conditions"]) + } + }) + } +} + +func TestClusterDescribe_PoolCallFailsStillPrintsCluster(t *testing.T) { + t.Parallel() + newAPI := func() *fakeAPI { + api := describedAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/pools"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"pools unavailable"}`} + return api + } + + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, newAPI()), nil), "cluster", "describe", "prod", "-l", "FIN-03") + if err != nil { + t.Fatalf("the cluster must still print: %v", err) + } + if !strings.Contains(out, "prod") || !strings.Contains(out, "Node pools: unavailable") { + t.Errorf("stdout = %q", out) + } + if !strings.Contains(errOut, "Warning: could not load the node pools of cluster prod") { + t.Errorf("stderr = %q", errOut) + } + + out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, newAPI())), "cluster", "describe", "prod", "-l", "FIN-03") + if err != nil { + t.Fatal(err) + } + got := decodeJSON[map[string]any](t, out) + if v, present := got["node_pools"]; !present || v != nil { + t.Errorf("node_pools = %v, want null", v) + } + if msg, _ := got["node_pools_error"].(string); !strings.Contains(msg, "pools unavailable") { + t.Errorf("node_pools_error = %q", msg) + } +} + +func TestClusterDescribe_NoPools(t *testing.T) { + t.Parallel() + api := seededClusters() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "staging") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Node pools: none") || !strings.Contains(out, "node-pool create --cluster staging") { + t.Errorf("output = %s", out) + } + out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api)), "cluster", "describe", "staging") + if err != nil { + t.Fatal(err) + } + if pools, ok := decodeJSON[map[string]any](t, out)["node_pools"].([]any); !ok || len(pools) != 0 { + t.Errorf("no pools must be [], got %v", pools) + } +} + +func TestClusterDescribe_Aliases(t *testing.T) { + t.Parallel() + client := newFakeClient(t, seededClusters()) + for _, alias := range []string{"get", "show"} { + if _, _, err := runMK8s(t, tableFactory(client, nil), "cluster", alias, "prod"); err != nil { + t.Errorf("alias %s: %v", alias, err) + } + } +} + +// wantDescribed is describedAPI's "prod" cluster. Every field has its own value, so a +// swapped or dropped field fails. +func wantDescribed() ClusterDescribeView { + return ClusterDescribeView{ + ClusterView: ClusterView{ + Name: "prod", Location: "FIN-03", KubernetesVersion: "1.37.0", Phase: "running", + ExternalEndpoint: "https://prod.example.test:6443", NetworkID: "pn-1", SubnetID: "sn-1", + PodCIDR: "10.100.0.0/16", ServiceCIDR: "10.200.0.0/16", DNSSuffix: "", + Conditions: []ConditionView{{Type: "Ready", Status: "True", Reason: "AllGood", Message: "all components ready", LastTransitionTime: testCreated}}, + CreatedAt: testCreated, + }, + NodePools: &[]PoolSummaryView{{Name: "workers", InstanceType: "1V100.6V", ReadyReplicas: 2, Replicas: 3, State: "active"}}, + } +} + +func TestClusterDescribe_JSONHasEveryFieldInItsPlace(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, describedAPI())), "cluster", "describe", "prod") + if err != nil { + t.Fatal(err) + } + got := decodeJSON[ClusterDescribeView](t, out) + if want := wantDescribed(); !reflect.DeepEqual(got, want) { + t.Errorf("got %+v\nwant %+v", got, want) + } +} + +var describeLine = regexp.MustCompile(`^([A-Za-z][A-Za-z ]*):\s+(\S.*)$`) + +func TestClusterDescribe_TableLabelsMatchValues(t *testing.T) { + t.Parallel() + api := describedAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod") + if err != nil { + t.Fatal(err) + } + got := map[string]string{} + for line := range strings.SplitSeq(out, "\n") { + if m := describeLine.FindStringSubmatch(line); m != nil { + got[m[1]] = strings.TrimSpace(m[2]) + } + } + want := map[string]string{ + "Name": "prod", "Location": "FIN-03", "Version": "1.37.0", "Phase": "running", + "Endpoint": "https://prod.example.test:6443", "DNS suffix": "-", "Network": "pn-1", "Subnet": "sn-1", + "Pod CIDR": "10.100.0.0/16", "Service CIDR": "10.200.0.0/16", "Created": "2026-11-19 09:30 UTC", + } + for k, v := range want { + if got[k] != v { + t.Errorf("%s: = %q, want %q", k, got[k], v) + } + } + for line := range strings.SplitSeq(out, "\n") { + if strings.Contains(line, "Ready") && strings.Contains(line, "AllGood") { + if strings.Index(line, "AllGood") > strings.Index(line, "all components ready") { + t.Errorf("condition columns are REASON then MESSAGE: %q", line) + } + } + } +} + +func TestClusterDescribe_YAMLNullVersusEmptyPools(t *testing.T) { + t.Parallel() + read := func(api *fakeAPI) map[string]any { + out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, api)), "cluster", "describe", "prod", "-l", "FIN-03") + if err != nil { + t.Fatal(err) + } + var m map[string]any + if err := yaml.Unmarshal([]byte(out), &m); err != nil { + t.Fatal(err) + } + return m + } + + failing := describedAPI() + failing.failures["GET /mk8sclusters/{loc}/{name}/pools"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"down"}`} + got := read(failing) + if v, present := got["node_pools"]; !present || v != nil { + t.Errorf("a failed pool read must be null in YAML too, got %v", v) + } + if got["node_pools_error"] == nil || got["name"] != "prod" || got["pod_cidr"] != "10.100.0.0/16" { + t.Errorf("YAML = %v", got) + } + + empty := seededClusters() + empty.clusters[0].Name = "prod" + if pools, ok := read(empty)["node_pools"].([]any); !ok || len(pools) != 0 { + t.Errorf("no pools must be [], got %v", read(empty)["node_pools"]) + } + + if pools, ok := read(describedAPI())["node_pools"].([]any); !ok || len(pools) != 1 { + t.Errorf("pools = %v", pools) + } +} + +func TestClusterDescribe_Debug(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, describedAPI()), nil) + f.DebugOverride = true + _, errOut, err := runMK8s(t, f, "cluster", "describe", "prod") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "API response: cluster:") || !strings.Contains(errOut, `"pod_cidr": "10.100.0.0/16"`) { + t.Errorf("debug output = %q", errOut) + } +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_list.go b/internal/verda-cli/cmd/mk8s/cluster_list.go new file mode 100644 index 00000000..0b3a1f83 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_list.go @@ -0,0 +1,117 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "bytes" + "fmt" + "io" + "strings" + "text/tabwriter" + "time" + "unicode/utf8" + + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdClusterList creates `verda mk8s cluster list`. +func NewCmdClusterList(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + return &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: "List clusters", + Long: longDesc(` + List the managed Kubernetes clusters in your project, in every + location. + `), + Example: cmdutil.Examples(` + verda mk8s cluster list + + # Names only, for scripts + verda mk8s cluster ls -o json | jq -r '.[].name' + `), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + return runClusterList(cmd, f, ioStreams) + }, + } +} + +func runClusterList(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams) error { + client, err := f.VerdaClient() + if err != nil { + return err + } + clusters, err := listClusters(cmd.Context(), f, client) + if err != nil { + return err + } + views := newClusterViews(clusters) + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), fmt.Sprintf("API response: %d cluster(s):", len(views)), views) + + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), views); wrote { + return err + } + if len(views) == 0 { + printNoClusters(ioStreams) + return nil + } + + now := time.Now() + for _, withEndpoint := range []bool{true, false} { + var buf bytes.Buffer + if err := writeClusterTable(&buf, views, now, withEndpoint); err != nil { + return err + } + if !withEndpoint || widestLine(buf.String()) <= clusterTableWidth { + _, err := ioStreams.Out.Write(buf.Bytes()) + return err + } + } + return nil +} + +// clusterTableWidth is the terminal width the table must fit in. The +// endpoint is long (it holds the project ID), so the column is dropped when the +// rows would wrap; list -o json and describe always show it. +const clusterTableWidth = 120 + +func writeClusterTable(w io.Writer, views []ClusterView, now time.Time, withEndpoint bool) error { + tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + header := "NAME\tLOCATION\tVERSION\tPHASE\t" + if withEndpoint { + header += "ENDPOINT\t" + } + _, _ = fmt.Fprintln(tw, header+"AGE") + for i := range views { + v := &views[i] + row := fmt.Sprintf("%s\t%s\t%s\t%s\t", v.Name, v.Location, v.KubernetesVersion, v.Phase) + if withEndpoint { + row += cmdutil.TextColumn(strings.TrimPrefix(v.ExternalEndpoint, "https://")) + "\t" + } + _, _ = fmt.Fprintln(tw, row+ageColumn(v.CreatedAt, now)) + } + return tw.Flush() +} + +func widestLine(s string) int { + widest := 0 + for line := range strings.SplitSeq(s, "\n") { + widest = max(widest, utf8.RuneCountInString(line)) + } + return widest +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_list_test.go b/internal/verda-cli/cmd/mk8s/cluster_list_test.go new file mode 100644 index 00000000..c60cb0d7 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_list_test.go @@ -0,0 +1,193 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "strings" + "testing" + "time" + + "go.yaml.in/yaml/v3" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func seededClusters() *fakeAPI { + api := newFakeAPI() + api.addCluster("prod", "FIN-03", "running") + api.addCluster("staging", "FIN-01", "provisioning") + return api +} + +func TestClusterList_Table(t *testing.T) { + t.Parallel() + api := seededClusters() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "list") + if err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(out), "\n") + if len(lines) != 3 { + t.Fatalf("want a header and 2 rows, got:\n%s", out) + } + if got := strings.Join(strings.Fields(lines[0]), " "); got != "NAME LOCATION VERSION PHASE ENDPOINT AGE" { + t.Errorf("header = %q", got) + } + prod := strings.Fields(lines[1]) + if prod[0] != "prod" || prod[1] != "FIN-03" || prod[2] != "1.37.0" || prod[3] != "running" || prod[4] != "prod.example.test:6443" { + t.Errorf("prod row = %v", prod) + } + if staging := strings.Fields(lines[2]); staging[3] != "provisioning" || staging[4] != "-" { + t.Errorf("a cluster without an endpoint shows '-': %v", staging) + } + if got := api.paths("GET"); len(got) != 1 || got[0] != "/mk8sclusters" { + t.Errorf("want one List call, got %v", got) + } + if len(api.requestsMatching("POST"))+len(api.requestsMatching("DELETE"))+len(api.requestsMatching("PATCH")) != 0 { + t.Error("list must not write") + } +} + +func TestClusterList_LongEndpointsDropTheColumnToFit(t *testing.T) { + t.Parallel() + api := seededClusters() + api.clusters[0].ExternalEndpoint = "https://prod.project-824e2c70-bfce-4227-9ca4-369fc72d9280.fin03.mk8s.example.test:9443" + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "list") + if err != nil { + t.Fatal(err) + } + if strings.Contains(out, "ENDPOINT") || strings.Contains(out, "mk8s.example.test") { + t.Errorf("the endpoint column must be dropped when rows exceed %d columns:\n%s", clusterTableWidth, out) + } + if got := widestLine(strings.TrimRight(out, "\n")); got > clusterTableWidth { + t.Errorf("widest line = %d", got) + } + jsonOut, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "cluster", "list") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(jsonOut, "https://prod.project-824e2c70") { + t.Errorf("JSON keeps the full endpoint:\n%s", jsonOut) + } +} + +func TestClusterList_Structured(t *testing.T) { + t.Parallel() + wantKeys := []string{ + "name", "location", "kubernetes_version", "phase", "external_endpoint", "network", "subnet", + "pod_cidr", "service_cidr", "dns_suffix", "conditions", "created_at", + } + for name, mk := range map[string]func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory{ + "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, mk(seededClusters(), t), "cluster", "list") + if err != nil { + t.Fatal(err) + } + got := decodeJSON[[]map[string]any](t, out) + if len(got) != 2 || got[0]["name"] != "prod" || got[1]["phase"] != "provisioning" { + t.Fatalf("got %v", got) + } + for _, k := range wantKeys { + if _, ok := got[0][k]; !ok { + t.Errorf("JSON misses %q", k) + } + } + if c, ok := got[0]["conditions"].([]any); !ok || len(c) != 0 { + t.Errorf("conditions = %v, want []", got[0]["conditions"]) + } + }) + } + + t.Run("yaml", func(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, seededClusters())), "cluster", "list") + if err != nil { + t.Fatal(err) + } + var got []map[string]any + if err := yaml.Unmarshal([]byte(out), &got); err != nil { + t.Fatal(err) + } + if len(got) != 2 || got[0]["kubernetes_version"] != "1.37.0" { + t.Errorf("got %v", got) + } + }) +} + +func TestClusterList_Empty(t *testing.T) { + t.Parallel() + client := newFakeClient(t, newFakeAPI()) + + out, errOut, err := runMK8s(t, tableFactory(client, nil), "cluster", "list") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "No clusters found.") || !strings.Contains(errOut, "verda mk8s cluster create") { + t.Errorf("stdout=%q stderr=%q", out, errOut) + } + + out, _, err = runMK8s(t, jsonFactory(client), "cluster", "list") + if err != nil { + t.Fatal(err) + } + if strings.TrimSpace(out) != "[]" { + t.Errorf("empty JSON = %q, want []", out) + } +} + +func TestClusterList_Debug(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, seededClusters()), nil) + f.DebugOverride = true + _, errOut, err := runMK8s(t, f, "cluster", "list") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "API response: 2 cluster(s):") || !strings.Contains(errOut, `"kubernetes_version": "1.37.0"`) { + t.Errorf("debug output = %q", errOut) + } +} + +func TestClusterList_NotEnabled(t *testing.T) { + t.Parallel() + api := newFakeAPI() + api.disabled = true + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "list") + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Fatalf("err = %v", err) + } +} + +func TestAgeColumn(t *testing.T) { + t.Parallel() + now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC) + for _, c := range []struct { + ago time.Duration + want string + }{ + {30 * time.Second, "30s"}, {5 * time.Minute, "5m"}, {3 * time.Hour, "3h"}, {49 * time.Hour, "2d"}, {-time.Minute, "0s"}, + } { + if got := ageColumn(now.Add(-c.ago), now); got != c.want { + t.Errorf("ageColumn(%v ago) = %q, want %q", c.ago, got, c.want) + } + } + if got := ageColumn(time.Time{}, now); got != "-" { + t.Errorf("zero time = %q", got) + } +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_upgrade.go b/internal/verda-cli/cmd/mk8s/cluster_upgrade.go new file mode 100644 index 00000000..a92f483f --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_upgrade.go @@ -0,0 +1,175 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "strings" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + "github.com/verda-cloud/verda-cli/pkg/tui" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +type clusterUpgradeOptions struct { + Location string + Version string + Yes bool +} + +// ClusterUpgradeResult is the structured output of `cluster upgrade`. +type ClusterUpgradeResult struct { + Status string `json:"status" yaml:"status"` + PreviousVersion string `json:"previous_version" yaml:"previous_version"` + Cluster ClusterView `json:"cluster" yaml:"cluster"` +} + +// NewCmdClusterUpgrade creates `verda mk8s cluster upgrade`. +func NewCmdClusterUpgrade(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + opts := &clusterUpgradeOptions{} + cmd := &cobra.Command{ + Use: "upgrade []", + Short: "Upgrade a cluster to a new Kubernetes version", + Long: longDesc(` + Change the Kubernetes version of a cluster. The API runs the change + as a rolling update. The command returns once the API accepts the + request. It has no --wait: the API reports the requested version, + not the running one, so the CLI cannot tell when the rollout ends. + Follow the rollout with cluster describe. + + Without , a terminal session shows a picker. Without + --kubernetes-version, it shows the current version and asks for + the new one; in scripts, agent mode and -o json|yaml the flag is + required. + + Asks for confirmation on a terminal. --yes skips it and is required + in scripts, agent mode and -o json|yaml. + `), + Example: cmdutil.Examples(` + # Pick the cluster and type the version (terminal only) + verda mk8s cluster upgrade + + verda mk8s cluster upgrade prod --kubernetes-version 1.38.0 + + # Without prompts + verda mk8s cluster upgrade prod -l FIN-03 --kubernetes-version 1.38.0 --yes + verda --agent mk8s cluster upgrade prod --kubernetes-version 1.38.0 --yes + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runClusterUpgrade(cmd, f, ioStreams, args, opts) + }, + } + addLocationFlag(cmd, &opts.Location) + cmd.Flags().StringVar(&opts.Version, flagKubernetesVersion, "", "New Kubernetes version, for example 1.38.0 (the API checks it)") + cmd.Flags().BoolVarP(&opts.Yes, flagYes, "y", false, yesUsage) + return cmd +} + +func runClusterUpgrade(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, args []string, opts *clusterUpgradeOptions) error { + name := argOrEmpty(args) + opts.Version = strings.TrimSpace(opts.Version) + if missing := upgradeMissing(f, name, len(args) > 0, opts.Version); len(missing) > 0 { + return cmdutil.NewMissingFlagsError(missing) + } + if err := checkConfirmed(f, opts.Yes, "upgrade"); err != nil { + return err + } + + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := resolveCluster(cmd, f, ioStreams, client, name, opts.Location) + if err != nil || target == nil { + return err + } + + current, err := getCluster(cmd.Context(), f, client, target) + if err != nil { + return err + } + + if opts.Version == "" { + version, ok, err := askVersion(cmd.Context(), f, ioStreams, current) + if err != nil || !ok { + return err + } + opts.Version = version + } + if !opts.Yes { + ok, err := confirmWarning(cmd.Context(), ioStreams, f.Prompter(), + fmt.Sprintf("Rolling upgrade from %s to %s", current.KubernetesVersion, opts.Version), + []string{fmt.Sprintf("The API upgrades cluster %s in %s as a rolling update.", current.Name, current.Location)}, + fmt.Sprintf("Upgrade cluster %s to %s?", current.Name, opts.Version)) + if err != nil || !ok { + return err + } + } + + req := verda.UpdateManagedKubernetesClusterRequest{KubernetesVersion: opts.Version} + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + updated, err := cmdutil.WithSpinner(ctx, f.Status(), "Upgrading cluster...", func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { + return client.ManagedKubernetes.Update(ctx, current.Location, current.Name, req) + }) + if err != nil { + return mapErr(err) + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response:", newClusterView(updated)) + + result := ClusterUpgradeResult{Status: statusAccepted, PreviousVersion: current.KubernetesVersion, Cluster: newClusterView(updated)} + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result); wrote { + return err + } + _, _ = fmt.Fprintf(ioStreams.Out, "Upgrade of cluster %s from %s to %s accepted (%s).\n", + result.Cluster.Name, result.PreviousVersion, opts.Version, result.Cluster.Location) + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Follow the rollout with: %s\n", describeCmd(result.Cluster.Name, result.Cluster.Location)) + _, _ = fmt.Fprintln(ioStreams.ErrOut, "`describe` may show `degraded` while the rollout runs.") + return nil +} + +// upgradeMissing lists the arguments an upgrade needs that were not given; +// without a terminal that includes the version, which a person is asked for. +func upgradeMissing(f cmdutil.Factory, name string, given bool, version string) []string { + var missing []string + if clusterArgMissing(f, name, given) { + missing = append(missing, argCluster) + } + if version == "" && !interactiveTTY(f) { + missing = append(missing, "--"+flagKubernetesVersion) + } + return missing +} + +// askVersion prompts for the new version. ok is false when the user canceled. +func askVersion(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, current *verda.ManagedKubernetesCluster) (version string, ok bool, err error) { + v, err := f.Prompter().TextInput(ctx, fmt.Sprintf("New Kubernetes version (cluster %s runs %s)", current.Name, current.KubernetesVersion), + tui.WithPlaceholder("1.38.0"), + tui.WithValidation(validateVersion)) + if err != nil { + if cmdutil.IsPromptCancel(err) { + printCanceled(ioStreams) + return "", false, nil + } + return "", false, err + } + return strings.TrimSpace(v), true, nil +} diff --git a/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go b/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go new file mode 100644 index 00000000..4f454765 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go @@ -0,0 +1,206 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "slices" + "strings" + "testing" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestClusterUpgrade_YesSendsTheNewVersion(t *testing.T) { + t.Parallel() + api := seededClusters() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), + "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0", "--yes") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Upgrade of cluster prod from 1.37.0 to 1.38.0 accepted (FIN-03).") { + t.Errorf("stdout = %q", out) + } + for _, want := range []string{"verda mk8s cluster describe prod -l FIN-03", "`describe` may show `degraded` while the rollout runs."} { + if !strings.Contains(errOut, want) { + t.Errorf("stderr misses %q: %q", want, errOut) + } + } + + patches := api.requestsMatching("PATCH") + if len(patches) != 1 || patches[0].Path != "/mk8sclusters/FIN-03/prod" { + t.Fatalf("PATCH requests = %v", patches) + } + if body := decodeJSON[map[string]string](t, patches[0].Body); len(body) != 1 || body["kubernetes_version"] != "1.38.0" { + t.Errorf("body = %v", body) + } + // Name lookup, then the read for the "from" version, then the update. No wait. + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod"}) { + t.Errorf("reads = %v", got) + } +} + +func TestClusterUpgrade_VersionIsTrimmed(t *testing.T) { + t.Parallel() + api := seededClusters() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "upgrade", "prod", "--kubernetes-version", " 1.38.0 ", "--yes"); err != nil { + t.Fatal(err) + } + if body := decodeJSON[map[string]string](t, api.requestsMatching("PATCH")[0].Body); body["kubernetes_version"] != "1.38.0" { + t.Errorf("body = %v", body) + } +} + +func TestClusterUpgrade_Structured(t *testing.T) { + t.Parallel() + for name, f := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, f(seededClusters(), t), "cluster", "upgrade", "prod", "-l", "FIN-03", "--kubernetes-version", "1.38.0", "--yes") + if err != nil { + t.Fatal(err) + } + got := decodeJSON[map[string]any](t, out) + if got["status"] != "accepted" || got["previous_version"] != "1.37.0" { + t.Errorf("result = %v", got) + } + if c, _ := got["cluster"].(map[string]any); c["kubernetes_version"] != "1.38.0" { + t.Errorf("cluster = %v", got["cluster"]) + } + }) + } +} + +func TestClusterUpgrade_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { + return tableFactory(newFakeClient(t, api), nil) + }, + } { + api := seededClusters() + _, _, err := runMK8s(t, mk(api, t), "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0") + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { + t.Errorf("%s: err = %v", name, err) + } + if n := len(api.requests); n != 0 { + t.Errorf("%s: %d request(s) before the check", name, n) + } + } +} + +func TestClusterUpgrade_MissingArguments(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, seededClusters())), "cluster", "upgrade", "--yes") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"", "--kubernetes-version"}) { + t.Errorf("missing = %v", missing) + } +} + +func TestClusterUpgrade_Interactive(t *testing.T) { + withTerminal(t) + + t.Run("confirm shows the rolling upgrade warning", func(t *testing.T) { + api := seededClusters() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), + "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "Rolling upgrade from 1.37.0 to 1.38.0") { + t.Errorf("stderr = %q", errOut) + } + if n := len(api.requestsMatching("PATCH")); n != 1 { + t.Errorf("PATCHes = %d", n) + } + }) + + t.Run("version is asked when the flag is missing", func(t *testing.T) { + api := seededClusters() + p := tuitest.New().AddTextInput("1.39.0").AddConfirm(true) + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "cluster", "upgrade", "prod"); err != nil { + t.Fatal(err) + } + if body := decodeJSON[map[string]string](t, api.requestsMatching("PATCH")[0].Body); body["kubernetes_version"] != "1.39.0" { + t.Errorf("body = %v", body) + } + }) + + t.Run("picker then confirm", func(t *testing.T) { + api := seededClusters() + p := tuitest.New().AddSelect(1).AddConfirm(true) + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "cluster", "upgrade", "--kubernetes-version", "1.38.0"); err != nil { + t.Fatal(err) + } + if got := api.requestsMatching("PATCH")[0].Path; got != "/mk8sclusters/FIN-01/staging" { + t.Errorf("PATCH path = %s", got) + } + }) + + t.Run("declined, Esc and Ctrl+C change nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "declined": {Prompter: tuitest.New().AddConfirm(false)}, + "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, + } { + api := seededClusters() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + _, errOut, err := runMK8s(t, f, "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0") + if err != nil || !strings.Contains(errOut, canceledMsg) { + t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + } + if n := len(api.requestsMatching("PATCH")); n != 0 { + t.Errorf("%s: %d PATCH(es)", name, n) + } + } + }) + + t.Run("canceling the version prompt changes nothing", func(t *testing.T) { + api := seededClusters() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = &textCancelPrompter{Prompter: tuitest.New()} + _, errOut, err := runMK8s(t, f, "cluster", "upgrade", "prod") + if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("PATCH")) != 0 { + t.Errorf("err=%v stderr=%q", err, errOut) + } + }) +} + +func TestClusterUpgrade_APIErrorsShownAsSent(t *testing.T) { + t.Parallel() + api := seededClusters() + api.failures["PATCH /mk8sclusters/{loc}/{name}"] = apiFailure{Status: 409, Body: `{"code":"conflict","message":"busy","details":"concurrent_modification"}`} + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0", "--yes") + if err == nil || !strings.Contains(err.Error(), "concurrent_modification") { + t.Fatalf("err = %v", err) + } + + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "upgrade", "ghost", "--kubernetes-version", "1.38.0", "--yes") + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } +} diff --git a/internal/verda-cli/cmd/mk8s/confirm.go b/internal/verda-cli/cmd/mk8s/confirm.go new file mode 100644 index 00000000..7c23d5e5 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/confirm.go @@ -0,0 +1,76 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + + "charm.land/lipgloss/v2" + + "github.com/verda-cloud/verda-cli/pkg/tui" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// confirmationRequired is CONFIRMATION_REQUIRED for every mk8s command. The +// cause is not always agent mode: a script, a non-terminal or -o json|yaml also cannot prompt. +func confirmationRequired(action string) *cmdutil.AgentError { + ae := cmdutil.NewConfirmationRequiredError(action) + ae.Message = fmt.Sprintf("destructive action %q requires --yes: there is no prompt in scripts, without a terminal, in agent mode or with -o json|yaml", action) + return ae +} + +// checkConfirmed: destructive commands (and cluster upgrade) need --yes unless +// a person at a terminal can confirm. It never prompts. +func checkConfirmed(f cmdutil.Factory, yes bool, action string) error { + if !yes && !interactiveTTY(f) { + return confirmationRequired(action) + } + return nil +} + +// confirmWarning prints a red warning with detail lines on stderr, then asks. +// It returns false, nil when the user declines or presses Esc or Ctrl+C, after +// printing "Canceled.". +func confirmWarning(ctx context.Context, ioStreams cmdutil.IOStreams, prompter tui.Prompter, heading string, detail []string, prompt string) (bool, error) { + warn := lipgloss.NewStyle().Foreground(lipgloss.Color("1")).Bold(true) + _, _ = fmt.Fprintf(ioStreams.ErrOut, "\n %s %s\n", warn.Render("⚠"), warn.Render(heading)) + for _, line := range detail { + _, _ = fmt.Fprintf(ioStreams.ErrOut, " %s\n", line) + } + _, _ = fmt.Fprintln(ioStreams.ErrOut) + + return askConfirm(ctx, ioStreams, prompter, prompt) +} + +// askConfirm asks yes/no. A no, Esc or Ctrl+C all give false, nil after printing "Canceled.". +func askConfirm(ctx context.Context, ioStreams cmdutil.IOStreams, prompter tui.Prompter, prompt string) (bool, error) { + ok, err := prompter.Confirm(ctx, prompt) + if err != nil { + if !cmdutil.IsPromptCancel(err) { + return false, err + } + ok = false + } + if !ok { + printCanceled(ioStreams) + } + return ok, nil +} + +func printCanceled(ioStreams cmdutil.IOStreams) { + _, _ = fmt.Fprintln(ioStreams.ErrOut, canceledMsg) +} diff --git a/internal/verda-cli/cmd/mk8s/errors.go b/internal/verda-cli/cmd/mk8s/errors.go new file mode 100644 index 00000000..0dd6e482 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/errors.go @@ -0,0 +1,126 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "fmt" + "io" + "maps" + "net" + "net/http" + "strings" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + reasonFeatureNotEnabled = "feature_not_enabled" + + notEnabledMessage = "Managed Kubernetes is not enabled for this project." +) + +// What a create makes, for the "may have been created" warning. +const ( + kindCluster = "cluster" + kindNodePool = "node pool" + kindKubeconfig = "kubeconfig" +) + +// SDK error texts that tell where a failed request stopped. The SDK wraps +// these errors as plain strings, so there is no type to match. +const ( + sdkAuthFailed = "failed to get authentication token" + sdkResponseUnread = "failed to read response body" + sdkResponseUnparsed = "failed to unmarshal response" +) + +// mapErr turns a 403 whose reason is feature_not_enabled into the "not enabled" +// message, still AUTH_ERROR (exit 3) through ClassifyError. It returns a plain +// APIError, not an AgentError: main prints AgentErrors as JSON even for humans. +// Every other error is shown as the API sent it. +func mapErr(err error) error { + if apiErr, ok := errors.AsType[*verda.APIError](err); ok && apiErr.StatusCode == http.StatusForbidden && + (strings.Contains(apiErr.Code, reasonFeatureNotEnabled) || strings.Contains(apiErr.Details, reasonFeatureNotEnabled)) { + return &verda.APIError{StatusCode: http.StatusForbidden, Code: reasonFeatureNotEnabled, Message: notEnabledMessage} + } + return err +} + +// createMayHaveSucceeded reports whether the API may have saved the object +// although the call failed: the request was sent and the answer is a 5xx, a +// 408, a timeout, a dropped or unreadable response. A 4xx (429 too) means +// nothing was created, and so does an error before the request left (a failed +// token fetch, no connection, a validation error). +func createMayHaveSucceeded(err error) bool { + if neverSent(err) { + return false + } + if apiErr, ok := errors.AsType[*verda.APIError](err); ok { + return apiErr.StatusCode >= http.StatusInternalServerError || apiErr.StatusCode == http.StatusRequestTimeout + } + var netErr net.Error + msg := err.Error() + return errors.Is(err, context.DeadlineExceeded) || + errors.Is(err, io.ErrUnexpectedEOF) || + errors.Is(err, io.EOF) || + errors.As(err, &netErr) || + strings.Contains(msg, sdkResponseUnread) || + strings.Contains(msg, sdkResponseUnparsed) +} + +// neverSent: the failure came before the create request reached the server. +func neverSent(err error) bool { + // A 5xx from the token endpoint is not a 5xx from the create. + if strings.Contains(err.Error(), sdkAuthFailed) { + return true + } + var opErr *net.OpError + return errors.As(err, &opErr) && opErr.Op == "dial" +} + +// createWarning is the "may have been created anyway" text. +func createWarning(kind, listCmd string) string { + w := fmt.Sprintf("The %s may have been created anyway. Before running create again, check `%s`.", kind, listCmd) + if kind == kindKubeconfig { + w += " Delete any kubeconfig you do not need with `verda mk8s kubeconfig delete`." + } + return w +} + +// createFailed returns the error of a failed create. When the create may have +// been saved anyway it adds the warning: in the agent error details, or on +// stderr for humans. Creates are never retried: the API can save an object and +// still fail the answer, and a retry would make a second one. +func createFailed(f cmdutil.Factory, ioStreams cmdutil.IOStreams, kind, listCmd string, err error) error { + err = mapErr(err) + if !createMayHaveSucceeded(err) { + return err + } + warning := createWarning(kind, listCmd) + if !f.AgentMode() { + _, _ = fmt.Fprintln(ioStreams.ErrOut, warning) + return err + } + ae := *cmdutil.ClassifyError(err) + details := make(map[string]any, len(ae.Details)+1) + maps.Copy(details, ae.Details) + details["warning"] = warning + ae.Details = details + return &ae +} diff --git a/internal/verda-cli/cmd/mk8s/errors_test.go b/internal/verda-cli/cmd/mk8s/errors_test.go new file mode 100644 index 00000000..b5f0498a --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/errors_test.go @@ -0,0 +1,270 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "bytes" + "context" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestMapErr_FeatureNotEnabled(t *testing.T) { + t.Parallel() + for name, e := range map[string]*verda.APIError{ + "details": {StatusCode: 403, Code: "forbidden", Message: "forbidden", Details: "feature_not_enabled"}, + "code": {StatusCode: 403, Code: "feature_not_enabled", Message: "forbidden"}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + ae := cmdutil.ClassifyError(mapErr(fmt.Errorf("wrapped: %w", e))) + if ae.Code != "AUTH_ERROR" || ae.ExitCode != cmdutil.ExitAuth { + t.Errorf("got %s exit %d", ae.Code, ae.ExitCode) + } + if ae.Message != notEnabledMessage { + t.Errorf("message = %q", ae.Message) + } + }) + } +} + +func TestMapErr_OtherErrorsUnchanged(t *testing.T) { + t.Parallel() + for name, err := range map[string]error{ + "other 403": &verda.APIError{StatusCode: 403, Message: "no access", Details: "something_else"}, + // Only the code and the details carry the reason, not free text. + "403 text": &verda.APIError{StatusCode: 403, Message: "reason: feature_not_enabled"}, + "404": &verda.APIError{StatusCode: 404, Message: "cluster not found"}, + "400": &verda.APIError{StatusCode: 400, Message: "bad", Details: "cluster_quota_exceeded"}, + "plain": errors.New("boom"), + } { + if got := mapErr(err); !errors.Is(got, err) && got != err { //nolint:errorlint // identity check on purpose + t.Errorf("%s: mapErr changed the error: %v", name, got) + } + } + // A 403 with the reason on a non-403 status is not the feature flag. + if got := mapErr(&verda.APIError{StatusCode: 400, Details: "feature_not_enabled"}); strings.Contains(got.Error(), "not enabled for this project") { + t.Error("only a 403 maps to the not-enabled message") + } +} + +type timeoutNetError struct{} + +func (timeoutNetError) Error() string { return "i/o timeout" } +func (timeoutNetError) Timeout() bool { return true } +func (timeoutNetError) Temporary() bool { return true } + +var _ net.Error = timeoutNetError{} + +func TestCreateMayHaveSucceeded(t *testing.T) { + t.Parallel() + yes := map[string]error{ + "500": &verda.APIError{StatusCode: 500, Message: "x"}, + "502": &verda.APIError{StatusCode: 502, Message: "x"}, + "503": &verda.APIError{StatusCode: 503, Message: "x"}, + "408": &verda.APIError{StatusCode: 408, Message: "x"}, + "deadline": fmt.Errorf("create: %w", context.DeadlineExceeded), + "network": fmt.Errorf("create: %w", timeoutNetError{}), + } + no := map[string]error{ + "400": &verda.APIError{StatusCode: 400, Message: "x"}, + "403": &verda.APIError{StatusCode: 403, Message: "x"}, + "409": &verda.APIError{StatusCode: 409, Message: "x"}, + "429": &verda.APIError{StatusCode: 429, Message: "x"}, + "sdk validation": &verda.ValidationError{Field: "name", Message: "bad"}, + "plain": errors.New("boom"), + } + for name, err := range yes { + if !createMayHaveSucceeded(err) { + t.Errorf("%s: want warning", name) + } + } + for name, err := range no { + if createMayHaveSucceeded(err) { + t.Errorf("%s: want no warning", name) + } + } +} + +// realClientError runs a request the way the SDK does (net/http) and returns +// the error it wraps, so these cases use errors the standard library produces, +// not hand-made ones. +func realClientError(t *testing.T, handler http.HandlerFunc) error { + t.Helper() + srv := httptest.NewServer(handler) + t.Cleanup(srv.Close) + resp, err := http.Post(srv.URL, "application/json", strings.NewReader("{}")) //nolint:noctx // test against a local server + if err != nil { + return fmt.Errorf("request failed: %w", err) + } + defer func() { _ = resp.Body.Close() }() + if _, err := io.ReadAll(resp.Body); err != nil { + return fmt.Errorf("failed to read response body: %w", err) + } + t.Fatal("the request was expected to fail") + return nil +} + +func TestCreateMayHaveSucceeded_RealNetworkErrors(t *testing.T) { + t.Parallel() + + t.Run("connection refused: nothing was sent", func(t *testing.T) { + t.Parallel() + ln, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + addr := ln.Addr().String() + _ = ln.Close() + resp, err := http.Post("http://"+addr, "application/json", strings.NewReader("{}")) //nolint:noctx // test against a closed port + if err == nil { + _ = resp.Body.Close() + t.Fatal("expected a dial error") + } + if createMayHaveSucceeded(fmt.Errorf("request failed: %w", err)) { + t.Errorf("no request was sent, but the warning would show: %v", err) + } + }) + + t.Run("response cut short after the headers", func(t *testing.T) { + t.Parallel() + err := realClientError(t, func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Length", "100") + _, _ = w.Write([]byte(`{"id":`)) + }) + if !createMayHaveSucceeded(err) { + t.Errorf("the server may have saved the object, but no warning: %v", err) + } + if !createMayHaveSucceeded(io.ErrUnexpectedEOF) || !createMayHaveSucceeded(io.EOF) { + t.Error("a bare EOF after the request was sent must warn") + } + }) + + t.Run("connection dropped before any response", func(t *testing.T) { + t.Parallel() + err := realClientError(t, func(w http.ResponseWriter, _ *http.Request) { + conn, _, hijackErr := w.(http.Hijacker).Hijack() + if hijackErr == nil { + _ = conn.Close() + } + }) + if !createMayHaveSucceeded(err) { + t.Errorf("the request reached the server, but no warning: %v", err) + } + }) +} + +func TestCreateMayHaveSucceeded_WhereTheRequestStopped(t *testing.T) { + t.Parallel() + yes := map[string]error{ + "canceled after sending": fmt.Errorf("request failed: %w", &url.Error{Op: "Post", URL: "https://x", Err: context.Canceled}), + "2xx body not readable": errors.New("failed to read response body: connection reset by peer"), + "2xx body not json": errors.New("failed to unmarshal response: invalid character '<'"), + } + no := map[string]error{ + "token endpoint 502": fmt.Errorf("failed to get authentication token: %w", &verda.APIError{StatusCode: 502, Message: "bad gateway"}), + "token endpoint dial": fmt.Errorf("failed to get authentication token: %w", &net.OpError{Op: "dial", Err: errors.New("refused")}), + "validation": errors.New("pod_cidr: must be a valid IPv4 CIDR"), + } + for name, err := range yes { + if !createMayHaveSucceeded(err) { + t.Errorf("%s: want warning", name) + } + } + for name, err := range no { + if createMayHaveSucceeded(err) { + t.Errorf("%s: want no warning", name) + } + } +} + +func TestCreateFailed_HumanWarnsOnStderr(t *testing.T) { + t.Parallel() + var out, errOut bytes.Buffer + io := cmdutil.IOStreams{Out: &out, ErrOut: &errOut} + apiErr := &verda.APIError{StatusCode: 502, Message: "bad gateway"} + + err := createFailed(&cmdutil.TestFactory{}, io, kindCluster, "verda mk8s cluster list", apiErr) + if !errors.Is(err, apiErr) { + t.Fatalf("the API error must be returned: %v", err) + } + if got := errOut.String(); !strings.Contains(got, "The cluster may have been created anyway.") || + !strings.Contains(got, "verda mk8s cluster list") { + t.Errorf("stderr = %q", got) + } + if out.Len() != 0 { + t.Errorf("stdout must stay empty: %q", out.String()) + } +} + +func TestCreateFailed_AgentWarnsInDetails(t *testing.T) { + t.Parallel() + var errOut bytes.Buffer + io := cmdutil.IOStreams{Out: &bytes.Buffer{}, ErrOut: &errOut} + apiErr := &verda.APIError{StatusCode: 500, Message: "boom"} + + err := createFailed(&cmdutil.TestFactory{AgentModeOverride: true}, io, kindKubeconfig, "verda mk8s kubeconfig list --cluster c", apiErr) + ae := cmdutil.ClassifyError(err) + if ae.Code != "API_ERROR" || ae.ExitCode != cmdutil.ExitAPI { + t.Errorf("got %s exit %d", ae.Code, ae.ExitCode) + } + w, _ := ae.Details["warning"].(string) + if !strings.Contains(w, "The kubeconfig may have been created anyway.") || !strings.Contains(w, "kubeconfig delete") { + t.Errorf("warning = %q", w) + } + if ae.Details["status"] != 500 { + t.Errorf("status detail lost: %v", ae.Details) + } + if errOut.Len() != 0 { + t.Errorf("agent mode reports through the error envelope only: %q", errOut.String()) + } +} + +func TestCreateFailed_4xxNoWarning(t *testing.T) { + t.Parallel() + for _, agent := range []bool{false, true} { + var errOut bytes.Buffer + io := cmdutil.IOStreams{Out: &bytes.Buffer{}, ErrOut: &errOut} + apiErr := &verda.APIError{StatusCode: 400, Message: "bad", Details: "cluster_quota_exceeded"} + + err := createFailed(&cmdutil.TestFactory{AgentModeOverride: agent}, io, kindNodePool, "verda mk8s node-pool list", apiErr) + if !errors.Is(err, apiErr) { + t.Errorf("agent=%v: the 4xx must come back as is: %v", agent, err) + } + if errOut.Len() != 0 { + t.Errorf("agent=%v: unexpected warning %q", agent, errOut.String()) + } + } +} + +func TestCreateFailed_FeatureNotEnabledIsMapped(t *testing.T) { + t.Parallel() + io := cmdutil.IOStreams{Out: &bytes.Buffer{}, ErrOut: &bytes.Buffer{}} + err := createFailed(&cmdutil.TestFactory{}, io, kindCluster, "x", &verda.APIError{StatusCode: 403, Details: "feature_not_enabled"}) + if ae := cmdutil.ClassifyError(err); ae.Code != "AUTH_ERROR" || !strings.Contains(ae.Message, "not enabled") { + t.Errorf("got %+v", ae) + } +} diff --git a/internal/verda-cli/cmd/mk8s/fake_test.go b/internal/verda-cli/cmd/mk8s/fake_test.go new file mode 100644 index 00000000..b720a9ea --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/fake_test.go @@ -0,0 +1,494 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "sync" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +var testCreated = time.Date(2026, 11, 19, 9, 30, 0, 0, time.UTC) + +type recordedRequest struct { + Method string + Path string + Body string +} + +// apiFailure makes a route answer with a fixed status and body. +type apiFailure struct { + Status int + Body string + // Times is how many requests fail before the route works again; 0 means always. + Times int +} + +// fakeAPI is an in-memory mk8s API. Pools and kubeconfigs are keyed by cluster name. +type fakeAPI struct { + mu sync.Mutex + locations []string + clusters []verda.ManagedKubernetesCluster + pools map[string][]verda.ManagedKubernetesNodePool + kubeconfigs map[string][]verda.ManagedKubernetesKubeconfig + // networks and subnets (keyed by network ID) serve the wizard's pickers. + networks []verda.PrivateNetwork + subnets map[string][]verda.Subnet + // availability lists instance types per location; sshKeys serve the pickers. + availability map[string][]string + sshKeys []verda.SSHKey + // onPoolGet runs before each pool GET answers, to move a pool through its + // states. It runs while the fake holds its lock: it must not call the + // fake's locking helpers (requestsMatching, paths); read a.requests directly. + onPoolGet func(p *verda.ManagedKubernetesNodePool) + // onKubeconfigGet runs before each kubeconfig GET answers (same locking rule as onPoolGet), + // to turn a pending kubeconfig ready and give it its YAML. + onKubeconfigGet func(k *verda.ManagedKubernetesKubeconfig) + requests []recordedRequest + // failures: route pattern (for example "POST /mk8sclusters") -> forced answer. + failures map[string]apiFailure + // disabled answers every route with 403 feature_not_enabled. + disabled bool + // onClusterGet runs before each cluster GET answers, so a test can move a + // cluster through its phases while a command waits. + onClusterGet func(c *verda.ManagedKubernetesCluster) + nextID int +} + +func newFakeAPI() *fakeAPI { + return &fakeAPI{ + locations: []string{"FIN-01", "FIN-03"}, + pools: map[string][]verda.ManagedKubernetesNodePool{}, + kubeconfigs: map[string][]verda.ManagedKubernetesKubeconfig{}, + subnets: map[string][]verda.Subnet{}, + availability: map[string][]string{}, + failures: map[string]apiFailure{}, + } +} + +func (a *fakeAPI) requestsMatching(method string) []recordedRequest { + a.mu.Lock() + defer a.mu.Unlock() + var out []recordedRequest + for _, r := range a.requests { + if r.Method == method { + out = append(out, r) + } + } + return out +} + +func (a *fakeAPI) id(prefix string) string { + a.nextID++ + return fmt.Sprintf("%s-%d", prefix, a.nextID) +} + +func writeJSON(w http.ResponseWriter, status int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(v) +} + +func notFound(w http.ResponseWriter, what string) { + writeJSON(w, http.StatusNotFound, map[string]string{"code": "not_found", "message": what + " not found"}) +} + +func (a *fakeAPI) findCluster(location, name string) int { + for i := range a.clusters { + if a.clusters[i].Name == name && (location == "" || a.clusters[i].Location == location) { + return i + } + } + return -1 +} + +func (a *fakeAPI) findPool(cluster, name string) int { + for i := range a.pools[cluster] { + if a.pools[cluster][i].Name == name { + return i + } + } + return -1 +} + +func (a *fakeAPI) findKubeconfig(cluster, id string) int { + for i := range a.kubeconfigs[cluster] { + if a.kubeconfigs[cluster][i].ID == id { + return i + } + } + return -1 +} + +// clusterOr404 resolves {location}/{name}; on a miss it answers 404 and returns -1. +func (a *fakeAPI) clusterOr404(w http.ResponseWriter, r *http.Request) int { + i := a.findCluster(r.PathValue("loc"), r.PathValue("name")) + if i < 0 { + notFound(w, "cluster") + } + return i +} + +func (a *fakeAPI) handler() http.Handler { + mux := http.NewServeMux() + route := func(pattern string, h http.HandlerFunc) { + mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { + if fl, ok := a.failures[pattern]; ok { + if fl.Times > 0 { + fl.Times-- + if fl.Times == 0 { + delete(a.failures, pattern) + } else { + a.failures[pattern] = fl + } + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(fl.Status) + _, _ = io.WriteString(w, fl.Body) + return + } + h(w, r) + }) + } + + mux.HandleFunc("POST /oauth2/token", func(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, http.StatusOK, map[string]string{"access_token": "test-token", "token_type": "Bearer"}) + }) + a.networkRoutes(route) + route("GET /mk8sclusters/enabled_locations", func(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, http.StatusOK, verda.ManagedKubernetesEnabledLocations{Locations: a.locations}) + }) + route("GET /mk8sclusters", func(w http.ResponseWriter, _ *http.Request) { + clusters := a.clusters + if clusters == nil { + clusters = []verda.ManagedKubernetesCluster{} + } + writeJSON(w, http.StatusOK, clusters) + }) + route("POST /mk8sclusters", a.createCluster) + route("GET /mk8sclusters/{loc}/{name}", func(w http.ResponseWriter, r *http.Request) { + if i := a.clusterOr404(w, r); i >= 0 { + if a.onClusterGet != nil { + a.onClusterGet(&a.clusters[i]) + } + writeJSON(w, http.StatusOK, a.clusters[i]) + } + }) + route("PATCH /mk8sclusters/{loc}/{name}", func(w http.ResponseWriter, r *http.Request) { + i := a.clusterOr404(w, r) + if i < 0 { + return + } + var req verda.UpdateManagedKubernetesClusterRequest + _ = json.NewDecoder(r.Body).Decode(&req) + a.clusters[i].KubernetesVersion = req.KubernetesVersion + writeJSON(w, http.StatusOK, a.clusters[i]) + }) + route("DELETE /mk8sclusters/{loc}/{name}", func(w http.ResponseWriter, r *http.Request) { + i := a.clusterOr404(w, r) + if i < 0 { + return + } + name := a.clusters[i].Name + a.clusters = append(a.clusters[:i], a.clusters[i+1:]...) + delete(a.pools, name) + delete(a.kubeconfigs, name) + w.WriteHeader(http.StatusAccepted) + }) + + route("GET /mk8sclusters/{loc}/{name}/pools", func(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + pools := a.pools[r.PathValue("name")] + if pools == nil { + pools = []verda.ManagedKubernetesNodePool{} + } + writeJSON(w, http.StatusOK, pools) + }) + route("POST /mk8sclusters/{loc}/{name}/pools", a.createPool) + route("GET /mk8sclusters/{loc}/{name}/pools/{pool}", func(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + i := a.findPool(r.PathValue("name"), r.PathValue("pool")) + if i < 0 { + notFound(w, "node pool") + return + } + if a.onPoolGet != nil { + a.onPoolGet(&a.pools[r.PathValue("name")][i]) + } + writeJSON(w, http.StatusOK, a.pools[r.PathValue("name")][i]) + }) + route("PATCH /mk8sclusters/{loc}/{name}/pools/{pool}", a.updatePool) + route("DELETE /mk8sclusters/{loc}/{name}/pools/{pool}", func(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + cluster := r.PathValue("name") + i := a.findPool(cluster, r.PathValue("pool")) + if i < 0 { + notFound(w, "node pool") + return + } + a.pools[cluster] = append(a.pools[cluster][:i], a.pools[cluster][i+1:]...) + w.WriteHeader(http.StatusAccepted) + }) + + a.kubeconfigRoutes(route) + + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + r.Body = io.NopCloser(bytes.NewReader(body)) + a.mu.Lock() + defer a.mu.Unlock() + if r.URL.Path != "/oauth2/token" { + a.requests = append(a.requests, recordedRequest{Method: r.Method, Path: r.URL.Path, Body: string(body)}) + if a.disabled { + writeJSON(w, http.StatusForbidden, map[string]string{"code": "forbidden", "message": "forbidden", "details": reasonFeatureNotEnabled}) + return + } + } + mux.ServeHTTP(w, r) + }) +} + +func (a *fakeAPI) createCluster(w http.ResponseWriter, r *http.Request) { + var req verda.CreateManagedKubernetesClusterRequest + _ = json.NewDecoder(r.Body).Decode(&req) + if a.findCluster("", req.Name) >= 0 { + writeJSON(w, http.StatusConflict, map[string]string{"code": "cluster_already_exists", "message": "cluster already exists"}) + return + } + c := verda.ManagedKubernetesCluster{ + Name: req.Name, KubernetesVersion: req.KubernetesVersion, Location: req.Location, + NetworkID: req.NetworkID, SubnetID: req.SubnetID, PodCIDR: req.PodCIDR, ServiceCIDR: req.ServiceCIDR, + Phase: verda.ManagedKubernetesClusterPhaseProvisioning, CreatedAt: testCreated, + } + a.clusters = append(a.clusters, c) + writeJSON(w, http.StatusCreated, c) +} + +func (a *fakeAPI) createPool(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + cluster := r.PathValue("name") + var req verda.CreateManagedKubernetesNodePoolRequest + _ = json.NewDecoder(r.Body).Decode(&req) + // Pool names are unique in the project, not only in the cluster. + for _, pools := range a.pools { + for i := range pools { + if pools[i].Name == req.Name { + writeJSON(w, http.StatusConflict, map[string]string{"code": "conflict", "message": "node pool already exists", "details": "pool_already_exists"}) + return + } + } + } + replicas := 1 + if req.Replicas != nil { + replicas = *req.Replicas + } + p := verda.ManagedKubernetesNodePool{ + Name: req.Name, Replicas: replicas, InstanceType: req.InstanceType, SSHKeyIDs: req.SSHKeyIDs, + State: verda.ManagedKubernetesNodePoolStateCreating, CreatedAt: testCreated, + } + a.pools[cluster] = append(a.pools[cluster], p) + writeJSON(w, http.StatusCreated, p) +} + +func (a *fakeAPI) updatePool(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + cluster := r.PathValue("name") + i := a.findPool(cluster, r.PathValue("pool")) + if i < 0 { + notFound(w, "node pool") + return + } + var req verda.UpdateManagedKubernetesNodePoolRequest + _ = json.NewDecoder(r.Body).Decode(&req) + if req.Replicas != nil { + a.pools[cluster][i].Replicas = *req.Replicas + } + writeJSON(w, http.StatusOK, a.pools[cluster][i]) +} + +// createKubeconfig answers pending with a server-chosen ID, as the API does. +func (a *fakeAPI) createKubeconfig(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + cluster := r.PathValue("name") + var req verda.CreateManagedKubernetesKubeconfigRequest + _ = json.NewDecoder(r.Body).Decode(&req) + k := verda.ManagedKubernetesKubeconfig{ + ID: a.id(cluster + "-kc"), DisplayName: req.DisplayName, CommonName: req.CommonName, + Groups: req.Groups, Status: verda.ManagedKubernetesKubeconfigStatusPending, + } + a.kubeconfigs[cluster] = append(a.kubeconfigs[cluster], k) + k.Kubeconfig = "" + writeJSON(w, http.StatusCreated, k) +} + +// newFakeClient serves api and returns an SDK client pointed at it. +func newFakeClient(t *testing.T, api *fakeAPI) *verda.Client { + t.Helper() + srv := httptest.NewServer(api.handler()) + t.Cleanup(srv.Close) + client, err := verda.NewClient( + verda.WithBaseURL(srv.URL), + verda.WithClientID("test-id"), + verda.WithClientSecret("test-secret"), + ) + if err != nil { + t.Fatalf("verda.NewClient: %v", err) + } + return client +} + +// Factory shapes for the modes the commands are tested in. +func tableFactory(client *verda.Client, p *tuitest.Prompter) *cmdutil.TestFactory { + f := &cmdutil.TestFactory{ClientOverride: client, OutputFormatOverride: outputTable} + if p != nil { + f.PrompterOverride = p + } + return f +} + +func jsonFactory(client *verda.Client) *cmdutil.TestFactory { + return &cmdutil.TestFactory{ClientOverride: client, OutputFormatOverride: "json"} +} + +func agentFactory(client *verda.Client) *cmdutil.TestFactory { + return &cmdutil.TestFactory{ClientOverride: client, AgentModeOverride: true, OutputFormatOverride: "json"} +} + +// runMK8s runs `verda mk8s ` and returns stdout and stderr. +func runMK8s(t *testing.T, f cmdutil.Factory, args ...string) (stdout, stderr string, err error) { + t.Helper() + var out, errOut bytes.Buffer + cmd := NewCmdMK8s(f, cmdutil.IOStreams{Out: &out, ErrOut: &errOut}) + cmd.SetArgs(args) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + err = cmd.ExecuteContext(context.Background()) + return out.String(), errOut.String(), err +} + +// withTerminal makes interactiveTTY see a terminal. Never call t.Parallel in or under a +// test that uses it, fastPoll or withWizardAnswers: they change package variables. +func withTerminal(t *testing.T) { + t.Helper() + orig := isInteractiveTerminal + isInteractiveTerminal = func() bool { return true } + t.Cleanup(func() { isInteractiveTerminal = orig }) +} + +type routeFunc func(pattern string, h http.HandlerFunc) + +// networkRoutes serve the private networks and subnets the wizard lists. +func (a *fakeAPI) networkRoutes(route routeFunc) { + route("GET /instance-availability", func(w http.ResponseWriter, r *http.Request) { + var out []verda.LocationAvailability + for loc, types := range a.availability { + if want := r.URL.Query().Get("location_code"); want == "" || want == loc { + out = append(out, verda.LocationAvailability{LocationCode: loc, Availabilities: types}) + } + } + if out == nil { + out = []verda.LocationAvailability{} + } + writeJSON(w, http.StatusOK, out) + }) + route("GET /ssh-keys", func(w http.ResponseWriter, _ *http.Request) { + keys := a.sshKeys + if keys == nil { + keys = []verda.SSHKey{} + } + writeJSON(w, http.StatusOK, keys) + }) + route("GET /private-networks", func(w http.ResponseWriter, _ *http.Request) { + networks := a.networks + if networks == nil { + networks = []verda.PrivateNetwork{} + } + writeJSON(w, http.StatusOK, networks) + }) + route("GET /private-networks/{id}/subnets", func(w http.ResponseWriter, r *http.Request) { + subnets := a.subnets[r.PathValue("id")] + if subnets == nil { + subnets = []verda.Subnet{} + } + writeJSON(w, http.StatusOK, subnets) + }) +} + +func (a *fakeAPI) kubeconfigRoutes(route routeFunc) { + route("GET /mk8sclusters/{loc}/{name}/kubeconfigs", func(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + list := make([]verda.ManagedKubernetesKubeconfig, 0, len(a.kubeconfigs[r.PathValue("name")])) + for _, k := range a.kubeconfigs[r.PathValue("name")] { + k.Kubeconfig = "" // the list never carries the YAML + list = append(list, k) + } + writeJSON(w, http.StatusOK, list) + }) + route("POST /mk8sclusters/{loc}/{name}/kubeconfigs", a.createKubeconfig) + route("GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}", func(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + i := a.findKubeconfig(r.PathValue("name"), r.PathValue("id")) + if i < 0 { + notFound(w, "kubeconfig") + return + } + if a.onKubeconfigGet != nil { + a.onKubeconfigGet(&a.kubeconfigs[r.PathValue("name")][i]) + } + writeJSON(w, http.StatusOK, a.kubeconfigs[r.PathValue("name")][i]) + }) + route("DELETE /mk8sclusters/{loc}/{name}/kubeconfigs/{id}", func(w http.ResponseWriter, r *http.Request) { + if a.clusterOr404(w, r) < 0 { + return + } + cluster := r.PathValue("name") + i := a.findKubeconfig(cluster, r.PathValue("id")) + if i < 0 { + notFound(w, "kubeconfig") + return + } + a.kubeconfigs[cluster] = append(a.kubeconfigs[cluster][:i], a.kubeconfigs[cluster][i+1:]...) + w.WriteHeader(http.StatusNoContent) + }) +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig.go b/internal/verda-cli/cmd/mk8s/kubeconfig.go new file mode 100644 index 00000000..dee2f3fa --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig.go @@ -0,0 +1,57 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdKubeconfig creates the `verda mk8s kubeconfig` parent command. +func NewCmdKubeconfig(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "kubeconfig", + Short: "Manage the kubeconfigs of a cluster", + Long: longDesc(` + Create, save, list, show and delete the kubeconfigs of a cluster. + A kubeconfig holds a client certificate that kubectl uses to reach + the cluster. Its YAML holds a private key: the CLI writes it only to + a file with mode 0600, or to stdout with --file -, and never prints + it in structured output or --debug. + + Kubeconfigs are named by an ID that the API chooses (see kubeconfig + list). Deleting a kubeconfig does not revoke the copies already + downloaded: they keep working until they expire. + `), + Example: cmdutil.Examples(` + # Admin access for alice, then use it with kubectl + verda mk8s kubeconfig create --cluster prod --common-name alice --admin + export KUBECONFIG=~/.kube/verda-prod.alice.yaml + kubectl get nodes + + verda mk8s kubeconfig list --cluster prod + `), + Run: cmdutil.DefaultSubCommandRun(ioStreams.Out), + } + cmd.AddCommand( + NewCmdKubeconfigList(f, ioStreams), + NewCmdKubeconfigDescribe(f, ioStreams), + NewCmdKubeconfigCreate(f, ioStreams), + NewCmdKubeconfigSave(f, ioStreams), + NewCmdKubeconfigDelete(f, ioStreams), + ) + return cmd +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_create.go b/internal/verda-cli/cmd/mk8s/kubeconfig_create.go new file mode 100644 index 00000000..02c6ae2c --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_create.go @@ -0,0 +1,336 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "fmt" + "maps" + "slices" + "strings" + "time" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + flagCommonName = "common-name" + flagAdmin = "admin" + flagGroup = "group" + flagDisplayName = "display-name" + flagDuration = "duration" + + // groupAdmin is the group that grants cluster-admin. + groupAdmin = "system:masters" + + defaultKubeconfigWaitTimeout = 3 * time.Minute + + kubeconfigAgainWarning = "Running create again issues another certificate." +) + +type kubeconfigCreateOptions struct { + poolScope + kubeconfigFileFlags + CommonName string + Admin bool + Groups []string + DisplayName string + Duration string + WaitTimeout time.Duration + + displayNameSet, durationSet bool +} + +// NewCmdKubeconfigCreate creates `verda mk8s kubeconfig create`. +func NewCmdKubeconfigCreate(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + opts := &kubeconfigCreateOptions{} + cmd := &cobra.Command{ + Use: "create", + Short: "Create a kubeconfig and write it to a file", + Long: longDesc(` + Issue a client certificate for a cluster and write the kubeconfig to + a file with mode 0600. The command always waits until the + kubeconfig is ready, for at most --wait-timeout. The YAML holds a + private key: it goes to the file only, never to structured output + or --debug. + + The default file is ~/.kube/verda-..yaml; + ~/.kube is created with mode 0700 if it is missing. The directory of + a --file path must exist. The command checks the file before it + issues a certificate, and stops if the file exists unless you pass + --force. It never writes through a symlink. --file - writes only + the YAML to stdout, for a pipe; it cannot be combined with + -o json|yaml or --agent. A file you redirect stdout to gets the + mode of your umask, not 0600. + + Access: --admin adds the group system:masters (full control of the + cluster) and --group adds another group; repeat --group for more, + and combine it with --admin if you like. In scripts, agent mode + and -o json|yaml, --cluster, --common-name and at least one of + --admin and --group are required. On a terminal, the CLI asks for + each value that you do not give, then shows a summary and asks + for confirmation. + + A certificate cannot be revoked before it expires: deleting the + kubeconfig does not disable copies already downloaded. Deleting the + cluster invalidates them. Without --duration the API issues it for + 90 days; the minimum is 1h. Use a short --duration for admin + credentials. + + A create is sent once and is never retried. If it fails with a + server or network error, the kubeconfig may exist anyway: check + kubeconfig list before you run it again. If the wait ends before the + kubeconfig is ready, or the file cannot be written, the error gives + the kubeconfig ID and the kubeconfig save command to run; running + create again issues another certificate. + `), + Example: cmdutil.Examples(` + # Pick the cluster and answer the prompts (terminal only) + verda mk8s kubeconfig create + + # Admin access, written to ~/.kube/verda-prod.alice.yaml + verda mk8s kubeconfig create --cluster prod --common-name alice --admin + + # Two groups, valid for 30 days, written to a file you choose + verda mk8s kubeconfig create --cluster prod --common-name ci --group developers --group viewers --duration 720h --file ./ci.yaml + + # Replace an existing file + verda mk8s kubeconfig create --cluster prod --common-name alice --admin --force + + # Agent mode: JSON metadata with the path in "file", never the YAML + verda --agent mk8s kubeconfig create --cluster prod -l FIN-03 --common-name ci --group developers --file ./ci.yaml + + # Only the YAML, on stdout, piped to a secret store + verda mk8s kubeconfig create --cluster prod --common-name ci --group developers --file - | gh secret set KUBECONFIG + `), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + opts.displayNameSet = cmd.Flags().Changed(flagDisplayName) + opts.durationSet = cmd.Flags().Changed(flagDuration) + return runKubeconfigCreate(cmd, f, ioStreams, opts) + }, + } + opts.poolScope.addFlags(cmd) + opts.kubeconfigFileFlags.addFlags(cmd) + flags := cmd.Flags() + flags.StringVar(&opts.CommonName, flagCommonName, "", "User name in the cluster: the common name of the certificate (not system:*)") + flags.BoolVar(&opts.Admin, flagAdmin, false, "Grant full control of the cluster (adds the group "+groupAdmin+")") + flags.StringArrayVar(&opts.Groups, flagGroup, nil, "Kubernetes group of the user, for RBAC (not system:*); repeat the flag for more groups") + flags.StringVar(&opts.DisplayName, flagDisplayName, "", "Label for the kubeconfig, at most 128 characters") + flags.StringVar(&opts.Duration, flagDuration, "", "Lifetime of the certificate in Go duration syntax, at least 1h, for example 720h (default: 90 days, set by the API). It cannot be revoked before it expires, only by deleting the cluster: use a short lifetime for admin credentials") + flags.DurationVar(&opts.WaitTimeout, flagWaitTimeout, defaultKubeconfigWaitTimeout, "Maximum time to wait until the kubeconfig is ready") + return cmd +} + +func (o *kubeconfigCreateOptions) trim() { + o.Cluster, o.Location = strings.TrimSpace(o.Cluster), strings.TrimSpace(o.Location) + o.CommonName, o.DisplayName, o.Duration = strings.TrimSpace(o.CommonName), strings.TrimSpace(o.DisplayName), strings.TrimSpace(o.Duration) + o.Groups = cleanGroups(o.Groups) +} + +// cleanGroups trims, drops blanks and duplicates, and keeps the order. +func cleanGroups(in []string) []string { + out := make([]string, 0, len(in)) + for _, g := range in { + if g = strings.TrimSpace(g); g != "" && !slices.Contains(out, g) { + out = append(out, g) + } + } + return out +} + +func (o *kubeconfigCreateOptions) hasAccess() bool { return o.Admin || len(o.Groups) > 0 } + +// groups is the access to request: the admin group first, then --group values. +func (o *kubeconfigCreateOptions) groups() []string { + if !o.Admin { + return slices.Clone(o.Groups) + } + return cleanGroups(append([]string{groupAdmin}, o.Groups...)) +} + +// request leaves duration out when unset: the API default applies. +func (o *kubeconfigCreateOptions) request() verda.CreateManagedKubernetesKubeconfigRequest { + return verda.CreateManagedKubernetesKubeconfigRequest{ + CommonName: o.CommonName, + Groups: o.groups(), + DisplayName: o.DisplayName, + Duration: o.Duration, + } +} + +// missingError is the error for what a script must give, or nil. A terminal +// asks instead, except for a blank --cluster, which no command accepts. +func (o *kubeconfigCreateOptions) missingError(f cmdutil.Factory, cmd *cobra.Command) error { + missing := o.missing(f, cmd) + if !interactiveTTY(f) { + if o.CommonName == "" { + missing = append(missing, "--"+flagCommonName) + } + if !o.hasAccess() { + return cmdutil.NewMissingOneOfFlagsError(missing, []string{"--" + flagAdmin, "--" + flagGroup}) + } + } + if len(missing) > 0 { + return cmdutil.NewMissingFlagsError(missing) + } + return nil +} + +func runKubeconfigCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *kubeconfigCreateOptions) error { + opts.trim() + if err := opts.validate(cmd, f); err != nil { + return err + } + if opts.WaitTimeout <= 0 { + return cmdutil.UsageErrorf(cmd, "--%s must be greater than 0", flagWaitTimeout) + } + if err := opts.missingError(f, cmd); err != nil { + return err + } + // Cheap input checks first, then the file, then whatever prompts or calls the + // API. On a terminal the request and the default path are known only after the prompts. + interactive := interactiveTTY(f) + var ( + req verda.CreateManagedKubernetesKubeconfigRequest + dest *kubeconfigDest + err error + ) + if interactive { + // A named file that is taken stops the command before any prompt or API call. + if opts.File != "" && opts.File != kubeconfigStdout { + if err := (kubeconfigTarget{path: opts.File}).checkFree(opts.Force); err != nil { + return asUsage(cmd, err) + } + } + } else if req, dest, err = opts.scriptedInput(cmd); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := opts.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + if interactive { + adminFlag := opts.Admin + if ok, err := promptKubeconfigCreate(cmd.Context(), f, ioStreams, opts, target); err != nil || !ok { + return err + } + if req, err = opts.validRequest(cmd); err != nil { + return err + } + if dest, err = opts.destination(cmd, target.Name, opts.CommonName); err != nil { + return err + } + if ok, err := confirmKubeconfigCreate(cmd.Context(), f, ioStreams, target, &req, dest, adminFlag); err != nil || !ok { + return err + } + } + return createKubeconfig(cmd, f, ioStreams, client, target, opts, &req, dest) +} + +// scriptedInput validates everything a script gave, before any API call: the +// request, then the file (the cluster name is the one given, names are exact). +func (o *kubeconfigCreateOptions) scriptedInput(cmd *cobra.Command) (verda.CreateManagedKubernetesKubeconfigRequest, *kubeconfigDest, error) { + req, err := o.validRequest(cmd) + if err != nil { + return req, nil, err + } + dest, err := o.destination(cmd, o.Cluster, o.CommonName) + return req, dest, err +} + +// validRequest builds the request and rejects what the API would refuse. +func (o *kubeconfigCreateOptions) validRequest(cmd *cobra.Command) (verda.CreateManagedKubernetesKubeconfigRequest, error) { + req := o.request() + if err := req.Validate(); err != nil { + return req, cmdutil.UsageErrorf(cmd, "invalid kubeconfig: %s", strings.TrimSuffix(err.Error(), ".")) + } + return req, nil +} + +func createKubeconfig(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, target *clusterTarget, opts *kubeconfigCreateOptions, req *verda.CreateManagedKubernetesKubeconfigRequest, dest *kubeconfigDest) error { + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + created, err := cmdutil.WithSpinner(ctx, f.Status(), "Creating kubeconfig...", func(ctx context.Context) (*verda.ManagedKubernetesKubeconfig, error) { + return client.ManagedKubernetes.CreateKubeconfig(ctx, target.Location, target.Name, *req) + }) + if err != nil { + return createFailed(f, ioStreams, kindKubeconfig, kubeconfigListCmd(target), err) + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response:", newKubeconfigView(created, "")) + + saveCmd := kubeconfigSaveCmd(target, created.ID, dest, opts.Force) + ready, err := waitForKubeconfig(cmd.Context(), f, ioStreams, client, target, created.ID, opts.WaitTimeout) + if err != nil { + return kubeconfigRecovery(f, kubeconfigWaitError(err, target, created, ready, opts.WaitTimeout, saveCmd), created.ID, saveCmd) + } + if err := dest.write(ioStreams, ready.Kubeconfig, opts.Force); err != nil { + err = fmt.Errorf("kubeconfig %s is ready but writing it failed: %w. Fix the cause and run `%s`. %s", created.ID, err, saveCmd, kubeconfigAgainWarning) + return kubeconfigRecovery(f, err, created.ID, saveCmd) + } + return reportKubeconfigWritten(f, ioStreams, target, dest, ready, dest.replaced) +} + +// waitForKubeconfig polls until the kubeconfig is ready with its YAML. It +// returns the last read, which holds the YAML: keep it out of every output. +func waitForKubeconfig(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, id string, timeout time.Duration) (*verda.ManagedKubernetesKubeconfig, error) { + if f.OutputFormat() == outputTable { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Kubeconfig %s created; waiting until it is ready (Ctrl+C stops waiting, the kubeconfig stays).\n", id) + } + return waitFor(ctx, f, waitWriter(f, ioStreams), timeout, + func(ctx context.Context) (*verda.ManagedKubernetesKubeconfig, error) { + return client.ManagedKubernetes.GetKubeconfig(ctx, t.Location, t.Name, id) + }, kubeconfigReady) +} + +// kubeconfigWaitError says what to do after a wait that did not end in a ready kubeconfig. +func kubeconfigWaitError(err error, t *clusterTarget, created, last *verda.ManagedKubernetesKubeconfig, timeout time.Duration, saveCmd string) error { + subject := fmt.Sprintf("kubeconfig %s of cluster %s (%s)", created.ID, t.Name, t.Location) + tail := fmt.Sprintf("Run `%s` once it is ready. %s", saveCmd, kubeconfigAgainWarning) + if readErr, ok := errors.AsType[*pollReadError](err); ok { + return fmt.Errorf("%s was created, but its status could not be read (%w). %s", subject, readErr.err, tail) + } + seen := created.Status + if last != nil { + seen = last.Status + } + return fmt.Errorf("timed out after %s waiting for %s to become ready (last status: %s). It was created and is still being issued. %s", timeout, subject, seen, tail) +} + +// kubeconfigRecovery adds the kubeconfig ID and the save command to the agent +// error details; people get them in the message. +func kubeconfigRecovery(f cmdutil.Factory, err error, id, saveCmd string) error { + if !f.AgentMode() { + return err + } + ae := *cmdutil.ClassifyError(err) + details := make(map[string]any, len(ae.Details)+3) + maps.Copy(details, ae.Details) + details["kubeconfig_id"] = id + details["save_command"] = saveCmd + details["warning"] = kubeconfigAgainWarning + ae.Details = details + ae.Message = err.Error() // the recovery text, not the classified API message + return &ae +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go new file mode 100644 index 00000000..f41c471b --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go @@ -0,0 +1,666 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "encoding/json" + "os" + "path/filepath" + "slices" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + "go.yaml.in/yaml/v3" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// kcYAML is the YAML the fake serves; kcPrivateKeyMarker stands for the private key. +const kcYAML = "apiVersion: v1\nkind: Config\nusers:\n- name: alice\n user:\n client-key-data: " + kcPrivateKeyMarker + "\n" + +const kcCreateRoute = "POST /mk8sclusters/{loc}/{name}/kubeconfigs" + +// kcReadyOnGet makes every kubeconfig GET answer ready with the YAML. +func kcReadyOnGet(api *fakeAPI) { + api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { + na := testCreated.Add(90 * 24 * time.Hour) + k.Status, k.Kubeconfig, k.NotAfter = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML, &na + } +} + +func kcAPI() *fakeAPI { + api := seededClusters() + kcReadyOnGet(api) + return api +} + +// kcArgs are the flags of a valid non-interactive create writing to file. +func kcArgs(file string, extra ...string) []string { + args := []string{"kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--admin", "--file", file} + return append(args, extra...) +} + +func kcPosts(api *fakeAPI) int { return len(api.requestsMatching("POST")) } + +// kcAssertErrNoKey checks the error text and, for an AgentError, its serialized details. +func kcAssertErrNoKey(t *testing.T, err error) { + t.Helper() + if err == nil { + return + } + assertNoPrivateKey(t, "error", err.Error()) + if ae := cmdutil.ClassifyError(err); ae != nil { + b, _ := json.Marshal(ae) + assertNoPrivateKey(t, "agent error", string(b)) + } +} + +func kcBody(t *testing.T, api *fakeAPI) map[string]any { + t.Helper() + return postBodyAny(t, api) +} + +func TestKubeconfigCreate_WritesFileInEveryOutputMode(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ + "table": func(c *verda.Client) *cmdutil.TestFactory { return tableFactory(c, nil) }, + "json": jsonFactory, + "yaml": yamlFactory, + "agent": agentFactory, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + out, errOut, err := runMK8s(t, mk(newFakeClient(t, api)), kcArgs(file)...) + if err != nil { + t.Fatal(err) + } + assertNoPrivateKey(t, "stdout", out) + assertNoPrivateKey(t, "stderr", errOut) + if got := readFile(t, file); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, file, 0o600) + if n := kcPosts(api); n != 1 { + t.Errorf("POSTs = %d, want 1", n) + } + switch name { + case "table": + if !strings.Contains(out, "Kubeconfig prod-kc-1 saved to "+file) { + t.Errorf("stdout = %q", out) + } + if !strings.Contains(errOut, "export KUBECONFIG="+file) || !strings.Contains(errOut, "kubectl get nodes") { + t.Errorf("stderr = %q", errOut) + } + case "yaml": + var v KubeconfigView + if err := yaml.Unmarshal([]byte(out), &v); err != nil || v.File != file || v.ID != "prod-kc-1" || v.Status != "ready" { + t.Errorf("view = %+v err=%v", v, err) + } + default: + v := decodeJSON[KubeconfigView](t, out) + if v.File != file || v.CommonName != "alice" || !slices.Equal(v.Groups, []string{groupAdmin}) || v.NotAfter == nil { + t.Errorf("view = %+v", v) + } + if errOut != "" { + t.Errorf("stderr = %q, want empty", errOut) + } + } + }) + } +} + +func TestKubeconfigCreate_RequestBody(t *testing.T) { + t.Parallel() + cases := map[string]struct { + flags []string + groups []string + extra map[string]any + }{ + "admin": {flags: []string{"--admin"}, groups: []string{groupAdmin}}, + "groups": {flags: []string{"--group", "dev", "--group", "ops"}, groups: []string{"dev", "ops"}}, + "both": {flags: []string{"--admin", "--group", "dev"}, groups: []string{groupAdmin, "dev"}}, + "admin twice": {flags: []string{"--admin", "--group", groupAdmin}, groups: []string{groupAdmin}}, + "duration": {flags: []string{"--admin", "--duration", "720h"}, groups: []string{groupAdmin}, extra: map[string]any{"duration": "720h"}}, + "display name": {flags: []string{"--group", "dev", "--display-name", "Alice laptop"}, groups: []string{"dev"}, extra: map[string]any{"display_name": "Alice laptop"}}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + t.Parallel() + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + args := []string{"kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--file", file} + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), append(args, tc.flags...)...); err != nil { + t.Fatal(err) + } + body := kcBody(t, api) + got, _ := body["groups"].([]any) + var groups []string + for _, g := range got { + groups = append(groups, g.(string)) + } + if body["common_name"] != "alice" || !slices.Equal(groups, tc.groups) { + t.Errorf("body = %v", body) + } + for _, key := range []string{"duration", "display_name"} { + want, has := tc.extra[key] + if got, ok := body[key]; ok != has || (has && got != want) { + t.Errorf("%s = %v (present %v), want %v (present %v)", key, got, ok, want, has) + } + } + if reqs := api.requestsMatching("POST"); reqs[0].Path != "/mk8sclusters/FIN-03/prod/kubeconfigs" { + t.Errorf("path = %s", reqs[0].Path) + } + }) + } +} + +func TestKubeconfigCreate_LocationFlagSkipsLookup(t *testing.T) { + t.Parallel() + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "-l", "FIN-03")...); err != nil { + t.Fatal(err) + } + if slices.Contains(api.paths("GET"), "/mk8sclusters") { + t.Errorf("-l must skip the cluster lookup, GETs = %v", api.paths("GET")) + } +} + +func TestKubeconfigCreate_DefaultPath(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + api := kcAPI() + args := []string{"kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--admin"} + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...) + if err != nil { + t.Fatal(err) + } + want := filepath.Join(home, ".kube", "verda-prod.alice.yaml") + if got := readFile(t, want); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, want, 0o600) + if !strings.Contains(out, want) || !strings.Contains(errOut, "export KUBECONFIG="+want) { + t.Errorf("out=%q err=%q", out, errOut) + } +} + +func TestKubeconfigCreate_ExportLineQuotesThePath(t *testing.T) { + t.Parallel() + file := filepath.Join(t.TempDir(), "my kube's.yaml") + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, kcAPI()), nil), kcArgs(file)...) + if err != nil { + t.Fatal(err) + } + if want := "export KUBECONFIG=" + shellQuote(file); !strings.Contains(errOut, want) || !strings.Contains(errOut, "'\\''") { + t.Errorf("stderr = %q, want %q", errOut, want) + } +} + +func TestKubeconfigCreate_ExistingTargetStopsBeforeAnyPOST(t *testing.T) { + t.Parallel() + t.Run("file without force", func(t *testing.T) { + t.Parallel() + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { + t.Fatal(err) + } + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file)...) + if err == nil || !strings.Contains(err.Error(), "--force") || !isUsageError(err) { + t.Fatalf("err = %v", err) + } + if n := kcPosts(api); n != 0 { + t.Errorf("%d POST(s): a certificate was issued for an unwritable path", n) + } + if got := readFile(t, file); got != "old" { + t.Errorf("file = %q", got) + } + }) + t.Run("directory", func(t *testing.T) { + t.Parallel() + api := kcAPI() + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(t.TempDir(), "--force")...) + if err == nil || !strings.Contains(err.Error(), "is a directory") || !isUsageError(err) || kcPosts(api) != 0 { + t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) + } + }) + t.Run("symlink", func(t *testing.T) { + t.Parallel() + skipModeTestOnWindows(t) + api := kcAPI() + dir := t.TempDir() + link := filepath.Join(dir, "link.yaml") + if err := os.Symlink(filepath.Join(dir, "elsewhere"), link); err != nil { + t.Fatal(err) + } + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(link, "--force")...) + if err == nil || !strings.Contains(err.Error(), "symlink") || !isUsageError(err) || kcPosts(api) != 0 { + t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) + } + }) + t.Run("missing parent directory", func(t *testing.T) { + t.Parallel() + api := kcAPI() + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(filepath.Join(t.TempDir(), "nope", "kc.yaml"))...) + if err == nil || !isUsageError(err) || kcPosts(api) != 0 { + t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) + } + }) +} + +func TestKubeconfigCreate_ForceReplacesTheFile(t *testing.T) { + t.Parallel() + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + if err := os.WriteFile(file, []byte("old"), 0o644); err != nil { //nolint:gosec // a loose mode proves the replacement is 0600 + t.Fatal(err) + } + _, errOut, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "--force")...) + if err != nil { + t.Fatal(err) + } + if got := readFile(t, file); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, file, 0o600) + if want := "still valid. Run `verda mk8s kubeconfig list --cluster prod -l FIN-03`"; !strings.Contains(errOut, want) { + t.Errorf("stderr misses the hint %q: %q", want, errOut) + } + + // --force with no file in the way says nothing. + _, errOut, err = runMK8s(t, jsonFactory(newFakeClient(t, kcAPI())), kcArgs(filepath.Join(t.TempDir(), "new.yaml"), "--force")...) + if err != nil || strings.Contains(errOut, "still valid") { + t.Errorf("err=%v stderr=%q", err, errOut) + } +} + +func TestKubeconfigCreate_FileDashWritesOnlyTheYAMLToStdout(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + api := kcAPI() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs("-")...) + if err != nil { + t.Fatal(err) + } + if out != kcYAML { + t.Errorf("stdout = %q, want exactly the YAML", out) + } + assertNoPrivateKey(t, "stderr", errOut) + if _, err := os.Stat(filepath.Join(home, ".kube")); err == nil { + t.Error("--file - must not write a file") + } +} + +func TestKubeconfigCreate_FileDashRejectedWithStructuredOutput(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ + "json": jsonFactory, "yaml": yamlFactory, "agent": agentFactory, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + api := kcAPI() + out, _, err := runMK8s(t, mk(newFakeClient(t, api)), kcArgs("-")...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Fatalf("err = %v", err) + } + if out != "" || len(api.requests) != 0 { + t.Errorf("stdout = %q, requests = %v: must fail before any API call", out, api.requests) + } + }) + } +} + +func TestKubeconfigCreate_MissingFlags(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ + "table": func(c *verda.Client) *cmdutil.TestFactory { return tableFactory(c, nil) }, + "agent": agentFactory, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + api := kcAPI() + f := mk(newFakeClient(t, api)) + _, _, err := runMK8s(t, f, "kubeconfig", "create") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if m, _ := ae.Details["missing"].([]string); !slices.Equal(m, []string{"--cluster", "--common-name"}) { + t.Errorf("missing = %v", ae.Details) + } + if o, _ := ae.Details["one_of"].([]string); !slices.Equal(o, []string{"--admin", "--group"}) { + t.Errorf("one_of = %v", ae.Details) + } + if want := "required flags not provided: --cluster, --common-name; set at least one of --admin, --group"; ae.Message != want { + t.Errorf("message = %q, want %q", ae.Message, want) + } + // With the rest given, a missing access level alone is reported. + _, _, err = runMK8s(t, f, "kubeconfig", "create", "--cluster", "prod", "--common-name", "alice") + ae = cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" || !strings.Contains(ae.Message, "--admin, --group") { + t.Errorf("no access: err = %v", err) + } + // Without a common name only that is missing. + _, _, err = runMK8s(t, f, "kubeconfig", "create", "--cluster", "prod", "--admin") + if m, _ := cmdutil.ClassifyError(err).Details["missing"].([]string); !slices.Equal(m, []string{"--common-name"}) { + t.Errorf("no common name: err = %v", err) + } + if len(api.requests) != 0 { + t.Errorf("requests = %v", api.requests) + } + }) + } +} + +func TestKubeconfigCreate_InvalidInput(t *testing.T) { + t.Parallel() + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "--wait-timeout", "0s")...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Errorf("zero wait timeout: %v", err) + } + for name, extra := range map[string][]string{ + "duration under 1h": {"--duration", "30m"}, + "system: common name": {"--common-name", "system:admin"}, + "system: group": {"--group", "system:nodes"}, + "newline in the display": {"--display-name", "a\nb"}, + } { + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, extra...)...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Errorf("%s: err = %v", name, err) + } + } + // Input validation comes before the file check. + if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { + t.Fatal(err) + } + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "--duration", "30m")...); err == nil || !strings.Contains(err.Error(), "invalid kubeconfig") { + t.Errorf("a bad duration must be reported before the taken file: %v", err) + } + if err := os.Remove(file); err != nil { + t.Fatal(err) + } + if usage := NewCmdKubeconfigCreate(nil, cmdutil.IOStreams{}).Flags().Lookup(flagDuration).Usage; !strings.Contains(usage, "cannot be revoked before it expires") { + t.Errorf("--duration help = %q", usage) + } + args := kcArgs(file) + args[slices.Index(args, "prod")] = "ghost" + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } + if n := kcPosts(api); n != 0 { + t.Errorf("%d POST(s) for invalid input", n) + } +} + +func TestKubeconfigCreate_FailureSendsOnePOST(t *testing.T) { + t.Parallel() + t.Run("502 warns once", func(t *testing.T) { + t.Parallel() + api := kcAPI() + api.failures[kcCreateRoute] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} + file := filepath.Join(t.TempDir(), "kc.yaml") + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(file)...) + if err == nil || !strings.Contains(err.Error(), "upstream failed") { + t.Fatalf("err = %v", err) + } + if !strings.Contains(errOut, "The kubeconfig may have been created anyway.") || !strings.Contains(errOut, "kubeconfig list --cluster prod -l FIN-03") { + t.Errorf("stderr = %q", errOut) + } + if n := kcPosts(api); n != 1 { + t.Errorf("POSTs = %d, want exactly 1", n) + } + if _, err := os.Stat(file); err == nil { + t.Error("no file may be written when the create failed") + } + }) + t.Run("502 in agent mode puts the warning in the details", func(t *testing.T) { + t.Parallel() + api := kcAPI() + api.failures[kcCreateRoute] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} + _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), kcArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) + ae := cmdutil.ClassifyError(err) + if ae == nil || !strings.Contains(ae.Details["warning"].(string), "may have been created anyway") { + t.Fatalf("err = %v", err) + } + if errOut != "" || kcPosts(api) != 1 { + t.Errorf("stderr = %q, POSTs = %d", errOut, kcPosts(api)) + } + }) + t.Run("400 has no warning", func(t *testing.T) { + t.Parallel() + api := kcAPI() + api.failures[kcCreateRoute] = apiFailure{Status: 400, Body: `{"code":"bad_request","message":"bad common name"}`} + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) + if err == nil || strings.Contains(errOut, "may have been created") || kcPosts(api) != 1 { + t.Errorf("err = %v, stderr = %q, POSTs = %d", err, errOut, kcPosts(api)) + } + }) + t.Run("403 not enabled", func(t *testing.T) { + t.Parallel() + api := kcAPI() + api.disabled = true + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(filepath.Join(t.TempDir(), "kc.yaml"), "-l", "FIN-03")...) + if agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled") { + t.Errorf("err = %v", err) + } + }) +} + +func TestKubeconfigCreate_WaitTimeoutGivesRecovery(t *testing.T) { + fastPoll(t) + for _, agent := range []bool{false, true} { + api := seededClusters() // never ready: the kubeconfig stays pending + file := filepath.Join(t.TempDir(), "my kc.yaml") + runFactory := tableFactory(newFakeClient(t, api), nil) + if agent { + runFactory = agentFactory(newFakeClient(t, api)) + } + _, _, err := runMK8s(t, runFactory, kcArgs(file, "--wait-timeout", "150ms", "--force")...) + if err == nil { + t.Fatal("want a timeout") + } + msg := err.Error() + saveCmd := "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file '" + file + "' --force" + for _, want := range []string{"prod-kc-1", saveCmd, "running create again issues another certificate", "last status: pending"} { + if !strings.Contains(strings.ToLower(msg), strings.ToLower(want)) { + t.Errorf("agent=%v: message %q lacks %q", agent, msg, want) + } + } + if _, statErr := os.Stat(file); statErr == nil { + t.Error("no file may be written before the kubeconfig is ready") + } + if n := kcPosts(api); n != 1 { + t.Errorf("POSTs = %d", n) + } + if agent { + ae := cmdutil.ClassifyError(err) + if ae.Details["kubeconfig_id"] != "prod-kc-1" || ae.Details["save_command"] != saveCmd { + t.Errorf("details = %v", ae.Details) + } + } + kcAssertErrNoKey(t, err) + } +} + +func TestKubeconfigCreate_WriteFailureAfterCreateGivesRecovery(t *testing.T) { + fastPoll(t) + api := seededClusters() + file := filepath.Join(t.TempDir(), "kc.yaml") + // The file appears after the path check passed: the exclusive create refuses it. + api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { + k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML + _ = os.WriteFile(file, []byte("raced"), 0o600) + } + _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), kcArgs(file)...) + if err == nil { + t.Fatal("want a write error") + } + saveCmd := "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file " + file + if !strings.Contains(err.Error(), saveCmd) || !strings.Contains(err.Error(), "another certificate") { + t.Errorf("err = %v", err) + } + ae := cmdutil.ClassifyError(err) + if ae.Details["kubeconfig_id"] != "prod-kc-1" || ae.Details["save_command"] != saveCmd { + t.Errorf("details = %v", ae.Details) + } + if got := readFile(t, file); got != "raced" { + t.Errorf("the other file was replaced: %q", got) + } + kcAssertErrNoKey(t, err) + assertNoPrivateKey(t, "stderr", errOut) + + // For a person the same text is the plain error. + api = seededClusters() + file = filepath.Join(t.TempDir(), "kc.yaml") + api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { + k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML + _ = os.WriteFile(file, []byte("raced"), 0o600) + } + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(file)...) + if err == nil || cmdutil.IsAgentError(err) || !strings.Contains(err.Error(), "prod-kc-1") { + t.Errorf("human err = %v", err) + } +} + +func TestKubeconfigCreate_PendingAndStaleReadsDoNotEndTheWait(t *testing.T) { + fastPoll(t) + api := seededClusters() + reads := 0 + api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { + reads++ + switch reads { + case 1: + k.Status = verda.ManagedKubernetesKubeconfigStatusPending + case 2: + // Ready without the YAML is a stale read. + k.Status = verda.ManagedKubernetesKubeconfigStatusReady + default: + k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML + } + } + file := filepath.Join(t.TempDir(), "kc.yaml") + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file)...) + if err != nil { + t.Fatal(err) + } + if got := readFile(t, file); got != kcYAML { + t.Errorf("file = %q", got) + } + if reads < 3 { + t.Errorf("reads = %d, the wait ended early", reads) + } + assertNoPrivateKey(t, "stdout", out) +} + +func TestKubeconfigCreate_DebugNeverLogsTheKey(t *testing.T) { + t.Parallel() + api := kcAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.DebugOverride = true + file := filepath.Join(t.TempDir(), "kc.yaml") + out, errOut, err := runMK8s(t, f, kcArgs(file)...) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "DEBUG: Request payload:") || !strings.Contains(errOut, "DEBUG: API response:") { + t.Errorf("no debug output: %q", errOut) + } + assertNoPrivateKey(t, "stdout", out) + assertNoPrivateKey(t, "debug output", errOut) + + f = agentFactory(newFakeClient(t, api)) + f.DebugOverride = true + out, errOut, err = runMK8s(t, f, kcArgs(filepath.Join(t.TempDir(), "kc2.yaml"))...) + if err != nil { + t.Fatal(err) + } + assertNoPrivateKey(t, "agent stdout", out) + assertNoPrivateKey(t, "agent debug output", errOut) +} + +func TestKubeconfigCreate_StdoutDashDebugKeepsStdoutPure(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, kcAPI()), nil) + f.DebugOverride = true + out, errOut, err := runMK8s(t, f, kcArgs("-")...) + if err != nil { + t.Fatal(err) + } + if out != kcYAML { + t.Errorf("stdout = %q", out) + } + assertNoPrivateKey(t, "debug output", errOut) +} + +// A fresh machine has no ~/.kube: the default path passes the prompt check, and +// the recovery command printed after a timed-out create (which leaves out --file) +// creates ~/.kube with 0700. +func TestKubeconfigCreate_FreshHomeDefaultPathAndRecovery(t *testing.T) { + fastPoll(t) + home := kcHome(t) + def := filepath.Join(home, ".kube", "verda-prod.alice.yaml") + if err := kubeconfigFileValidator("prod", "alice", false)(def); err != nil { + t.Errorf("the pre-filled default path must pass the prompt check: %v", err) + } + var ready atomic.Bool + api := seededClusters() + api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { + if ready.Load() { + k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML + } + } + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--admin", "--wait-timeout", "100ms") + ae := cmdutil.ClassifyError(err) + saveCmd, _ := ae.Details["save_command"].(string) + if saveCmd == "" || strings.Contains(saveCmd, "--file") { + t.Fatalf("save_command = %q, want one without --file", saveCmd) + } + ready.Store(true) + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), strings.Fields(saveCmd)[2:]...); err != nil { + t.Fatalf("recovery command %q failed: %v", saveCmd, err) + } + if got := readFile(t, def); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, filepath.Dir(def), 0o700) +} + +// A certificate was issued, so the recovery text leads the error even when the +// classified API message ("no access") is about the failed read. +func TestKubeconfigCreate_ReadFailureKeepsTheRecoveryText(t *testing.T) { + fastPoll(t) + api := seededClusters() + api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access"}`} + file := filepath.Join(t.TempDir(), "kc.yaml") + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), kcArgs(file)...) + ae := cmdutil.ClassifyError(err) + saveCmd := "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file " + file + if ae == nil || ae.Code != "AUTH_ERROR" || ae.Details["kubeconfig_id"] != "prod-kc-1" || ae.Details["save_command"] != saveCmd { + t.Fatalf("err = %v, details = %v", err, ae) + } + if !strings.Contains(ae.Message, "Running create again issues another certificate") || !strings.Contains(ae.Message, saveCmd) { + t.Errorf("message = %q, want the recovery text", ae.Message) + } +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_delete.go b/internal/verda-cli/cmd/mk8s/kubeconfig_delete.go new file mode 100644 index 00000000..fe5a1580 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_delete.go @@ -0,0 +1,126 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "time" + + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdKubeconfigDelete creates `verda mk8s kubeconfig delete`. +func NewCmdKubeconfigDelete(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + scope := &poolScope{} + var yes bool + cmd := &cobra.Command{ + Use: "delete []", + Aliases: []string{"rm"}, + Short: "Delete a kubeconfig", + Long: longDesc(` + Delete a kubeconfig. This does not revoke it: copies that were + already downloaded keep working until they expire (EXPIRES in + kubeconfig list, not_after in JSON). Deleting the cluster + invalidates them; a copy bound to a group can also be cut off by + removing that group's RBAC binding in the cluster. + + Without or --cluster, a terminal session shows a picker. + + Asks for confirmation on a terminal. --yes skips it and is required + in scripts, agent mode and -o json|yaml. + `), + Example: cmdutil.Examples(` + verda mk8s kubeconfig delete $KUBECONFIG_ID --cluster prod + + # Without prompts + verda mk8s kubeconfig delete $KUBECONFIG_ID --cluster prod -l FIN-03 --yes + verda --agent mk8s kubeconfig delete $KUBECONFIG_ID --cluster prod --yes + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runKubeconfigDelete(cmd, f, ioStreams, scope, args, yes) + }, + } + scope.addFlags(cmd) + cmd.Flags().BoolVarP(&yes, flagYes, "y", false, yesUsage) + return cmd +} + +func runKubeconfigDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string, yes bool) error { + if err := checkKubeconfigTarget(f, cmd, scope, args); err != nil { + return err + } + if err := checkConfirmed(f, yes, "delete"); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := scope.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + // A named id is deleted without reading it first, like node pools: one that cannot be read must stay deletable. + kc, err := resolveKubeconfig(cmd, f, ioStreams, client, target, args, false) + if err != nil || kc == nil { + return err + } + + if !yes { + ok, err := confirmWarning(cmd.Context(), ioStreams, f.Prompter(), + fmt.Sprintf("Deletes kubeconfig %s.", kc.ID), + []string{ + "Copies already downloaded are NOT revoked: they keep working " + validUntil(kc.NotAfter) + ".", + copiesNote, + "This action cannot be undone.", + }, + fmt.Sprintf("Delete kubeconfig %s (cluster %s)?", kc.ID, target.Name)) + if err != nil || !ok { + return err + } + } + + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Delete kubeconfig:", map[string]string{"id": kc.ID, "cluster": target.Name, "location": target.Location}) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + err = cmdutil.RunWithSpinner(ctx, f.Status(), "Deleting kubeconfig...", func(ctx context.Context) error { + return client.ManagedKubernetes.DeleteKubeconfig(ctx, target.Location, target.Name, kc.ID) + }) + if err != nil { + return mapErr(err) + } + + result := ActionResult{Resource: kindKubeconfig, ID: kc.ID, Name: kc.ID, Location: target.Location, Cluster: target.Name, Action: "delete", Status: statusAccepted} + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result); wrote { + return err + } + _, _ = fmt.Fprintf(ioStreams.Out, "Deletion accepted. Copies already downloaded are not revoked and still work %s. %s\n", validUntil(kc.NotAfter), copiesNote) + return nil +} + +// copiesNote: the CLI cannot revoke a certificate. system:masters is bound to cluster-admin by the control plane itself, so no RBAC change cuts that group off. +const copiesNote = "Deleting the cluster invalidates them. A copy that does not use system:masters can also be cut off by removing its group's RBAC binding in the cluster." + +// validUntil: the date is known only when the id came from the picker; a named id is not read first. +func validUntil(notAfter *time.Time) string { + if notAfter == nil { + return "until they expire" + } + return "until " + utcTime(*notAfter) +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go new file mode 100644 index 00000000..7b543179 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go @@ -0,0 +1,284 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "slices" + "strings" + "testing" + + "go.yaml.in/yaml/v3" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const kcDeletePath = "/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-aaa" + +func TestKubeconfigDelete_YesMakesOneDeleteCall(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "--yes") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Deletion accepted.") || !strings.Contains(out, "not revoked") || !strings.Contains(out, "still work until they expire.") || !strings.Contains(out, "Deleting the cluster invalidates them") || !strings.Contains(out, "does not use system:masters") { + t.Errorf("stdout = %q", out) + } + if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != kcDeletePath { + t.Fatalf("DELETE requests = %v", dels) + } + if len(api.requestsMatching("POST"))+len(api.requestsMatching("PATCH")) != 0 { + t.Error("delete must not write anything else") + } + // A named id is not read first (no GET of the kubeconfig, so no YAML either), and there is no --wait. + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters"}) { + t.Errorf("reads = %v, want only the cluster lookup", got) + } + if len(api.kubeconfigs["prod"]) != 1 || api.kubeconfigs["prod"][0].ID != "prod-kc-bbb" { + t.Errorf("kubeconfigs left = %+v", api.kubeconfigs["prod"]) + } +} + +func TestKubeconfigDelete_AKubeconfigThatCannotBeReadCanStillBeDeleted(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "rm", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes"); err != nil { + t.Fatal(err) + } + if got := api.paths("GET"); len(got) != 0 { + t.Errorf("reads = %v, want none with -l", got) + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("DELETEs = %d", n) + } +} + +func TestKubeconfigDelete_Structured(t *testing.T) { + t.Parallel() + want := ActionResult{Resource: "kubeconfig", ID: "prod-kc-aaa", Name: "prod-kc-aaa", Location: "FIN-03", Cluster: "prod", Action: "delete", Status: "accepted"} + wantKeys := []string{"action", "cluster", "id", "location", "name", "resource", "status"} + for name, f := range map[string]*cmdutil.TestFactory{ + "json": jsonFactory(newFakeClient(t, kcFixtureAPI())), + "agent": agentFactory(newFakeClient(t, kcFixtureAPI())), + } { + out, _, err := runMK8s(t, f, "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes") + if err != nil { + t.Fatal(err) + } + if got := decodeJSON[ActionResult](t, out); got != want { + t.Errorf("%s: got %+v, want %+v", name, got, want) + } + if got := sortedKeys(decodeJSON[map[string]any](t, out)); !slices.Equal(got, wantKeys) { + t.Errorf("%s: keys = %v", name, got) + } + } + + out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes") + if err != nil { + t.Fatal(err) + } + var got ActionResult + if err := yaml.Unmarshal([]byte(out), &got); err != nil || got != want { + t.Errorf("yaml: got %+v (%v), want %+v", got, err, want) + } +} + +func TestKubeconfigDelete_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { + return tableFactory(newFakeClient(t, api), nil) + }, + } { + api := kcFixtureAPI() + _, _, err := runMK8s(t, mk(api, t), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod") + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { + t.Errorf("%s: err = %v", name, err) + } + if n := len(api.requests); n != 0 { + t.Errorf("%s: %d request(s) before the check", name, n) + } + } +} + +func TestKubeconfigDelete_MissingTargets(t *testing.T) { + t.Parallel() + client := newFakeClient(t, kcFixtureAPI()) + _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "delete", "--yes") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { + t.Errorf("missing = %v", missing) + } + // Missing targets are reported before the missing --yes, as in node-pool delete. + if _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "delete"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Errorf("no args, no --yes: %v", err) + } + if _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "delete", " ", "--cluster", "prod", "--yes"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Errorf("blank id: %v", err) + } +} + +func TestKubeconfigDelete_Interactive(t *testing.T) { + withTerminal(t) + + t.Run("the warning says copies are not revoked", func(t *testing.T) { + api := kcFixtureAPI() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + for _, s := range []string{"Deletes kubeconfig prod-kc-aaa.", "Copies already downloaded are NOT revoked", "keep working until they expire.", "Deleting the cluster invalidates them", "does not use system:masters", "cannot be undone"} { + if !strings.Contains(errOut, s) { + t.Errorf("warning misses %q:\n%s", s, errOut) + } + } + if !strings.Contains(out, "still work until they expire.") { + t.Errorf("stdout = %q", out) + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("DELETEs = %d", n) + } + }) +} + +func TestKubeconfigDelete_InteractivePickers(t *testing.T) { + withTerminal(t) + + t.Run("a picked kubeconfig names its expiry", func(t *testing.T) { + api := kcFixtureAPI() + p := &kcSelectRecorder{Prompter: pickerAnswers(0, 0).AddConfirm(true)} + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + out, errOut, err := runMK8s(t, f, "kubeconfig", "delete") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "keep working until 2027-02-17 09:30 UTC.") || !strings.Contains(out, "still work until 2027-02-17 09:30 UTC.") { + t.Errorf("stdout = %q\nstderr = %q", out, errOut) + } + if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != kcDeletePath { + t.Errorf("DELETE requests = %v", dels) + } + if len(p.calls) != 2 || p.calls[1].Message != "Select kubeconfig" || !p.calls[1].Hints || p.calls[1].Labels[len(p.calls[1].Labels)-1] != cancelLabel { + t.Errorf("picker calls = %+v", p.calls) + } + }) + + t.Run("a picked pending kubeconfig has no expiry yet", func(t *testing.T) { + api := kcFixtureAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 1).AddConfirm(true)), "kubeconfig", "delete") + if err != nil || !strings.Contains(errOut, "keep working until they expire.") { + t.Fatalf("err=%v stderr=%q", err, errOut) + } + if dels := api.requestsMatching("DELETE"); len(dels) != 1 || !strings.HasSuffix(dels[0].Path, "/prod-kc-bbb") { + t.Errorf("DELETE requests = %v", dels) + } + }) + + t.Run("--yes skips the confirmation but not the pickers", func(t *testing.T) { + api := kcFixtureAPI() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 0)), "kubeconfig", "delete", "--yes"); err != nil { + t.Fatal(err) + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("DELETEs = %d", n) + } + }) +} + +func TestKubeconfigDelete_InteractiveDeclined(t *testing.T) { + withTerminal(t) + + t.Run("declined, Esc and Ctrl+C delete nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "declined": {Prompter: confirmNo()}, + "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, + } { + api := kcFixtureAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + out, errOut, err := runMK8s(t, f, "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod") + if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("DELETE")) != 0 { + t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) + } + } + }) +} + +func TestKubeconfigDelete_InteractivePickerCancelAndEmpty(t *testing.T) { + withTerminal(t) + + t.Run("picker cancel paths delete nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "cancel row": {Prompter: pickerAnswers(2)}, + "esc": {Prompter: tuitest.New(), selectErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), selectErr: errInterrupted()}, + } { + api := kcFixtureAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + out, errOut, err := runMK8s(t, f, "kubeconfig", "delete", "--cluster", "prod") + if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("DELETE")) != 0 { + t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) + } + } + }) + + t.Run("no kubeconfigs", func(t *testing.T) { + api := seededClusters() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), "kubeconfig", "delete", "--cluster", "prod") + if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "kubeconfig create --cluster prod") || len(api.requestsMatching("DELETE")) != 0 { + t.Errorf("out=%q err=%q error=%v", out, errOut, err) + } + }) +} + +func TestKubeconfigDelete_Errors(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, kcFixtureAPI()), nil), "kubeconfig", "delete", "ghost", "--cluster", "prod", "--yes") + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown id: %v", err) + } + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, kcFixtureAPI()), nil), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "ghost", "--yes") + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } + api := newFakeAPI() + api.disabled = true + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "delete", "x", "--cluster", "prod", "-l", "FIN-03", "--yes") + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Errorf("not enabled: %v", err) + } +} + +func TestKubeconfigDelete_Debug(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) + f.DebugOverride = true + _, errOut, err := runMK8s(t, f, "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes") + if err != nil || !strings.Contains(errOut, "Delete kubeconfig:") || !strings.Contains(errOut, `"id": "prod-kc-aaa"`) { + t.Errorf("err=%v debug=%q", err, errOut) + } + assertNoPrivateKey(t, "delete debug", errOut) +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_describe.go b/internal/verda-cli/cmd/mk8s/kubeconfig_describe.go new file mode 100644 index 00000000..077653e1 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_describe.go @@ -0,0 +1,97 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "fmt" + "io" + "text/tabwriter" + + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdKubeconfigDescribe creates `verda mk8s kubeconfig describe`. +func NewCmdKubeconfigDescribe(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + scope := &poolScope{} + cmd := &cobra.Command{ + Use: "describe []", + Aliases: []string{"get", "show"}, + Short: "Show a kubeconfig", + Long: longDesc(` + Show a kubeconfig: who it is for, its groups, its status and when + it expires. This never prints the kubeconfig file or its private + key, in any output format. To write the file, use kubeconfig save. + + Without or --cluster, a terminal session shows a picker. + `), + Example: cmdutil.Examples(` + # Pick the cluster and the kubeconfig from lists (terminal only) + verda mk8s kubeconfig describe + + verda mk8s kubeconfig describe $KUBECONFIG_ID --cluster prod + verda mk8s kubeconfig describe $KUBECONFIG_ID --cluster prod -o json + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runKubeconfigDescribe(cmd, f, ioStreams, scope, args) + }, + } + scope.addFlags(cmd) + return cmd +} + +func runKubeconfigDescribe(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string) error { + if err := checkKubeconfigTarget(f, cmd, scope, args); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := scope.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + view, err := resolveKubeconfig(cmd, f, ioStreams, client, target, args, true) + if err != nil || view == nil { + return err + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response: kubeconfig:", view) + + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), view); wrote { + return err + } + return printKubeconfigDescribe(ioStreams.Out, target, view) +} + +func printKubeconfigDescribe(w io.Writer, t *clusterTarget, v *KubeconfigView) error { + tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + rows := [][2]string{ + {"ID", v.ID}, + {"Display name", cmdutil.TextColumn(v.DisplayName)}, + {"Common name", v.CommonName}, + {"Groups", groupsColumn(v.Groups)}, + {"Status", v.Status}, + {"Expires", expiresColumn(v.NotAfter)}, + {"Cluster", t.Name}, + {"Location", t.Location}, + } + for _, r := range rows { + _, _ = fmt.Fprintf(tw, "%s:\t%s\n", r[0], r[1]) + } + return tw.Flush() +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go new file mode 100644 index 00000000..42c182ec --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go @@ -0,0 +1,331 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "reflect" + "regexp" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + "go.yaml.in/yaml/v3" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestKubeconfigDescribe_JSONHasEveryFieldInItsPlace(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + if got := decodeJSON[KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()[0]) { + t.Errorf("got %+v\nwant %+v", got, kcWantViews()[0]) + } + if got := sortedKeys(decodeJSON[map[string]any](t, out)); !slices.Equal(got, []string{"common_name", "display_name", "groups", "id", "not_after", "status"}) { + t.Errorf("keys = %v: the view has no YAML and no file", got) + } + + out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "describe", "prod-kc-bbb", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + if got := decodeJSON[KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()[1]) { + t.Errorf("pending: got %+v, want %+v", got, kcWantViews()[1]) + } + raw := decodeJSON[map[string]any](t, out) + if groups, ok := raw["groups"].([]any); !ok || len(groups) != 0 { + t.Errorf("groups = %#v, want []", raw["groups"]) + } + if v, ok := raw["not_after"]; !ok || v != nil { + t.Errorf("not_after = %#v, want null", v) + } +} + +func TestKubeconfigDescribe_YAMLShape(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "show", "prod-kc-aaa", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + var got map[string]any + if err := yaml.Unmarshal([]byte(out), &got); err != nil { + t.Fatal(err) + } + want := map[string]any{ + "id": "prod-kc-aaa", "display_name": "alice-laptop", "common_name": "alice", + "groups": []any{"system:masters", "dev"}, "status": "ready", "not_after": kcExpiry, + } + if !reflect.DeepEqual(got, want) { + t.Errorf("got %v\nwant %v", got, want) + } +} + +var kcDescribeLine = regexp.MustCompile(`^([A-Za-z][A-Za-z ]*):\s+(\S.*)$`) + +func TestKubeconfigDescribe_Table(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + parse := func(id string) map[string]string { + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "describe", id, "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + got := map[string]string{} + for line := range strings.SplitSeq(out, "\n") { + if m := kcDescribeLine.FindStringSubmatch(line); m != nil { + got[m[1]] = strings.TrimSpace(m[2]) + } + } + return got + } + want := map[string]string{ + "ID": "prod-kc-aaa", "Display name": "alice-laptop", "Common name": "alice", "Groups": "system:masters,dev", + "Status": "ready", "Expires": "2027-02-17 09:30 UTC", "Cluster": "prod", "Location": "FIN-03", + } + if got := parse("prod-kc-aaa"); !reflect.DeepEqual(got, want) { + t.Errorf("got %v\nwant %v", got, want) + } + want = map[string]string{ + "ID": "prod-kc-bbb", "Display name": "-", "Common name": "ci-bot", "Groups": "-", + "Status": "pending", "Expires": "-", "Cluster": "prod", "Location": "FIN-03", + } + if got := parse("prod-kc-bbb"); !reflect.DeepEqual(got, want) { + t.Errorf("pending: got %v\nwant %v", got, want) + } + wantReqs := []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-aaa", "/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-bbb"} + if got := api.paths("GET"); !slices.Equal(got, wantReqs) { + t.Errorf("requests = %v, want %v", got, wantReqs) + } +} + +func TestKubeconfigDescribe_AgentAndLocation(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "kubeconfig", "get", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03") + if err != nil { + t.Fatal(err) + } + if v := decodeJSON[KubeconfigView](t, out); !reflect.DeepEqual(v, kcWantViews()[0]) { + t.Errorf("view = %+v", v) + } + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-aaa"}) { + t.Errorf("-l must skip the cluster lookup, got %v", got) + } +} + +func TestKubeconfigDescribe_MissingTargetsAndErrors(t *testing.T) { + t.Parallel() + client := newFakeClient(t, kcFixtureAPI()) + for _, f := range []*cmdutil.TestFactory{agentFactory(client), tableFactory(client, nil)} { + _, _, err := runMK8s(t, f, "kubeconfig", "describe") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { + t.Errorf("missing = %v", missing) + } + } + _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "describe", "prod-kc-aaa") + if ae := cmdutil.ClassifyError(err); ae == nil || !slices.Equal(ae.Details["missing"].([]string), []string{"--cluster"}) { + t.Errorf("only --cluster missing: %v", err) + } + _, _, err = runMK8s(t, agentFactory(client), "kubeconfig", "describe", "--cluster", "prod") + if ae := cmdutil.ClassifyError(err); ae == nil || !slices.Equal(ae.Details["missing"].([]string), []string{""}) { + t.Errorf("only missing: %v", err) + } + if _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "describe", " ", "--cluster", "prod"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Errorf("blank id: %v", err) + } + if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "describe", "ghost", "--cluster", "prod"); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown id: %v", err) + } + if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "ghost"); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } + + api := newFakeAPI() + api.disabled = true + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "describe", "x", "--cluster", "prod", "-l", "FIN-03") + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Errorf("not enabled: %v", err) + } +} + +func TestKubeconfigDescribe_Debug(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) + f.DebugOverride = true + _, errOut, err := runMK8s(t, f, "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + for _, s := range []string{"API response: kubeconfig:", `"id": "prod-kc-aaa"`, `"common_name": "alice"`, `"status": "ready"`} { + if !strings.Contains(errOut, s) { + t.Errorf("debug output misses %q:\n%s", s, errOut) + } + } + if strings.Contains(errOut, `"kubeconfig"`) || strings.Contains(errOut, "client-key-data") { + t.Errorf("debug output has the kubeconfig YAML:\n%s", errOut) + } +} + +func TestKubeconfigDescribe_Pickers(t *testing.T) { + withTerminal(t) + + t.Run("cluster picker then kubeconfig picker", func(t *testing.T) { + api := kcFixtureAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 1)), "kubeconfig", "describe") + if err != nil || !strings.Contains(out, "prod-kc-bbb") || !strings.Contains(out, "ci-bot") { + t.Fatalf("out=%q err=%v", out, err) + } + // The list has every field of the view and never the YAML: no second read. + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs"}) { + t.Errorf("requests = %v", got) + } + }) + + t.Run("kubeconfig picker only", func(t *testing.T) { + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, kcFixtureAPI()), pickerAnswers(0)), "kubeconfig", "describe", "--cluster", "prod") + if err != nil || !strings.Contains(out, "prod-kc-aaa") { + t.Fatalf("out=%q err=%v", out, err) + } + }) + + t.Run("the picker offers every kubeconfig and Cancel", func(t *testing.T) { + p := &kcSelectRecorder{Prompter: pickerAnswers(2)} + f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) + f.PrompterOverride = p + if _, _, err := runMK8s(t, f, "kubeconfig", "describe", "--cluster", "prod"); err != nil { + t.Fatal(err) + } + calls := p.calls + if len(calls) != 1 || calls[0].Message != "Select kubeconfig" || !calls[0].Hints || len(calls[0].Labels) != 3 || calls[0].Labels[2] != cancelLabel { + t.Fatalf("calls = %+v", calls) + } + if !strings.Contains(calls[0].Labels[0], "prod-kc-aaa") || !strings.Contains(calls[0].Labels[1], "prod-kc-bbb") { + t.Errorf("labels = %v", calls[0].Labels) + } + }) +} + +func TestKubeconfigDescribe_PickerCancelAndEmpty(t *testing.T) { + withTerminal(t) + + t.Run("cancel row, Esc and Ctrl+C end cleanly", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "cancel row": {Prompter: pickerAnswers(2)}, + "esc": {Prompter: tuitest.New(), selectErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), selectErr: errInterrupted()}, + } { + f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) + f.PrompterOverride = p + out, errOut, err := runMK8s(t, f, "kubeconfig", "describe", "--cluster", "prod") + if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { + t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) + } + } + }) + + t.Run("no kubeconfigs", func(t *testing.T) { + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), tuitest.New()), "kubeconfig", "describe", "--cluster", "prod") + if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "kubeconfig create --cluster prod") { + t.Errorf("out=%q err=%q error=%v", out, errOut, err) + } + }) +} + +// kcOutputFormats runs one command in each output mode, with --debug on. +func kcOutputFormats(client *verda.Client) map[string]*cmdutil.TestFactory { + return map[string]*cmdutil.TestFactory{ + "table": tableFactory(client, nil), + "json": jsonFactory(client), + "yaml": yamlFactory(client), + "agent": agentFactory(client), + } +} + +func assertNoPrivateKey(t *testing.T, label string, outs ...string) { + t.Helper() + for _, s := range outs { + if strings.Contains(s, kcPrivateKeyMarker) || strings.Contains(s, "client-key-data") || strings.Contains(s, "apiVersion: v1") { + t.Errorf("%s: the private key reached the output:\n%s", label, s) + } + } +} + +// Security: the YAML (with the client key) never reaches stdout, stderr, --debug or an error, in any format. +func TestKubeconfigOutputNeverHasThePrivateKey(t *testing.T) { + t.Parallel() + commands := map[string][]string{ + "describe": {"kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod"}, + "describe other": {"kubeconfig", "describe", "prod-kc-bbb", "--cluster", "prod", "-l", "FIN-03"}, + "list": {"kubeconfig", "list", "--cluster", "prod"}, + "delete": {"kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "--yes"}, + "describe 404": {"kubeconfig", "describe", "ghost", "--cluster", "prod"}, + } + for cname, args := range commands { + for fname := range kcOutputFormats(nil) { + t.Run(cname+"/"+fname, func(t *testing.T) { + t.Parallel() + f := kcOutputFormats(newFakeClient(t, kcFixtureAPI()))[fname] + f.DebugOverride = true + out, errOut, err := runMK8s(t, f, args...) + errText := "" + if err != nil { + errText = err.Error() + } + assertNoPrivateKey(t, cname+"/"+fname, out, errOut, errText) + }) + } + } + + t.Run("API errors", func(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} + f := agentFactory(newFakeClient(t, api)) + f.DebugOverride = true + out, errOut, err := runMK8s(t, f, "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod") + if err == nil { + t.Fatal("want an error") + } + assertNoPrivateKey(t, "500", out, errOut, err.Error()) + }) +} + +func TestKubeconfigPickerAndConfirmPathsNeverPrintTheKey(t *testing.T) { + withTerminal(t) + f := tableFactory(newFakeClient(t, kcFixtureAPI()), pickerAnswers(0, 0)) + f.DebugOverride = true + out, errOut, err := runMK8s(t, f, "kubeconfig", "describe") + if err != nil { + t.Fatal(err) + } + assertNoPrivateKey(t, "describe pickers", out, errOut) + + f = tableFactory(newFakeClient(t, kcFixtureAPI()), pickerAnswers(0, 0).AddConfirm(true)) + f.DebugOverride = true + out, errOut, err = runMK8s(t, f, "kubeconfig", "delete") + if err != nil { + t.Fatal(err) + } + assertNoPrivateKey(t, "delete pickers", out, errOut) +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_file.go b/internal/verda-cli/cmd/mk8s/kubeconfig_file.go new file mode 100644 index 00000000..8e09d982 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_file.go @@ -0,0 +1,239 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "regexp" + + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" + "github.com/verda-cloud/verda-cli/internal/verda-cli/options" +) + +var nonFileNameChars = regexp.MustCompile(`[^A-Za-z0-9_-]`) + +// inputError is a problem with the file the user chose (taken, a symlink, a +// missing or read-only directory), as opposed to an I/O failure while writing. +type inputError struct{ msg string } + +func (e *inputError) Error() string { return e.msg } + +// asUsage turns an inputError into a usage error (VALIDATION_ERROR, exit 2 in +// agent mode); any other error is returned as it is. +func asUsage(cmd *cobra.Command, err error) error { + if ie, ok := errors.AsType[*inputError](err); ok { + return cmdutil.UsageErrorf(cmd, "%s", ie.msg) + } + return err +} + +func existsMessage(path string) string { + return path + " already exists; use --force to replace it or --file to choose another path" +} + +// kubeconfigTarget is where a kubeconfig YAML is written. The YAML holds a +// client private key: the file is 0600, never created over an existing file +// without force, and never written through a symlink. +type kubeconfigTarget struct { + path string + // defaultDir is true for the default path: its parent (~/.kube) is created + // with 0700 if missing. A custom --file parent must already exist. + defaultDir bool +} + +// newKubeconfigTarget resolves --file: empty, or equal to the default path, +// gives ~/.kube/verda-..yaml. +func newKubeconfigTarget(file, cluster, commonName string) (kubeconfigTarget, error) { + home, err := os.UserHomeDir() + if err != nil { + if file != "" { + return kubeconfigTarget{path: file}, nil + } + return kubeconfigTarget{}, fmt.Errorf("find home directory for the default kubeconfig path (use --file): %w", err) + } + // '.' is not a sanitized character, so it keeps (cluster "a-b", cn "c") and + // (cluster "a", cn "b-c") apart: with '-' both would be verda-a-b-c.yaml and + // --force would overwrite the other cluster's key. + name := "verda-" + sanitizeFileName(cluster) + "." + sanitizeFileName(commonName) + ".yaml" + def := filepath.Join(home, ".kube", name) + if file != "" && filepath.Clean(file) != def { + return kubeconfigTarget{path: file}, nil + } + return kubeconfigTarget{path: def, defaultDir: true}, nil +} + +// sanitizeFileName replaces every character other than letters, digits, '-' and '_' with '-'. +func sanitizeFileName(s string) string { + return nonFileNameChars.ReplaceAllString(s, "-") +} + +// Check validates the target before any API call that would issue a +// certificate: the path must be free (or replaceable with force) and its +// directory writable. It leaves nothing behind. +func (t kubeconfigTarget) Check(force bool) error { + if err := t.checkFree(force); err != nil { + return err + } + dir := filepath.Dir(t.path) + if t.defaultDir { + // ~/.kube may not exist yet: it is created on write, so probe the + // nearest directory that does. + dir = nearestExistingDir(dir) + } else if _, err := os.Stat(dir); err != nil { + return &inputError{msg: fmt.Sprintf("directory %s for the kubeconfig file does not exist or is not readable: %v", dir, err)} + } + return probeWritable(dir) +} + +// checkFree: nothing is at the path, or force may replace a regular file. +func (t kubeconfigTarget) checkFree(force bool) error { + info, err := os.Lstat(t.path) + if errors.Is(err, fs.ErrNotExist) { + return nil + } + if err != nil { + return fmt.Errorf("check %s: %w", t.path, err) + } + switch { + case info.Mode()&fs.ModeSymlink != 0: + return &inputError{msg: t.path + " is a symlink; refusing to write a private key through it"} + case info.IsDir(): + return &inputError{msg: t.path + " is a directory"} + case !info.Mode().IsRegular(): + return &inputError{msg: t.path + " is not a regular file"} + case !force: + return &inputError{msg: existsMessage(t.path)} + } + return nil +} + +func nearestExistingDir(dir string) string { + for { + if _, err := os.Stat(dir); err == nil { + return dir + } + parent := filepath.Dir(dir) + if parent == dir { + return dir + } + dir = parent + } +} + +// probeWritable fails now, before a certificate is issued, when the key could +// not be saved afterwards (read-only mount, root-owned ~/.kube after sudo). +func probeWritable(dir string) error { + f, err := os.CreateTemp(dir, ".verda-write-check-*") + if err != nil { + return &inputError{msg: fmt.Sprintf("cannot write to %s: %v", dir, err)} + } + name := f.Name() + _ = f.Close() + _ = os.Remove(name) + return nil +} + +// Write stores data in the target with mode 0600. Without force the file is +// created exclusively (O_EXCL decides, not an earlier check, so a file that +// appears after Check is never overwritten). Force replaces an existing +// regular file through a temp file in the same directory and a rename. +func (t kubeconfigTarget) Write(data []byte, force bool) error { + if err := t.Check(force); err != nil { + return err + } + if t.defaultDir { + if err := t.ensureDefaultDir(); err != nil { + return err + } + } + if !force { + return t.create(data) + } + if _, err := os.Lstat(t.path); err != nil { + if errors.Is(err, fs.ErrNotExist) { + return t.create(data) + } + return fmt.Errorf("check %s: %w", t.path, err) + } + return t.replace(data) +} + +// ensureDefaultDir creates ~/.kube with 0700 only when it is missing; an +// existing directory keeps its mode. +func (t kubeconfigTarget) ensureDefaultDir() error { + dir := filepath.Dir(t.path) + if _, err := os.Stat(dir); err == nil { + return nil + } + if err := options.MkdirSecure(dir); err != nil { + return fmt.Errorf("create %s: %w", dir, err) + } + return nil +} + +// create makes a new file; the 0600 mode is already the most permissive result +// of any umask, so no chmod follows. +func (t kubeconfigTarget) create(data []byte) error { + f, err := os.OpenFile(t.path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) //nolint:gosec // path is the user's --file or the default kubeconfig path + if err != nil { + if errors.Is(err, fs.ErrExist) { + return errors.New(existsMessage(t.path)) + } + return fmt.Errorf("create %s: %w", t.path, err) + } + if _, err := f.Write(data); err != nil { + _ = f.Close() + _ = os.Remove(t.path) + return fmt.Errorf("write %s: %w", t.path, err) + } + if err := f.Close(); err != nil { + _ = os.Remove(t.path) + return fmt.Errorf("write %s: %w", t.path, err) + } + return nil +} + +func (t kubeconfigTarget) replace(data []byte) error { + tmp, err := os.CreateTemp(filepath.Dir(t.path), ".verda-kubeconfig-*") // created 0600 + if err != nil { + return fmt.Errorf("create temp file next to %s: %w", t.path, err) + } + tmpPath := tmp.Name() + cleanup := func(err error) error { + _ = tmp.Close() + _ = os.Remove(tmpPath) + return err + } + if _, err := tmp.Write(data); err != nil { + return cleanup(fmt.Errorf("write %s: %w", tmpPath, err)) + } + // Sync before the rename: a crash must not leave an empty file in place of the old one. + if err := tmp.Sync(); err != nil { + return cleanup(fmt.Errorf("write %s: %w", tmpPath, err)) + } + if err := tmp.Close(); err != nil { + return cleanup(fmt.Errorf("write %s: %w", tmpPath, err)) + } + if err := os.Rename(tmpPath, t.path); err != nil { + return cleanup(fmt.Errorf("replace %s: %w", t.path, err)) + } + return nil +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go new file mode 100644 index 00000000..1199b966 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go @@ -0,0 +1,462 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "encoding/json" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + "go.yaml.in/yaml/v3" +) + +func skipModeTestOnWindows(t *testing.T) { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("POSIX file modes are not enforced on Windows") + } +} + +func readFile(t *testing.T, path string) string { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read %s: %v", path, err) + } + return string(b) +} + +// assertMode checks the POSIX mode; Windows has none, so only that check is skipped. +func assertMode(t *testing.T, path string, want os.FileMode) { + t.Helper() + if runtime.GOOS == "windows" { + return + } + info, err := os.Stat(path) + if err != nil { + t.Fatalf("stat %s: %v", path, err) + } + if got := info.Mode().Perm(); got != want { + t.Errorf("%s mode = %o, want %o", path, got, want) + } +} + +func TestSanitizeFileName(t *testing.T) { + t.Parallel() + cases := map[string]string{ + "alice": "alice", + "a_b-c9": "a_b-c9", + "alice@corp.com": "alice-corp-com", + "a b/c\\d": "a-b-c-d", + "../etc": "---etc", + } + for in, want := range cases { + if got := sanitizeFileName(in); got != want { + t.Errorf("sanitizeFileName(%q) = %q, want %q", in, got, want) + } + } +} + +func TestNewKubeconfigTarget_DefaultPath(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + + tgt, err := newKubeconfigTarget("", "prod", "alice@corp.com") + if err != nil { + t.Fatal(err) + } + want := filepath.Join(home, ".kube", "verda-prod.alice-corp-com.yaml") + if tgt.path != want { + t.Errorf("path = %q, want %q", tgt.path, want) + } + if !tgt.defaultDir { + t.Error("default path must create its parent") + } + + other, _ := newKubeconfigTarget("", "prod", "bob") + if other.path == tgt.path { + t.Error("different common names must give different files") + } + + custom, _ := newKubeconfigTarget("/x/y.yaml", "prod", "alice") + if custom.path != "/x/y.yaml" || custom.defaultDir { + t.Errorf("custom target = %+v", custom) + } +} + +func TestKubeconfigTarget_DefaultCreatesKubeDir0700(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + + tgt, err := newKubeconfigTarget("", "prod", "alice") + if err != nil { + t.Fatal(err) + } + if err := tgt.Check(false); err != nil { + t.Fatalf("Check with missing ~/.kube: %v", err) + } + if _, err := os.Stat(filepath.Join(home, ".kube")); err == nil { + t.Fatal("Check must not create ~/.kube") + } + if err := tgt.Write([]byte(kcPrivateKeyMarker), false); err != nil { + t.Fatal(err) + } + assertMode(t, filepath.Join(home, ".kube"), 0o700) + assertMode(t, tgt.path, 0o600) + if got := readFile(t, tgt.path); got != kcPrivateKeyMarker { + t.Errorf("content = %q", got) + } +} + +func TestKubeconfigTarget_ExistingKubeDirKeepsMode(t *testing.T) { + skipModeTestOnWindows(t) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + kube := filepath.Join(home, ".kube") + if err := os.Mkdir(kube, 0o755); err != nil { + t.Fatal(err) + } + if err := os.Chmod(kube, 0o755); err != nil { + t.Fatal(err) + } + + tgt, _ := newKubeconfigTarget("", "prod", "alice") + if err := tgt.Write([]byte("x"), false); err != nil { + t.Fatal(err) + } + assertMode(t, kube, 0o755) + assertMode(t, tgt.path, 0o600) +} + +func TestKubeconfigTarget_CustomParentMustExist(t *testing.T) { + t.Parallel() + tgt := kubeconfigTarget{path: filepath.Join(t.TempDir(), "missing", "kc.yaml")} + if err := tgt.Check(false); err == nil { + t.Fatal("Check must fail when the parent directory is missing") + } + if err := tgt.Write([]byte("x"), false); err == nil { + t.Fatal("Write must fail when the parent directory is missing") + } + if _, err := os.Stat(filepath.Dir(tgt.path)); err == nil { + t.Error("a custom --file parent must not be created") + } +} + +func TestKubeconfigTarget_ParentIsFile(t *testing.T) { + t.Parallel() + dir := t.TempDir() + parent := filepath.Join(dir, "afile") + if err := os.WriteFile(parent, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + if err := (kubeconfigTarget{path: filepath.Join(parent, "kc.yaml")}).Check(false); err == nil { + t.Fatal("a parent that is a file must be rejected") + } +} + +func TestKubeconfigTarget_NewFile0600(t *testing.T) { + t.Parallel() + tgt := kubeconfigTarget{path: filepath.Join(t.TempDir(), "kc.yaml")} + if err := tgt.Write([]byte(kcPrivateKeyMarker), false); err != nil { + t.Fatal(err) + } + assertMode(t, tgt.path, 0o600) + if got := readFile(t, tgt.path); got != kcPrivateKeyMarker { + t.Errorf("content = %q", got) + } +} + +func TestKubeconfigTarget_NoOverwriteWithoutForce(t *testing.T) { + t.Parallel() + path := filepath.Join(t.TempDir(), "kc.yaml") + if err := os.WriteFile(path, []byte("original"), 0o600); err != nil { + t.Fatal(err) + } + tgt := kubeconfigTarget{path: path} + + err := tgt.Check(false) + if err == nil || !strings.Contains(err.Error(), "--force") { + t.Fatalf("Check error = %v, want mention of --force", err) + } + if err := tgt.Write([]byte("new"), false); err == nil { + t.Fatal("Write must not overwrite without force") + } + if got := readFile(t, path); got != "original" { + t.Errorf("file changed to %q", got) + } +} + +func TestKubeconfigTarget_ForceReplacesAtomically(t *testing.T) { + t.Parallel() + dir := t.TempDir() + path := filepath.Join(dir, "kc.yaml") + if err := os.WriteFile(path, []byte("original"), 0o644); err != nil { //nolint:gosec // test: a permissive old file must end up 0600 + t.Fatal(err) + } + tgt := kubeconfigTarget{path: path} + if err := tgt.Check(true); err != nil { + t.Fatalf("Check with force: %v", err) + } + if err := tgt.Write([]byte("new"), true); err != nil { + t.Fatal(err) + } + if got := readFile(t, path); got != "new" { + t.Errorf("content = %q", got) + } + assertMode(t, path, 0o600) + + entries, _ := os.ReadDir(dir) + if len(entries) != 1 { + t.Errorf("temp file left behind: %v", entries) + } +} + +func TestKubeconfigTarget_ForceOnNewFileCreates(t *testing.T) { + t.Parallel() + tgt := kubeconfigTarget{path: filepath.Join(t.TempDir(), "kc.yaml")} + if err := tgt.Write([]byte("x"), true); err != nil { + t.Fatal(err) + } + assertMode(t, tgt.path, 0o600) +} + +func TestKubeconfigTarget_RejectsSymlink(t *testing.T) { + t.Parallel() + if runtime.GOOS == "windows" { + t.Skip("symlinks need privileges on Windows") + } + dir := t.TempDir() + victim := filepath.Join(dir, "victim") + if err := os.WriteFile(victim, []byte("victim"), 0o600); err != nil { + t.Fatal(err) + } + link := filepath.Join(dir, "kc.yaml") + if err := os.Symlink(victim, link); err != nil { + t.Fatal(err) + } + tgt := kubeconfigTarget{path: link} + for _, force := range []bool{false, true} { + if err := tgt.Check(force); err == nil || !strings.Contains(err.Error(), "symlink") { + t.Errorf("Check(force=%v) = %v, want symlink error", force, err) + } + if err := tgt.Write([]byte(kcPrivateKeyMarker), force); err == nil { + t.Errorf("Write(force=%v) must fail on a symlink", force) + } + } + if got := readFile(t, victim); got != "victim" { + t.Errorf("symlink target was written: %q", got) + } +} + +func TestNewKubeconfigTarget_NoCollisions(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + + // With '-' as the separator, the first two pairs would both be verda-a-b-c.yaml. + // Names that differ only in replaced characters (b.c vs b-c) still share a + // file for one cluster; only the cluster/common-name boundary is guaranteed. + pairs := [][2]string{{"a-b", "c"}, {"a", "b-c"}, {"a-b-c", ""}, {"", "a-b-c"}, {"a", ""}, {"", "a"}} + seen := map[string][2]string{} + for _, p := range pairs { + tgt, err := newKubeconfigTarget("", p[0], p[1]) + if err != nil { + t.Fatal(err) + } + if prev, dup := seen[tgt.path]; dup { + t.Errorf("%v and %v both map to %s", prev, p, tgt.path) + } + seen[tgt.path] = p + } +} + +func TestKubeconfigTarget_CreateIsExclusive(t *testing.T) { + t.Parallel() + dir := t.TempDir() + + // A file that appears after Check must not be overwritten by Write without force. + path := filepath.Join(dir, "kc.yaml") + tgt := kubeconfigTarget{path: path} + if err := tgt.Check(false); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte("appeared"), 0o600); err != nil { + t.Fatal(err) + } + err := tgt.create([]byte("new")) + if err == nil || !strings.Contains(err.Error(), "--force") { + t.Fatalf("create over an existing file = %v, want an error naming --force", err) + } + if got := readFile(t, path); got != "appeared" { + t.Errorf("file changed to %q", got) + } +} + +func TestKubeconfigTarget_CreateRefusesPlantedSymlink(t *testing.T) { + t.Parallel() + if runtime.GOOS == "windows" { + t.Skip("symlinks need privileges on Windows") + } + dir := t.TempDir() + victim := filepath.Join(dir, "victim") + link := filepath.Join(dir, "kc.yaml") + if err := os.Symlink(victim, link); err != nil { // dangling: O_EXCL must still refuse + t.Fatal(err) + } + tgt := kubeconfigTarget{path: link} + if err := tgt.Check(true); err == nil || !strings.Contains(err.Error(), "symlink") { + t.Errorf("Check on a dangling symlink = %v, want symlink error", err) + } + if err := tgt.create([]byte(kcPrivateKeyMarker)); err == nil { + t.Error("create must refuse a symlink planted after Check") + } + if _, err := os.Stat(victim); err == nil { + t.Error("the symlink target was created") + } +} + +func TestKubeconfigTarget_UnwritableDirFailsCheck(t *testing.T) { + t.Parallel() + skipModeTestOnWindows(t) + if os.Geteuid() == 0 { + t.Skip("root can write anywhere") + } + dir := t.TempDir() + if err := os.Chmod(dir, 0o500); err != nil { //nolint:gosec // test: a read-only directory + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chmod(dir, 0o700) }) //nolint:gosec // test cleanup + + tgt := kubeconfigTarget{path: filepath.Join(dir, "kc.yaml")} + if err := tgt.Check(false); err == nil || !strings.Contains(err.Error(), "cannot write") { + t.Errorf("Check in a read-only directory = %v, want a cannot-write error", err) + } + if entries, _ := os.ReadDir(dir); len(entries) != 0 { + t.Errorf("Check left files behind: %v", entries) + } +} + +func TestKubeconfigTarget_UnwritableHomeFailsCheckWithoutKubeDir(t *testing.T) { + skipModeTestOnWindows(t) + if os.Geteuid() == 0 { + t.Skip("root can write anywhere") + } + home := t.TempDir() + if err := os.Chmod(home, 0o500); err != nil { //nolint:gosec // test: a read-only home + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chmod(home, 0o700) }) //nolint:gosec // test cleanup + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + + tgt, err := newKubeconfigTarget("", "prod", "alice") + if err != nil { + t.Fatal(err) + } + if err := tgt.Check(false); err == nil { + t.Fatal("Check must fail when ~/.kube is missing and the home directory is not writable") + } +} + +func TestKubeconfigTarget_RejectsDirectory(t *testing.T) { + t.Parallel() + tgt := kubeconfigTarget{path: t.TempDir()} + for _, force := range []bool{false, true} { + if err := tgt.Check(force); err == nil || !strings.Contains(err.Error(), "directory") { + t.Errorf("Check(force=%v) = %v, want directory error", force, err) + } + } +} + +func TestKubeconfigView_NeverContainsYAML(t *testing.T) { + t.Parallel() + notAfter := time.Date(2027, 2, 1, 0, 0, 0, 0, time.UTC) + k := &verda.ManagedKubernetesKubeconfig{ + ID: "prod-kc-abc", + DisplayName: "ci", + CommonName: "alice", + Groups: []string{"system:masters"}, + Status: verda.ManagedKubernetesKubeconfigStatusReady, + NotAfter: ¬After, + Kubeconfig: "client-key-data: " + kcPrivateKeyMarker, + } + v := newKubeconfigView(k, "/home/a/.kube/kc.yaml") + + j, err := json.Marshal(v) + if err != nil { + t.Fatal(err) + } + y, err := yaml.Marshal(v) + if err != nil { + t.Fatal(err) + } + for name, out := range map[string]string{"json": string(j), "yaml": string(y)} { + if strings.Contains(out, kcPrivateKeyMarker) || strings.Contains(out, "client-key-data") { + t.Errorf("%s leaks the kubeconfig YAML: %s", name, out) + } + if !strings.Contains(out, "prod-kc-abc") || !strings.Contains(out, "/home/a/.kube/kc.yaml") { + t.Errorf("%s misses metadata: %s", name, out) + } + } + + var m map[string]any + if err := json.Unmarshal(j, &m); err != nil { + t.Fatal(err) + } + for _, key := range []string{"id", "display_name", "common_name", "groups", "status", "not_after", "file"} { + if _, ok := m[key]; !ok { + t.Errorf("view JSON misses %q", key) + } + } + if len(m) != 7 { + t.Errorf("view has %d fields, want exactly 7: %v", len(m), m) + } +} + +func TestKubeconfigView_StableShape(t *testing.T) { + t.Parallel() + // A pending kubeconfig: no groups, no expiry, nothing written yet. + v := newKubeconfigView(&verda.ManagedKubernetesKubeconfig{ID: "c-kc-1", CommonName: "alice", Status: "pending"}, "") + j, err := json.Marshal(v) + if err != nil { + t.Fatal(err) + } + var m map[string]any + if err := json.Unmarshal(j, &m); err != nil { + t.Fatal(err) + } + for _, key := range []string{"id", "display_name", "common_name", "groups", "status", "not_after"} { + if _, ok := m[key]; !ok { + t.Errorf("pending view JSON misses %q: %s", key, j) + } + } + if g, ok := m["groups"].([]any); !ok || len(g) != 0 { + t.Errorf("groups = %v, want []", m["groups"]) + } + if m["not_after"] != nil { + t.Errorf("not_after = %v, want null", m["not_after"]) + } + if _, ok := m["file"]; ok { + t.Errorf("file must be absent before a write: %s", j) + } +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_list.go b/internal/verda-cli/cmd/mk8s/kubeconfig_list.go new file mode 100644 index 00000000..b3d03903 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_list.go @@ -0,0 +1,191 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "strings" + "text/tabwriter" + "time" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const argKubeconfigRef = "" + +// NewCmdKubeconfigList creates `verda mk8s kubeconfig list`. +func NewCmdKubeconfigList(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + scope := &poolScope{} + cmd := &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: "List the kubeconfigs of a cluster", + Long: longDesc(` + List the kubeconfigs of a cluster: their IDs, who each one is for, + its groups, its status and when it expires. EXPIRES is "-" while a + kubeconfig is pending. The list never shows the kubeconfig YAML. + + Without --cluster, a terminal session shows a picker. + `), + Example: cmdutil.Examples(` + verda mk8s kubeconfig list --cluster prod + + # JSON for scripts: expiry is in not_after + verda mk8s kubeconfig ls --cluster prod -o json + `), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + return runKubeconfigList(cmd, f, ioStreams, scope) + }, + } + scope.addFlags(cmd) + return cmd +} + +func runKubeconfigList(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope) error { + if missing := scope.missing(f, cmd); len(missing) > 0 { + return cmdutil.NewMissingFlagsError(missing) + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := scope.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + views, err := listKubeconfigViews(cmd.Context(), f, client, target) + if err != nil { + return err + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), fmt.Sprintf("API response: %d kubeconfig(s):", len(views)), views) + + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), views); wrote { + return err + } + if len(views) == 0 { + printNoKubeconfigs(ioStreams, target.Name) + return nil + } + + tw := tabwriter.NewWriter(ioStreams.Out, 0, 0, 2, ' ', 0) + _, _ = fmt.Fprintln(tw, "ID\tDISPLAY NAME\tCOMMON NAME\tGROUPS\tSTATUS\tEXPIRES") + for i := range views { + v := &views[i] + _, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\n", v.ID, cmdutil.TextColumn(v.DisplayName), v.CommonName, groupsColumn(v.Groups), v.Status, expiresColumn(v.NotAfter)) + } + return tw.Flush() +} + +// listKubeconfigViews and getKubeconfigView convert inside the call, so an SDK +// struct (with the YAML and its private key) never reaches a printer or the debug log. +func listKubeconfigViews(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget) ([]KubeconfigView, error) { + return apiCall(ctx, f, "Loading kubeconfigs...", func(ctx context.Context) ([]KubeconfigView, error) { + list, err := client.ManagedKubernetes.ListKubeconfigs(ctx, t.Location, t.Name) + if err != nil { + return nil, err + } + views := make([]KubeconfigView, 0, len(list)) + for i := range list { + views = append(views, newKubeconfigView(&list[i], "")) + } + return views, nil + }) +} + +func getKubeconfigView(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget, id string) (*KubeconfigView, error) { + return apiCall(ctx, f, "Loading kubeconfig...", func(ctx context.Context) (*KubeconfigView, error) { + k, err := client.ManagedKubernetes.GetKubeconfig(ctx, t.Location, t.Name, id) + if err != nil { + return nil, err + } + v := newKubeconfigView(k, "") + return &v, nil + }) +} + +// checkKubeconfigTarget fails before any call when --cluster or cannot be +// asked for. It lists everything missing at once. +func checkKubeconfigTarget(f cmdutil.Factory, cmd *cobra.Command, scope *poolScope, args []string) error { + missing := scope.missing(f, cmd) + if poolArgMissing(f, args) { + missing = append(missing, argKubeconfigRef) + } + if len(missing) > 0 { + return cmdutil.NewMissingFlagsError(missing) + } + return nil +} + +// resolveKubeconfig: a named id is read when read is set and used as given +// otherwise; with no id a picker lists the cluster's kubeconfigs. A nil result +// with a nil error means the user canceled or the cluster has none. +func resolveKubeconfig(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, args []string, read bool) (*KubeconfigView, error) { + if len(args) > 0 { + id := strings.TrimSpace(args[0]) + if !read { + return &KubeconfigView{ID: id}, nil + } + return getKubeconfigView(cmd.Context(), f, client, t, id) + } + views, err := listKubeconfigViews(cmd.Context(), f, client, t) + if err != nil { + return nil, err + } + return pickKubeconfig(cmd.Context(), f, ioStreams, t, views) +} + +func pickKubeconfig(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, views []KubeconfigView) (*KubeconfigView, error) { + if len(views) == 0 { + printNoKubeconfigs(ioStreams, t.Name) + return nil, nil + } + labels := make([]string, 0, len(views)+1) + for i := range views { + v := &views[i] + labels = append(labels, fmt.Sprintf("%s (%s, %s)", v.ID, v.CommonName, v.Status)) + } + labels = append(labels, cancelLabel) + idx, err := f.Prompter().Select(ctx, "Select kubeconfig", labels, pickerHints()...) + switch { + case err == nil && idx >= 0 && idx < len(views): + return &views[idx], nil + case err != nil && !cmdutil.IsPromptCancel(err): + return nil, err + } + printCanceled(ioStreams) + return nil, nil +} + +func printNoKubeconfigs(ioStreams cmdutil.IOStreams, cluster string) { + _, _ = fmt.Fprintf(ioStreams.Out, "No kubeconfigs in cluster %s.\n", cluster) + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Create one with: verda mk8s kubeconfig create --cluster %s --common-name --admin\n", cluster) +} + +func groupsColumn(groups []string) string { + return cmdutil.TextColumn(strings.Join(groups, ",")) +} + +// expiresColumn is "-" while the kubeconfig is pending: the API sets not_after when it issues the certificate. +func expiresColumn(t *time.Time) string { + if t == nil { + return "-" + } + return timeColumn(*t) +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go new file mode 100644 index 00000000..cca402bf --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go @@ -0,0 +1,262 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "reflect" + "slices" + "strings" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + "go.yaml.in/yaml/v3" + + "github.com/verda-cloud/verda-cli/pkg/tui" + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// kcPrivateKeyMarker stands for the client key in the fake kubeconfig YAML: it must never be printed. +const kcPrivateKeyMarker = "MARKER-PRIVATE-KEY" + +var kcExpiry = time.Date(2027, 2, 17, 9, 30, 0, 0, time.UTC) + +// kcFixtureAPI has the clusters of seededClusters and two kubeconfigs in "prod": a +// ready one with groups and an expiry, and a pending one with neither. +func kcFixtureAPI() *fakeAPI { + api := seededClusters() + api.addKubeconfig("prod", verda.ManagedKubernetesKubeconfig{ + ID: "prod-kc-aaa", DisplayName: "alice-laptop", CommonName: "alice", Groups: []string{"system:masters", "dev"}, + Status: verda.ManagedKubernetesKubeconfigStatusReady, NotAfter: &kcExpiry, + Kubeconfig: "apiVersion: v1\nusers:\n- user:\n client-key-data: " + kcPrivateKeyMarker + "\n", + }) + api.addKubeconfig("prod", verda.ManagedKubernetesKubeconfig{ + ID: "prod-kc-bbb", CommonName: "ci-bot", Status: verda.ManagedKubernetesKubeconfigStatusPending, + Kubeconfig: "client-key-data: " + kcPrivateKeyMarker, + }) + return api +} + +func kcWantViews() []KubeconfigView { + return []KubeconfigView{ + {ID: "prod-kc-aaa", DisplayName: "alice-laptop", CommonName: "alice", Groups: []string{"system:masters", "dev"}, Status: "ready", NotAfter: &kcExpiry}, + {ID: "prod-kc-bbb", CommonName: "ci-bot", Groups: []string{}, Status: "pending"}, + } +} + +func TestKubeconfigList_Table(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "list", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(out), "\n") + if len(lines) != 3 { + t.Fatalf("want a header and 2 rows:\n%s", out) + } + if got := strings.Join(strings.Fields(lines[0]), " "); got != "ID DISPLAY NAME COMMON NAME GROUPS STATUS EXPIRES" { + t.Errorf("header = %q", got) + } + want := [][]string{ + {"prod-kc-aaa", "alice-laptop", "alice", "system:masters,dev", "ready", "2027-02-17", "09:30", "UTC"}, + {"prod-kc-bbb", "-", "ci-bot", "-", "pending", "-"}, + } + for i, w := range want { + if got := strings.Fields(lines[i+1]); !slices.Equal(got, w) { + t.Errorf("row %d = %v, want %v", i, got, w) + } + } + wantReqs := []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs"} + if got := api.paths("GET"); !slices.Equal(got, wantReqs) { + t.Errorf("requests = %v, want %v", got, wantReqs) + } +} + +func TestKubeconfigList_LocationSkipsTheClusterLookup(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "ls", "--cluster", "prod", "-l", "FIN-03"); err != nil { + t.Fatal(err) + } + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters/FIN-03/prod/kubeconfigs"}) { + t.Errorf("requests = %v", got) + } +} + +func TestKubeconfigList_Structured(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, mk(kcFixtureAPI(), t), "kubeconfig", "list", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + if got := decodeJSON[[]KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()) { + t.Errorf("got %+v\nwant %+v", got, kcWantViews()) + } + for i, m := range decodeJSON[[]map[string]any](t, out) { + if got := sortedKeys(m); !slices.Equal(got, []string{"common_name", "display_name", "groups", "id", "not_after", "status"}) { + t.Errorf("item %d keys = %v", i, got) + } + } + }) + } + + t.Run("yaml", func(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "list", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + var got []map[string]any + if err := yaml.Unmarshal([]byte(out), &got); err != nil { + t.Fatal(err) + } + if len(got) != 2 || got[0]["id"] != "prod-kc-aaa" || got[1]["status"] != "pending" { + t.Fatalf("got %v", got) + } + if groups, _ := got[0]["groups"].([]any); !reflect.DeepEqual(groups, []any{"system:masters", "dev"}) { + t.Errorf("groups = %v", got[0]["groups"]) + } + if groups, ok := got[1]["groups"].([]any); !ok || len(groups) != 0 { + t.Errorf("pending groups = %#v, want an empty list", got[1]["groups"]) + } + if v, ok := got[1]["not_after"]; !ok || v != nil { + t.Errorf("pending not_after = %#v, want null", v) + } + if _, ok := got[0]["kubeconfig"]; ok { + t.Error("the YAML key must not be in the output") + } + }) +} + +func sortedKeys(m map[string]any) []string { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + slices.Sort(keys) + return keys +} + +func TestKubeconfigList_Empty(t *testing.T) { + t.Parallel() + client := newFakeClient(t, seededClusters()) + out, errOut, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "list", "--cluster", "prod") + if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "verda mk8s kubeconfig create --cluster prod") { + t.Errorf("out=%q err=%q error=%v", out, errOut, err) + } + for name, f := range map[string]*cmdutil.TestFactory{"json": jsonFactory(client), "agent": agentFactory(client)} { + out, _, err = runMK8s(t, f, "kubeconfig", "list", "--cluster", "prod") + if err != nil || strings.TrimSpace(out) != "[]" { + t.Errorf("%s: empty output = %q, error=%v", name, out, err) + } + } + out, _, err = runMK8s(t, yamlFactory(client), "kubeconfig", "list", "--cluster", "prod") + if err != nil || strings.TrimSpace(out) != "[]" { + t.Errorf("yaml: empty output = %q, error=%v", out, err) + } +} + +func TestKubeconfigList_ClusterErrors(t *testing.T) { + t.Parallel() + client := newFakeClient(t, kcFixtureAPI()) + for _, f := range []*cmdutil.TestFactory{tableFactory(client, nil), agentFactory(client), jsonFactory(client)} { + _, _, err := runMK8s(t, f, "kubeconfig", "list") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster"}) { + t.Errorf("missing = %v", missing) + } + } + if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "list", "--cluster", "ghost"); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } + if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "list", "--cluster", " "); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Errorf("blank cluster: %v", err) + } + if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "list", "extra", "--cluster", "prod"); err == nil { + t.Error("a positional argument must be rejected") + } +} + +func TestKubeconfigList_Debug(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) + f.DebugOverride = true + _, errOut, err := runMK8s(t, f, "kubeconfig", "list", "--cluster", "prod") + if err != nil || !strings.Contains(errOut, "API response: 2 kubeconfig(s):") || !strings.Contains(errOut, `"common_name": "alice"`) { + t.Errorf("err=%v debug=%q", err, errOut) + } + if strings.Contains(errOut, `"kubeconfig"`) { + t.Errorf("debug output has a kubeconfig key: %q", errOut) + } +} + +func TestKubeconfigList_ClusterPicker(t *testing.T) { + withTerminal(t) + api := kcFixtureAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0)), "kubeconfig", "list") + if err != nil || !strings.Contains(out, "prod-kc-aaa") { + t.Fatalf("out=%q err=%v", out, err) + } + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs"}) { + t.Errorf("requests = %v", got) + } + + f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) + f.PrompterOverride = &cancelPrompter{Prompter: pickerAnswers(), selectErr: contextCanceled()} + out, errOut, err := runMK8s(t, f, "kubeconfig", "list") + if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { + t.Errorf("esc: out=%q err=%q error=%v", out, errOut, err) + } +} + +func TestKubeconfigList_NotEnabled(t *testing.T) { + t.Parallel() + api := newFakeAPI() + api.disabled = true + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "list", "--cluster", "prod", "-l", "FIN-03") + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Fatalf("err = %v", err) + } +} + +type kcSelectCall struct { + Message string + Labels []string + Hints bool +} + +// kcSelectRecorder answers like the wrapped prompter and keeps what each Select showed. +type kcSelectRecorder struct { + *tuitest.Prompter + calls []kcSelectCall +} + +func (p *kcSelectRecorder) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { + p.calls = append(p.calls, kcSelectCall{Message: msg, Labels: labels, Hints: tui.ResolveSelectConfig(opts).ShowHints}) + return p.Prompter.Select(ctx, msg, labels, opts...) +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go new file mode 100644 index 00000000..72bff5d3 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go @@ -0,0 +1,231 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "fmt" + "os" + "os/user" + "strings" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + "github.com/verda-cloud/verda-cli/pkg/tui" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + accessAdminLabel = "Admin (full control of the cluster)" + accessCustomLabel = "Custom groups" + + revokeNote = "A downloaded certificate cannot be revoked before it expires; deleting the kubeconfig does not disable it." +) + +// currentUserName is the OS user name that pre-fills the common name; a var so tests can set it. +var currentUserName = func() string { + if u, err := user.Current(); err == nil && u.Username != "" { + // Windows gives DOMAIN\user. + return u.Username[strings.LastIndex(u.Username, `\`)+1:] + } + return os.Getenv("USER") +} + +// kubeconfigAsker asks the prompts of `kubeconfig create`. Each ask reports +// ok=false after printing "Canceled." when the user pressed Esc or Ctrl+C. +type kubeconfigAsker struct { + ctx context.Context + ioStreams cmdutil.IOStreams + prompter tui.Prompter +} + +func (a *kubeconfigAsker) text(msg string, opts ...tui.TextInputOption) (v string, ok bool, err error) { + v, err = a.prompter.TextInput(a.ctx, msg, opts...) + if err != nil { + ok, err = a.canceled(err) + return "", ok, err + } + return strings.TrimSpace(v), true, nil +} + +func (a *kubeconfigAsker) selectOne(msg string, labels []string) (idx int, ok bool, err error) { + idx, err = a.prompter.Select(a.ctx, msg, labels, pickerHints()...) + if err != nil { + ok, err = a.canceled(err) + return 0, ok, err + } + return idx, true, nil +} + +// canceled turns a prompt error into (false, nil) for Esc and Ctrl+C, else (false, err). +func (a *kubeconfigAsker) canceled(err error) (ok bool, retErr error) { + if !cmdutil.IsPromptCancel(err) { + return false, err + } + printCanceled(a.ioStreams) + return false, nil +} + +// promptKubeconfigCreate asks, in prompt order, for each value its flag did not +// give. It reports false when the user canceled. +func promptKubeconfigCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *kubeconfigCreateOptions, t *clusterTarget) (bool, error) { + a := &kubeconfigAsker{ctx: ctx, ioStreams: ioStreams, prompter: f.Prompter()} + steps := []func() (bool, error){ + func() (bool, error) { return a.askCommonName(opts) }, + func() (bool, error) { return a.askAccess(opts) }, + func() (bool, error) { return a.askDisplayName(opts) }, + func() (bool, error) { return a.askDuration(opts) }, + func() (bool, error) { return a.askFile(opts, t) }, + } + for _, step := range steps { + if ok, err := step(); err != nil || !ok { + return false, err + } + } + return true, nil +} + +func nonBlank(what string) func(string) error { + return func(s string) error { + if strings.TrimSpace(s) == "" { + return errors.New(what + " must not be empty") + } + return nil + } +} + +func (a *kubeconfigAsker) askCommonName(opts *kubeconfigCreateOptions) (bool, error) { + if opts.CommonName != "" { + return true, nil + } + v, ok, err := a.text("Common name (the user name in the cluster)", tui.WithDefault(currentUserName()), tui.WithValidation(nonBlank("common name"))) + if ok { + opts.CommonName = v + } + return ok, err +} + +func (a *kubeconfigAsker) askAccess(opts *kubeconfigCreateOptions) (bool, error) { + if opts.hasAccess() { + return true, nil + } + _, _ = fmt.Fprintf(a.ioStreams.ErrOut, "%s\n", revokeNote) + idx, ok, err := a.selectOne("Access", []string{accessAdminLabel, accessCustomLabel}) + if !ok { + return false, err + } + if idx == 0 { + opts.Admin = true + return true, nil + } + v, ok, err := a.text("Group names, separated by commas", tui.WithValidation(func(s string) error { + if len(cleanGroups(strings.Split(s, ","))) == 0 { + return errors.New("enter at least one group") + } + return nil + })) + if ok { + opts.Groups = cleanGroups(strings.Split(v, ",")) + } + return ok, err +} + +func (a *kubeconfigAsker) askDisplayName(opts *kubeconfigCreateOptions) (bool, error) { + if opts.displayNameSet { + return true, nil + } + v, ok, err := a.text("Display name (optional)") + if ok { + opts.DisplayName = v + } + return ok, err +} + +// askDuration shows the API default as a hint only: an empty answer leaves duration out. +func (a *kubeconfigAsker) askDuration(opts *kubeconfigCreateOptions) (bool, error) { + if opts.durationSet { + return true, nil + } + v, ok, err := a.text("Duration (optional, for example 720h)", tui.WithPlaceholder("default: 90 days"), tui.WithValidation(validateDurationInput)) + if ok { + opts.Duration = v + } + return ok, err +} + +// validateDurationInput applies the request's duration rule; empty leaves the API default. +func validateDurationInput(s string) error { + req := verda.CreateManagedKubernetesKubeconfigRequest{CommonName: "x", Duration: strings.TrimSpace(s)} + if err := req.Validate(); err != nil { + return errors.New(strings.TrimSuffix(err.Error(), ".")) + } + return nil +} + +func (a *kubeconfigAsker) askFile(opts *kubeconfigCreateOptions, t *clusterTarget) (bool, error) { + if opts.fileSet { + return true, nil + } + textOpts := []tui.TextInputOption{tui.WithValidation(kubeconfigFileValidator(t.Name, opts.CommonName, opts.Force))} + if def, err := newKubeconfigTarget("", t.Name, opts.CommonName); err == nil { + textOpts = append(textOpts, tui.WithDefault(def.path)) + } + v, ok, err := a.text("File to write the kubeconfig to", textOpts...) + if !ok { + return false, err + } + opts.File, opts.fileSet = v, true + return true, nil +} + +// kubeconfigFileValidator checks the typed path with the same rules as the write. +// "-" is refused: it would print the private key on the terminal. +func kubeconfigFileValidator(cluster, commonName string, force bool) func(string) error { + return func(s string) error { + s = strings.TrimSpace(s) + if s == kubeconfigStdout { + return errors.New("enter a file path; use --file - to print the YAML to stdout") + } + target, err := newKubeconfigTarget(s, cluster, commonName) + if err != nil { + return err + } + return target.Check(force) + } +} + +// confirmKubeconfigCreate prints the summary and asks. adminFlag: --admin was +// given, so the access prompt (which prints the note itself) did not run. +func confirmKubeconfigCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, req *verda.CreateManagedKubernetesKubeconfigRequest, dest *kubeconfigDest, adminFlag bool) (bool, error) { + display, duration, file := req.DisplayName, req.Duration, dest.path() + if display == "" { + display = "-" + } + if duration == "" { + duration = "API default (90 days)" + } + if dest.stdout { + file = "stdout" + } + _, _ = fmt.Fprintf(ioStreams.ErrOut, "\nCreate kubeconfig\n Cluster: %s (%s)\n Common name: %s\n Groups: %s\n Display name: %s\n Duration: %s\n File: %s\n", + t.Name, t.Location, req.CommonName, strings.Join(req.Groups, ", "), display, duration, file) + if adminFlag { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "\n%s\n", revokeNote) + } + _, _ = fmt.Fprintln(ioStreams.ErrOut) + return askConfirm(ctx, ioStreams, f.Prompter(), "Create kubeconfig?") +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go new file mode 100644 index 00000000..2e5a5e78 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go @@ -0,0 +1,439 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "os" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + "github.com/verda-cloud/verda-cli/pkg/tui" + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +type kcPrompt struct { + msg string + def string + placeholder string + hints bool + escLabel string +} + +// kcRecorder wraps a prompter and records what each prompt asked and how. +type kcRecorder struct { + tui.Prompter + texts []kcPrompt + selects []kcPrompt +} + +func (r *kcRecorder) TextInput(ctx context.Context, msg string, opts ...tui.TextInputOption) (string, error) { + var c tui.TextInputConfig + for _, o := range opts { + o(&c) + } + r.texts = append(r.texts, kcPrompt{msg: msg, def: c.Default, placeholder: c.Placeholder}) + return r.Prompter.TextInput(ctx, msg, opts...) +} + +func (r *kcRecorder) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { + var c tui.SelectConfig + for _, o := range opts { + o(&c) + } + r.selects = append(r.selects, kcPrompt{msg: msg, hints: c.ShowHints, escLabel: c.RelabelByID["esc"]}) + return r.Prompter.Select(ctx, msg, labels, opts...) +} + +// asked reports whether a text or select prompt started with prefix. +func (r *kcRecorder) asked(prefix string) bool { + for _, p := range slices.Concat(r.texts, r.selects) { + if strings.HasPrefix(p.msg, prefix) { + return true + } + } + return false +} + +func kcTable(client *verda.Client, p tui.Prompter) *cmdutil.TestFactory { + return &cmdutil.TestFactory{ClientOverride: client, OutputFormatOverride: outputTable, PrompterOverride: p} +} + +// kcHome points the default kubeconfig path at a temp dir and fixes the OS user name. +func kcHome(t *testing.T) string { + t.Helper() + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + orig := currentUserName + currentUserName = func() string { return "tester" } + t.Cleanup(func() { currentUserName = orig }) + return home +} + +var kcInteractiveBase = []string{"kubeconfig", "create", "--cluster", "prod"} + +// kcAssertFullFlowPrompts checks the order, defaults and hints of the prompts of a full interactive create. +func kcAssertFullFlowPrompts(t *testing.T, rec *kcRecorder, home string) { + t.Helper() + msgs := make([]string, 0, len(rec.texts)) + for _, p := range rec.texts { + msgs = append(msgs, p.msg) + } + wantOrder := []string{"Common name", "Display name", "Duration", "File to write"} + if len(msgs) != len(wantOrder) { + t.Fatalf("text prompts = %v", msgs) + } + for i, prefix := range wantOrder { + if !strings.HasPrefix(msgs[i], prefix) { + t.Errorf("prompt %d = %q, want prefix %q", i, msgs[i], prefix) + } + } + if rec.texts[0].def != "tester" { + t.Errorf("common name default = %q, want the OS user name", rec.texts[0].def) + } + if rec.texts[2].def != "" || !strings.Contains(rec.texts[2].placeholder, "90 days") { + t.Errorf("duration prompt = %+v: the 90 day default is a hint only", rec.texts[2]) + } + if want := filepath.Join(home, ".kube", "verda-prod.alice.yaml"); rec.texts[3].def != want { + t.Errorf("file default = %q, want %q", rec.texts[3].def, want) + } + if len(rec.selects) != 1 || rec.selects[0].msg != "Access" || !rec.selects[0].hints || rec.selects[0].escLabel != "esc cancel" { + t.Errorf("selects = %+v, want the access select with hints", rec.selects) + } +} + +func TestKubeconfigCreateInteractive_FullFlow(t *testing.T) { + withTerminal(t) + home := kcHome(t) + file := filepath.Join(t.TempDir(), "kc.yaml") + api := kcAPI() + rec := &kcRecorder{Prompter: tuitest.New(). + AddTextInput("alice").AddTextInput("Alice laptop").AddTextInput("720h").AddTextInput(file). + AddSelect(0).AddConfirm(true)} + out, errOut, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), kcInteractiveBase...) + if err != nil { + t.Fatal(err) + } + kcAssertFullFlowPrompts(t, rec, home) + body := kcBody(t, api) + if body["common_name"] != "alice" || body["display_name"] != "Alice laptop" || body["duration"] != "720h" { + t.Errorf("body = %v", body) + } + if g, _ := body["groups"].([]any); len(g) != 1 || g[0] != groupAdmin { + t.Errorf("groups = %v", body["groups"]) + } + if got := readFile(t, file); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, file, 0o600) + for _, want := range []string{"Create kubeconfig", "alice", "cannot be revoked", "Canceled."} { + if want == "Canceled." { + if strings.Contains(errOut, want) { + t.Errorf("stderr has %q", want) + } + continue + } + if !strings.Contains(errOut, want) { + t.Errorf("stderr lacks %q: %q", want, errOut) + } + } + assertNoPrivateKey(t, "stdout", out) + assertNoPrivateKey(t, "stderr", errOut) +} + +func TestKubeconfigCreateInteractive_CustomGroupsAndDefaultPath(t *testing.T) { + withTerminal(t) + home := kcHome(t) + api := kcAPI() + def := filepath.Join(home, ".kube", "verda-prod.alice.yaml") + rec := &kcRecorder{Prompter: tuitest.New(). + AddTextInput("alice").AddTextInput("dev, ops, dev").AddTextInput("").AddTextInput("").AddTextInput(def). + AddSelect(1).AddConfirm(true)} + if _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), kcInteractiveBase...); err != nil { + t.Fatal(err) + } + body := kcBody(t, api) + if g, _ := body["groups"].([]any); len(g) != 2 || g[0] != "dev" || g[1] != "ops" { + t.Errorf("groups = %v", body["groups"]) + } + for _, key := range []string{"duration", "display_name"} { + if _, ok := body[key]; ok { + t.Errorf("%s sent although the answer was empty: %v", key, body) + } + } + // The default path keeps default-path behavior: ~/.kube is created with 0700. + if got := readFile(t, def); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, filepath.Dir(def), 0o700) +} + +func TestKubeconfigCreateInteractive_EachFlagSkipsItsPrompt(t *testing.T) { + withTerminal(t) + file := filepath.Join(t.TempDir(), "flag.yaml") + cases := []struct { + name string + flags []string + prefix string + }{ + {"common name", []string{"--common-name", "bob"}, "Common name"}, + {"admin", []string{"--admin"}, "Access"}, + {"group", []string{"--group", "dev"}, "Access"}, + {"display name", []string{"--display-name", "x"}, "Display name"}, + {"duration", []string{"--duration", "720h"}, "Duration"}, + {"file", []string{"--file", file}, "File to write"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + home := kcHome(t) + def := filepath.Join(home, ".kube", "verda-prod.bob.yaml") + // Answers in prompt order, minus the prompt whose flag is set. + p := tuitest.New().AddSelect(0).AddConfirm(true) + for _, a := range []struct{ prefix, answer string }{{"Common name", "bob"}, {"Display name", ""}, {"Duration", ""}, {"File to write", def}} { + if a.prefix != tc.prefix { + p.AddTextInput(a.answer) + } + } + rec := &kcRecorder{Prompter: p} + args := append(slices.Clone(kcInteractiveBase), tc.flags...) + if _, _, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), rec), args...); err != nil { + t.Fatal(err) + } + if rec.asked(tc.prefix) { + t.Errorf("%q was asked although its flag is set", tc.prefix) + } + }) + } +} + +func TestKubeconfigCreateInteractive_AllFlagsOnlyConfirm(t *testing.T) { + withTerminal(t) + kcHome(t) + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + rec := &kcRecorder{Prompter: tuitest.New().AddConfirm(true)} + args := append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--group", "dev", "--display-name", "x", "--duration", "2h", "--file", file) + if _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), args...); err != nil { + t.Fatal(err) + } + if len(rec.texts) != 0 || len(rec.selects) != 0 { + t.Errorf("prompts asked: texts=%v selects=%v", rec.texts, rec.selects) + } + if body := kcBody(t, api); body["duration"] != "2h" || body["display_name"] != "x" { + t.Errorf("body = %v", body) + } +} + +func TestKubeconfigCreateInteractive_FileDashSkipsFilePromptAndPrintsYAML(t *testing.T) { + withTerminal(t) + kcHome(t) + rec := &kcRecorder{Prompter: tuitest.New().AddConfirm(true)} + args := append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--admin", "--display-name", "", "--duration", "", "--file", "-") + out, errOut, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), rec), args...) + if err != nil { + t.Fatal(err) + } + if out != kcYAML || len(rec.texts) != 0 { + t.Errorf("stdout = %q, text prompts = %v", out, rec.texts) + } + assertNoPrivateKey(t, "stderr", errOut) + if !strings.Contains(errOut, "File: stdout") { + t.Errorf("summary = %q", errOut) + } +} + +func TestKubeconfigCreateInteractive_PicksTheClusterWhenOmitted(t *testing.T) { + withTerminal(t) + kcHome(t) + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + rec := &kcRecorder{Prompter: tuitest.New().AddSelect(0).AddConfirm(true)} + args := []string{"kubeconfig", "create", "--common-name", "bob", "--admin", "--display-name", "", "--duration", "", "--file", file} + if _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), args...); err != nil { + t.Fatal(err) + } + if len(rec.selects) != 1 || rec.selects[0].msg != "Select cluster" || !rec.selects[0].hints || rec.selects[0].escLabel != "esc cancel" { + t.Errorf("selects = %+v", rec.selects) + } + if reqs := api.requestsMatching("POST"); len(reqs) != 1 || reqs[0].Path != "/mk8sclusters/FIN-03/prod/kubeconfigs" { + t.Errorf("POSTs = %v", reqs) + } +} + +func TestKubeconfigCreateInteractive_CancelAndDecline(t *testing.T) { + withTerminal(t) + kcHome(t) + full := func() []string { + return append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--admin", "--display-name", "", "--duration", "", "--file", filepath.Join(t.TempDir(), "kc.yaml")) + } + cases := map[string]struct { + prompter tui.Prompter + args []string + }{ + "Esc at a text prompt": {&textCancelPrompter{Prompter: tuitest.New()}, kcInteractiveBase}, + "Ctrl+C at the select": {&cancelPrompter{Prompter: tuitest.New(), selectErr: errInterrupted()}, append(slices.Clone(kcInteractiveBase), "--common-name", "bob")}, + "Esc at the confirm": {&cancelPrompter{Prompter: tuitest.New(), confirmErr: contextCanceled()}, full()}, + "Ctrl+C at the confirm": {&cancelPrompter{Prompter: tuitest.New(), confirmErr: errInterrupted()}, full()}, + "decline the summary": {confirmNo(), full()}, + "Esc at the cluster pick": {&cancelPrompter{Prompter: tuitest.New(), selectErr: contextCanceled()}, []string{"kubeconfig", "create", "--common-name", "bob", "--admin"}}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + api := kcAPI() + out, errOut, err := runMK8s(t, kcTable(newFakeClient(t, api), tc.prompter), tc.args...) + if err != nil { + t.Fatalf("a cancel is a clean exit, got %v", err) + } + if !strings.Contains(errOut, "Canceled.") || out != "" { + t.Errorf("stdout = %q, stderr = %q", out, errOut) + } + if n := kcPosts(api); n != 0 { + t.Errorf("%d POST(s) after a cancel", n) + } + }) + } +} + +func TestKubeconfigCreateInteractive_PromptFailureIsReturned(t *testing.T) { + withTerminal(t) + kcHome(t) + // No answers queued: the fake prompter fails, which is not a cancel. + _, _, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), tuitest.New()), kcInteractiveBase...) + if err == nil || strings.Contains(err.Error(), "Canceled") { + t.Errorf("err = %v", err) + } +} + +func TestKubeconfigCreateInteractive_ExistingFileStopsBeforeConfirmAndPOST(t *testing.T) { + withTerminal(t) + home := kcHome(t) + def := filepath.Join(home, ".kube", "verda-prod.bob.yaml") + if err := os.MkdirAll(filepath.Dir(def), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(def, []byte("old"), 0o600); err != nil { + t.Fatal(err) + } + api := kcAPI() + // No confirm queued: reaching it would fail with a different error. + p := tuitest.New().AddTextInput("").AddTextInput("").AddTextInput(def) + args := append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--admin") + _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), p), args...) + if err == nil || !strings.Contains(err.Error(), "--force") || kcPosts(api) != 0 { + t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) + } + if got := readFile(t, def); got != "old" { + t.Errorf("file = %q", got) + } +} + +// A named --file that is taken stops the command before any prompt or API call. +func TestKubeconfigCreateInteractive_ExistingFlagFileStopsBeforePrompts(t *testing.T) { + withTerminal(t) + file := filepath.Join(t.TempDir(), "kc.yaml") + if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { + t.Fatal(err) + } + api := kcAPI() + rec := &kcRecorder{Prompter: tuitest.New()} + _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), "kubeconfig", "create", "--cluster", "prod", "--file", file) + if err == nil || !strings.Contains(err.Error(), "--force") { + t.Errorf("err = %v", err) + } + if len(rec.texts)+len(rec.selects) != 0 || len(api.requests) != 0 { + t.Errorf("prompts = %v %v, requests = %v: want none", rec.texts, rec.selects, api.requests) + } +} + +// A blank --cluster is an error in create and save, as in list and describe: no picker. +func TestKubeconfigCreateAndSave_BlankClusterNeverPicks(t *testing.T) { + withTerminal(t) + for _, args := range [][]string{{"kubeconfig", "create", "--cluster", ""}, {"kubeconfig", "save", "prod-kc-1", "--cluster", ""}} { + rec := &kcRecorder{Prompter: tuitest.New()} + _, _, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), rec), args...) + if agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" || len(rec.texts)+len(rec.selects) != 0 { + t.Errorf("%v: err = %v, prompts = %v %v", args, err, rec.texts, rec.selects) + } + } +} + +func TestKubeconfigCreate_NeverPromptsOffTheTable(t *testing.T) { + withTerminal(t) + for name, f := range map[string]*cmdutil.TestFactory{ + "json": jsonFactory(newFakeClient(t, kcAPI())), + "agent": agentFactory(newFakeClient(t, kcAPI())), + } { + rec := &kcRecorder{Prompter: tuitest.New()} + f.PrompterOverride = rec + _, _, err := runMK8s(t, f, "kubeconfig", "create") + if agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Errorf("%s: err = %v", name, err) + } + if len(rec.texts)+len(rec.selects) != 0 { + t.Errorf("%s prompted", name) + } + } +} + +func TestKubeconfigFileValidator(t *testing.T) { + t.Parallel() + dir := t.TempDir() + existing := filepath.Join(dir, "exists.yaml") + if err := os.WriteFile(existing, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + if err := kubeconfigFileValidator("prod", "alice", false)("-"); err == nil { + t.Error("- must be refused: it prints the key on the terminal") + } + if err := kubeconfigFileValidator("prod", "alice", false)(existing); err == nil || !strings.Contains(err.Error(), "--force") { + t.Errorf("existing file without force: %v", err) + } + if err := kubeconfigFileValidator("prod", "alice", true)(existing); err != nil { + t.Errorf("existing file with force: %v", err) + } + if err := kubeconfigFileValidator("prod", "alice", false)(filepath.Join(dir, "new.yaml")); err != nil { + t.Errorf("new file: %v", err) + } + if err := kubeconfigFileValidator("prod", "alice", false)(filepath.Join(dir, "no", "such", "new.yaml")); err == nil { + t.Error("a missing parent directory must be refused") + } +} + +func TestValidateDurationInput(t *testing.T) { + t.Parallel() + for in, ok := range map[string]bool{"": true, " ": true, "720h": true, "1h": true, "5m": false, "30m": false, "abc": false, "-1h": false} { + err := validateDurationInput(in) + if (err == nil) != ok { + t.Errorf("validateDurationInput(%q) = %v, ok=%v", in, err, ok) + } + if err != nil && strings.HasSuffix(err.Error(), ".") { + t.Errorf("validateDurationInput(%q) = %q, trailing dot", in, err) + } + } +} + +func TestCurrentUserName(t *testing.T) { + t.Parallel() + if currentUserName() == "" { + t.Skip("no OS user name in this environment") + } +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_save.go b/internal/verda-cli/cmd/mk8s/kubeconfig_save.go new file mode 100644 index 00000000..436c229f --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_save.go @@ -0,0 +1,271 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "io" + "os" + "regexp" + "strings" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + flagFile = "file" + flagForce = "force" + + // kubeconfigStdout as --file writes the YAML to stdout. + kubeconfigStdout = "-" +) + +// shellSafe leaves out ':', the separator of a KUBECONFIG list. +var shellSafe = regexp.MustCompile(`^[A-Za-z0-9_@%+=,./-]+$`) + +// shellQuote quotes s for a POSIX shell only when it has characters the shell would interpret. +func shellQuote(s string) string { + if shellSafe.MatchString(s) { + return s + } + return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" +} + +// kubeconfigFileFlags are --file and --force, shared by create and save. +type kubeconfigFileFlags struct { + File string + Force bool + + fileSet bool +} + +func (k *kubeconfigFileFlags) addFlags(cmd *cobra.Command) { + cmd.Flags().StringVar(&k.File, flagFile, "", "File to write the kubeconfig to, or - for stdout (default ~/.kube/verda-..yaml)") + cmd.Flags().BoolVar(&k.Force, flagForce, false, "Replace the file if it exists (never a symlink or a directory)") +} + +// validate rejects --file - with structured output: the YAML would share stdout with the JSON. +func (k *kubeconfigFileFlags) validate(cmd *cobra.Command, f cmdutil.Factory) error { + k.File = strings.TrimSpace(k.File) + k.fileSet = cmd.Flags().Changed(flagFile) + if k.File == kubeconfigStdout && (f.AgentMode() || f.OutputFormat() != outputTable) { + return cmdutil.UsageErrorf(cmd, "--%s %s writes the YAML to stdout and cannot be combined with --agent or -o json|yaml", flagFile, kubeconfigStdout) + } + return nil +} + +// kubeconfigDest is where the YAML goes: a checked file, or stdout. +type kubeconfigDest struct { + stdout bool + target kubeconfigTarget + // replaced is set by write when an existing file was replaced. + replaced bool +} + +// destination resolves --file for a cluster and common name and checks the +// target, so a path that cannot be written fails before a certificate is +// issued. A bad path is a usage error. +func (k *kubeconfigFileFlags) destination(cmd *cobra.Command, cluster, commonName string) (*kubeconfigDest, error) { + if k.File == kubeconfigStdout { + return &kubeconfigDest{stdout: true}, nil + } + target, err := newKubeconfigTarget(k.File, cluster, commonName) + if err != nil { + return nil, err + } + if err := target.Check(k.Force); err != nil { + return nil, asUsage(cmd, err) + } + return &kubeconfigDest{target: target}, nil +} + +// path is the file written, or - for stdout. +func (d *kubeconfigDest) path() string { + if d.stdout { + return kubeconfigStdout + } + return d.target.path +} + +func (d *kubeconfigDest) write(ioStreams cmdutil.IOStreams, yaml string, force bool) error { + if d.stdout { + if _, err := io.WriteString(ioStreams.Out, yaml); err != nil { + return fmt.Errorf("write kubeconfig to stdout: %w", err) + } + return nil + } + if force { + _, statErr := os.Lstat(d.target.path) + d.replaced = statErr == nil + } + return d.target.Write([]byte(yaml), force) +} + +func kubeconfigListCmd(t *clusterTarget) string { + return fmt.Sprintf("verda mk8s kubeconfig list --cluster %s -l %s", shellQuote(t.Name), shellQuote(t.Location)) +} + +// kubeconfigSaveCmd is the command that writes a ready kubeconfig, for recovery +// messages. The default path is left out (save derives it and creates ~/.kube), +// and so is stdout: a recovery command must not print the key. +func kubeconfigSaveCmd(t *clusterTarget, id string, d *kubeconfigDest, force bool) string { + s := fmt.Sprintf("verda mk8s kubeconfig save %s --cluster %s -l %s", shellQuote(id), shellQuote(t.Name), shellQuote(t.Location)) + if !d.stdout && !d.target.defaultDir { + s += " --file " + shellQuote(d.target.path) + } + if force && !d.stdout { + s += " --force" + } + return s +} + +// reportKubeconfigWritten prints the result after the YAML was written: a +// KubeconfigView for structured output (never the YAML), a short text and the +// KUBECONFIG hint for people. earlierStillValid adds the hint that a file's +// earlier kubeconfig is a different credential that still works: only create +// issues a new certificate, save rewrites the one it was asked for. +func reportKubeconfigWritten(f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, d *kubeconfigDest, k *verda.ManagedKubernetesKubeconfig, earlierStillValid bool) error { + file := "" + if !d.stdout { + file = d.path() + } + view := newKubeconfigView(k, file) + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Kubeconfig:", view) + if earlierStillValid { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "The earlier kubeconfig in that file is still valid. Run `%s` and delete it if you no longer need it.\n", kubeconfigListCmd(t)) + } + if written, err := writeResult(f, ioStreams, view, nil); written { + return err + } + expires := "" + if k.NotAfter != nil { + expires = ", valid until " + utcTime(*k.NotAfter) + } + if d.stdout { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Kubeconfig %s written to stdout%s.\n", k.ID, expires) + return nil + } + _, _ = fmt.Fprintf(ioStreams.Out, "Kubeconfig %s saved to %s%s.\n", k.ID, file, expires) + _, _ = fmt.Fprintf(ioStreams.ErrOut, "To use it:\n export KUBECONFIG=%s\n kubectl get nodes\n", shellQuote(file)) + return nil +} + +type kubeconfigSaveOptions struct { + poolScope + kubeconfigFileFlags +} + +// NewCmdKubeconfigSave creates `verda mk8s kubeconfig save`. +func NewCmdKubeconfigSave(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + opts := &kubeconfigSaveOptions{} + cmd := &cobra.Command{ + Use: "save []", + Short: "Write a ready kubeconfig to a file", + Long: longDesc(` + Write the YAML of a ready kubeconfig to a file with mode 0600. Use + it when kubeconfig create stopped waiting before the kubeconfig was + ready or could not write the file, or to write the file again. It + issues no new certificate. + + The file rules are those of kubeconfig create: the default file is + ~/.kube/verda-..yaml, an existing file is + replaced only with --force, and nothing is written through a + symlink. --file - writes only the YAML to stdout, for a pipe; it + cannot be combined with -o json|yaml or --agent. The YAML holds a + private key: it is never part of structured output or --debug. + + If the kubeconfig is not ready yet, the command exits with an + error: run it again in a moment. + + Without or --cluster, a terminal session shows a picker. + `), + Example: cmdutil.Examples(` + # Pick the cluster and the kubeconfig from lists (terminal only) + verda mk8s kubeconfig save + + # Write ~/.kube/verda-..yaml + verda mk8s kubeconfig save $KUBECONFIG_ID --cluster prod + + # Replace a file you choose + verda mk8s kubeconfig save $KUBECONFIG_ID --cluster prod --file ./prod.yaml --force + + # Agent mode: JSON metadata with the path in "file", never the YAML + verda --agent mk8s kubeconfig save $KUBECONFIG_ID --cluster prod -l FIN-03 --file ./prod.yaml + + # Only the YAML, on stdout, piped to a secret store + verda mk8s kubeconfig save $KUBECONFIG_ID --cluster prod --file - | gh secret set KUBECONFIG + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runKubeconfigSave(cmd, f, ioStreams, opts, args) + }, + } + opts.poolScope.addFlags(cmd) + opts.kubeconfigFileFlags.addFlags(cmd) + return cmd +} + +func runKubeconfigSave(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *kubeconfigSaveOptions, args []string) error { + if err := opts.validate(cmd, f); err != nil { + return err + } + opts.Cluster, opts.Location = strings.TrimSpace(opts.Cluster), strings.TrimSpace(opts.Location) + if err := checkKubeconfigTarget(f, cmd, &opts.poolScope, args); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := opts.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + kc, err := resolveKubeconfig(cmd, f, ioStreams, client, target, args, false) + if err != nil || kc == nil { + return err + } + id := kc.ID + k, err := apiCall(cmd.Context(), f, "Loading kubeconfig...", func(ctx context.Context) (*verda.ManagedKubernetesKubeconfig, error) { + return client.ManagedKubernetes.GetKubeconfig(ctx, target.Location, target.Name, id) + }) + if err != nil { + return err + } + if _, ready := kubeconfigReady(k); !ready { + return notReadyError(k) + } + dest, err := opts.destination(cmd, target.Name, k.CommonName) + if err != nil { + return err + } + if err := dest.write(ioStreams, k.Kubeconfig, opts.Force); err != nil { + return err + } + return reportKubeconfigWritten(f, ioStreams, target, dest, k, false) +} + +// notReadyError is the one "not ready" message of save: not ready, or ready without its content yet. +func notReadyError(k *verda.ManagedKubernetesKubeconfig) error { + detail := "status " + k.Status + if k.Status == verda.ManagedKubernetesKubeconfigStatusReady { + detail = "status ready, content not available yet" + } + return fmt.Errorf("kubeconfig %s is not ready yet (%s); run save again in a moment", k.ID, detail) +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go new file mode 100644 index 00000000..e31e9279 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go @@ -0,0 +1,298 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + "go.yaml.in/yaml/v3" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func kcSaveAPI(status string) *fakeAPI { + api := seededClusters() + api.addKubeconfig("prod", verda.ManagedKubernetesKubeconfig{ + ID: "prod-kc-abc", CommonName: "alice", Groups: []string{groupAdmin}, Status: status, + }) + kcReadyOnGet(api) + if status != verda.ManagedKubernetesKubeconfigStatusReady { + api.onKubeconfigGet = nil + } + return api +} + +func kcSaveArgs(file string, extra ...string) []string { + args := []string{"kubeconfig", "save", "prod-kc-abc", "--cluster", "prod", "--file", file} + return append(args, extra...) +} + +func TestKubeconfigSave_WritesFileInEveryOutputMode(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ + "table": func(c *verda.Client) *cmdutil.TestFactory { return tableFactory(c, nil) }, + "json": jsonFactory, + "yaml": yamlFactory, + "agent": agentFactory, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + file := filepath.Join(t.TempDir(), "kc.yaml") + out, errOut, err := runMK8s(t, mk(newFakeClient(t, api)), kcSaveArgs(file)...) + if err != nil { + t.Fatal(err) + } + assertNoPrivateKey(t, "stdout", out) + assertNoPrivateKey(t, "stderr", errOut) + if got := readFile(t, file); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, file, 0o600) + if n := kcPosts(api); n != 0 { + t.Errorf("save made %d POST(s)", n) + } + switch name { + case "table": + if !strings.Contains(errOut, "export KUBECONFIG="+file) || !strings.Contains(errOut, "kubectl get nodes") { + t.Errorf("stderr = %q", errOut) + } + case "yaml": + var v KubeconfigView + if err := yaml.Unmarshal([]byte(out), &v); err != nil || v.File != file || v.ID != "prod-kc-abc" { + t.Errorf("view = %+v err=%v", v, err) + } + default: + if v := decodeJSON[KubeconfigView](t, out); v.File != file || v.ID != "prod-kc-abc" || v.Status != "ready" { + t.Errorf("view = %+v", v) + } + } + }) + } +} + +func TestKubeconfigSave_NotReady(t *testing.T) { + t.Parallel() + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusPending) + file := filepath.Join(t.TempDir(), "kc.yaml") + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcSaveArgs(file)...) + if err == nil || err.Error() != "kubeconfig prod-kc-abc is not ready yet (status pending); run save again in a moment" { + t.Fatalf("err = %v", err) + } + if out != "" { + t.Errorf("stdout = %q", out) + } + if _, statErr := os.Stat(file); statErr == nil { + t.Error("no file may be written for a kubeconfig that is not ready") + } +} + +func TestKubeconfigSave_ReadyWithoutYAMLIsNotReady(t *testing.T) { + t.Parallel() + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + api.onKubeconfigGet = nil // ready, but the fake serves no YAML + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcSaveArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) + if err == nil || err.Error() != "kubeconfig prod-kc-abc is not ready yet (status ready, content not available yet); run save again in a moment" { + t.Errorf("err = %v", err) + } +} + +func TestKubeconfigSave_FileDash(t *testing.T) { + t.Parallel() + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcSaveArgs("-")...) + if err != nil { + t.Fatal(err) + } + if out != kcYAML { + t.Errorf("stdout = %q, want exactly the YAML", out) + } + assertNoPrivateKey(t, "stderr", errOut) + + for name, f := range map[string]*cmdutil.TestFactory{ + "json": jsonFactory(newFakeClient(t, api)), + "yaml": yamlFactory(newFakeClient(t, api)), + "agent": agentFactory(newFakeClient(t, api)), + } { + before := len(api.requests) + out, _, err := runMK8s(t, f, kcSaveArgs("-")...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) || out != "" { + t.Errorf("%s: out=%q err=%v", name, out, err) + } + if len(api.requests) != before { + t.Errorf("%s: --file - with structured output reached the API", name) + } + } +} + +func TestKubeconfigSave_ExistingFile(t *testing.T) { + t.Parallel() + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + file := filepath.Join(t.TempDir(), "kc.yaml") + if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { + t.Fatal(err) + } + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcSaveArgs(file)...) + if err == nil || !strings.Contains(err.Error(), "--force") || !isUsageError(err) { + t.Fatalf("err = %v", err) + } + kcAssertErrNoKey(t, err) + if got := readFile(t, file); got != "old" { + t.Errorf("file = %q", got) + } + _, errOut, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcSaveArgs(file, "--force")...) + if err != nil || strings.Contains(errOut, "still valid") || strings.Contains(errOut, "kubeconfig list") { + t.Fatalf("save issues no new certificate, so it must not advise deleting one: err=%v stderr=%q", err, errOut) + } + if got := readFile(t, file); got != kcYAML { + t.Errorf("file = %q", got) + } + assertMode(t, file, 0o600) +} + +func TestKubeconfigSave_DefaultPathUsesTheCommonName(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "kubeconfig", "save", "prod-kc-abc", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + if got := readFile(t, filepath.Join(home, ".kube", "verda-prod.alice.yaml")); got != kcYAML { + t.Errorf("file = %q", got) + } +} + +func TestKubeconfigSave_MissingAndUnknown(t *testing.T) { + t.Parallel() + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + for name, f := range map[string]*cmdutil.TestFactory{ + "table": tableFactory(newFakeClient(t, api), nil), + "agent": agentFactory(newFakeClient(t, api)), + } { + _, _, err := runMK8s(t, f, "kubeconfig", "save") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("%s: err = %v", name, err) + } + if m, _ := ae.Details["missing"].([]string); len(m) != 2 || m[0] != "--cluster" || m[1] != "" { + t.Errorf("%s: missing = %v", name, ae.Details) + } + } + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "kubeconfig", "save", "ghost", "--cluster", "prod", "--file", filepath.Join(t.TempDir(), "x")) + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown id: %v", err) + } +} + +func TestKubeconfigSave_DebugNeverLogsTheKey(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady)), nil) + f.DebugOverride = true + out, errOut, err := runMK8s(t, f, kcSaveArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "DEBUG: Kubeconfig:") { + t.Errorf("no debug output: %q", errOut) + } + assertNoPrivateKey(t, "stdout", out) + assertNoPrivateKey(t, "debug output", errOut) +} + +func TestKubeconfigSave_PickerWhenIDOmitted(t *testing.T) { + withTerminal(t) + t.Run("picks and writes", func(t *testing.T) { + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + file := filepath.Join(t.TempDir(), "kc.yaml") + rec := &kcRecorder{Prompter: pickerAnswers(0)} + _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), "kubeconfig", "save", "--cluster", "prod", "--file", file) + if err != nil { + t.Fatal(err) + } + if got := readFile(t, file); got != kcYAML { + t.Errorf("file = %q", got) + } + if len(rec.selects) != 1 || !rec.selects[0].hints { + t.Errorf("selects = %+v, want one with hints", rec.selects) + } + }) + t.Run("cancel row and Ctrl+C", func(t *testing.T) { + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + file := filepath.Join(t.TempDir(), "kc.yaml") + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(1)), "kubeconfig", "save", "--cluster", "prod", "--file", file) + if err != nil || !strings.Contains(errOut, "Canceled.") { + t.Errorf("Cancel row: err=%v stderr=%q", err, errOut) + } + if _, statErr := os.Stat(file); statErr == nil { + t.Error("the Cancel row must not write a file") + } + api = kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + p := &cancelPrompter{Prompter: tuitest.New(), selectErr: errInterrupted()} + _, errOut, err = runMK8s(t, kcTable(newFakeClient(t, api), p), "kubeconfig", "save", "--cluster", "prod") + if err != nil || !strings.Contains(errOut, "Canceled.") { + t.Errorf("Ctrl+C: err=%v stderr=%q", err, errOut) + } + }) + t.Run("no kubeconfigs", func(t *testing.T) { + api := seededClusters() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), "kubeconfig", "save", "--cluster", "prod") + if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "kubeconfig create --cluster prod") { + t.Errorf("out=%q err=%q %v", out, errOut, err) + } + }) +} + +func TestShellQuote(t *testing.T) { + t.Parallel() + for in, want := range map[string]string{ + "/home/a/.kube/x.yaml": "/home/a/.kube/x.yaml", + "-": "-", + "a b": "'a b'", + "it's": `'it'\''s'`, + "$HOME/x": "'$HOME/x'", + "/a:/b": "'/a:/b'", // ':' separates the paths of KUBECONFIG + } { + if got := shellQuote(in); got != want { + t.Errorf("shellQuote(%q) = %q, want %q", in, got, want) + } + } +} + +func TestKubeconfigSaveCmd(t *testing.T) { + t.Parallel() + tgt := &clusterTarget{Name: "my cluster", Location: "FIN-03"} + cases := map[string]struct { + dest *kubeconfigDest + force bool + want string + }{ + "custom file": {&kubeconfigDest{target: kubeconfigTarget{path: "/tmp/a b.yaml"}}, true, "verda mk8s kubeconfig save kc-1 --cluster 'my cluster' -l FIN-03 --file '/tmp/a b.yaml' --force"}, + "default path": {&kubeconfigDest{target: kubeconfigTarget{path: "/h/.kube/x.yaml", defaultDir: true}}, false, "verda mk8s kubeconfig save kc-1 --cluster 'my cluster' -l FIN-03"}, + "stdout": {&kubeconfigDest{stdout: true}, true, "verda mk8s kubeconfig save kc-1 --cluster 'my cluster' -l FIN-03"}, + } + for name, tc := range cases { + if got := kubeconfigSaveCmd(tgt, "kc-1", tc.dest, tc.force); got != tc.want { + t.Errorf("%s: got %q, want %q", name, got, tc.want) + } + } +} diff --git a/internal/verda-cli/cmd/mk8s/locations.go b/internal/verda-cli/cmd/mk8s/locations.go new file mode 100644 index 00000000..98e3dc47 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/locations.go @@ -0,0 +1,84 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdLocations creates `verda mk8s locations`. +func NewCmdLocations(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + return &cobra.Command{ + Use: "locations", + Aliases: []string{"location", "loc"}, + Short: "List locations enabled for managed Kubernetes", + Long: longDesc(` + List the locations where this project can create managed Kubernetes + clusters. Use one of them with -l when you create a cluster. A + project without access to managed Kubernetes gets an error that + says so. + `), + Example: cmdutil.Examples(` + verda mk8s locations + + # A JSON array of location codes, for scripts + verda mk8s locations -o json + `), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + return runLocations(cmd, f, ioStreams) + }, + } +} + +func runLocations(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams) error { + client, err := f.VerdaClient() + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + + res, err := cmdutil.WithSpinner(ctx, f.Status(), "Loading locations...", func(ctx context.Context) (*verda.ManagedKubernetesEnabledLocations, error) { + return client.ManagedKubernetes.GetEnabledLocations(ctx) + }) + if err != nil { + return mapErr(err) + } + codes := res.Locations + if codes == nil { + codes = []string{} + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), fmt.Sprintf("API response: %d location(s):", len(codes)), codes) + + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), codes); wrote { + return err + } + if len(codes) == 0 { + _, _ = fmt.Fprintln(ioStreams.Out, "Managed Kubernetes is enabled for this project, but no location is configured.") + return nil + } + _, _ = fmt.Fprintln(ioStreams.Out, "LOCATION") + for _, c := range codes { + _, _ = fmt.Fprintln(ioStreams.Out, c) + } + return nil +} diff --git a/internal/verda-cli/cmd/mk8s/locations_test.go b/internal/verda-cli/cmd/mk8s/locations_test.go new file mode 100644 index 00000000..fb928269 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/locations_test.go @@ -0,0 +1,176 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestLocations_Table(t *testing.T) { + api := newFakeAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "locations") + if err != nil { + t.Fatal(err) + } + for _, want := range []string{"LOCATION", "FIN-01", "FIN-03"} { + if !strings.Contains(out, want) { + t.Errorf("output misses %q:\n%s", want, out) + } + } + if n := len(api.requestsMatching("GET")); n != 1 { + t.Errorf("locations made %d GET calls, want 1", n) + } + if n := len(api.requestsMatching("POST")); n != 0 { + t.Errorf("locations must not write: %d POST calls", n) + } +} + +func TestLocations_JSONAndAgent(t *testing.T) { + for name, newFactory := range map[string]func(*verda.Client) *cmdutil.TestFactory{ + "json": jsonFactory, + "agent": agentFactory, + } { + t.Run(name, func(t *testing.T) { + out, _, err := runMK8s(t, newFactory(newFakeClient(t, newFakeAPI())), "locations") + if err != nil { + t.Fatal(err) + } + var got []string + if err := json.Unmarshal([]byte(out), &got); err != nil { + t.Fatalf("output is not a JSON string array: %v\n%s", err, out) + } + if len(got) != 2 || got[0] != "FIN-01" { + t.Errorf("locations = %v", got) + } + }) + } +} + +func TestLocations_EnabledButEmpty(t *testing.T) { + api := newFakeAPI() + api.locations = nil + + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "locations") + if err != nil { + t.Fatalf("an empty list is not an error: %v", err) + } + if !strings.Contains(out, "no location is configured") { + t.Errorf("table output = %q", out) + } + + out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api)), "locations") + if err != nil { + t.Fatal(err) + } + if strings.TrimSpace(out) != "[]" { + t.Errorf("JSON output = %q, want []", out) + } +} + +func TestLocations_FeatureNotEnabled(t *testing.T) { + api := newFakeAPI() + api.disabled = true + + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "locations") + if err == nil { + t.Fatal("want an error") + } + ae := cmdutil.ClassifyError(err) + if ae.Code != "AUTH_ERROR" || ae.ExitCode != cmdutil.ExitAuth { + t.Errorf("classified as %s exit %d, want AUTH_ERROR exit %d", ae.Code, ae.ExitCode, cmdutil.ExitAuth) + } + if !strings.Contains(ae.Message, "Managed Kubernetes is not enabled for this project") { + t.Errorf("message = %q", ae.Message) + } + if cmdutil.IsAgentError(err) { + t.Error("must stay a plain error: main prints AgentErrors as JSON even for humans") + } +} + +func TestLocations_Other403StaysPlain(t *testing.T) { + api := newFakeAPI() + api.failures["GET /mk8sclusters/enabled_locations"] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access","details":"some_other_reason"}`} + + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "locations") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "AUTH_ERROR" { + t.Fatalf("classified = %+v", ae) + } + if strings.Contains(ae.Message, "not enabled") { + t.Errorf("only feature_not_enabled gets the not-enabled message: %q", ae.Message) + } +} + +func TestLocations_Debug(t *testing.T) { + api := newFakeAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.DebugOverride = true + _, errOut, err := runMK8s(t, f, "locations") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "FIN-03") { + t.Errorf("debug output misses the API response: %q", errOut) + } +} + +func TestMK8s_HelpStartsWithCaution(t *testing.T) { + f := tableFactory(nil, nil) + cmd := NewCmdMK8s(f, cmdutil.IOStreams{}) + if !strings.HasPrefix(cmd.Long, "Experimental: the managed Kubernetes API may change") { + t.Errorf("Long = %q", cmd.Long) + } + if cmd.Annotations[cmdutil.TagAnnotation] != "experimental" { + t.Errorf("tag = %q", cmd.Annotations[cmdutil.TagAnnotation]) + } + for _, alias := range []string{"k8s", "kubernetes"} { + found := false + for _, a := range cmd.Aliases { + found = found || a == alias + } + if !found { + t.Errorf("missing alias %q", alias) + } + } + for _, sub := range cmd.Commands() { + if !strings.HasPrefix(sub.Long, "Experimental:") { + t.Errorf("%s: Long must start with the caution", sub.Name()) + } + } +} + +func TestInteractiveTTY(t *testing.T) { + // Not parallel: withTerminal swaps a package variable. + client := newFakeClient(t, newFakeAPI()) + if interactiveTTY(tableFactory(client, nil)) { + t.Error("no terminal: must not be interactive") + } + withTerminal(t) + if !interactiveTTY(tableFactory(client, nil)) { + t.Error("terminal + table output: want interactive") + } + if interactiveTTY(agentFactory(client)) { + t.Error("agent mode must never be interactive") + } + if interactiveTTY(jsonFactory(client)) { + t.Error("-o json must never be interactive") + } +} diff --git a/internal/verda-cli/cmd/mk8s/mk8s.go b/internal/verda-cli/cmd/mk8s/mk8s.go new file mode 100644 index 00000000..f7a702fa --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/mk8s.go @@ -0,0 +1,119 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s implements `verda mk8s`: managed Kubernetes clusters, node +// pools and kubeconfigs (experimental). +package mk8s + +import ( + "os" + + "github.com/charmbracelet/x/term" + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + outputTable = "table" + + // Results of a command that only accepts a request, or that waited for it. + statusAccepted = "accepted" + statusCompleted = "completed" + // statusUnchanged: the resource already had the requested state, nothing was sent. + statusUnchanged = "unchanged" + + timeLayout = "2006-01-02 15:04" + + // experimentalCaution starts the Long help of every mk8s command; the line + // break keeps it within 80 columns like the rest of the help. + experimentalCaution = "Experimental: the managed Kubernetes API may change in incompatible ways.\n" + + "Commands, flags and output may change without a deprecation period." + + // Help text shared by several commands, so the same rule reads the same everywhere. + yesUsage = "Skip confirmation (required in scripts, agent mode and -o json|yaml)" + clusterUsage = "Cluster name (required in scripts, agent mode and -o json|yaml)" + locationUsage = "Location of the cluster (found from the cluster name when omitted)" + waitTimeUsage = "Maximum time to wait with --wait" +) + +// longDesc prefixes the experimental caution to a command's long description. +func longDesc(s string) string { + return experimentalCaution + "\n\n" + cmdutil.LongDesc(s) +} + +// NewCmdMK8s creates the `verda mk8s` parent command. +func NewCmdMK8s(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "mk8s", + Aliases: []string{"k8s", "kubernetes"}, + Short: "Manage Kubernetes clusters, node pools and kubeconfigs", + Annotations: map[string]string{cmdutil.TagAnnotation: "experimental"}, + Long: longDesc(` + Create and manage managed Kubernetes clusters, their node pools + (the worker VMs) and their kubeconfigs (client certificates for + kubectl). + + Before you create a cluster, create a private network and a subnet + in the cluster's location with verda network. mk8s takes their IDs + and never creates them. + + Cluster and node pool names are unique in the project, so commands + find a cluster's location from its name; -l gives the location and + skips that lookup. Kubeconfigs are named by an ID that the API + chooses. + + On a terminal with table output, a command asks for each value that + you do not give, and asks for confirmation before it creates, + changes or deletes anything. In scripts, agent mode and + -o json|yaml it never asks: give the required flags, and --yes to + delete, to scale a node pool down or to upgrade a cluster. + + The API does the work in the background after it accepts a + request. Cluster and node pool create and node pool scale can wait + for the result with --wait; kubeconfig create always waits. + Deletes and cluster upgrade do not wait. + `), + Example: cmdutil.Examples(` + # Locations where this project can create clusters + verda mk8s locations + + # A cluster with one worker, then a kubeconfig for kubectl + verda mk8s cluster create --name prod -l FIN-03 --kubernetes-version 1.37.0 --network $NETWORK_ID --subnet $SUBNET_ID --wait + verda mk8s node-pool create --cluster prod --name workers --instance-type 1V100.6V --ssh-key $KEY_ID --wait + verda mk8s kubeconfig create --cluster prod --common-name alice --admin + export KUBECONFIG=~/.kube/verda-prod.alice.yaml + kubectl get nodes + `), + Run: cmdutil.DefaultSubCommandRun(ioStreams.Out), + } + + cmd.AddCommand( + NewCmdLocations(f, ioStreams), + NewCmdCluster(f, ioStreams), + NewCmdNodePool(f, ioStreams), + NewCmdKubeconfig(f, ioStreams), + ) + return cmd +} + +// isInteractiveTerminal: prompts need stdin, stdout and stderr on a TTY; a var so tests can fake one. +var isInteractiveTerminal = func() bool { + return term.IsTerminal(os.Stdin.Fd()) && cmdutil.IsStdoutTerminal() && cmdutil.IsStderrTerminal() +} + +// interactiveTTY: prompts only on a terminal, outside agent mode, with table output. +func interactiveTTY(f cmdutil.Factory) bool { + return isInteractiveTerminal() && !f.AgentMode() && f.OutputFormat() == outputTable +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool.go b/internal/verda-cli/cmd/mk8s/nodepool.go new file mode 100644 index 00000000..6292653b --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool.go @@ -0,0 +1,171 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "strings" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + flagCluster = "cluster" + argPool = "" +) + +// NewCmdNodePool creates the `verda mk8s node-pool` parent command. +func NewCmdNodePool(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "node-pool", + Aliases: []string{"pool", "np"}, + Short: "Manage the node pools of a cluster", + Long: longDesc(` + List, show, create, scale and delete the node pools of a cluster. + A node pool is a set of workers: VMs of one instance type that join + the cluster as nodes. Every command takes the cluster with + --cluster and finds its location from the name; -l gives the + location and skips that lookup. + + Node pool names are unique in the project, not only in their + cluster. + `), + Example: cmdutil.Examples(` + verda mk8s node-pool list --cluster prod + verda mk8s node-pool create --cluster prod --name workers --instance-type 1V100.6V --ssh-key $KEY_ID + verda mk8s node-pool scale workers --cluster prod --replicas 3 + `), + Run: cmdutil.DefaultSubCommandRun(ioStreams.Out), + } + cmd.AddCommand( + NewCmdNodePoolList(f, ioStreams), + NewCmdNodePoolDescribe(f, ioStreams), + NewCmdNodePoolCreate(f, ioStreams), + NewCmdNodePoolScale(f, ioStreams), + NewCmdNodePoolDelete(f, ioStreams), + ) + return cmd +} + +// poolScope is the cluster a node-pool command acts on: --cluster and -l. +type poolScope struct { + Cluster string + Location string +} + +func (s *poolScope) addFlags(cmd *cobra.Command) { + cmd.Flags().StringVar(&s.Cluster, flagCluster, "", clusterUsage) + addLocationFlag(cmd, &s.Location) +} + +// missing lists --cluster when a terminal cannot ask for it (or it is blank). +func (s *poolScope) missing(f cmdutil.Factory, cmd *cobra.Command) []string { + if clusterArgMissing(f, s.Cluster, cmd.Flags().Changed(flagCluster)) { + return []string{"--" + flagCluster} + } + return nil +} + +func (s *poolScope) resolve(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client) (*clusterTarget, error) { + return resolveCluster(cmd, f, ioStreams, client, s.Cluster, s.Location) +} + +// poolArgMissing: the pool was not given where it has to be, or was blank. +func poolArgMissing(f cmdutil.Factory, args []string) bool { + blank := len(args) > 0 && strings.TrimSpace(args[0]) == "" + return blank || (len(args) == 0 && !interactiveTTY(f)) +} + +// checkPoolTarget fails before any call when --cluster or cannot be +// asked for. It lists everything missing at once. +func checkPoolTarget(f cmdutil.Factory, cmd *cobra.Command, scope *poolScope, args []string) error { + missing := scope.missing(f, cmd) + if poolArgMissing(f, args) { + missing = append(missing, argPool) + } + if len(missing) > 0 { + return cmdutil.NewMissingFlagsError(missing) + } + return nil +} + +// resolvePool reads the pool named by args[0], or asks for one. A nil pool with +// a nil error means the user canceled or the cluster has no pool. +func resolvePool(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, args []string) (*verda.ManagedKubernetesNodePool, error) { + if len(args) > 0 { + return getPool(cmd.Context(), f, client, t, strings.TrimSpace(args[0])) + } + pools, err := listPools(cmd.Context(), f, client, t) + if err != nil { + return nil, err + } + return pickPool(cmd.Context(), f, ioStreams, t, pools) +} + +func getPool(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget, name string) (*verda.ManagedKubernetesNodePool, error) { + return apiCall(ctx, f, "Loading node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { + return client.ManagedKubernetes.GetNodePool(ctx, t.Location, t.Name, name) + }) +} + +func listPools(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget) ([]verda.ManagedKubernetesNodePool, error) { + return apiCall(ctx, f, "Loading node pools...", func(ctx context.Context) ([]verda.ManagedKubernetesNodePool, error) { + return client.ManagedKubernetes.ListNodePools(ctx, t.Location, t.Name) + }) +} + +// poolDescribeCmd, poolDeleteCmd and poolScaleCmd are the commands named in "check with" and "to stop" hints. +func poolDescribeCmd(t *clusterTarget, pool string) string { + return fmt.Sprintf("verda mk8s node-pool describe %s --cluster %s -l %s", pool, t.Name, t.Location) +} + +func poolDeleteCmd(t *clusterTarget, pool string) string { + return fmt.Sprintf("verda mk8s node-pool delete %s --cluster %s -l %s", pool, t.Name, t.Location) +} + +func poolScaleCmd(t *clusterTarget, pool string, replicas int) string { + return fmt.Sprintf("verda mk8s node-pool scale %s --cluster %s -l %s --replicas %d", pool, t.Name, t.Location, replicas) +} + +func pickPool(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, pools []verda.ManagedKubernetesNodePool) (*verda.ManagedKubernetesNodePool, error) { + if len(pools) == 0 { + printNoPools(ioStreams, t.Name) + return nil, nil + } + labels := make([]string, 0, len(pools)+1) + for i := range pools { + p := &pools[i] + labels = append(labels, fmt.Sprintf("%s (%s, %d/%d, %s)", p.Name, p.InstanceType, p.ReadyReplicas, p.Replicas, p.State)) + } + labels = append(labels, cancelLabel) + idx, err := f.Prompter().Select(ctx, "Select node pool", labels, pickerHints()...) + switch { + case err == nil && idx >= 0 && idx < len(pools): + return &pools[idx], nil + case err != nil && !cmdutil.IsPromptCancel(err): + return nil, err + } + printCanceled(ioStreams) + return nil, nil +} + +func printNoPools(ioStreams cmdutil.IOStreams, cluster string) { + _, _ = fmt.Fprintf(ioStreams.Out, "No node pools in cluster %s.\n", cluster) + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Create one with: verda mk8s node-pool create --cluster %s\n", cluster) +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_create.go b/internal/verda-cli/cmd/mk8s/nodepool_create.go new file mode 100644 index 00000000..c57cab48 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_create.go @@ -0,0 +1,274 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "fmt" + "slices" + "strings" + "time" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + flagInstanceType = "instance-type" + flagSSHKey = "ssh-key" + flagReplicas = "replicas" + + defaultPoolReplicas = 1 + defaultPoolWaitTimeout = 15 * time.Minute + poolBackendKeepsTrying = "The backend keeps trying to create workers. Workers that already exist keep billing, even if they have not joined the cluster." + nodePoolCreateWaitUsage = "Wait until the pool is active with all workers ready" +) + +type nodePoolCreateOptions struct { + poolScope + Name string + InstanceType string + SSHKeys []string + Replicas int + Wait cmdutil.WaitOptions + + replicasSet bool +} + +// PoolResult is the structured output of `node-pool create`. +type PoolResult struct { + Status string `json:"status" yaml:"status"` + Pool PoolView `json:"pool" yaml:"pool"` + WaitError string `json:"wait_error,omitempty" yaml:"wait_error,omitempty"` +} + +// NewCmdNodePoolCreate creates `verda mk8s node-pool create`. +func NewCmdNodePoolCreate(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + opts := &nodePoolCreateOptions{} + cmd := &cobra.Command{ + Use: "create", + Short: "Create a node pool", + Long: longDesc(` + Create a node pool: workers of one instance type that accept the + SSH keys you name by ID. Node pool names are unique in the project, + not only in their cluster. + + On a terminal, the CLI asks for each value that you do not give, + then shows a summary and asks for confirmation. In scripts, agent + mode and -o json|yaml, --cluster, --name, --instance-type and at + least one --ssh-key are required and the command never asks. + + The CLI does not check the instance type: a type the API cannot + provide gives a pool whose workers never become ready, and + node-pool describe shows the reason. The wizard lists the types + that the availability API reports for the cluster's location; that + list does not take private-network capacity into account. + + The command returns once the API accepts the request (status + "accepted"). With --wait it polls until the pool is active with all + workers ready (status "completed"). If the wait times out, the + backend keeps trying to create workers, and workers that already + exist keep billing: delete the pool to stop. + + A create is sent once and is never retried: if it fails with a + server or network error, the node pool may exist anyway, so check + node-pool list before you run it again. + `), + Example: cmdutil.Examples(` + # Ask for each value (terminal only) + verda mk8s node-pool create + + # Find SSH key IDs and the instance types of a location + verda ssh-key list + verda availability --location FIN-03 + + # One worker, without prompts + verda mk8s node-pool create --cluster prod --name workers --instance-type 1V100.6V --ssh-key $KEY_ID + + # Three workers with two SSH keys; wait up to 30 minutes until all are ready + verda mk8s node-pool create --cluster prod --name gpu --instance-type 1V100.6V --ssh-key $KEY_ID --ssh-key $OTHER_KEY_ID --replicas 3 --wait --wait-timeout 30m + + # Agent mode: JSON result and structured errors + verda --agent mk8s node-pool create --cluster prod -l FIN-03 --name workers --instance-type 1V100.6V --ssh-key $KEY_ID --wait + `), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + opts.replicasSet = cmd.Flags().Changed(flagReplicas) + return runNodePoolCreate(cmd, f, ioStreams, opts) + }, + } + opts.addFlags(cmd) + flags := cmd.Flags() + flags.StringVar(&opts.Name, flagName, "", "Node pool name: a DNS label of at most 30 characters, unique in the project") + flags.StringVar(&opts.InstanceType, flagInstanceType, "", "Instance type of the workers, for example 1V100.6V") + flags.StringArrayVar(&opts.SSHKeys, flagSSHKey, nil, "ID of an SSH key for the workers (see verda ssh-key list); repeat the flag for more keys") + flags.IntVar(&opts.Replicas, flagReplicas, defaultPoolReplicas, "Number of workers, 0 or more") + flags.BoolVar(&opts.Wait.Wait, flagWait, false, nodePoolCreateWaitUsage) + flags.DurationVar(&opts.Wait.Timeout, flagWaitTimeout, defaultPoolWaitTimeout, waitTimeUsage) + return cmd +} + +func (o *nodePoolCreateOptions) trim() { + o.Cluster, o.Location = strings.TrimSpace(o.Cluster), strings.TrimSpace(o.Location) + o.Name, o.InstanceType = strings.TrimSpace(o.Name), strings.TrimSpace(o.InstanceType) + keys := make([]string, 0, len(o.SSHKeys)) + for _, k := range o.SSHKeys { + if k = strings.TrimSpace(k); k != "" && !slices.Contains(keys, k) { + keys = append(keys, k) + } + } + o.SSHKeys = keys +} + +// validate rejects values that cannot work before any prompt or call. The errors +// are UsageErrors: main prints an AgentError as JSON even for a person. +func (o *nodePoolCreateOptions) validate(cmd *cobra.Command) error { + if o.Wait.Wait && o.Wait.Timeout <= 0 { + return cmdutil.UsageErrorf(cmd, "--%s must be greater than 0", flagWaitTimeout) + } + if o.Replicas < 0 { + return cmdutil.UsageErrorf(cmd, "--%s must be 0 or more", flagReplicas) + } + return nil +} + +// missingValues lists the required values that are empty. +func (o *nodePoolCreateOptions) missingValues() []string { + var m []string + if o.Cluster == "" { + m = append(m, "--"+flagCluster) + } + if o.Name == "" { + m = append(m, "--"+flagName) + } + if o.InstanceType == "" { + m = append(m, "--"+flagInstanceType) + } + if len(o.SSHKeys) == 0 { + m = append(m, "--"+flagSSHKey) + } + return m +} + +func (o *nodePoolCreateOptions) request() verda.CreateManagedKubernetesNodePoolRequest { + replicas := o.Replicas + return verda.CreateManagedKubernetesNodePoolRequest{ + Name: o.Name, + Replicas: &replicas, + InstanceType: o.InstanceType, + SSHKeyIDs: o.SSHKeys, + } +} + +func runNodePoolCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *nodePoolCreateOptions) error { + opts.trim() + if err := opts.validate(cmd); err != nil { + return err + } + missing := opts.missingValues() + interactive := interactiveTTY(f) + if len(missing) > 0 && !interactive { + return cmdutil.NewMissingFlagsError(missing) + } + client, err := f.VerdaClient() + if err != nil { + return err + } + + // A cluster given by flag is found first, so a wrong name fails before any prompt. + if opts.Cluster != "" { + t, err := resolveCluster(cmd, f, ioStreams, client, opts.Cluster, opts.Location) + if err != nil { + return err + } + opts.Location = t.Location + } + if len(missing) > 0 { + flow := buildNodePoolCreateFlow(opts, newWizardCache(f, client)) + if err := newWizardEngine(f, ioStreams).Run(cmd.Context(), flow); err != nil { + if stop, ok := errors.AsType[*stopError](err); ok { + return stop + } + return err + } + opts.trim() + } + target := &clusterTarget{Name: opts.Cluster, Location: opts.Location} + req := opts.request() + + if interactive { + ok, err := confirmNodePoolCreate(cmd.Context(), f, ioStreams, target, &req) + if err != nil || !ok { + return err + } + } + + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + pool, err := cmdutil.WithSpinner(ctx, f.Status(), "Creating node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { + return client.ManagedKubernetes.CreateNodePool(ctx, target.Location, target.Name, req) + }) + if err != nil { + listCmd := fmt.Sprintf("verda mk8s node-pool list --cluster %s -l %s", target.Name, target.Location) + return createFailed(f, ioStreams, kindNodePool, listCmd, err) + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response:", newPoolView(pool)) + + result := PoolResult{Status: statusAccepted, Pool: newPoolView(pool)} + var waitErr error + if opts.Wait.Wait { + last, err := waitForPool(cmd.Context(), f, ioStreams, client, target, pool.Name, poolCreated(opts.Replicas), opts.Wait.Timeout) + if last != nil { + result.Pool = newPoolView(last) + } + if err == nil { + result.Status = statusCompleted + } else { + waitErr = poolWaitFailure(err, poolWaitCreate, target, pool, last, opts.Wait.Timeout, 0) + result.WaitError = waitErr.Error() + } + } + if written, err := writeResult(f, ioStreams, result, waitErr); written { + return err + } + printPoolCreated(ioStreams, target, &result) + return waitErr +} + +func confirmNodePoolCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, req *verda.CreateManagedKubernetesNodePoolRequest) (bool, error) { + replicas := 0 + if req.Replicas != nil { + replicas = *req.Replicas + } + _, _ = fmt.Fprintf(ioStreams.ErrOut, "\nCreate node pool %s\n Cluster: %s (%s)\n Instance type: %s\n Workers: %d\n SSH keys: %s\n\n", + req.Name, t.Name, t.Location, req.InstanceType, replicas, strings.Join(req.SSHKeyIDs, ", ")) + return askConfirm(ctx, ioStreams, f.Prompter(), "Create node pool "+req.Name+"?") +} + +func printPoolCreated(ioStreams cmdutil.IOStreams, t *clusterTarget, r *PoolResult) { + p := &r.Pool + if r.Status == statusCompleted { + _, _ = fmt.Fprintf(ioStreams.Out, "Node pool %s completed (cluster %s, %s): %d/%d workers ready\n", p.Name, t.Name, t.Location, p.ReadyReplicas, p.Replicas) + return + } + _, _ = fmt.Fprintf(ioStreams.Out, "Node pool %s accepted (cluster %s, %s), state %s.\n", p.Name, t.Name, t.Location, p.State) + if r.WaitError == "" { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Check progress with: %s\n", poolDescribeCmd(t, p.Name)) + } +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_create_test.go b/internal/verda-cli/cmd/mk8s/nodepool_create_test.go new file mode 100644 index 00000000..1c90adc2 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_create_test.go @@ -0,0 +1,367 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "slices" + "strings" + "sync/atomic" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +var poolCreateArgs = []string{ + "node-pool", "create", "--cluster", "prod", "--name", "workers", "--instance-type", "1V100.6V", + "--ssh-key", "key-1", "--ssh-key", "key-2", +} + +func withPoolArgs(extra ...string) []string { return append(slices.Clone(poolCreateArgs), extra...) } + +func TestNodePoolCreate_FlagsSendOneRequest(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), poolCreateArgs...) + if err != nil { + t.Fatal(err) + } + result := decodeJSON[PoolResult](t, out) + if result.Status != "accepted" || result.Pool.Name != "workers" || result.Pool.State != "creating" { + t.Errorf("result = %+v", result) + } + + posts := api.requestsMatching("POST") + if len(posts) != 1 || posts[0].Path != "/mk8sclusters/FIN-03/prod/pools" { + t.Fatalf("POST requests = %v", posts) + } + body := postBodyAny(t, api) + if body["name"] != "workers" || body["instance_type"] != "1V100.6V" || body["replicas"] != float64(1) { + t.Errorf("body = %v", body) + } + if keys, _ := body["ssh_key_ids"].([]any); len(keys) != 2 || keys[0] != "key-1" || keys[1] != "key-2" { + t.Errorf("ssh_key_ids = %v", body["ssh_key_ids"]) + } + // The instance type is not checked: no availability or SSH key lookup. + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters"}) { + t.Errorf("reads = %v, want only the cluster lookup", got) + } +} + +func TestNodePoolCreate_ReplicasAndLocation(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--replicas", "0", "-l", "FIN-03")...); err != nil { + t.Fatal(err) + } + if body := postBodyAny(t, api); body["replicas"] != float64(0) { + t.Errorf("an explicit 0 must be sent, body = %v", body) + } + if got := api.paths("GET"); len(got) != 0 { + t.Errorf("-l skips the cluster lookup, reads = %v", got) + } +} + +func TestNodePoolCreate_TableSaysAccepted(t *testing.T) { + t.Parallel() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, poolWizardAPI()), nil), poolCreateArgs...) + if err != nil || !strings.Contains(out, "Node pool workers accepted (cluster prod, FIN-03)") || strings.Contains(out, "completed") { + t.Fatalf("out=%q err=%v", out, err) + } + if !strings.Contains(errOut, "verda mk8s node-pool describe workers --cluster prod -l FIN-03") { + t.Errorf("stderr = %q", errOut) + } +} + +func TestNodePoolCreate_MissingFlagsOffTerminal(t *testing.T) { + t.Parallel() + for name, f := range map[string]*cmdutil.TestFactory{ + "table": tableFactory(newFakeClient(t, poolWizardAPI()), nil), + "agent": agentFactory(newFakeClient(t, poolWizardAPI())), + } { + _, _, err := runMK8s(t, f, "node-pool", "create") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("%s: err = %v", name, err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "--name", "--instance-type", "--ssh-key"}) { + t.Errorf("%s: missing = %v", name, missing) + } + } +} + +func TestNodePoolCreate_UnknownClusterAndInvalidInput(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + args := slices.Clone(poolCreateArgs) + args[slices.Index(args, "--cluster")+1] = "ghost" + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs("--replicas", "-1")...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Errorf("a negative count is a usage error, got %v", err) + } + if n := len(api.requestsMatching("POST")); n != 0 { + t.Errorf("%d POST(s) for invalid input", n) + } +} + +func TestNodePoolCreate_FailureWarnings(t *testing.T) { + t.Parallel() + const route = "POST /mk8sclusters/{loc}/{name}/pools" + + t.Run("502 warns once", func(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), poolCreateArgs...) + if err == nil || !strings.Contains(err.Error(), "upstream failed") { + t.Fatalf("err = %v", err) + } + if !strings.Contains(errOut, "The node pool may have been created anyway.") || !strings.Contains(errOut, "node-pool list --cluster prod -l FIN-03") { + t.Errorf("stderr = %q", errOut) + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d, a create is sent once", n) + } + }) + + t.Run("502 in agent details", func(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} + _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), poolCreateArgs...) + if w, _ := cmdutil.ClassifyError(err).Details["warning"].(string); !strings.Contains(w, "node pool may have been created anyway") || errOut != "" { + t.Errorf("details=%v stderr=%q", cmdutil.ClassifyError(err).Details, errOut) + } + }) + + t.Run("400 and 409 do not warn", func(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + api.addPool("staging", "workers", "active", 1, 1) // the name is taken in another cluster + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), poolCreateArgs...) + if err == nil || !strings.Contains(err.Error(), "pool_already_exists") { + t.Fatalf("the API reason must be shown: %v", err) + } + if errOut != "" { + t.Errorf("unexpected warning %q", errOut) + } + }) +} + +func TestNodePoolCreate_WaitCompletes(t *testing.T) { + fastPoll(t) + api := poolWizardAPI() + var reads atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + switch reads.Add(1) { + case 1: // stale: active with no worker yet, not what was asked for + p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 0 + case 2: + p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 2 + default: + p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 3 + } + } + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs("--replicas", "3", "--wait", "--wait-timeout", "30s")...) + if err != nil || !strings.Contains(out, "Node pool workers completed (cluster prod, FIN-03): 3/3 workers ready") { + t.Fatalf("out=%q err=%v", out, err) + } + if got := reads.Load(); got != 3 { + t.Errorf("reads = %d, want 3: a stale or partial read must not end the wait", got) + } +} + +func TestNodePoolCreate_WaitKeepsGoingWhileDegraded(t *testing.T) { + fastPoll(t) + api := poolWizardAPI() + var reads atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + if reads.Add(1) < 3 { + p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateDegraded, 0 + p.Errors = []verda.ManagedKubernetesNodePoolError{{Reason: "insufficient_capacity", Since: testCreated}} + return + } + p.State, p.ReadyReplicas, p.Errors = verda.ManagedKubernetesNodePoolStateActive, 1, nil + } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--wait")...) + if err != nil { + t.Fatal(err) + } + if r := decodeJSON[PoolResult](t, out); r.Status != "completed" || reads.Load() != 3 { + t.Errorf("result=%+v reads=%d", r, reads.Load()) + } +} + +func TestNodePoolCreate_WaitTimeout(t *testing.T) { + fastPoll(t) + api := poolWizardAPI() + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateDegraded, 1 + p.Errors = []verda.ManagedKubernetesNodePoolError{{Reason: "insufficient_capacity", Since: testCreated}} + } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--replicas", "3", "--wait", "--wait-timeout", "300ms")...) + if err == nil { + t.Fatal("a timeout must fail") + } + for _, want := range []string{ + "timed out after 300ms", "node pool workers in cluster prod (FIN-03)", "last state: degraded, 1/3 workers ready", + "errors: insufficient_capacity", "The backend keeps trying to create workers.", "keep billing", + "verda mk8s node-pool delete workers --cluster prod -l FIN-03", + } { + if !strings.Contains(err.Error(), want) { + t.Errorf("error misses %q: %v", want, err) + } + } + if r := decodeJSON[PoolResult](t, out); r.Status != "accepted" || r.WaitError == "" || r.Pool.ReadyReplicas != 1 { + t.Errorf("result = %+v", r) + } +} + +func TestNodePoolCreate_WaitReadFailure(t *testing.T) { + fastPoll(t) + api := poolWizardAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/pools/{pool}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs("--wait")...) + if err == nil || !strings.Contains(err.Error(), "node pool workers in cluster prod (FIN-03) was accepted, but --wait could not read it") { + t.Fatalf("err = %v", err) + } + // The pool exists and may keep creating workers, so this ending carries the billing note too. + if !strings.Contains(err.Error(), "keep billing") { + t.Errorf("err = %v, want the billing note", err) + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d", n) + } +} + +func TestNodePoolCreate_WaitTimeoutMustBePositive(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolWizardAPI()), nil), withPoolArgs("--wait", "--wait-timeout", "0s")...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Fatalf("err = %v: want a usage error", err) + } +} + +func TestNodePoolCreate_ValuesAreTrimmed(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + args := []string{ + "node-pool", "create", "--cluster", " prod ", "--name", " workers ", "--instance-type", " 1V100.6V ", + "--ssh-key", " key-1 ", "--ssh-key", " ", "--ssh-key", "key-1", + } + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); err != nil { + t.Fatal(err) + } + body := postBodyAny(t, api) + if body["name"] != "workers" || body["instance_type"] != "1V100.6V" { + t.Errorf("body = %v", body) + } + if keys, _ := body["ssh_key_ids"].([]any); len(keys) != 1 || keys[0] != "key-1" { + t.Errorf("a blank key is dropped and the others trimmed: %v", body["ssh_key_ids"]) + } +} + +func TestNodePoolCreate_NegativeReplicasNeverReachesTheSummary(t *testing.T) { + withTerminal(t) + api := poolWizardAPI() + // No confirm queued: reaching the summary would fail the test. + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), withPoolArgs("--replicas", "-1")...) + if err == nil || strings.Contains(errOut, "Create node pool") { + t.Errorf("err=%v stderr=%q", err, errOut) + } +} + +func TestNodePoolCreate_WaitUntilActiveAndNotJustReady(t *testing.T) { + fastPoll(t) + // A pool that is degraded or still creating is not done, even when ready == requested. + for name, replicas := range map[string]string{"zero workers": "0", "three workers": "3"} { + api := poolWizardAPI() + want := 0 + if replicas == "3" { + want = 3 + } + var reads atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + state := verda.ManagedKubernetesNodePoolStateCreating + switch reads.Add(1) { + case 2: + state = verda.ManagedKubernetesNodePoolStateDegraded + case 3: + state = verda.ManagedKubernetesNodePoolStateActive + } + p.State, p.ReadyReplicas = state, want + } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--replicas", replicas, "--wait")...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + if r := decodeJSON[PoolResult](t, out); r.Status != "completed" || reads.Load() != 3 { + t.Errorf("%s: status=%s reads=%d, want completed after the third read", name, r.Status, reads.Load()) + } + } +} + +func TestNodePoolCreate_InteractiveSummaryAndConfirm(t *testing.T) { + withTerminal(t) + + t.Run("confirmed", func(t *testing.T) { + api := poolWizardAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), poolCreateArgs...) + if err != nil { + t.Fatal(err) + } + for _, want := range []string{"Create node pool workers", "prod (FIN-03)", "1V100.6V", "key-1, key-2"} { + if !strings.Contains(errOut, want) { + t.Errorf("summary misses %q: %s", want, errOut) + } + } + if n := len(api.requestsMatching("POST")); n != 1 { + t.Errorf("POSTs = %d", n) + } + }) + + t.Run("declined, Esc and Ctrl+C create nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "declined": {Prompter: tuitest.New().AddConfirm(false)}, + "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, + } { + api := poolWizardAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + _, errOut, err := runMK8s(t, f, poolCreateArgs...) + if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("POST")) != 0 { + t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + } + } + }) + + t.Run("agent and json never prompt", func(t *testing.T) { + for name, f := range map[string]*cmdutil.TestFactory{ + "agent": agentFactory(newFakeClient(t, poolWizardAPI())), + "json": jsonFactory(newFakeClient(t, poolWizardAPI())), + } { + if _, _, err := runMK8s(t, f, poolCreateArgs...); err != nil { + t.Errorf("%s: %v", name, err) + } + } + }) +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_delete.go b/internal/verda-cli/cmd/mk8s/nodepool_delete.go new file mode 100644 index 00000000..e5ab5ce2 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_delete.go @@ -0,0 +1,123 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "strings" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdNodePoolDelete creates `verda mk8s node-pool delete`. +func NewCmdNodePoolDelete(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + scope := &poolScope{} + var yes bool + cmd := &cobra.Command{ + Use: "delete []", + Aliases: []string{"rm"}, + Short: "Delete a node pool", + Long: longDesc(` + Delete a node pool. After the API accepts the request, it deletes + the workers and their VMs in the background: the node pool is gone + from node-pool list at once, but deleting the VMs takes longer. The + command has no --wait. + + Without or --cluster, a terminal session shows a picker. + + Asks for confirmation on a terminal. --yes skips it and is required + in scripts, agent mode and -o json|yaml. + `), + Example: cmdutil.Examples(` + verda mk8s node-pool delete workers --cluster prod + + # Without prompts + verda mk8s node-pool delete workers --cluster prod -l FIN-03 --yes + verda --agent mk8s node-pool delete workers --cluster prod --yes + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runNodePoolDelete(cmd, f, ioStreams, scope, args, yes) + }, + } + scope.addFlags(cmd) + cmd.Flags().BoolVarP(&yes, flagYes, "y", false, yesUsage) + return cmd +} + +func runNodePoolDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string, yes bool) error { + if err := checkPoolTarget(f, cmd, scope, args); err != nil { + return err + } + if err := checkConfirmed(f, yes, "delete"); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := scope.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + // A named pool is deleted without reading it first: a pool whose read fails must stay deletable. + name, err := poolToDelete(cmd, f, ioStreams, client, target, args) + if err != nil || name == "" { + return err + } + + if !yes { + ok, err := confirmWarning(cmd.Context(), ioStreams, f.Prompter(), + fmt.Sprintf("Deletes node pool %s and its worker VMs.", name), + []string{"This action cannot be undone."}, + fmt.Sprintf("Delete node pool %s (cluster %s)?", name, target.Name)) + if err != nil || !ok { + return err + } + } + + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Delete node pool:", map[string]string{"name": name, "cluster": target.Name, "location": target.Location}) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + err = cmdutil.RunWithSpinner(ctx, f.Status(), "Deleting node pool...", func(ctx context.Context) error { + return client.ManagedKubernetes.DeleteNodePool(ctx, target.Location, target.Name, name) + }) + if err != nil { + return mapErr(err) + } + + result := ActionResult{Resource: kindNodePool, Name: name, Location: target.Location, Cluster: target.Name, Action: "delete", Status: statusAccepted} + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result); wrote { + return err + } + _, _ = fmt.Fprintln(ioStreams.Out, "Deletion accepted. Worker VMs are deleted in the background.") + return nil +} + +// poolToDelete is args[0], or the pool the user picks; "" means canceled or no pool. +func poolToDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, args []string) (string, error) { + if len(args) > 0 { + return strings.TrimSpace(args[0]), nil + } + pool, err := resolvePool(cmd, f, ioStreams, client, t, nil) + if err != nil || pool == nil { + return "", err + } + return pool.Name, nil +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go b/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go new file mode 100644 index 00000000..1c89a7be --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go @@ -0,0 +1,169 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "slices" + "strings" + "testing" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestNodePoolDelete_YesMakesOneDeleteCall(t *testing.T) { + t.Parallel() + api := scaleAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "delete", "workers", "--cluster", "prod", "--yes") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Deletion accepted. Worker VMs are deleted in the background.") { + t.Errorf("stdout = %q", out) + } + dels := api.requestsMatching("DELETE") + if len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-03/prod/pools/workers" { + t.Fatalf("DELETE requests = %v", dels) + } + if len(api.requestsMatching("POST"))+len(api.requestsMatching("PATCH")) != 0 { + t.Error("delete must not write anything else") + } + // A named pool is not read first (a pool whose read fails must stay deletable), and there is no --wait. + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters"}) { + t.Errorf("reads = %v, want only the cluster lookup", got) + } +} + +func TestNodePoolDelete_APoolThatCannotBeReadCanStillBeDeleted(t *testing.T) { + t.Parallel() + api := scaleAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/pools/{pool}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "delete", "workers", "--cluster", "prod", "-l", "FIN-03", "--yes"); err != nil { + t.Fatal(err) + } + if got := api.paths("GET"); len(got) != 0 { + t.Errorf("reads = %v, want none with -l", got) + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("DELETEs = %d", n) + } +} + +func TestNodePoolDelete_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "np", "rm", "workers", "--cluster", "prod", "-l", "FIN-03", "--yes") + if err != nil { + t.Fatal(err) + } + got := decodeJSON[map[string]any](t, out) + want := map[string]any{"resource": "node pool", "name": "workers", "cluster": "prod", "location": "FIN-03", "action": "delete", "status": "accepted"} + for k, v := range want { + if got[k] != v { + t.Errorf("result[%s] = %v, want %v", k, got[k], v) + } + } +} + +func TestNodePoolDelete_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { + return tableFactory(newFakeClient(t, api), nil) + }, + } { + api := scaleAPI() + _, _, err := runMK8s(t, mk(api, t), "node-pool", "delete", "workers", "--cluster", "prod") + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { + t.Errorf("%s: err = %v", name, err) + } + if n := len(api.requests); n != 0 { + t.Errorf("%s: %d request(s) before the check", name, n) + } + } +} + +func TestNodePoolDelete_MissingTargets(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "node-pool", "delete", "--yes") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { + t.Errorf("missing = %v", missing) + } +} + +func TestNodePoolDelete_Interactive(t *testing.T) { + withTerminal(t) + + t.Run("the warning names the worker VMs", func(t *testing.T) { + api := scaleAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), "node-pool", "delete", "workers", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "Deletes node pool workers and its worker VMs.") || !strings.Contains(errOut, "cannot be undone") { + t.Errorf("warning = %q", errOut) + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("DELETEs = %d", n) + } + }) + + t.Run("pickers then confirm", func(t *testing.T) { + api := scaleAPI() + p := pickerAnswers(0, 0).AddConfirm(true) + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "node-pool", "delete"); err != nil { + t.Fatal(err) + } + if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-03/prod/pools/workers" { + t.Errorf("DELETE requests = %v", dels) + } + }) + + t.Run("declined, Esc and Ctrl+C delete nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "declined": {Prompter: tuitest.New().AddConfirm(false)}, + "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, + } { + api := scaleAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + _, errOut, err := runMK8s(t, f, "node-pool", "delete", "workers", "--cluster", "prod") + if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("DELETE")) != 0 { + t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + } + } + }) +} + +func TestNodePoolDelete_Errors(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, scaleAPI()), nil), "node-pool", "delete", "ghost", "--cluster", "prod", "--yes") + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown pool: %v", err) + } + api := newFakeAPI() + api.disabled = true + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "delete", "workers", "--cluster", "prod", "-l", "FIN-03", "--yes") + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Errorf("not enabled: %v", err) + } +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_describe.go b/internal/verda-cli/cmd/mk8s/nodepool_describe.go new file mode 100644 index 00000000..8273b8b3 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_describe.go @@ -0,0 +1,113 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "fmt" + "io" + "strings" + "text/tabwriter" + + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdNodePoolDescribe creates `verda mk8s node-pool describe`. +func NewCmdNodePoolDescribe(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + scope := &poolScope{} + cmd := &cobra.Command{ + Use: "describe []", + Aliases: []string{"get", "show"}, + Short: "Show a node pool", + Long: longDesc(` + Show a node pool: its instance type, worker counts, state, SSH key + IDs and, when it is not healthy, the reasons the API reports. + + Without or --cluster, a terminal session shows a picker. + `), + Example: cmdutil.Examples(` + # Pick the cluster and the node pool from lists (terminal only) + verda mk8s node-pool describe + + verda mk8s node-pool describe workers --cluster prod + + # JSON for scripts: state, worker counts and errors[] + verda mk8s node-pool describe workers --cluster prod -o json + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runNodePoolDescribe(cmd, f, ioStreams, scope, args) + }, + } + scope.addFlags(cmd) + return cmd +} + +func runNodePoolDescribe(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string) error { + if err := checkPoolTarget(f, cmd, scope, args); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := scope.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + pool, err := resolvePool(cmd, f, ioStreams, client, target, args) + if err != nil || pool == nil { + return err + } + view := newPoolView(pool) + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response: node pool:", view) + + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), view); wrote { + return err + } + return printPoolDescribe(ioStreams.Out, target, &view) +} + +func printPoolDescribe(w io.Writer, t *clusterTarget, v *PoolView) error { + tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + rows := [][2]string{ + {"Name", v.Name}, + {"Cluster", t.Name}, + {"Location", t.Location}, + {"Instance type", v.InstanceType}, + {"Workers", fmt.Sprintf("%d/%d ready", v.ReadyReplicas, v.Replicas)}, + {"State", v.State}, + {"SSH keys", cmdutil.TextColumn(strings.Join(v.SSHKeyIDs, ", "))}, + {"Created", timeColumn(v.CreatedAt)}, + } + for _, r := range rows { + _, _ = fmt.Fprintf(tw, "%s:\t%s\n", r[0], r[1]) + } + if err := tw.Flush(); err != nil { + return err + } + if len(v.Errors) == 0 { + return nil + } + _, _ = fmt.Fprintln(w, "\nErrors:") + etw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + _, _ = fmt.Fprintln(etw, " REASON\tMESSAGE\tSINCE") + for i := range v.Errors { + e := &v.Errors[i] + _, _ = fmt.Fprintf(etw, " %s\t%s\t%s\n", e.Reason, cmdutil.TextColumn(e.Message), timeColumn(e.Since)) + } + return etw.Flush() +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go b/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go new file mode 100644 index 00000000..f8dbb342 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go @@ -0,0 +1,151 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "reflect" + "regexp" + "slices" + "strings" + "testing" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestNodePoolDescribe_JSONHasEveryFieldInItsPlace(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, poolAPI())), "node-pool", "describe", "gpu", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + want := PoolView{ + Name: "gpu", InstanceType: "1V100.6V", Replicas: 2, ReadyReplicas: 1, State: "degraded", SSHKeyIDs: []string{"key-1"}, + Errors: []PoolErrorView{{Reason: "insufficient_capacity", Message: "no free 1V100.6V", Since: testCreated}}, + CreatedAt: testCreated, + } + if got := decodeJSON[PoolView](t, out); !reflect.DeepEqual(got, want) { + t.Errorf("got %+v\nwant %+v", got, want) + } +} + +var poolDescribeLine = regexp.MustCompile(`^([A-Za-z][A-Za-z ]*):\s+(\S.*)$`) + +func TestNodePoolDescribe_Table(t *testing.T) { + t.Parallel() + api := poolAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "describe", "gpu", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + got := map[string]string{} + for line := range strings.SplitSeq(out, "\n") { + if m := poolDescribeLine.FindStringSubmatch(line); m != nil { + got[m[1]] = strings.TrimSpace(m[2]) + } + } + want := map[string]string{ + "Name": "gpu", "Cluster": "prod", "Location": "FIN-03", "Instance type": "1V100.6V", + "Workers": "1/2 ready", "State": "degraded", "SSH keys": "key-1", "Created": "2026-11-19 09:30 UTC", + } + for k, v := range want { + if got[k] != v { + t.Errorf("%s: = %q, want %q", k, got[k], v) + } + } + for _, s := range []string{"Errors:", "REASON", "insufficient_capacity", "no free 1V100.6V"} { + if !strings.Contains(out, s) { + t.Errorf("output misses %q:\n%s", s, out) + } + } + + out, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "describe", "workers", "--cluster", "prod") + if err != nil || strings.Contains(out, "Errors:") { + t.Errorf("a healthy pool shows no Errors section: err=%v\n%s", err, out) + } + if got := api.paths("GET"); !slices.Contains(got, "/mk8sclusters/FIN-03/prod/pools/gpu") { + t.Errorf("requests = %v", got) + } +} + +func TestNodePoolDescribe_AgentAndAliases(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "pool", "show", "workers", "--cluster", "prod", "-l", "FIN-03") + if err != nil { + t.Fatal(err) + } + if v := decodeJSON[PoolView](t, out); v.Name != "workers" || v.Errors == nil || len(v.Errors) != 0 { + t.Errorf("view = %+v", v) + } +} + +func TestNodePoolDescribe_MissingTargets(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "node-pool", "describe") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { + t.Errorf("missing = %v", missing) + } + if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "node-pool", "describe", " ", "--cluster", "prod"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Errorf("blank pool: %v", err) + } + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolAPI()), nil), "node-pool", "describe", "ghost", "--cluster", "prod"); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown pool: %v", err) + } +} + +func TestNodePoolDescribe_Pickers(t *testing.T) { + withTerminal(t) + + t.Run("cluster picker then pool picker", func(t *testing.T) { + api := poolAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 1)), "node-pool", "describe") + if err != nil || !strings.Contains(out, "gpu") { + t.Fatalf("out=%q err=%v", out, err) + } + }) + + t.Run("pool picker only", func(t *testing.T) { + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolAPI()), pickerAnswers(0)), "node-pool", "describe", "--cluster", "prod") + if err != nil || !strings.Contains(out, "workers") { + t.Fatalf("out=%q err=%v", out, err) + } + }) + + t.Run("cancel row and Esc end cleanly", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "cancel row": {Prompter: pickerAnswers(2)}, + "esc": {Prompter: tuitest.New(), selectErr: contextCanceled()}, + } { + f := tableFactory(newFakeClient(t, poolAPI()), nil) + f.PrompterOverride = p + out, errOut, err := runMK8s(t, f, "node-pool", "describe", "--cluster", "prod") + if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { + t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) + } + } + }) + + t.Run("no pools", func(t *testing.T) { + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), tuitest.New()), "node-pool", "describe", "--cluster", "prod") + if err != nil || !strings.Contains(out, "No node pools in cluster prod.") || !strings.Contains(errOut, "node-pool create") { + t.Errorf("out=%q err=%q error=%v", out, errOut, err) + } + }) +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_list.go b/internal/verda-cli/cmd/mk8s/nodepool_list.go new file mode 100644 index 00000000..66506b8b --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_list.go @@ -0,0 +1,93 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "fmt" + "text/tabwriter" + "time" + + "github.com/spf13/cobra" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// NewCmdNodePoolList creates `verda mk8s node-pool list`. +func NewCmdNodePoolList(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + scope := &poolScope{} + cmd := &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: "List the node pools of a cluster", + Long: longDesc(` + List the node pools of a cluster. READY/DESIRED is the number of + workers that joined the cluster over the number you asked for. + + Without --cluster, a terminal session shows a picker. + `), + Example: cmdutil.Examples(` + # Pick the cluster from a list (terminal only) + verda mk8s node-pool list + + verda mk8s node-pool list --cluster prod + + # Short aliases, JSON for scripts + verda mk8s np ls --cluster prod -l FIN-03 -o json + `), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + return runNodePoolList(cmd, f, ioStreams, scope) + }, + } + scope.addFlags(cmd) + return cmd +} + +func runNodePoolList(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope) error { + if missing := scope.missing(f, cmd); len(missing) > 0 { + return cmdutil.NewMissingFlagsError(missing) + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := scope.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + pools, err := listPools(cmd.Context(), f, client, target) + if err != nil { + return err + } + views := newPoolViews(pools) + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), fmt.Sprintf("API response: %d node pool(s):", len(views)), views) + + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), views); wrote { + return err + } + if len(views) == 0 { + printNoPools(ioStreams, target.Name) + return nil + } + + now := time.Now() + tw := tabwriter.NewWriter(ioStreams.Out, 0, 0, 2, ' ', 0) + _, _ = fmt.Fprintln(tw, "NAME\tINSTANCE TYPE\tREADY/DESIRED\tSTATE\tAGE") + for i := range views { + v := &views[i] + _, _ = fmt.Fprintf(tw, "%s\t%s\t%d/%d\t%s\t%s\n", v.Name, v.InstanceType, v.ReadyReplicas, v.Replicas, v.State, ageColumn(v.CreatedAt, now)) + } + return tw.Flush() +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_list_test.go b/internal/verda-cli/cmd/mk8s/nodepool_list_test.go new file mode 100644 index 00000000..90af35c9 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_list_test.go @@ -0,0 +1,178 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "reflect" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + "go.yaml.in/yaml/v3" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// poolAPI has the clusters of seededClusters and two pools in "prod", one degraded. +func poolAPI() *fakeAPI { + api := seededClusters() + api.addPool("prod", "workers", "active", 3, 3) + api.addPool("prod", "gpu", "degraded", 2, 1) + api.pools["prod"][1].Errors = []verda.ManagedKubernetesNodePoolError{ + {Reason: "insufficient_capacity", Message: "no free 1V100.6V", Since: testCreated}, + } + return api +} + +func TestNodePoolList_Table(t *testing.T) { + t.Parallel() + api := poolAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "list", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(out), "\n") + if len(lines) != 3 { + t.Fatalf("want a header and 2 rows:\n%s", out) + } + if got := strings.Join(strings.Fields(lines[0]), " "); got != "NAME INSTANCE TYPE READY/DESIRED STATE AGE" { + t.Errorf("header = %q", got) + } + if f := strings.Fields(lines[1]); f[0] != "workers" || f[1] != "1V100.6V" || f[2] != "3/3" || f[3] != "active" { + t.Errorf("workers row = %v", f) + } + if f := strings.Fields(lines[2]); f[0] != "gpu" || f[2] != "1/2" || f[3] != "degraded" { + t.Errorf("gpu row = %v", f) + } + want := []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/pools"} + if got := api.paths("GET"); !slices.Equal(got, want) { + t.Errorf("requests = %v, want %v", got, want) + } +} + +func TestNodePoolList_LocationSkipsTheClusterLookup(t *testing.T) { + t.Parallel() + api := poolAPI() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "np", "ls", "--cluster", "prod", "-l", "FIN-03"); err != nil { + t.Fatal(err) + } + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters/FIN-03/prod/pools"}) { + t.Errorf("requests = %v", got) + } +} + +func TestNodePoolList_Structured(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, mk(poolAPI(), t), "node-pool", "list", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + got := decodeJSON[[]PoolView](t, out) + want := []PoolView{ + {Name: "workers", InstanceType: "1V100.6V", Replicas: 3, ReadyReplicas: 3, State: "active", SSHKeyIDs: []string{"key-1"}, Errors: []PoolErrorView{}, CreatedAt: testCreated}, + { + Name: "gpu", InstanceType: "1V100.6V", Replicas: 2, ReadyReplicas: 1, State: "degraded", SSHKeyIDs: []string{"key-1"}, + Errors: []PoolErrorView{{Reason: "insufficient_capacity", Message: "no free 1V100.6V", Since: testCreated}}, CreatedAt: testCreated, + }, + } + if !reflect.DeepEqual(got, want) { + t.Errorf("got %+v\nwant %+v", got, want) + } + }) + } + + t.Run("yaml", func(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, poolAPI())), "node-pool", "list", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + var got []map[string]any + if err := yaml.Unmarshal([]byte(out), &got); err != nil { + t.Fatal(err) + } + if len(got) != 2 || got[1]["state"] != "degraded" || got[0]["errors"] == nil { + t.Errorf("got %v", got) + } + }) +} + +func TestNodePoolList_Empty(t *testing.T) { + t.Parallel() + client := newFakeClient(t, seededClusters()) + out, errOut, err := runMK8s(t, tableFactory(client, nil), "node-pool", "list", "--cluster", "prod") + if err != nil || !strings.Contains(out, "No node pools in cluster prod.") || !strings.Contains(errOut, "node-pool create --cluster prod") { + t.Errorf("out=%q err=%q error=%v", out, errOut, err) + } + out, _, err = runMK8s(t, jsonFactory(client), "node-pool", "list", "--cluster", "prod") + if err != nil || strings.TrimSpace(out) != "[]" { + t.Errorf("empty JSON = %q, error=%v", out, err) + } +} + +func TestNodePoolList_ClusterErrors(t *testing.T) { + t.Parallel() + client := newFakeClient(t, seededClusters()) + _, _, err := runMK8s(t, tableFactory(client, nil), "node-pool", "list") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster"}) { + t.Errorf("missing = %v", missing) + } + if _, _, err := runMK8s(t, tableFactory(client, nil), "node-pool", "list", "--cluster", "ghost"); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown cluster: %v", err) + } + if _, _, err := runMK8s(t, tableFactory(client, nil), "node-pool", "list", "--cluster", " "); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Errorf("blank cluster: %v", err) + } +} + +func TestNodePoolList_Debug(t *testing.T) { + t.Parallel() + f := tableFactory(newFakeClient(t, poolAPI()), nil) + f.DebugOverride = true + _, errOut, err := runMK8s(t, f, "node-pool", "list", "--cluster", "prod") + if err != nil || !strings.Contains(errOut, "API response: 2 node pool(s):") || !strings.Contains(errOut, `"ready_replicas": 1`) { + t.Errorf("err=%v debug=%q", err, errOut) + } +} + +func TestNodePoolList_ClusterPicker(t *testing.T) { + withTerminal(t) + api := poolAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0)), "node-pool", "list") + if err != nil || !strings.Contains(out, "workers") { + t.Fatalf("out=%q err=%v", out, err) + } +} + +func TestNodePoolList_NotEnabled(t *testing.T) { + t.Parallel() + api := newFakeAPI() + api.disabled = true + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "list", "--cluster", "prod", "-l", "FIN-03") + if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { + t.Fatalf("err = %v", err) + } +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_scale.go b/internal/verda-cli/cmd/mk8s/nodepool_scale.go new file mode 100644 index 00000000..3990965f --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_scale.go @@ -0,0 +1,295 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "strconv" + "strings" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + "github.com/verda-cloud/verda-cli/pkg/tui" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +type nodePoolScaleOptions struct { + poolScope + Replicas int + Yes bool + Wait cmdutil.WaitOptions + + replicasSet bool +} + +// PoolScaleResult is the structured output of `node-pool scale`. +type PoolScaleResult struct { + Status string `json:"status" yaml:"status"` + PreviousReplicas int `json:"previous_replicas" yaml:"previous_replicas"` + Pool PoolView `json:"pool" yaml:"pool"` + WaitError string `json:"wait_error,omitempty" yaml:"wait_error,omitempty"` +} + +// NewCmdNodePoolScale creates `verda mk8s node-pool scale`. +func NewCmdNodePoolScale(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { + opts := &nodePoolScaleOptions{} + cmd := &cobra.Command{ + Use: "scale []", + Short: "Change the number of workers of a node pool", + Long: longDesc(` + Set the number of workers of a node pool. Scaling down, to 0 too, + removes workers and deletes their VMs: it asks for confirmation on + a terminal, and --yes is required in scripts, agent mode and + -o json|yaml. Scaling up never needs --yes; on a terminal it shows + a summary and asks. + + Without or --cluster, a terminal session shows a picker. + Without --replicas, it shows the current number and asks for the + new one; in scripts, agent mode and -o json|yaml the flag is + required. + + The command returns once the API accepts the request (status + "accepted"). With --wait it polls until the pool is active with the + new number of workers, all ready (status "completed"). After a + scale-down the VMs of the removed workers can still be deleting in + the background when the wait ends. Scaling to the current number + sends nothing; with --wait it still waits when the pool is not yet + active with all of them ready, so a retry after a timeout works. + `), + Example: cmdutil.Examples(` + # Pick the node pool and type the number (terminal only) + verda mk8s node-pool scale + + # Scale up and wait until the new workers are ready + verda mk8s node-pool scale workers --cluster prod --replicas 5 --wait + + # Scale down to zero without prompts (deletes the worker VMs) + verda mk8s node-pool scale workers --cluster prod --replicas 0 --yes + verda --agent mk8s node-pool scale workers --cluster prod --replicas 0 --yes --wait + `), + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.replicasSet = cmd.Flags().Changed(flagReplicas) + return runNodePoolScale(cmd, f, ioStreams, opts, args) + }, + } + opts.addFlags(cmd) + flags := cmd.Flags() + flags.IntVar(&opts.Replicas, flagReplicas, 0, "New number of workers, 0 or more (a lower number deletes workers)") + flags.BoolVarP(&opts.Yes, flagYes, "y", false, "Skip confirmation (required to scale down in scripts, agent mode and -o json|yaml)") + flags.BoolVar(&opts.Wait.Wait, flagWait, false, "Wait until the pool is active with the new number of workers ready") + flags.DurationVar(&opts.Wait.Timeout, flagWaitTimeout, defaultPoolWaitTimeout, waitTimeUsage) + return cmd +} + +func runNodePoolScale(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *nodePoolScaleOptions, args []string) error { + if err := opts.validate(f, cmd, args); err != nil { + return err + } + client, err := f.VerdaClient() + if err != nil { + return err + } + target, err := opts.resolve(cmd, f, ioStreams, client) + if err != nil || target == nil { + return err + } + pool, err := resolvePool(cmd, f, ioStreams, client, target, args) + if err != nil || pool == nil { + return err + } + if !opts.replicasSet { + n, ok, err := askReplicas(cmd.Context(), f, ioStreams, pool) + if err != nil || !ok { + return err + } + opts.Replicas = n + } + if opts.Replicas == pool.Replicas && (!opts.Wait.Wait || poolSettled(pool, opts.Replicas)) { + return reportScaleUnchanged(f, ioStreams, pool) + } + + result := PoolScaleResult{Status: statusAccepted, PreviousReplicas: pool.Replicas, Pool: newPoolView(pool)} + accepted := pool + if opts.Replicas != pool.Replicas { + updated, err := opts.applyScale(cmd, f, ioStreams, client, target, pool) + if err != nil || updated == nil { + return err + } + accepted = updated + result.Pool = newPoolView(updated) + } + var waitErr error + if opts.Wait.Wait { + waitErr = opts.wait(cmd.Context(), f, ioStreams, client, target, accepted, &result) + } + if written, err := writeResult(f, ioStreams, result, waitErr); written { + return err + } + printPoolScaled(ioStreams, target, &result, opts.Replicas < pool.Replicas) + return waitErr +} + +// validate checks the flags before any API call. +func (o *nodePoolScaleOptions) validate(f cmdutil.Factory, cmd *cobra.Command, args []string) error { + if o.Wait.Wait && o.Wait.Timeout <= 0 { + return cmdutil.UsageErrorf(cmd, "--%s must be greater than 0", flagWaitTimeout) + } + if o.replicasSet && o.Replicas < 0 { + return cmdutil.UsageErrorf(cmd, "--%s must be 0 or more", flagReplicas) + } + if missing := o.scaleMissing(f, cmd, args); len(missing) > 0 { + return cmdutil.NewMissingFlagsError(missing) + } + return nil +} + +// poolSettled: the pool is already at the scale-done condition for want. +func poolSettled(pool *verda.ManagedKubernetesNodePool, want int) bool { + _, done := poolScaled(want)(pool) + return done +} + +// applyScale confirms and sends the update. It returns nil without an error when the user declined. +func (o *nodePoolScaleOptions) applyScale(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, target *clusterTarget, pool *verda.ManagedKubernetesNodePool) (*verda.ManagedKubernetesNodePool, error) { + if ok, err := o.confirm(cmd.Context(), f, ioStreams, target, pool); err != nil || !ok { + return nil, err + } + replicas := o.Replicas + req := verda.UpdateManagedKubernetesNodePoolRequest{Replicas: &replicas} + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) + ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) + defer cancel() + updated, err := cmdutil.WithSpinner(ctx, f.Status(), "Scaling node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { + return client.ManagedKubernetes.UpdateNodePool(ctx, target.Location, target.Name, pool.Name, req) + }) + if err != nil { + return nil, mapErr(err) + } + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response:", newPoolView(updated)) + return updated, nil +} + +func pluralize(n int, noun string) string { + if n == 1 { + return "1 " + noun + } + return fmt.Sprintf("%d %ss", n, noun) +} + +// reportScaleUnchanged: the pool already has the requested count, so nothing is sent. +func reportScaleUnchanged(f cmdutil.Factory, ioStreams cmdutil.IOStreams, pool *verda.ManagedKubernetesNodePool) error { + result := PoolScaleResult{Status: statusUnchanged, PreviousReplicas: pool.Replicas, Pool: newPoolView(pool)} + if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result); wrote { + return err + } + _, _ = fmt.Fprintf(ioStreams.Out, "Node pool %s already has %s; nothing to do.\n", pool.Name, pluralize(pool.Replicas, "replica")) + return nil +} + +// scaleMissing lists what a scale needs that a terminal cannot ask for. +func (o *nodePoolScaleOptions) scaleMissing(f cmdutil.Factory, cmd *cobra.Command, args []string) []string { + missing := o.missing(f, cmd) + if poolArgMissing(f, args) { + missing = append(missing, argPool) + } + if !o.replicasSet && !interactiveTTY(f) { + missing = append(missing, "--"+flagReplicas) + } + return missing +} + +// confirm applies the confirmation rule: only a scale-down is destructive, and +// only now is the current count known. It reports whether to go on. +func (o *nodePoolScaleOptions) confirm(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, pool *verda.ManagedKubernetesNodePool) (bool, error) { + if o.Yes { + return true, nil + } + down := o.Replicas < pool.Replicas + if !interactiveTTY(f) { + if down { + return false, confirmationRequired("scale") + } + return true, nil + } + return confirmScale(ctx, f, ioStreams, t, pool, o.Replicas, down) +} + +// wait polls until the new count is ready and records the outcome in result. +func (o *nodePoolScaleOptions) wait(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, updated *verda.ManagedKubernetesNodePool, result *PoolScaleResult) error { + last, err := waitForPool(ctx, f, ioStreams, client, t, updated.Name, poolScaled(o.Replicas), o.Wait.Timeout) + if last != nil { + result.Pool = newPoolView(last) + } + if err == nil { + result.Status = statusCompleted + return nil + } + kind := poolWaitScaleUp + switch { + case o.Replicas < result.PreviousReplicas: + kind = poolWaitScaleDown + case o.Replicas == result.PreviousReplicas: + kind = poolWaitSettle + } + err = poolWaitFailure(err, kind, t, updated, last, o.Wait.Timeout, result.PreviousReplicas) + result.WaitError = err.Error() + return err +} + +// askReplicas prompts for the new count. ok is false when the user canceled. +func askReplicas(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, pool *verda.ManagedKubernetesNodePool) (n int, ok bool, err error) { + v, err := f.Prompter().TextInput(ctx, fmt.Sprintf("New number of workers (pool %s has %d)", pool.Name, pool.Replicas), + tui.WithDefault(strconv.Itoa(pool.Replicas)), tui.WithValidation(validateReplicas)) + if err != nil { + if cmdutil.IsPromptCancel(err) { + printCanceled(ioStreams) + return 0, false, nil + } + return 0, false, err + } + n, _ = strconv.Atoi(strings.TrimSpace(v)) + return n, true, nil +} + +func confirmScale(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, pool *verda.ManagedKubernetesNodePool, want int, down bool) (bool, error) { + prompt := fmt.Sprintf("Scale node pool %s to %s?", pool.Name, pluralize(want, "worker")) + if down { + return confirmWarning(ctx, ioStreams, f.Prompter(), + fmt.Sprintf("Scales node pool %s down from %d to %s.", pool.Name, pool.Replicas, pluralize(want, "worker")), + []string{"The removed workers and their VMs are deleted.", "This action cannot be undone."}, prompt) + } + _, _ = fmt.Fprintf(ioStreams.ErrOut, "\nScale node pool %s\n Cluster: %s (%s)\n Workers: %d -> %d\n\n", pool.Name, t.Name, t.Location, pool.Replicas, want) + return askConfirm(ctx, ioStreams, f.Prompter(), prompt) +} + +func printPoolScaled(ioStreams cmdutil.IOStreams, t *clusterTarget, r *PoolScaleResult, down bool) { + p := &r.Pool + if r.Status == statusCompleted { + _, _ = fmt.Fprintf(ioStreams.Out, "Node pool %s scaled from %d to %s (cluster %s, %s).", p.Name, r.PreviousReplicas, pluralize(p.Replicas, "worker"), t.Name, t.Location) + if down { + _, _ = fmt.Fprint(ioStreams.Out, " The removed workers' VMs are still being deleted in the background.") + } + _, _ = fmt.Fprintln(ioStreams.Out) + return + } + _, _ = fmt.Fprintf(ioStreams.Out, "Scaling of node pool %s from %d to %s accepted (cluster %s, %s).\n", p.Name, r.PreviousReplicas, pluralize(p.Replicas, "worker"), t.Name, t.Location) + if r.WaitError == "" { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Check progress with: %s\n", poolDescribeCmd(t, p.Name)) + } +} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go b/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go new file mode 100644 index 00000000..0d8f1cb9 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go @@ -0,0 +1,482 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "net/http" + "slices" + "strconv" + "strings" + "sync/atomic" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// scaleAPI has pool "workers" in cluster "prod": 3 of 3 workers, active. +func scaleAPI() *fakeAPI { + api := seededClusters() + api.addPool("prod", "workers", "active", 3, 3) + return api +} + +func scaleArgs(extra ...string) []string { + return append([]string{"node-pool", "scale", "workers", "--cluster", "prod"}, extra...) +} + +func patchedReplicas(t *testing.T, api *fakeAPI) any { + t.Helper() + patches := api.requestsMatching("PATCH") + if len(patches) != 1 || patches[0].Path != "/mk8sclusters/FIN-03/prod/pools/workers" { + t.Fatalf("PATCH requests = %v", patches) + } + return decodeJSON[map[string]any](t, patches[0].Body)["replicas"] +} + +func TestNodePoolScale_UpNeedsNoYesOffTerminal(t *testing.T) { + t.Parallel() + api := scaleAPI() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5")...) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "Scaling of node pool workers from 3 to 5 workers accepted (cluster prod, FIN-03).") { + t.Errorf("stdout = %q", out) + } + if !strings.Contains(errOut, "verda mk8s node-pool describe workers --cluster prod -l FIN-03") { + t.Errorf("stderr = %q", errOut) + } + if got := patchedReplicas(t, api); got != float64(5) { + t.Errorf("replicas = %v", got) + } + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/pools/workers"}) { + t.Errorf("reads = %v", got) + } +} + +func TestNodePoolScale_DownNeedsYesOffTerminal(t *testing.T) { + t.Parallel() + for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ + "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, + "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { + return tableFactory(newFakeClient(t, api), nil) + }, + } { + for _, replicas := range []string{"1", "0"} { + api := scaleAPI() + _, _, err := runMK8s(t, mk(api, t), scaleArgs("--replicas", replicas)...) + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" || ae.Message != confirmationRequired("scale").Message { + t.Errorf("%s to %s: err = %v", name, replicas, err) + } + if n := len(api.requestsMatching("PATCH")); n != 0 { + t.Errorf("%s to %s: %d PATCH(es) without --yes", name, replicas, n) + } + } + } + + api := scaleAPI() + if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), scaleArgs("--replicas", "0", "--yes")...); err != nil { + t.Fatal(err) + } + if got := patchedReplicas(t, api); got != float64(0) { + t.Errorf("replicas = %v, want an explicit 0", got) + } +} + +func TestNodePoolScale_SameCountSendsNothing(t *testing.T) { + t.Parallel() + api := scaleAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "3")...) + if err != nil || out != "Node pool workers already has 3 replicas; nothing to do.\n" { + t.Fatalf("out=%q err=%v", out, err) + } + api.pools["prod"][0].Replicas, api.pools["prod"][0].ReadyReplicas = 1, 1 + out, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "1")...) + if err != nil || out != "Node pool workers already has 1 replica; nothing to do.\n" { + t.Fatalf("out=%q err=%v", out, err) + } + api.pools["prod"][0].Replicas, api.pools["prod"][0].ReadyReplicas = 3, 3 + out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "3", "--wait")...) + if r := decodeJSON[PoolScaleResult](t, out); err != nil || r.Status != "unchanged" || r.PreviousReplicas != 3 || r.Pool.Replicas != 3 { + t.Errorf("result = %+v err=%v", r, err) + } + if n := len(api.requestsMatching("PATCH")); n != 0 { + t.Errorf("PATCH requests = %d, want none", n) + } +} + +func mustAtoi(t *testing.T, s string) int { + t.Helper() + n, err := strconv.Atoi(s) + if err != nil { + t.Fatal(err) + } + return n +} + +func TestNodePoolScale_SameCountWaitsUntilSettled(t *testing.T) { + fastPoll(t) + cases := map[string]struct { + state string + ready int + wait bool + wantStatus string + }{ + "not ready, no --wait": {state: "active", ready: 1, wantStatus: "unchanged"}, + "fully ready, --wait": {state: "active", ready: 2, wait: true, wantStatus: "unchanged"}, + "not ready, --wait": {state: "active", ready: 1, wait: true, wantStatus: "completed"}, + "degraded at the count, --wait": {state: "degraded", ready: 2, wait: true, wantStatus: "completed"}, + } + for name, tc := range cases { + api := seededClusters() + api.addPool("prod", "workers", tc.state, 2, tc.ready) + var reads atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + if reads.Add(1) > 2 { + p.State, p.Replicas, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 2, 2 + } + } + args := scaleArgs("--replicas", "2") + if tc.wait { + args = append(args, "--wait", "--wait-timeout", "30s") + } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + if r := decodeJSON[PoolScaleResult](t, out); r.Status != tc.wantStatus || r.PreviousReplicas != 2 || r.Pool.Replicas != 2 { + t.Errorf("%s: result = %+v", name, r) + } + if n := len(api.requestsMatching("PATCH")); n != 0 { + t.Errorf("%s: %d PATCH(es), want none", name, n) + } + if waited := reads.Load() > 1; waited != (tc.wantStatus == "completed") { + t.Errorf("%s: waited = %v, pool reads = %d", name, waited, reads.Load()) + } + } +} + +func TestNodePoolScale_SameCountWaitTimeout(t *testing.T) { + fastPoll(t) + api := seededClusters() + api.addPool("prod", "workers", "active", 2, 1) + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 1 } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "2", "--wait", "--wait-timeout", "300ms")...) + if err == nil { + t.Fatal("a timeout must fail") + } + for _, want := range []string{"timed out after 300ms", "scaling of node pool workers in cluster prod (FIN-03)", "1/2 workers ready", "The backend keeps trying to create workers."} { + if !strings.Contains(err.Error(), want) { + t.Errorf("error misses %q: %v", want, err) + } + } + if strings.Contains(err.Error(), "scale back") { + t.Errorf("there is no previous count to scale back to: %v", err) + } + if r := decodeJSON[PoolScaleResult](t, out); r.Status != "accepted" || r.WaitError == "" || len(api.requestsMatching("PATCH")) != 0 { + t.Errorf("result = %+v", r) + } +} + +func TestNodePoolScale_MissingArguments(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "node-pool", "scale") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "", "--replicas"}) { + t.Errorf("missing = %v", missing) + } +} + +func TestNodePoolScale_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), scaleArgs("--replicas", "5", "-l", "FIN-03")...) + if err != nil { + t.Fatal(err) + } + r := decodeJSON[PoolScaleResult](t, out) + if r.Status != "accepted" || r.PreviousReplicas != 3 || r.Pool.Name != "workers" || r.Pool.Replicas != 5 { + t.Errorf("result = %+v", r) + } +} + +func TestNodePoolScale_Interactive(t *testing.T) { + withTerminal(t) + + t.Run("scale-up shows a summary and asks", func(t *testing.T) { + api := scaleAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), scaleArgs("--replicas", "5")...) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "Workers: 3 -> 5") || strings.Contains(errOut, "cannot be undone") { + t.Errorf("summary = %q", errOut) + } + if got := patchedReplicas(t, api); got != float64(5) { + t.Errorf("replicas = %v", got) + } + }) + + t.Run("scale-down, to 0 too, shows the red warning", func(t *testing.T) { + for _, replicas := range []string{"1", "0"} { + api := scaleAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), scaleArgs("--replicas", replicas)...) + if err != nil { + t.Fatal(err) + } + if want := "Scales node pool workers down from 3 to " + pluralize(mustAtoi(t, replicas), "worker") + "."; !strings.Contains(errOut, want) || !strings.Contains(errOut, "VMs are deleted") { + t.Errorf("to %s: warning = %q", replicas, errOut) + } + } + }) + + t.Run("declined, Esc and Ctrl+C change nothing", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "declined": {Prompter: tuitest.New().AddConfirm(false)}, + "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, + "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, + } { + api := scaleAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + _, errOut, err := runMK8s(t, f, scaleArgs("--replicas", "1")...) + if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("PATCH")) != 0 { + t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + } + } + }) + + t.Run("--yes skips the confirmation", func(t *testing.T) { + api := scaleAPI() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), scaleArgs("--replicas", "1", "--yes")...); err != nil { + t.Fatal(err) + } + if got := patchedReplicas(t, api); got != float64(1) { + t.Errorf("replicas = %v", got) + } + }) + + t.Run("pickers and the replicas prompt", func(t *testing.T) { + api := scaleAPI() + p := pickerAnswers(0, 0).AddTextInput("4").AddConfirm(true) // cluster, pool, count, confirm + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "node-pool", "scale"); err != nil { + t.Fatal(err) + } + if got := patchedReplicas(t, api); got != float64(4) { + t.Errorf("replicas = %v", got) + } + }) + + t.Run("canceling the replicas prompt changes nothing", func(t *testing.T) { + api := scaleAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = &textCancelPrompter{Prompter: tuitest.New()} + _, errOut, err := runMK8s(t, f, scaleArgs()...) + if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("PATCH")) != 0 { + t.Errorf("err=%v stderr=%q", err, errOut) + } + }) +} + +// patchSeen reports whether the fake has taken an update; it runs inside a handler, which holds the lock. +func patchSeen(api *fakeAPI) bool { + for _, r := range api.requests { + if r.Method == http.MethodPatch { + return true + } + } + return false +} + +func TestNodePoolScale_WaitUp(t *testing.T) { + fastPoll(t) + api := scaleAPI() + var waited atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + if !patchSeen(api) { + return // the read that finds the pool, before the update + } + switch waited.Add(1) { + case 1: // stale: the old count, all ready, active + p.Replicas, p.ReadyReplicas = 3, 3 + case 2: // new count, workers still joining + p.Replicas, p.ReadyReplicas = 5, 3 + default: + p.Replicas, p.ReadyReplicas = 5, 5 + } + p.State = verda.ManagedKubernetesNodePoolStateActive + } + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "30s")...) + if err != nil || !strings.Contains(out, "Node pool workers scaled from 3 to 5 workers (cluster prod, FIN-03).") { + t.Fatalf("out=%q err=%v", out, err) + } + if strings.Contains(out, "VMs are still being deleted") { + t.Errorf("a scale-up has no removed VMs: %q", out) + } + if got := waited.Load(); got != 3 { + t.Errorf("reads while waiting = %d, want 3: the stale first read must not end the wait", got) + } +} + +func TestNodePoolScale_WaitDownSaysVMsAreStillDeleted(t *testing.T) { + fastPoll(t) + api := scaleAPI() + var reads atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + if reads.Add(1) == 2 { // the first read after the update: workers not removed yet + p.Replicas, p.ReadyReplicas = 1, 3 + return + } + if reads.Load() > 2 { + p.Replicas, p.ReadyReplicas = 1, 1 + } + } + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "1", "--yes", "--wait")...) + if err != nil || !strings.Contains(out, "scaled from 3 to 1 worker (") || !strings.Contains(out, "The removed workers' VMs are still being deleted in the background.") { + t.Fatalf("out=%q err=%v", out, err) + } +} + +func TestNodePoolScale_WaitTimeout(t *testing.T) { + fastPoll(t) + api := scaleAPI() + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 3 } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "300ms")...) + if err == nil { + t.Fatal("a timeout must fail") + } + for _, want := range []string{"timed out after 300ms", "node pool workers in cluster prod (FIN-03)", "3/5 workers ready", "The backend keeps trying to create workers.", "To stop, scale back: `verda mk8s node-pool scale workers --cluster prod -l FIN-03 --replicas 3 --yes`"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("error misses %q: %v", want, err) + } + } + if strings.Contains(err.Error(), "node-pool delete") { + t.Errorf("a scale-up timeout must not advise deleting the pool: %v", err) + } + if r := decodeJSON[PoolScaleResult](t, out); r.Status != "accepted" || r.WaitError == "" { + t.Errorf("result = %+v", r) + } + + // For a person the advice is in the error only, not also on stderr before it. + api = scaleAPI() + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 3 } + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "300ms")...) + if err == nil || strings.Contains(errOut, "Check progress with") { + t.Errorf("err=%v stderr=%q", err, errOut) + } +} + +func TestNodePoolScale_WaitDownTimeoutDoesNotTalkAboutCreatingWorkers(t *testing.T) { + fastPoll(t) + api := scaleAPI() + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 3 } + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "1", "--yes", "--wait", "--wait-timeout", "300ms")...) + if err == nil { + t.Fatal("a timeout must fail") + } + if !strings.Contains(err.Error(), "scale-down of node pool workers") || !strings.Contains(err.Error(), "still removing workers") { + t.Errorf("err = %v", err) + } + for _, bad := range []string{"keeps trying to create", "keep billing", "node-pool delete"} { + if strings.Contains(err.Error(), bad) { + t.Errorf("a scale-down timeout must not say %q: %v", bad, err) + } + } +} + +func TestNodePoolScale_WaitUntilActiveAndTheNewCount(t *testing.T) { + fastPoll(t) + cases := map[string]struct { + want int + reads []verda.ManagedKubernetesNodePool // the waiting reads, in order + }{ + "to 0, a stale read still shows the workers": {want: 0, reads: []verda.ManagedKubernetesNodePool{ + {State: "active", Replicas: 0, ReadyReplicas: 3}, + {State: "active", Replicas: 0, ReadyReplicas: 0}, + }}, + "degraded at the new count is not done": {want: 5, reads: []verda.ManagedKubernetesNodePool{ + {State: "degraded", Replicas: 5, ReadyReplicas: 5}, + {State: "active", Replicas: 5, ReadyReplicas: 5}, + }}, + "ready equals the target but replicas is still the old count": {want: 5, reads: []verda.ManagedKubernetesNodePool{ + {State: "active", Replicas: 3, ReadyReplicas: 5}, + {State: "active", Replicas: 5, ReadyReplicas: 5}, + }}, + } + for name, tc := range cases { + api := scaleAPI() + var waited atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + if !patchSeen(api) { + return + } + i := min(int(waited.Add(1))-1, len(tc.reads)-1) + p.State, p.Replicas, p.ReadyReplicas = tc.reads[i].State, tc.reads[i].Replicas, tc.reads[i].ReadyReplicas + } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", strconv.Itoa(tc.want), "--yes", "--wait")...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + if r := decodeJSON[PoolScaleResult](t, out); r.Status != "completed" || int(waited.Load()) != len(tc.reads) { + t.Errorf("%s: status=%s waiting reads=%d, want completed after %d", name, r.Status, waited.Load(), len(tc.reads)) + } + } +} + +func TestNodePoolScale_NegativeReplicasIsAUsageErrorBeforeAnyConfirmation(t *testing.T) { + t.Parallel() + for name, args := range map[string][]string{ + "with --yes": scaleArgs("--replicas", "-1", "--yes"), + "without --yes": scaleArgs("--replicas", "-1"), + } { + api := scaleAPI() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), args...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Errorf("%s: err = %v, want a usage error, not CONFIRMATION_REQUIRED", name, err) + } + if len(api.requests) != 0 { + t.Errorf("%s: %d request(s) before the check", name, len(api.requests)) + } + } +} + +func TestNodePoolScale_WaitTimeoutMustBePositive(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, scaleAPI()), nil), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "0s")...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { + t.Errorf("err = %v", err) + } +} + +func TestNodePoolScale_APIErrorAndValidation(t *testing.T) { + t.Parallel() + api := scaleAPI() + api.failures["PATCH /mk8sclusters/{loc}/{name}/pools/{pool}"] = apiFailure{Status: 409, Body: `{"code":"conflict","message":"busy","details":"concurrent_modification"}`} + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5")...) + if err == nil || !strings.Contains(err.Error(), "concurrent_modification") { + t.Errorf("err = %v", err) + } + + if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "node-pool", "scale", "ghost", "--cluster", "prod", "--replicas", "2"); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown pool: %v", err) + } +} diff --git a/internal/verda-cli/cmd/mk8s/resolve.go b/internal/verda-cli/cmd/mk8s/resolve.go new file mode 100644 index 00000000..24876fa6 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/resolve.go @@ -0,0 +1,182 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "fmt" + "net/http" + "strings" + + "github.com/spf13/cobra" + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + "github.com/verda-cloud/verda-cli/pkg/tui" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +const ( + flagLocation = "location" + flagYes = "yes" + argCluster = "" + cancelLabel = "Cancel" + canceledMsg = "Canceled." +) + +// clusterTarget names an existing cluster: names are unique per project, but +// every API path also needs the location. +type clusterTarget struct { + Name string + Location string +} + +// addLocationFlag registers -l/--location for commands that act on a cluster. +func addLocationFlag(cmd *cobra.Command, location *string) { + cmd.Flags().StringVarP(location, flagLocation, "l", "", locationUsage) +} + +// checkClusterArg: off a terminal the cluster is required, and a blank one is +// never sent. given says whether the user passed it; label names it in the +// error ( or --cluster). Call it before f.VerdaClient() so bad input +// fails the same with or without credentials. +func checkClusterArg(f cmdutil.Factory, name string, given bool, label string) error { + if clusterArgMissing(f, name, given) { + return cmdutil.NewMissingFlagsError([]string{label}) + } + return nil +} + +// clusterArgMissing: the cluster was not given where it has to be, or was blank. +func clusterArgMissing(f cmdutil.Factory, name string, given bool) bool { + blank := given && strings.TrimSpace(name) == "" + return blank || (!given && !interactiveTTY(f)) +} + +// resolveCluster finds the cluster to act on: with a name and a location no +// call, with a name alone one List, with no name a picker. A nil target with a +// nil error means the user canceled or there is nothing to pick. Call +// checkClusterArg first. +func resolveCluster(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, name, location string) (*clusterTarget, error) { + name, location = strings.TrimSpace(name), strings.TrimSpace(location) + if name != "" && location != "" { + return &clusterTarget{Name: name, Location: location}, nil + } + clusters, err := listClusters(cmd.Context(), f, client) + if err != nil { + return nil, err + } + if name == "" { + return pickCluster(cmd.Context(), f, ioStreams, clusters, location) + } + return matchCluster(cmd, clusters, name) +} + +// argOrEmpty is the first positional argument, or "". +func argOrEmpty(args []string) string { + if len(args) == 0 { + return "" + } + return args[0] +} + +// describeCmd is the command that shows a cluster, for "check with" hints. +func describeCmd(name, location string) string { + return fmt.Sprintf("verda mk8s cluster describe %s -l %s", name, location) +} + +func getCluster(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget) (*verda.ManagedKubernetesCluster, error) { + return apiCall(ctx, f, "Loading cluster...", func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { + return client.ManagedKubernetes.Get(ctx, t.Location, t.Name) + }) +} + +// apiCall runs one API call bounded by --timeout, under a spinner, with its error mapped. +func apiCall[T any](ctx context.Context, f cmdutil.Factory, msg string, fn func(context.Context) (T, error)) (T, error) { + ctx, cancel := context.WithTimeout(ctx, f.Options().Timeout) + defer cancel() + v, err := cmdutil.WithSpinner(ctx, f.Status(), msg, fn) + return v, mapErr(err) +} + +func listClusters(ctx context.Context, f cmdutil.Factory, client *verda.Client) ([]verda.ManagedKubernetesCluster, error) { + return apiCall(ctx, f, "Loading clusters...", func(ctx context.Context) ([]verda.ManagedKubernetesCluster, error) { + return client.ManagedKubernetes.List(ctx) + }) +} + +// matchCluster finds name in the project's clusters. The errors are plain, not +// AgentErrors, which main prints as JSON even for a person: ClassifyError still +// gives NOT_FOUND and VALIDATION_ERROR. +func matchCluster(cmd *cobra.Command, clusters []verda.ManagedKubernetesCluster, name string) (*clusterTarget, error) { + var locations []string + for i := range clusters { + if clusters[i].Name == name { + locations = append(locations, clusters[i].Location) + } + } + switch len(locations) { + case 0: + return nil, &verda.APIError{StatusCode: http.StatusNotFound, Message: fmt.Sprintf("cluster %q not found", name)} + case 1: + return &clusterTarget{Name: name, Location: locations[0]}, nil + default: + return nil, cmdutil.UsageErrorf(cmd, "cluster %q exists in several locations (%s); choose one with -l", name, strings.Join(locations, ", ")) + } +} + +// pickCluster prompts for a cluster, limited to one location when given. +func pickCluster(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, clusters []verda.ManagedKubernetesCluster, location string) (*clusterTarget, error) { + var shown []verda.ManagedKubernetesCluster + for i := range clusters { + if location == "" || clusters[i].Location == location { + shown = append(shown, clusters[i]) + } + } + if len(shown) == 0 { + if location != "" { + _, _ = fmt.Fprintf(ioStreams.Out, "No clusters found in %s.\n", location) + return nil, nil + } + printNoClusters(ioStreams) + return nil, nil + } + labels := make([]string, 0, len(shown)+1) + for i := range shown { + labels = append(labels, fmt.Sprintf("%s (%s, %s)", shown[i].Name, shown[i].Location, shown[i].Phase)) + } + labels = append(labels, cancelLabel) + idx, err := f.Prompter().Select(ctx, "Select cluster", labels, pickerHints()...) + switch { + case err == nil && idx >= 0 && idx < len(shown): + return &clusterTarget{Name: shown[idx].Name, Location: shown[idx].Location}, nil + case err != nil && !cmdutil.IsPromptCancel(err): + return nil, err + } + // The Cancel row, Esc and Ctrl+C all end the command cleanly. + printCanceled(ioStreams) + return nil, nil +} + +// pickerHints shows the hint bar of a picker outside a wizard. Esc ends the +// command there, so the bar says cancel, not back. +func pickerHints() []tui.SelectOption { + return []tui.SelectOption{tui.WithShowHints(true), tui.WithSelectRelabel("esc", "esc cancel")} +} + +func printNoClusters(ioStreams cmdutil.IOStreams) { + _, _ = fmt.Fprintln(ioStreams.Out, "No clusters found.") + _, _ = fmt.Fprintln(ioStreams.ErrOut, "Create one with: verda mk8s cluster create") +} diff --git a/internal/verda-cli/cmd/mk8s/resolve_test.go b/internal/verda-cli/cmd/mk8s/resolve_test.go new file mode 100644 index 00000000..87e1afa2 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/resolve_test.go @@ -0,0 +1,210 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verda-cli/pkg/tui" + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestResolve_LocationGivenMakesNoListCall(t *testing.T) { + t.Parallel() + api := seededClusters() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod", "-l", "FIN-03"); err != nil { + t.Fatal(err) + } + if slices.Contains(api.paths("GET"), "/mk8sclusters") { + t.Errorf("-l must skip the List call: %v", api.paths("GET")) + } +} + +func TestResolve_NotFound(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "describe", "ghost") + if err == nil || agentErrorCode(err) != "NOT_FOUND" || !strings.Contains(err.Error(), "ghost") { + t.Fatalf("err = %v", err) + } +} + +func TestResolve_ErrorsArePlainSoMainPrintsThemAsText(t *testing.T) { + t.Parallel() + api := seededClusters() + api.addCluster("prod", "FIN-01", "running") + for name, args := range map[string][]string{ + "not found": {"cluster", "describe", "ghost"}, + "ambiguous": {"cluster", "describe", "prod"}, + } { + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...) + if err == nil { + t.Fatalf("%s: no error", name) + } + // main prints an AgentError as a JSON envelope even for a person. + if cmdutil.IsAgentError(err) { + t.Errorf("%s: %v is an AgentError, a person would get JSON", name, err) + } + } +} + +func TestResolve_TrimsNameAndLocation(t *testing.T) { + t.Parallel() + api := seededClusters() + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", " prod ", "-l", " FIN-03 "); err != nil { + t.Fatal(err) + } + if got := api.paths("GET"); got[0] != "/mk8sclusters/FIN-03/prod" { + t.Errorf("requests = %v", got) + } +} + +func TestResolve_ExplicitLocationWrongIsAPI404(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "describe", "prod", "-l", "FIN-01") + if err == nil || agentErrorCode(err) != "NOT_FOUND" { + t.Fatalf("err = %v", err) + } +} + +func TestResolve_NameInTwoLocationsAsksForLocation(t *testing.T) { + t.Parallel() + api := seededClusters() + api.addCluster("prod", "FIN-01", "running") + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod") + if err == nil || agentErrorCode(err) != "VALIDATION_ERROR" { + t.Fatalf("err = %v", err) + } + for _, want := range []string{"FIN-01", "FIN-03", "-l"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("error misses %q: %v", want, err) + } + } +} + +func TestResolve_MissingClusterOffTerminal(t *testing.T) { + t.Parallel() + for name, f := range map[string]*cmdutil.TestFactory{ + "table": tableFactory(newFakeClient(t, seededClusters()), nil), + "agent": agentFactory(newFakeClient(t, seededClusters())), + } { + for _, args := range [][]string{{"cluster", "describe"}, {"cluster", "describe", " "}} { + _, _, err := runMK8s(t, f, args...) + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Errorf("%s %v: err = %v", name, args, err) + } + } + } +} + +func TestResolve_Picker(t *testing.T) { + withTerminal(t) + + t.Run("picks the second cluster", func(t *testing.T) { + api := seededClusters() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddSelect(1)), "cluster", "describe") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, "staging") || strings.Contains(out, "prod.example") { + t.Errorf("describe output = %s", out) + } + if got := api.paths("GET"); !slices.Contains(got, "/mk8sclusters/FIN-01/staging") { + t.Errorf("requests = %v", got) + } + }) + + t.Run("limited to -l", func(t *testing.T) { + var labels []string + p := &recordingPrompter{Prompter: tuitest.New().AddSelect(0), labels: &labels} + f := tableFactory(newFakeClient(t, seededClusters()), nil) + f.PrompterOverride = p + if _, _, err := runMK8s(t, f, "cluster", "describe", "-l", "FIN-01"); err != nil { + t.Fatal(err) + } + if len(labels) != 2 || !strings.HasPrefix(labels[0], "staging") || labels[1] != cancelLabel { + t.Errorf("labels = %v, want staging and Cancel only", labels) + } + }) + + t.Run("cancel row, Esc and Ctrl+C end cleanly", func(t *testing.T) { + for name, p := range map[string]*cancelPrompter{ + "cancel row": {Prompter: tuitest.New().AddSelect(2)}, + "esc": {Prompter: tuitest.New(), selectErr: context.Canceled}, + "ctrl+c": {Prompter: tuitest.New(), selectErr: tui.ErrInterrupted}, + } { + api := seededClusters() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = p + out, errOut, err := runMK8s(t, f, "cluster", "describe") + if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { + t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) + } + if len(api.paths("GET")) != 1 { + t.Errorf("%s: only the List call may happen, got %v", name, api.paths("GET")) + } + } + }) +} + +func TestResolve_PickerEdgeCases(t *testing.T) { + withTerminal(t) + + t.Run("a picker error that is not a cancel is returned", func(t *testing.T) { + f := tableFactory(newFakeClient(t, seededClusters()), nil) + f.PrompterOverride = &cancelPrompter{Prompter: tuitest.New(), selectErr: errors.New("terminal broke")} + if _, _, err := runMK8s(t, f, "cluster", "describe"); err == nil || !strings.Contains(err.Error(), "terminal broke") { + t.Errorf("err = %v", err) + } + }) + + t.Run("no cluster in the chosen location", func(t *testing.T) { + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), tuitest.New()), "cluster", "describe", "-l", "FIN-99") + if err != nil || !strings.Contains(out, "No clusters found in FIN-99.") { + t.Errorf("out=%q err=%v", out, err) + } + }) + + t.Run("no clusters", func(t *testing.T) { + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, newFakeAPI()), tuitest.New()), "cluster", "describe") + if err != nil || !strings.Contains(out, "No clusters found.") || !strings.Contains(errOut, "cluster create") { + t.Errorf("out=%q err=%q error=%v", out, errOut, err) + } + }) + + t.Run("not offered in agent mode", func(t *testing.T) { + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, seededClusters())), "cluster", "describe") + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Errorf("err = %v", err) + } + }) +} + +// recordingPrompter records the labels of every Select. +type recordingPrompter struct { + *tuitest.Prompter + labels *[]string +} + +func (p *recordingPrompter) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { + *p.labels = append(*p.labels, labels...) + return p.Prompter.Select(ctx, msg, labels, opts...) +} diff --git a/internal/verda-cli/cmd/mk8s/testhelpers_test.go b/internal/verda-cli/cmd/mk8s/testhelpers_test.go new file mode 100644 index 00000000..aca2da07 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/testhelpers_test.go @@ -0,0 +1,187 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "encoding/json" + "os" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + "github.com/verda-cloud/verda-cli/pkg/tui" + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// TestMain makes every test start off a terminal, whatever runs the tests; withTerminal opts in. +func TestMain(m *testing.M) { + isInteractiveTerminal = func() bool { return false } + os.Exit(m.Run()) +} + +// addCluster seeds a cluster; a running one has an endpoint, like the API. +func (a *fakeAPI) addCluster(name, location, phase string) { + c := verda.ManagedKubernetesCluster{ + Name: name, + KubernetesVersion: "1.37.0", + Location: location, + NetworkID: "pn-1", + SubnetID: "sn-1", + PodCIDR: "10.100.0.0/16", + ServiceCIDR: "10.200.0.0/16", + Phase: phase, + CreatedAt: testCreated, + } + if phase == verda.ManagedKubernetesClusterPhaseRunning { + c.ExternalEndpoint = "https://" + name + ".example.test:6443" + } + a.clusters = append(a.clusters, c) +} + +func (a *fakeAPI) addPool(cluster, name, state string, replicas, ready int) { + a.pools[cluster] = append(a.pools[cluster], verda.ManagedKubernetesNodePool{ + Name: name, + Replicas: replicas, + ReadyReplicas: ready, + InstanceType: "1V100.6V", + SSHKeyIDs: []string{"key-1"}, + State: state, + CreatedAt: testCreated, + }) +} + +// paths returns the path of every recorded request with this method, in order. +func (a *fakeAPI) paths(method string) []string { + reqs := a.requestsMatching(method) + out := make([]string, 0, len(reqs)) + for _, r := range reqs { + out = append(out, r.Path) + } + return out +} + +// cancelPrompter answers Select or Confirm with an error (Esc, Ctrl+C) when set. +type cancelPrompter struct { + *tuitest.Prompter + selectErr error + confirmErr error +} + +func (p *cancelPrompter) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { + if p.selectErr != nil { + return -1, p.selectErr + } + return p.Prompter.Select(ctx, msg, labels, opts...) +} + +func (p *cancelPrompter) Confirm(ctx context.Context, msg string, opts ...tui.ConfirmOption) (bool, error) { + if p.confirmErr != nil { + return false, p.confirmErr + } + return p.Prompter.Confirm(ctx, msg, opts...) +} + +// agentErrorCode is the code main reports, so an API 404 counts as NOT_FOUND. +func agentErrorCode(err error) string { + if ae := cmdutil.ClassifyError(err); ae != nil { + return ae.Code + } + return "" +} + +// isUsageError: VALIDATION_ERROR for agents, and not an AgentError, which main prints as JSON for people too. +func isUsageError(err error) bool { + return agentErrorCode(err) == "VALIDATION_ERROR" && !cmdutil.IsAgentError(err) +} + +func decodeJSON[T any](t *testing.T, s string) T { + t.Helper() + var v T + if err := json.Unmarshal([]byte(s), &v); err != nil { + t.Fatalf("decode %q: %v", s, err) + } + return v +} + +func yamlFactory(client *verda.Client) *cmdutil.TestFactory { + return &cmdutil.TestFactory{ClientOverride: client, OutputFormatOverride: "yaml"} +} + +func conditionReady() verda.ManagedKubernetesCondition { + return verda.ManagedKubernetesCondition{Type: "Ready", Status: "True", Reason: "AllGood", Message: "all components ready", LastTransitionTime: testCreated} +} + +func contextCanceled() error { return context.Canceled } + +func errInterrupted() error { return tui.ErrInterrupted } + +func (a *fakeAPI) addNetwork(id, name string) { + a.networks = append(a.networks, verda.PrivateNetwork{ID: id, Name: name, Mode: verda.NetworkModeCustom, CreatedAt: testCreated}) +} + +func (a *fakeAPI) addSubnet(networkID, id, name, location, cidr string) { + a.subnets[networkID] = append(a.subnets[networkID], verda.Subnet{ + ID: id, Name: name, CIDR: cidr, Location: verda.Location{Code: location}, CreatedAt: testCreated, + }) +} + +func confirmYes() *tuitest.Prompter { return tuitest.New().AddConfirm(true) } + +func confirmNo() *tuitest.Prompter { return tuitest.New().AddConfirm(false) } + +// textCancelPrompter answers TextInput with Esc. +type textCancelPrompter struct{ *tuitest.Prompter } + +func (p *textCancelPrompter) TextInput(context.Context, string, ...tui.TextInputOption) (string, error) { + return "", context.Canceled +} + +// pickerAnswers queues Select answers (indices) for a picker. +func pickerAnswers(indices ...int) *tuitest.Prompter { + p := tuitest.New() + for _, i := range indices { + p.AddSelect(i) + } + return p +} + +// poolWizardAPI has two clusters, instance types per location and two SSH keys. +func poolWizardAPI() *fakeAPI { + api := seededClusters() + api.availability["FIN-03"] = []string{"2A100.80S", "1V100.6V", "CPU.4V.16G"} + api.availability["FIN-01"] = []string{"1A100.22V"} + api.sshKeys = []verda.SSHKey{ + {ID: "key-1", Name: "laptop", CreatedAt: testCreated}, + {ID: "key-2", Name: "ci", CreatedAt: testCreated}, + } + return api +} + +func postBodyAny(t *testing.T, api *fakeAPI) map[string]any { + t.Helper() + reqs := api.requestsMatching("POST") + if len(reqs) != 1 { + t.Fatalf("want one POST, got %v", reqs) + } + return decodeJSON[map[string]any](t, reqs[0].Body) +} + +// addKubeconfig seeds a kubeconfig of a cluster. +func (a *fakeAPI) addKubeconfig(cluster string, k verda.ManagedKubernetesKubeconfig) { + a.kubeconfigs[cluster] = append(a.kubeconfigs[cluster], k) +} diff --git a/internal/verda-cli/cmd/mk8s/views.go b/internal/verda-cli/cmd/mk8s/views.go new file mode 100644 index 00000000..e6548453 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/views.go @@ -0,0 +1,240 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "fmt" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" +) + +// KubeconfigView is the JSON/YAML (and --debug) shape of a kubeconfig. It has +// no field for the YAML, which holds a client private key: never marshal the +// SDK struct instead. +// +// Every key is always present, so scripts see one shape (a pending kubeconfig +// has not_after null, one without groups has []); only file is conditional. +type KubeconfigView struct { + ID string `json:"id" yaml:"id"` + DisplayName string `json:"display_name" yaml:"display_name"` + CommonName string `json:"common_name" yaml:"common_name"` + Groups []string `json:"groups" yaml:"groups"` + Status string `json:"status" yaml:"status"` + NotAfter *time.Time `json:"not_after" yaml:"not_after"` + File string `json:"file,omitempty" yaml:"file,omitempty"` +} + +// newKubeconfigView copies the metadata only; file is the path written, if any. +func newKubeconfigView(k *verda.ManagedKubernetesKubeconfig, file string) KubeconfigView { + groups := k.Groups + if groups == nil { + groups = []string{} + } + return KubeconfigView{ + ID: k.ID, + DisplayName: k.DisplayName, + CommonName: k.CommonName, + Groups: groups, + Status: k.Status, + NotAfter: k.NotAfter, + File: file, + } +} + +// ConditionView is one cluster condition. +type ConditionView struct { + Type string `json:"type" yaml:"type"` + Status string `json:"status" yaml:"status"` + Reason string `json:"reason" yaml:"reason"` + Message string `json:"message" yaml:"message"` + LastTransitionTime time.Time `json:"last_transition_time" yaml:"last_transition_time"` +} + +// ClusterView is the JSON/YAML shape of a cluster; the keys follow the API. +type ClusterView struct { + Name string `json:"name" yaml:"name"` + Location string `json:"location" yaml:"location"` + KubernetesVersion string `json:"kubernetes_version" yaml:"kubernetes_version"` + Phase string `json:"phase" yaml:"phase"` + ExternalEndpoint string `json:"external_endpoint" yaml:"external_endpoint"` + NetworkID string `json:"network" yaml:"network"` + SubnetID string `json:"subnet" yaml:"subnet"` + PodCIDR string `json:"pod_cidr" yaml:"pod_cidr"` + ServiceCIDR string `json:"service_cidr" yaml:"service_cidr"` + DNSSuffix string `json:"dns_suffix" yaml:"dns_suffix"` + Conditions []ConditionView `json:"conditions" yaml:"conditions"` + CreatedAt time.Time `json:"created_at" yaml:"created_at"` +} + +func newClusterView(c *verda.ManagedKubernetesCluster) ClusterView { + conds := make([]ConditionView, 0, len(c.Conditions)) + for i := range c.Conditions { + cond := &c.Conditions[i] + conds = append(conds, ConditionView{ + Type: cond.Type, + Status: cond.Status, + Reason: cond.Reason, + Message: cond.Message, + LastTransitionTime: cond.LastTransitionTime, + }) + } + return ClusterView{ + Name: c.Name, + Location: c.Location, + KubernetesVersion: c.KubernetesVersion, + Phase: c.Phase, + ExternalEndpoint: c.ExternalEndpoint, + NetworkID: c.NetworkID, + SubnetID: c.SubnetID, + PodCIDR: c.PodCIDR, + ServiceCIDR: c.ServiceCIDR, + DNSSuffix: c.DNSSuffix, + Conditions: conds, + CreatedAt: c.CreatedAt, + } +} + +func newClusterViews(clusters []verda.ManagedKubernetesCluster) []ClusterView { + views := make([]ClusterView, 0, len(clusters)) + for i := range clusters { + views = append(views, newClusterView(&clusters[i])) + } + return views +} + +// PoolSummaryView is a node pool as `cluster describe` lists it. +type PoolSummaryView struct { + Name string `json:"name" yaml:"name"` + InstanceType string `json:"instance_type" yaml:"instance_type"` + ReadyReplicas int `json:"ready_replicas" yaml:"ready_replicas"` + Replicas int `json:"replicas" yaml:"replicas"` + State string `json:"state" yaml:"state"` +} + +func newPoolSummaryViews(pools []verda.ManagedKubernetesNodePool) []PoolSummaryView { + views := make([]PoolSummaryView, 0, len(pools)) + for i := range pools { + p := &pools[i] + views = append(views, PoolSummaryView{ + Name: p.Name, + InstanceType: p.InstanceType, + ReadyReplicas: p.ReadyReplicas, + Replicas: p.Replicas, + State: p.State, + }) + } + return views +} + +// ClusterDescribeView is a cluster with its node pools. node_pools is null +// (and node_pools_error set) when the pool call failed, [] when there are none. +// It is a pointer so YAML, which writes a nil slice as [], also says null. +type ClusterDescribeView struct { + ClusterView `yaml:",inline"` + NodePools *[]PoolSummaryView `json:"node_pools" yaml:"node_pools"` + NodePoolsError string `json:"node_pools_error,omitempty" yaml:"node_pools_error,omitempty"` +} + +// ageColumn renders the time since t the way kubectl does: 45s, 12m, 3h, 2d. +func ageColumn(t, now time.Time) string { + if t.IsZero() { + return "-" + } + d := now.Sub(t) + switch { + case d < time.Minute: + return fmt.Sprintf("%ds", max(int(d.Seconds()), 0)) + case d < time.Hour: + return fmt.Sprintf("%dm", int(d.Minutes())) + case d < 24*time.Hour: + return fmt.Sprintf("%dh", int(d.Hours())) + default: + return fmt.Sprintf("%dd", int(d.Hours()/24)) + } +} + +// ActionResult is the structured output of a command that only accepts a +// request, such as a delete. Cluster is set for resources that belong to one. +type ActionResult struct { + Resource string `json:"resource" yaml:"resource"` + ID string `json:"id,omitempty" yaml:"id,omitempty"` + Name string `json:"name" yaml:"name"` + Location string `json:"location" yaml:"location"` + Cluster string `json:"cluster,omitempty" yaml:"cluster,omitempty"` + Action string `json:"action" yaml:"action"` + Status string `json:"status" yaml:"status"` +} + +// utcTime renders a time as "2006-01-02 15:04 UTC", the form of every date in mk8s output. +func utcTime(t time.Time) string { + return t.UTC().Format(timeLayout) + " UTC" +} + +// timeColumn renders a time for a table, "-" when it is not set. +func timeColumn(t time.Time) string { + if t.IsZero() { + return "-" + } + return utcTime(t) +} + +// PoolErrorView is one reason a node pool is not healthy. +type PoolErrorView struct { + Reason string `json:"reason" yaml:"reason"` + Message string `json:"message" yaml:"message"` + Since time.Time `json:"since" yaml:"since"` +} + +// PoolView is the JSON/YAML shape of a node pool; the keys follow the API. +type PoolView struct { + Name string `json:"name" yaml:"name"` + InstanceType string `json:"instance_type" yaml:"instance_type"` + Replicas int `json:"replicas" yaml:"replicas"` + ReadyReplicas int `json:"ready_replicas" yaml:"ready_replicas"` + State string `json:"state" yaml:"state"` + SSHKeyIDs []string `json:"ssh_key_ids" yaml:"ssh_key_ids"` + Errors []PoolErrorView `json:"errors" yaml:"errors"` + CreatedAt time.Time `json:"created_at" yaml:"created_at"` +} + +func newPoolView(p *verda.ManagedKubernetesNodePool) PoolView { + errs := make([]PoolErrorView, 0, len(p.Errors)) + for i := range p.Errors { + errs = append(errs, PoolErrorView{Reason: p.Errors[i].Reason, Message: p.Errors[i].Message, Since: p.Errors[i].Since}) + } + keys := p.SSHKeyIDs + if keys == nil { + keys = []string{} + } + return PoolView{ + Name: p.Name, + InstanceType: p.InstanceType, + Replicas: p.Replicas, + ReadyReplicas: p.ReadyReplicas, + State: p.State, + SSHKeyIDs: keys, + Errors: errs, + CreatedAt: p.CreatedAt, + } +} + +func newPoolViews(pools []verda.ManagedKubernetesNodePool) []PoolView { + views := make([]PoolView, 0, len(pools)) + for i := range pools { + views = append(views, newPoolView(&pools[i])) + } + return views +} diff --git a/internal/verda-cli/cmd/mk8s/wait.go b/internal/verda-cli/cmd/mk8s/wait.go new file mode 100644 index 00000000..cd8ba4e8 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wait.go @@ -0,0 +1,257 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "slices" + "strings" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// maxReadFailures is how many reads in a row may fail before a wait gives up. +// One 502 from a load balancer must not fail a 15-minute wait. +const maxReadFailures = 3 + +// pollInterval is the time between reads while --wait runs; a var so tests can shorten it. +var pollInterval = 5 * time.Second + +var errWaitTimeout = errors.New("timed out waiting") + +// pollReadError marks reads that kept failing, so the failure is not mistaken for a timeout. +type pollReadError struct{ err error } + +func (e *pollReadError) Error() string { return e.err.Error() } +func (e *pollReadError) Unwrap() error { return e.err } + +// fatalRead: a read that retrying cannot fix (bad request, no access). A 404 +// is not fatal: right after a create the read can lag behind. +func fatalRead(err error) bool { + apiErr, ok := errors.AsType[*verda.APIError](err) + if !ok || apiErr.StatusCode < http.StatusBadRequest || apiErr.StatusCode >= http.StatusInternalServerError { + return false + } + switch apiErr.StatusCode { + case http.StatusNotFound, http.StatusRequestTimeout, http.StatusTooManyRequests: + return false + } + return true +} + +// waitFor polls get until check reports done. Each read has its own --timeout; +// the whole wait ends after timeout. It returns the last resource read and one +// of: nil (done), a *pollReadError (reads kept failing), or errWaitTimeout. +func waitFor[T any]( + ctx context.Context, f cmdutil.Factory, w io.Writer, timeout time.Duration, + get func(context.Context) (*T, error), check func(*T) (status string, done bool), +) (*T, error) { + var last *T + failures := 0 + status := "waiting" + pollFn := func(ctx context.Context) (string, bool, error) { + readCtx, cancel := context.WithTimeout(ctx, f.Options().Timeout) + defer cancel() + v, err := get(readCtx) + if err != nil { + // The whole wait ran out during this read: that is a timeout, not a failed read. + if ctx.Err() != nil { + return status, false, nil + } + // Until a first read works, a 404 is a read lagging behind the create: wait it out. + if apiErr, ok := errors.AsType[*verda.APIError](err); ok && apiErr.StatusCode == http.StatusNotFound && last == nil { + return status, false, nil + } + failures++ + if fatalRead(err) || failures >= maxReadFailures { + return "", false, &pollReadError{err: mapErr(err)} + } + return status, false, nil + } + failures = 0 + last = v + var done bool + status, done = check(v) + return status, done, nil + } + _, err := cmdutil.Poll(ctx, w, pollInterval, cmdutil.WaitOptions{Wait: true, Timeout: timeout}, pollFn) + if readErr, ok := errors.AsType[*pollReadError](err); ok { + return last, readErr + } + if err != nil { + return last, errWaitTimeout + } + return last, nil +} + +// waitWriter is where the wait's spinner line goes: stderr, and only for a +// human at a terminal, so stdout and structured output stay clean. +func waitWriter(f cmdutil.Factory, ioStreams cmdutil.IOStreams) io.Writer { + if f.OutputFormat() == outputTable && !f.AgentMode() && cmdutil.IsStderrTerminal() { + return ioStreams.ErrOut + } + return nil +} + +// clusterReady: the cluster is usable once it is running and has an endpoint. +func clusterReady(c *verda.ManagedKubernetesCluster) (status string, done bool) { + return c.Phase, c.Phase == verda.ManagedKubernetesClusterPhaseRunning && c.ExternalEndpoint != "" +} + +// waitFailureInfo describes what a failed wait was waiting for. +type waitFailureInfo struct { + Subject string // for example "cluster prod in FIN-03" + Timeout time.Duration + LastSeen string // what the last read showed + TimeoutTail string // what the user should know or do after a timeout + ReadNote string // optional: what the user should know when the reads kept failing + CheckCmd string +} + +// waitFailure turns the error of waitFor into the message the command returns. +func waitFailure(err error, w *waitFailureInfo) error { + if readErr, ok := errors.AsType[*pollReadError](err); ok { + note := "" + if w.ReadNote != "" { + note = ". " + w.ReadNote + } + return fmt.Errorf("%s was accepted, but --wait could not read it (%w); check with `%s`%s", w.Subject, readErr.err, w.CheckCmd, note) + } + return fmt.Errorf("timed out after %s waiting for %s (%s). %s", w.Timeout, w.Subject, w.LastSeen, w.TimeoutTail) +} + +// poolReasons lists the distinct error reasons of a pool, in order. +func poolReasons(p *verda.ManagedKubernetesNodePool) []string { + var reasons []string + for i := range p.Errors { + if !slices.Contains(reasons, p.Errors[i].Reason) { + reasons = append(reasons, p.Errors[i].Reason) + } + } + return reasons +} + +// poolStatus is the one-line state shown while waiting: the state, the error +// reasons while the pool is degraded, and ready/desired workers. +func poolStatus(p *verda.ManagedKubernetesNodePool) string { + s := p.State + if reasons := poolReasons(p); p.State == verda.ManagedKubernetesNodePoolStateDegraded && len(reasons) > 0 { + s += " (" + strings.Join(reasons, ", ") + ")" + } + return fmt.Sprintf("%s, %d/%d workers ready", s, p.ReadyReplicas, p.Replicas) +} + +// poolCreated is done when the pool is active with the requested number of +// ready workers. Comparing with the requested count, not the count read, keeps +// a stale first read from ending the wait. +func poolCreated(want int) func(*verda.ManagedKubernetesNodePool) (string, bool) { + return func(p *verda.ManagedKubernetesNodePool) (string, bool) { + return poolStatus(p), p.State == verda.ManagedKubernetesNodePoolStateActive && p.ReadyReplicas == want + } +} + +// poolScaled is done when the API reports the new count, all of it ready, and the pool active. +func poolScaled(want int) func(*verda.ManagedKubernetesNodePool) (string, bool) { + return func(p *verda.ManagedKubernetesNodePool) (string, bool) { + done := p.State == verda.ManagedKubernetesNodePoolStateActive && p.Replicas == want && p.ReadyReplicas == want + return poolStatus(p), done + } +} + +// writeResult writes a structured result and returns waitErr, so a failed wait +// still leaves the result on stdout. It reports false for table output. +func writeResult(f cmdutil.Factory, ioStreams cmdutil.IOStreams, result any, waitErr error) (written bool, err error) { + wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result) + if !wrote { + return false, nil + } + if err != nil { + return true, err + } + return true, waitErr +} + +// waitForPool polls the pool until done. It returns the last pool read. +func waitForPool(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, name string, done func(*verda.ManagedKubernetesNodePool) (string, bool), timeout time.Duration) (*verda.ManagedKubernetesNodePool, error) { + if f.OutputFormat() == outputTable { + _, _ = fmt.Fprintf(ioStreams.ErrOut, "Node pool %s accepted; waiting for its workers (Ctrl+C stops waiting, the pool stays).\n", name) + } + return waitFor(ctx, f, waitWriter(f, ioStreams), timeout, + func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { + return client.ManagedKubernetes.GetNodePool(ctx, t.Location, t.Name, name) + }, done) +} + +// poolWaitKind says which change a pool wait was for: its timeout text differs. +type poolWaitKind int + +const ( + poolWaitCreate poolWaitKind = iota + poolWaitScaleUp + poolWaitScaleDown + poolWaitSettle +) + +// poolWaitFailure is the error of a pool wait that did not finish. When workers +// are being created, a timeout also says they keep billing and how to stop +// them: delete the pool after a create, scale back to previous after a scale-up. +func poolWaitFailure(err error, kind poolWaitKind, t *clusterTarget, accepted, last *verda.ManagedKubernetesNodePool, timeout time.Duration, previous int) error { + seen := accepted + if last != nil { + seen = last + } + describe := poolDescribeCmd(t, accepted.Name) + subject := fmt.Sprintf("node pool %s in cluster %s (%s)", accepted.Name, t.Name, t.Location) + creating := poolBackendKeepsTrying + " To stop: `" + poolDeleteCmd(t, accepted.Name) + "`. Check with `" + describe + "`" + info := waitFailureInfo{Subject: subject, Timeout: timeout, LastSeen: poolSeen(seen), CheckCmd: describe} + switch kind { + case poolWaitScaleDown: + info.Subject = "scale-down of " + subject + info.TimeoutTail = "The API is still removing workers. Check with `" + describe + "`" + case poolWaitSettle: + info.Subject = "scaling of " + subject + info.TimeoutTail = poolBackendKeepsTrying + " Check with `" + describe + "`" + info.ReadNote = poolBackendKeepsTrying + case poolWaitScaleUp: + info.Subject = "scaling of " + subject + info.TimeoutTail = poolBackendKeepsTrying + " To stop, scale back: `" + poolScaleCmd(t, accepted.Name, previous) + " --yes`. Check with `" + describe + "`" + info.ReadNote = poolBackendKeepsTrying + default: + info.TimeoutTail, info.ReadNote = creating, poolBackendKeepsTrying + } + return waitFailure(err, &info) +} + +// poolSeen is what a timeout message says about the last read: state, ready/desired, error reasons. +func poolSeen(p *verda.ManagedKubernetesNodePool) string { + s := fmt.Sprintf("last state: %s, %d/%d workers ready", p.State, p.ReadyReplicas, p.Replicas) + if reasons := poolReasons(p); len(reasons) > 0 { + s += ", errors: " + strings.Join(reasons, ", ") + } + return s +} + +// kubeconfigReady: the YAML exists only once the kubeconfig is ready. Both are +// checked, so a read that says ready without the YAML does not end the wait. +func kubeconfigReady(k *verda.ManagedKubernetesKubeconfig) (status string, done bool) { + return k.Status, k.Status == verda.ManagedKubernetesKubeconfigStatusReady && k.Kubeconfig != "" +} diff --git a/internal/verda-cli/cmd/mk8s/wait_test.go b/internal/verda-cli/cmd/mk8s/wait_test.go new file mode 100644 index 00000000..207fb3fa --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wait_test.go @@ -0,0 +1,199 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "net/http" + "strings" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// scriptedGet answers each read from a list: an error, or a value. +type step struct { + err error + val int +} + +func scriptedGet(steps []step) func(context.Context) (*int, error) { + i := 0 + return func(context.Context) (*int, error) { + s := steps[min(i, len(steps)-1)] + i++ + if s.err != nil { + return nil, s.err + } + v := s.val + return &v, nil + } +} + +func apiErrStatus(code int) error { return &verda.APIError{StatusCode: code, Message: "x"} } + +func wait(t *testing.T, steps []step, timeout time.Duration) (*int, error) { + t.Helper() + fastPoll(t) + return waitFor(t.Context(), &cmdutil.TestFactory{}, nil, timeout, scriptedGet(steps), + func(v *int) (string, bool) { return "reading", *v >= 3 }) +} + +func TestWaitFor_FailuresMustBeConsecutive(t *testing.T) { + // fail, ok, fail, ok, fail, ok: never maxReadFailures in a row, so the wait goes on to done. + steps := []step{{err: apiErrStatus(502)}, {val: 1}, {err: apiErrStatus(502)}, {val: 2}, {err: apiErrStatus(502)}, {val: 3}} + last, err := wait(t, steps, 5*time.Second) + if err != nil || last == nil || *last != 3 { + t.Fatalf("last=%v err=%v", last, err) + } +} + +func TestWaitFor_GivesUpAfterConsecutiveFailures(t *testing.T) { + steps := []step{{val: 1}, {err: apiErrStatus(500)}, {err: apiErrStatus(500)}, {err: apiErrStatus(500)}, {val: 3}} + last, err := wait(t, steps, 5*time.Second) + if _, ok := errors.AsType[*pollReadError](err); !ok { + t.Fatalf("err = %v, want a pollReadError", err) + } + if last == nil || *last != 1 { + t.Errorf("last = %v, want the last good read", last) + } +} + +func TestWaitFor_RetriableAndFatalStatuses(t *testing.T) { + for _, code := range []int{http.StatusRequestTimeout, http.StatusTooManyRequests, http.StatusNotFound, http.StatusBadGateway} { + // one failure then done: retriable statuses are tolerated + if _, err := wait(t, []step{{val: 1}, {err: apiErrStatus(code)}, {val: 3}}, 5*time.Second); err != nil { + t.Errorf("status %d must be retried: %v", code, err) + } + } + for _, code := range []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden} { + if _, err := wait(t, []step{{val: 1}, {err: apiErrStatus(code)}, {val: 3}}, 5*time.Second); err == nil { + t.Errorf("status %d must end the wait at once", code) + } + } +} + +func TestWaitFor_A404BeforeTheFirstGoodReadIsWaitedOut(t *testing.T) { + steps := []step{{err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {val: 3}} + if _, err := wait(t, steps, 5*time.Second); err != nil { + t.Errorf("a lagging read after a create must not fail the wait: %v", err) + } + // After a good read, the same 404s are failures. + steps = []step{{val: 1}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {val: 3}} + if _, err := wait(t, steps, 5*time.Second); err == nil { + t.Error("404s after a good read count as failures") + } +} + +func TestWaitFor_TimeoutWhileAReadIsInFlight(t *testing.T) { + fastPoll(t) + hang := func(ctx context.Context) (*int, error) { + <-ctx.Done() + return nil, ctx.Err() + } + _, err := waitFor(t.Context(), &cmdutil.TestFactory{}, nil, 50*time.Millisecond, hang, func(*int) (string, bool) { return "", false }) + if !errors.Is(err, errWaitTimeout) { + t.Errorf("err = %v, want errWaitTimeout, not a read failure", err) + } +} + +func TestWaitFailure_Messages(t *testing.T) { + t.Parallel() + info := waitFailureInfo{Subject: "thing x", Timeout: time.Minute, LastSeen: "last: y", TimeoutTail: "Tail.", ReadNote: "Note.", CheckCmd: "check it"} + read := waitFailure(&pollReadError{err: errors.New("boom")}, &info).Error() + for _, want := range []string{"thing x was accepted, but --wait could not read it (boom)", "check with `check it`", "Note."} { + if !strings.Contains(read, want) { + t.Errorf("read failure %q misses %q", read, want) + } + } + if got := waitFailure(errWaitTimeout, &info).Error(); got != "timed out after 1m0s waiting for thing x (last: y). Tail." { + t.Errorf("timeout = %q", got) + } + info.ReadNote = "" + if read := waitFailure(&pollReadError{err: errors.New("boom")}, &info).Error(); strings.Contains(read, ". ") && strings.Contains(read, "Note") { + t.Errorf("no ReadNote, no note: %q", read) + } +} + +func TestPoolConditions(t *testing.T) { + t.Parallel() + pool := func(state string, replicas, ready int) *verda.ManagedKubernetesNodePool { + return &verda.ManagedKubernetesNodePool{State: state, Replicas: replicas, ReadyReplicas: ready} + } + type c struct { + name string + p *verda.ManagedKubernetesNodePool + want int + done bool + } + for _, tc := range []c{ + {"active and ready", pool("active", 3, 3), 3, true}, + {"creating at the count", pool("creating", 3, 3), 3, false}, + {"degraded at the count", pool("degraded", 3, 3), 3, false}, + {"active, short of the count", pool("active", 3, 2), 3, false}, + {"zero workers, active", pool("active", 0, 0), 0, true}, + {"zero workers, still creating", pool("creating", 0, 0), 0, false}, + } { + if _, got := poolCreated(tc.want)(tc.p); got != tc.done { + t.Errorf("poolCreated %s = %v, want %v", tc.name, got, tc.done) + } + } + for _, tc := range []c{ + {"new count, all ready", pool("active", 5, 5), 5, true}, + {"old count, all ready", pool("active", 3, 3), 5, false}, + {"ready at target, replicas not", pool("active", 3, 5), 5, false}, + {"new count, degraded", pool("degraded", 5, 5), 5, false}, + {"to zero, stale workers", pool("active", 0, 3), 0, false}, + {"to zero, done", pool("active", 0, 0), 0, true}, + } { + if _, got := poolScaled(tc.want)(tc.p); got != tc.done { + t.Errorf("poolScaled %s = %v, want %v", tc.name, got, tc.done) + } + } + if s, _ := poolCreated(3)(&verda.ManagedKubernetesNodePool{State: "degraded", Replicas: 3, ReadyReplicas: 1, Errors: []verda.ManagedKubernetesNodePoolError{{Reason: "insufficient_capacity"}, {Reason: "insufficient_capacity"}, {Reason: "cloud_api_error"}}}); s != "degraded (insufficient_capacity, cloud_api_error), 1/3 workers ready" { + t.Errorf("status = %q", s) + } + if s, done := clusterReady(&verda.ManagedKubernetesCluster{Phase: "running"}); done || s != "running" { + t.Errorf("running without an endpoint: %q %v", s, done) + } + if _, done := clusterReady(&verda.ManagedKubernetesCluster{Phase: "running", ExternalEndpoint: "https://x"}); !done { + t.Error("running with an endpoint is ready") + } + if _, done := clusterReady(&verda.ManagedKubernetesCluster{Phase: "degraded", ExternalEndpoint: "https://x"}); done { + t.Error("degraded is not ready") + } +} + +func TestKubeconfigReady(t *testing.T) { + t.Parallel() + for name, tc := range map[string]struct { + status, yaml string + done bool + }{ + "pending without YAML": {verda.ManagedKubernetesKubeconfigStatusPending, "", false}, + "pending with YAML": {verda.ManagedKubernetesKubeconfigStatusPending, "y", false}, + "ready without YAML": {verda.ManagedKubernetesKubeconfigStatusReady, "", false}, + "ready with YAML": {verda.ManagedKubernetesKubeconfigStatusReady, "y", true}, + } { + status, done := kubeconfigReady(&verda.ManagedKubernetesKubeconfig{Status: tc.status, Kubeconfig: tc.yaml}) + if status != tc.status || done != tc.done { + t.Errorf("%s: got (%q, %v), want (%q, %v)", name, status, done, tc.status, tc.done) + } + } +} diff --git a/internal/verda-cli/cmd/mk8s/wizard_cache.go b/internal/verda-cli/cmd/mk8s/wizard_cache.go new file mode 100644 index 00000000..1c5ea7ae --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_cache.go @@ -0,0 +1,174 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "slices" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// wizardCache memoizes the lookups the create wizards share, so going back to +// a step or filling a second step from the same list costs no extra call. +type wizardCache struct { + f cmdutil.Factory + client *verda.Client + + locations []string + haveLocation bool + networks []verda.PrivateNetwork + haveNetworks bool + subnets map[string][]verda.Subnet + + clusters []verda.ManagedKubernetesCluster + haveClusters bool + types map[string][]string + sshKeys []verda.SSHKey + haveKeys bool +} + +func newWizardCache(f cmdutil.Factory, client *verda.Client) *wizardCache { + return &wizardCache{f: f, client: client, subnets: map[string][]verda.Subnet{}, types: map[string][]string{}} +} + +// wizardCall bounds one lookup by --timeout and shows the spinner. +func wizardCall[T any](ctx context.Context, c *wizardCache, msg string, fn func(context.Context) (T, error)) (T, error) { + return apiCall(ctx, c.f, msg, fn) +} + +func (c *wizardCache) enabledLocations(ctx context.Context) ([]string, error) { + if c.haveLocation { + return c.locations, nil + } + res, err := wizardCall(ctx, c, "Loading locations...", func(ctx context.Context) (*verda.ManagedKubernetesEnabledLocations, error) { + return c.client.ManagedKubernetes.GetEnabledLocations(ctx) + }) + if err != nil { + return nil, err + } + c.locations, c.haveLocation = res.Locations, true + return c.locations, nil +} + +func (c *wizardCache) privateNetworks(ctx context.Context) ([]verda.PrivateNetwork, error) { + if c.haveNetworks { + return c.networks, nil + } + networks, err := wizardCall(ctx, c, "Loading private networks...", func(ctx context.Context) ([]verda.PrivateNetwork, error) { + return c.client.PrivateNetworks.Get(ctx) + }) + if err != nil { + return nil, err + } + c.networks, c.haveNetworks = networks, true + return c.networks, nil +} + +func (c *wizardCache) networkSubnets(ctx context.Context, networkID string) ([]verda.Subnet, error) { + if s, ok := c.subnets[networkID]; ok { + return s, nil + } + subnets, err := wizardCall(ctx, c, "Loading subnets...", func(ctx context.Context) ([]verda.Subnet, error) { + return c.client.PrivateNetworks.GetSubnets(ctx, networkID) + }) + if err != nil { + return nil, err + } + c.subnets[networkID] = subnets + return subnets, nil +} + +// networkName is the name of a network the wizard has already listed, else the ID. +func (c *wizardCache) networkName(id string) string { + for i := range c.networks { + if c.networks[i].ID == id { + return c.networks[i].Name + } + } + return id +} + +// stopError ends a wizard before any create call. The engine wraps loader +// errors with the step name; callers unwrap a stopError to show its text as written. +type stopError struct{ msg string } + +func (e *stopError) Error() string { return e.msg } + +func stopMessage(msg string) error { return &stopError{msg: msg} } + +func (c *wizardCache) clusterList(ctx context.Context) ([]verda.ManagedKubernetesCluster, error) { + if c.haveClusters { + return c.clusters, nil + } + clusters, err := wizardCall(ctx, c, "Loading clusters...", func(ctx context.Context) ([]verda.ManagedKubernetesCluster, error) { + return c.client.ManagedKubernetes.List(ctx) + }) + if err != nil { + return nil, err + } + c.clusters, c.haveClusters = clusters, true + return c.clusters, nil +} + +// clusterLocation is the location of a cluster the wizard has listed, or "". +func (c *wizardCache) clusterLocation(name string) string { + for i := range c.clusters { + if c.clusters[i].Name == name { + return c.clusters[i].Location + } + } + return "" +} + +// instanceTypes lists the types the availability API reports for a location. +// It ignores private-network capacity, so a listed type can still be refused. +func (c *wizardCache) instanceTypes(ctx context.Context, location string) ([]string, error) { + if t, ok := c.types[location]; ok { + return t, nil + } + avail, err := wizardCall(ctx, c, "Loading instance types...", func(ctx context.Context) ([]verda.LocationAvailability, error) { + return c.client.InstanceAvailability.GetAllAvailabilities(ctx, false, location) + }) + if err != nil { + return nil, err + } + var types []string + for i := range avail { + if avail[i].LocationCode == location { + types = append(types, avail[i].Availabilities...) + } + } + slices.Sort(types) + types = slices.Compact(types) + c.types[location] = types + return types, nil +} + +func (c *wizardCache) sshKeyList(ctx context.Context) ([]verda.SSHKey, error) { + if c.haveKeys { + return c.sshKeys, nil + } + keys, err := wizardCall(ctx, c, "Loading SSH keys...", func(ctx context.Context) ([]verda.SSHKey, error) { + return c.client.SSHKeys.GetAllSSHKeys(ctx) + }) + if err != nil { + return nil, err + } + c.sshKeys, c.haveKeys = keys, true + return c.sshKeys, nil +} diff --git a/internal/verda-cli/cmd/mk8s/wizard_cluster.go b/internal/verda-cli/cmd/mk8s/wizard_cluster.go new file mode 100644 index 00000000..2877f063 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_cluster.go @@ -0,0 +1,214 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "errors" + "fmt" + "net/netip" + "strings" + + "github.com/verda-cloud/verda-cli/pkg/tui" + "github.com/verda-cloud/verda-cli/pkg/tui/wizard" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// newWizardEngine builds the wizard engine; a var so tests can drive it with canned answers. +var newWizardEngine = func(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *wizard.Engine { + return wizard.NewEngine(f.Prompter(), f.Status(), wizard.WithOutput(ioStreams.ErrOut)) +} + +// wizardLayout has no hint bar view: the engine renders its own below each prompt. +func wizardLayout() []wizard.ViewDef { + return []wizard.ViewDef{ + {ID: "progress", View: wizard.NewProgressView(wizard.WithProgressPercent())}, + } +} + +// The SDK and the API own the real rules (name format and length, CIDR size and +// overlap); the wizard only rejects input that cannot be sent at all. +func validateClusterName(s string) error { + if s == "" { + return errors.New("name must not be empty") + } + return nil +} + +func validateCIDR(s, example string) error { + if _, err := netip.ParsePrefix(s); err != nil { + return fmt.Errorf("enter a range in CIDR form, for example %s", example) + } + return nil +} + +func validateVersion(s string) error { + if strings.TrimSpace(s) == "" { + return errors.New("enter a Kubernetes version, for example 1.37.0") + } + return nil +} + +// buildClusterCreateFlow asks for every value the flags did not give. Each step +// loads its choices when it is reached, through the shared cache. +func buildClusterCreateFlow(opts *clusterCreateOptions, cache *wizardCache) *wizard.Flow { + nameSet, locationSet, versionSet := opts.Name != "", opts.Location != "", opts.Version != "" + networkSet, subnetSet := opts.Network != "", opts.Subnet != "" + podSet, serviceSet := opts.podCIDRSet, opts.serviceCIDRSet + text := func(v any) string { s, _ := v.(string); return strings.TrimSpace(s) } + + return &wizard.Flow{ + Name: "mk8s-cluster-create", + Layout: wizardLayout(), + Steps: []wizard.Step{ + { + Name: flagName, + Description: "Cluster name (unique in the project)", + Prompt: wizard.TextInputPrompt, + Required: true, + Validate: func(v any) error { return validateClusterName(text(v)) }, + Setter: func(v any) { opts.Name = text(v) }, + Resetter: func() { opts.Name = "" }, + IsSet: func() bool { return nameSet }, + Value: func() any { return opts.Name }, + }, + { + Name: flagLocation, + Description: "Location", + Prompt: wizard.SelectPrompt, + Required: true, + Loader: locationLoader(cache), + Setter: func(v any) { opts.Location = text(v) }, + Resetter: func() { opts.Location = "" }, + IsSet: func() bool { return locationSet }, + Value: func() any { return opts.Location }, + }, + { + Name: flagKubernetesVersion, + Description: "Kubernetes version (for example 1.37.0)", + Prompt: wizard.TextInputPrompt, + Required: true, + Validate: func(v any) error { return validateVersion(text(v)) }, + Setter: func(v any) { opts.Version = text(v) }, + Resetter: func() { opts.Version = "" }, + IsSet: func() bool { return versionSet }, + Value: func() any { return opts.Version }, + }, + { + Name: flagNetwork, + Description: "Private network", + Prompt: wizard.SelectPrompt, + Required: true, + Loader: networkLoader(cache), + Setter: func(v any) { opts.Network = text(v) }, + Resetter: func() { opts.Network = "" }, + IsSet: func() bool { return networkSet }, + Value: func() any { return opts.Network }, + }, + { + Name: flagSubnet, + Description: "Subnet (in the network and location you chose)", + Prompt: wizard.SelectPrompt, + Required: true, + DependsOn: []string{flagNetwork, flagLocation}, + Loader: subnetLoader(opts, cache), + Setter: func(v any) { opts.Subnet = text(v) }, + Resetter: func() { opts.Subnet = "" }, + IsSet: func() bool { return subnetSet }, + Value: func() any { return opts.Subnet }, + }, + { + Name: flagPodCIDR, + Description: "Pod IP range (CIDR)", + Prompt: wizard.TextInputPrompt, + Default: func(_ map[string]any) any { return defaultPodCIDR }, + Validate: func(v any) error { return validateCIDR(text(v), defaultPodCIDR) }, + Setter: func(v any) { opts.PodCIDR = text(v) }, + Resetter: func() { opts.PodCIDR = defaultPodCIDR }, + IsSet: func() bool { return podSet }, + Value: func() any { return opts.PodCIDR }, + }, + { + Name: flagServiceCIDR, + Description: "Service IP range (CIDR)", + Prompt: wizard.TextInputPrompt, + Default: func(_ map[string]any) any { return defaultServiceCIDR }, + Validate: func(v any) error { return validateCIDR(text(v), defaultServiceCIDR) }, + Setter: func(v any) { opts.ServiceCIDR = text(v) }, + Resetter: func() { opts.ServiceCIDR = defaultServiceCIDR }, + IsSet: func() bool { return serviceSet }, + Value: func() any { return opts.ServiceCIDR }, + }, + }, + } +} + +func locationLoader(cache *wizardCache) wizard.LoaderFunc { + return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { + locations, err := cache.enabledLocations(ctx) + if err != nil { + return nil, err + } + if len(locations) == 0 { + return nil, stopMessage("Managed Kubernetes is enabled for this project, but no location is configured.") + } + choices := make([]wizard.Choice, len(locations)) + for i, code := range locations { + choices[i] = wizard.Choice{Label: code, Value: code} + } + return choices, nil + } +} + +func networkLoader(cache *wizardCache) wizard.LoaderFunc { + return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { + networks, err := cache.privateNetworks(ctx) + if err != nil { + return nil, err + } + if len(networks) == 0 { + return nil, stopMessage("No private network found. Create one with `verda network create`.") + } + choices := make([]wizard.Choice, len(networks)) + for i := range networks { + choices[i] = wizard.Choice{Label: networks[i].Name, Value: networks[i].ID, Description: networks[i].ID + ", " + networks[i].Mode} + } + return choices, nil + } +} + +// subnetLoader lists the subnets of the chosen network that are in the chosen location. +func subnetLoader(opts *clusterCreateOptions, cache *wizardCache) wizard.LoaderFunc { + return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { + subnets, err := cache.networkSubnets(ctx, opts.Network) + if err != nil { + return nil, err + } + // Only for the stop message below: a network given by --network was never listed. + _, _ = cache.privateNetworks(ctx) + var choices []wizard.Choice + for i := range subnets { + if subnets[i].Location.Code == opts.Location { + choices = append(choices, wizard.Choice{Label: subnets[i].Name, Value: subnets[i].ID, Description: subnets[i].ID + ", " + subnets[i].CIDR}) + } + } + if len(choices) == 0 { + return nil, stopMessage(fmt.Sprintf("Network %s has no subnet in %s. Create one with `verda network subnet create`.", + cache.networkName(opts.Network), opts.Location)) + } + return choices, nil + } +} diff --git a/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go b/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go new file mode 100644 index 00000000..0d9e862d --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go @@ -0,0 +1,235 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "context" + "io" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verda-cli/pkg/tui/wizard" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func wizardAPI() *fakeAPI { + api := newFakeAPI() + api.addNetwork("pn-a", "alpha") + api.addNetwork("pn-123", "beta") + api.addSubnet("pn-123", "sn-1", "other-loc", "FIN-01", "10.0.1.0/24") + api.addSubnet("pn-123", "sn-456", "fin-03", "FIN-03", "10.0.3.0/24") + return api +} + +// withWizardAnswers makes the wizard take canned answers instead of a terminal. +func withWizardAnswers(t *testing.T, results ...wizard.TestResult) { + t.Helper() + orig := newWizardEngine + newWizardEngine = func(cmdutil.Factory, cmdutil.IOStreams) *wizard.Engine { + return wizard.NewEngine(nil, nil, wizard.WithOutput(io.Discard), wizard.WithTestResults(results...)) + } + t.Cleanup(func() { newWizardEngine = orig }) +} + +func postBody(t *testing.T, api *fakeAPI) map[string]string { + t.Helper() + reqs := api.requestsMatching("POST") + if len(reqs) != 1 { + t.Fatalf("want one POST, got %v", reqs) + } + return decodeJSON[map[string]string](t, reqs[0].Body) +} + +func TestClusterCreateWizard_EndsInTheSameRequestAsFlags(t *testing.T) { + withTerminal(t) + + flagsAPI := wizardAPI() + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, flagsAPI)), createArgs...); err != nil { + t.Fatal(err) + } + + wizAPI := wizardAPI() + withWizardAnswers(t, + wizard.TextResult("prod"), + wizard.SelectResult(1), // FIN-03 (locations are FIN-01, FIN-03) + wizard.TextResult("1.37.0"), + wizard.SelectResult(1), // beta = pn-123 + wizard.SelectResult(0), // only FIN-03 subnet: sn-456 + wizard.TextResult(""), // pod CIDR: default + wizard.TextResult(""), // service CIDR: default + ) + f := tableFactory(newFakeClient(t, wizAPI), nil) + f.PrompterOverride = confirmYes() + if _, _, err := runMK8s(t, f, "cluster", "create"); err != nil { + t.Fatal(err) + } + + want, got := postBody(t, flagsAPI), postBody(t, wizAPI) + for k, v := range want { + if got[k] != v { + t.Errorf("wizard body[%s] = %q, flags body has %q", k, got[k], v) + } + } + if len(got) != len(want) { + t.Errorf("wizard body = %v, flags body = %v", got, want) + } +} + +func TestClusterCreateWizard_FlagsSkipTheirSteps(t *testing.T) { + withTerminal(t) + api := wizardAPI() + // The name and --service-cidr are not given: two answers. --pod-cidr and the + // rest skip their steps. + withWizardAnswers(t, wizard.TextResult("prod"), wizard.TextResult("")) + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = confirmYes() + args := []string{"cluster", "create", "-l", "FIN-03", "--kubernetes-version", "1.37.0", "--network", "pn-123", "--subnet", "sn-456", "--pod-cidr", "10.50.0.0/16"} + if _, _, err := runMK8s(t, f, args...); err != nil { + t.Fatal(err) + } + body := postBody(t, api) + if body["name"] != "prod" || body["pod_cidr"] != "10.50.0.0/16" || body["service_cidr"] != defaultServiceCIDR { + t.Errorf("body = %v", body) + } + // Flags need no lookups. + if got := api.paths("GET"); len(got) != 0 { + t.Errorf("unexpected reads: %v", got) + } +} + +func TestClusterCreateWizard_StopsBeforeCreateOnEmptyPickers(t *testing.T) { + withTerminal(t) + for name, tc := range map[string]struct { + api func() *fakeAPI + answers []wizard.TestResult + want string // exactly as shown, with no wizard step prefix + }{ + "no network": { + api: func() *fakeAPI { return newFakeAPI() }, + answers: []wizard.TestResult{wizard.TextResult("prod"), wizard.SelectResult(1), wizard.TextResult("1.37.0")}, + want: "No private network found. Create one with `verda network create`.", + }, + "no subnet in the location": { + api: func() *fakeAPI { + api := newFakeAPI() + api.addNetwork("pn-123", "beta") + api.addSubnet("pn-123", "sn-1", "other-loc", "FIN-01", "10.0.1.0/24") + return api + }, + answers: []wizard.TestResult{wizard.TextResult("prod"), wizard.SelectResult(1), wizard.TextResult("1.37.0"), wizard.SelectResult(0)}, + want: "Network beta has no subnet in FIN-03. Create one with `verda network subnet create`.", + }, + "no location": { + api: func() *fakeAPI { + api := newFakeAPI() + api.locations = nil + return api + }, + answers: []wizard.TestResult{wizard.TextResult("prod")}, + want: "Managed Kubernetes is enabled for this project, but no location is configured.", + }, + } { + api := tc.api() + withWizardAnswers(t, tc.answers...) + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = confirmYes() + _, _, err := runMK8s(t, f, "cluster", "create") + if err == nil || err.Error() != tc.want { + t.Errorf("%s: err = %v, want exactly %q", name, err, tc.want) + } + if n := len(api.requestsMatching("POST")); n != 0 { + t.Errorf("%s: %d POST(s) before the stop", name, n) + } + } +} + +func TestClusterCreateWizard_DeclinedSummaryCreatesNothing(t *testing.T) { + withTerminal(t) + api := wizardAPI() + withWizardAnswers(t, wizard.TextResult("prod"), wizard.TextResult(""), wizard.TextResult("")) + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = confirmNo() + args := []string{"cluster", "create", "-l", "FIN-03", "--kubernetes-version", "1.37.0", "--network", "pn-123", "--subnet", "sn-456"} + _, errOut, err := runMK8s(t, f, args...) + if err != nil || !strings.Contains(errOut, canceledMsg) { + t.Fatalf("err=%v stderr=%q", err, errOut) + } + if n := len(api.requestsMatching("POST")); n != 0 { + t.Errorf("%d POST(s)", n) + } +} + +func TestClusterCreateWizard_NotUsedOffTerminal(t *testing.T) { + t.Parallel() + // A wizard run here would hang the test: off a terminal the command fails first. + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, wizardAPI()), nil), "cluster", "create", "--name", "prod") + if agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } +} + +func TestWizardCache_MemoizesLookups(t *testing.T) { + t.Parallel() + api := wizardAPI() + f := tableFactory(newFakeClient(t, api), nil) + cache := newWizardCache(f, newFakeClient(t, api)) + ctx := context.Background() + for range 3 { + if _, err := cache.enabledLocations(ctx); err != nil { + t.Fatal(err) + } + if _, err := cache.privateNetworks(ctx); err != nil { + t.Fatal(err) + } + if _, err := cache.networkSubnets(ctx, "pn-123"); err != nil { + t.Fatal(err) + } + } + want := []string{"/mk8sclusters/enabled_locations", "/private-networks", "/private-networks/pn-123/subnets"} + got := api.paths("GET") + slices.Sort(got) + slices.Sort(want) + if !slices.Equal(got, want) { + t.Errorf("requests = %v, want one each: %v", got, want) + } + if cache.networkName("pn-123") != "beta" || cache.networkName("pn-x") != "pn-x" { + t.Error("networkName falls back to the ID") + } +} + +func TestValidateClusterWizardInputs(t *testing.T) { + t.Parallel() + // Only input that cannot be sent is rejected; the SDK and the API own the real rules. + for name, ok := range map[string]bool{"prod": true, "Prod": true, "a_b": true, "": false} { + if err := validateClusterName(name); (err == nil) != ok { + t.Errorf("validateClusterName(%q) = %v, ok=%v", name, err, ok) + } + } + for cidr, ok := range map[string]bool{"10.100.0.0/16": true, "": false, "10.0.0.1": false, "x/16": false} { + if err := validateCIDR(cidr, defaultPodCIDR); (err == nil) != ok { + t.Errorf("validateCIDR(%q) = %v, ok=%v", cidr, err, ok) + } + } + for example, want := range map[string]string{defaultPodCIDR: "for example 10.100.0.0/16", defaultServiceCIDR: "for example 10.200.0.0/16"} { + if err := validateCIDR("x", example); err == nil || !strings.Contains(err.Error(), want) { + t.Errorf("validateCIDR error = %v, want %q", err, want) + } + } + if validateVersion(" ") == nil || validateVersion("1.37.0") != nil { + t.Error("validateVersion") + } +} diff --git a/internal/verda-cli/cmd/mk8s/wizard_nodepool.go b/internal/verda-cli/cmd/mk8s/wizard_nodepool.go new file mode 100644 index 00000000..02635c3e --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_nodepool.go @@ -0,0 +1,203 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "cmp" + "context" + "errors" + "slices" + "strconv" + "strings" + + "github.com/verda-cloud/verda-cli/pkg/tui" + "github.com/verda-cloud/verda-cli/pkg/tui/wizard" +) + +func validateReplicas(s string) error { + n, err := strconv.Atoi(strings.TrimSpace(s)) + if err != nil || n < 0 { + return errors.New("enter a whole number, 0 or more") + } + return nil +} + +func validateName(s string) error { + if s == "" { + return errors.New("name must not be empty") + } + return nil +} + +// buildNodePoolCreateFlow asks for every value the flags did not give. The +// cluster is already known when it came from --cluster. +func buildNodePoolCreateFlow(opts *nodePoolCreateOptions, cache *wizardCache) *wizard.Flow { + clusterSet, nameSet, typeSet := opts.Cluster != "", opts.Name != "", opts.InstanceType != "" + // -l as the user gave it. The cluster step sets the location from the + // cluster it picks, and going Back must undo that, not keep it. + locationFlag := opts.Location + keysSet, replicasSet := len(opts.SSHKeys) > 0, opts.replicasSet + text := func(v any) string { s, _ := v.(string); return strings.TrimSpace(s) } + + return &wizard.Flow{ + Name: "mk8s-node-pool-create", + Layout: wizardLayout(), + Steps: []wizard.Step{ + { + Name: flagCluster, + Description: "Cluster", + Prompt: wizard.SelectPrompt, + Required: true, + Loader: clusterLoader(locationFlag, cache), + Setter: func(v any) { + opts.Cluster = text(v) + if locationFlag == "" { + opts.Location = cache.clusterLocation(opts.Cluster) + } + }, + Resetter: func() { + opts.Cluster = "" + opts.Location = locationFlag + }, + IsSet: func() bool { return clusterSet }, + Value: func() any { return opts.Cluster }, + }, + { + Name: flagName, + Description: "Node pool name (unique in the project, not only in the cluster)", + Prompt: wizard.TextInputPrompt, + Required: true, + Validate: func(v any) error { return validateName(text(v)) }, + Setter: func(v any) { opts.Name = text(v) }, + Resetter: func() { opts.Name = "" }, + IsSet: func() bool { return nameSet }, + Value: func() any { return opts.Name }, + }, + { + Name: flagInstanceType, + Description: "Instance type", + Prompt: wizard.SelectPrompt, + Required: true, + DependsOn: []string{flagCluster}, + Loader: instanceTypeLoader(opts, cache), + Setter: func(v any) { opts.InstanceType = text(v) }, + Resetter: func() { opts.InstanceType = "" }, + IsSet: func() bool { return typeSet }, + Value: func() any { return opts.InstanceType }, + }, + { + Name: flagSSHKey, + Description: "SSH keys for the workers", + Prompt: wizard.MultiSelectPrompt, + Required: true, + Loader: sshKeyLoader(cache), + MinError: func(int) string { return "select at least one SSH key" }, + Setter: func(v any) { opts.SSHKeys, _ = v.([]string) }, + Resetter: func() { opts.SSHKeys = nil }, + IsSet: func() bool { return keysSet }, + Value: func() any { return opts.SSHKeys }, + }, + { + Name: flagReplicas, + Description: "Number of workers", + Prompt: wizard.TextInputPrompt, + Default: func(map[string]any) any { return strconv.Itoa(defaultPoolReplicas) }, + Validate: func(v any) error { return validateReplicas(text(v)) }, + Setter: func(v any) { opts.Replicas, _ = strconv.Atoi(text(v)) }, + Resetter: func() { opts.Replicas = defaultPoolReplicas }, + IsSet: func() bool { return replicasSet }, + Value: func() any { return strconv.Itoa(opts.Replicas) }, + }, + }, + } +} + +// clusterLoader lists the clusters, only those in -l when it is given. +func clusterLoader(locationFlag string, cache *wizardCache) wizard.LoaderFunc { + return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { + clusters, err := cache.clusterList(ctx) + if err != nil { + return nil, err + } + var choices []wizard.Choice + for i := range clusters { + if locationFlag == "" || clusters[i].Location == locationFlag { + choices = append(choices, wizard.Choice{Label: clusters[i].Name, Value: clusters[i].Name, Description: clusters[i].Location + ", " + clusters[i].Phase}) + } + } + if len(choices) == 0 { + where := "" + if locationFlag != "" { + where = " in " + locationFlag + } + return nil, stopMessage("No cluster found" + where + ". Create one with `verda mk8s cluster create`.") + } + return choices, nil + } +} + +// instanceTypeRank orders types smallest first: CPU-only types, then by GPU +// count. The availability data carries no prices, so this stands in for cheapest first. +func instanceTypeRank(t string) int { + if strings.HasPrefix(strings.ToUpper(t), "CPU") { + return 0 + } + n := 0 + for _, r := range t { + if r < '0' || r > '9' { + break + } + n = n*10 + int(r-'0') + } + return n +} + +// instanceTypeLoader lists the types for the cluster's location, smallest +// first so Enter on the first row picks the cheapest, with no prices. +func instanceTypeLoader(opts *nodePoolCreateOptions, cache *wizardCache) wizard.LoaderFunc { + return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { + types, err := cache.instanceTypes(ctx, opts.Location) + if err != nil { + return nil, err + } + if len(types) == 0 { + return nil, stopMessage("No instance types are listed for " + opts.Location + ".") + } + sorted := slices.Clone(types) + slices.SortStableFunc(sorted, func(a, b string) int { return cmp.Compare(instanceTypeRank(a), instanceTypeRank(b)) }) + choices := make([]wizard.Choice, len(sorted)) + for i, t := range sorted { + choices[i] = wizard.Choice{Label: t, Value: t, Description: "instance types in " + opts.Location} + } + return choices, nil + } +} + +func sshKeyLoader(cache *wizardCache) wizard.LoaderFunc { + return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { + keys, err := cache.sshKeyList(ctx) + if err != nil { + return nil, err + } + if len(keys) == 0 { + return nil, stopMessage("No SSH key found. Add one with `verda ssh-key add`.") + } + choices := make([]wizard.Choice, len(keys)) + for i := range keys { + choices[i] = wizard.Choice{Label: keys[i].Name, Value: keys[i].ID, Description: keys[i].ID} + } + return choices, nil + } +} diff --git a/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go b/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go new file mode 100644 index 00000000..7a198593 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go @@ -0,0 +1,272 @@ +// Copyright 2026 Verda Cloud Oy +// +// 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 mk8s + +import ( + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verda-cli/pkg/tui/wizard" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestNodePoolCreateWizard_EndsInTheSameRequestAsFlags(t *testing.T) { + withTerminal(t) + + flagsAPI := poolWizardAPI() + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, flagsAPI)), withPoolArgs("--replicas", "2")...); err != nil { + t.Fatal(err) + } + + wizAPI := poolWizardAPI() + withWizardAnswers(t, + wizard.SelectResult(0), // cluster: prod + wizard.TextResult("workers"), // name + wizard.SelectResult(1), // instance type: 1V100.6V, after the CPU type + wizard.MultiSelectResult([]int{0, 1}), // key-1, key-2 + wizard.TextResult("2"), // replicas + ) + f := tableFactory(newFakeClient(t, wizAPI), nil) + f.PrompterOverride = confirmYes() + if _, _, err := runMK8s(t, f, "node-pool", "create"); err != nil { + t.Fatal(err) + } + + want, got := postBodyAny(t, flagsAPI), postBodyAny(t, wizAPI) + if !equalJSON(want, got) { + t.Errorf("wizard body = %v, flags body = %v", got, want) + } + if posts := wizAPI.requestsMatching("POST"); posts[0].Path != "/mk8sclusters/FIN-03/prod/pools" { + t.Errorf("POST path = %s", posts[0].Path) + } +} + +func TestNodePoolCreateWizard_FlagsSkipTheirSteps(t *testing.T) { + withTerminal(t) + api := poolWizardAPI() + // Only --cluster and --ssh-key are given: name, type and replicas are asked. + withWizardAnswers(t, wizard.TextResult("workers"), wizard.SelectResult(2), wizard.TextResult("")) + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = confirmYes() + if _, _, err := runMK8s(t, f, "node-pool", "create", "--cluster", "prod", "--ssh-key", "key-2"); err != nil { + t.Fatal(err) + } + body := postBodyAny(t, api) + if body["instance_type"] != "2A100.80S" || body["replicas"] != float64(1) { + t.Errorf("body = %v", body) + } + if keys, _ := body["ssh_key_ids"].([]any); len(keys) != 1 || keys[0] != "key-2" { + t.Errorf("ssh_key_ids = %v", body["ssh_key_ids"]) + } + // The cluster was found once, before the wizard; no SSH key lookup for a key given by flag. + for _, p := range api.paths("GET") { + if p == "/ssh-keys" { + t.Error("SSH keys were listed although --ssh-key was given") + } + } +} + +func TestNodePoolCreateWizard_UnknownClusterFailsBeforeAnyPrompt(t *testing.T) { + withTerminal(t) + api := poolWizardAPI() + // No wizard answers queued: a prompt would hang or fail. + withWizardAnswers(t) + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = confirmYes() + _, _, err := runMK8s(t, f, "node-pool", "create", "--cluster", "ghost") + if agentErrorCode(err) != "NOT_FOUND" { + t.Fatalf("err = %v", err) + } +} + +func TestNodePoolCreateWizard_LocationLimitsTheClusters(t *testing.T) { + withTerminal(t) + api := poolWizardAPI() + // -l FIN-01 leaves one cluster, staging; its location offers 1A100.22V. + withWizardAnswers(t, wizard.SelectResult(0), wizard.TextResult("w"), wizard.SelectResult(0), wizard.MultiSelectResult([]int{0}), wizard.TextResult("")) + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = confirmYes() + if _, _, err := runMK8s(t, f, "node-pool", "create", "-l", "FIN-01"); err != nil { + t.Fatal(err) + } + if got := api.requestsMatching("POST")[0].Path; got != "/mk8sclusters/FIN-01/staging/pools" { + t.Errorf("POST path = %s", got) + } + if body := postBodyAny(t, api); body["instance_type"] != "1A100.22V" { + t.Errorf("body = %v", body) + } +} + +func TestNodePoolCreateWizard_InstanceTypesAreSmallestFirstAndHaveNoPrice(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + opts := &nodePoolCreateOptions{} + opts.Location = "FIN-03" + choices, err := instanceTypeLoader(opts, newWizardCache(tableFactory(newFakeClient(t, api), nil), newFakeClient(t, api)))(t.Context(), nil, nil, nil) + if err != nil { + t.Fatal(err) + } + labels := make([]string, 0, len(choices)) + for _, c := range choices { + labels = append(labels, c.Label) + if c.Description != "instance types in FIN-03" { + t.Errorf("description = %q", c.Description) + } + if s := strings.ToLower(c.Label + c.Description + c.Value); strings.Contains(s, "$") || strings.Contains(s, "price") || strings.Contains(s, "/h") { + t.Errorf("a price must not be shown: %+v", c) + } + } + if want := []string{"CPU.4V.16G", "1V100.6V", "2A100.80S"}; !slices.Equal(labels, want) { + t.Errorf("labels = %v, want %v (only FIN-03, smallest first)", labels, want) + } +} + +func TestNodePoolCreateWizard_StopsOnEmptyPickers(t *testing.T) { + withTerminal(t) + for name, tc := range map[string]struct { + api func() *fakeAPI + answers []wizard.TestResult + want string + }{ + "no cluster": { + api: func() *fakeAPI { return newFakeAPI() }, + want: "No cluster found. Create one with `verda mk8s cluster create`.", + }, + "no instance types": { + api: func() *fakeAPI { + api := poolWizardAPI() + api.availability = map[string][]string{} + return api + }, + answers: []wizard.TestResult{wizard.SelectResult(0), wizard.TextResult("w")}, + want: "No instance types are listed for FIN-03.", + }, + "no ssh key": { + api: func() *fakeAPI { + api := poolWizardAPI() + api.sshKeys = nil + return api + }, + answers: []wizard.TestResult{wizard.SelectResult(0), wizard.TextResult("w"), wizard.SelectResult(0)}, + want: "No SSH key found. Add one with `verda ssh-key add`.", + }, + } { + api := tc.api() + withWizardAnswers(t, tc.answers...) + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = confirmYes() + _, _, err := runMK8s(t, f, "node-pool", "create") + if err == nil || err.Error() != tc.want { + t.Errorf("%s: err = %v, want exactly %q", name, err, tc.want) + } + if n := len(api.requestsMatching("POST")); n != 0 { + t.Errorf("%s: %d POST(s) before the stop", name, n) + } + } +} + +func TestNodePoolCreateWizard_NotUsedOffTerminal(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolWizardAPI()), nil), "node-pool", "create", "--cluster", "prod") + if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } +} + +func TestValidateReplicas(t *testing.T) { + t.Parallel() + for in, ok := range map[string]bool{"0": true, "1": true, " 5 ": true, "": false, "-1": false, "two": false, "1.5": false} { + if err := validateReplicas(in); (err == nil) != ok { + t.Errorf("validateReplicas(%q) = %v, ok=%v", in, err, ok) + } + } +} + +func equalJSON(a, b map[string]any) bool { + if len(a) != len(b) { + return false + } + for k, v := range a { + if !deepEqualAny(v, b[k]) { + return false + } + } + return true +} + +func deepEqualAny(a, b any) bool { + as, aok := a.([]any) + bs, bok := b.([]any) + if aok && bok { + return slices.EqualFunc(as, bs, deepEqualAny) + } + return a == b +} + +func TestNodePoolCreateWizard_BackRestoresTheLocationFilter(t *testing.T) { + t.Parallel() + api := poolWizardAPI() + client := newFakeClient(t, api) + cache := newWizardCache(tableFactory(client, nil), client) + + t.Run("no -l: going Back offers every cluster again", func(t *testing.T) { + opts := &nodePoolCreateOptions{} + flow := buildNodePoolCreateFlow(opts, cache) + step := flow.Steps[0] + if _, err := step.Loader(t.Context(), nil, nil, nil); err != nil { // the engine loads the choices first + t.Fatal(err) + } + step.Setter("staging") // FIN-01 + if opts.Location != "FIN-01" { + t.Fatalf("location = %q after picking staging", opts.Location) + } + step.Resetter() + if opts.Cluster != "" || opts.Location != "" { + t.Errorf("after Back: cluster=%q location=%q, want both cleared", opts.Cluster, opts.Location) + } + choices, err := step.Loader(t.Context(), nil, nil, nil) + if err != nil || len(choices) != 2 { + t.Errorf("choices = %v err=%v, want both clusters", choices, err) + } + }) + + t.Run("with -l: Back keeps the flag", func(t *testing.T) { + opts := &nodePoolCreateOptions{} + opts.Location = "FIN-03" + flow := buildNodePoolCreateFlow(opts, cache) + step := flow.Steps[0] + step.Setter("prod") + step.Resetter() + if opts.Location != "FIN-03" { + t.Errorf("location = %q, want the -l value kept", opts.Location) + } + if choices, _ := step.Loader(t.Context(), nil, nil, nil); len(choices) != 1 || choices[0].Value != "prod" { + t.Errorf("choices = %v, want prod only", choices) + } + }) +} + +func TestInstanceTypeRank(t *testing.T) { + t.Parallel() + for in, want := range map[string]int{ + "CPU-TURIN.4V.16G": 0, "CPU.4V.16G": 0, "1V100.6V": 1, "8B200.240V.CC.AMD": 8, "16H100.1": 16, "x": 0, + } { + if got := instanceTypeRank(in); got != want { + t.Errorf("instanceTypeRank(%q) = %d, want %d", in, got, want) + } + } +} diff --git a/internal/verda-cli/cmd/util/agent_error.go b/internal/verda-cli/cmd/util/agent_error.go index 1d7d1e4f..dcef0bf2 100644 --- a/internal/verda-cli/cmd/util/agent_error.go +++ b/internal/verda-cli/cmd/util/agent_error.go @@ -57,6 +57,7 @@ type agentErrorEnvelope struct { // WriteAgentError writes a structured JSON error to w. func WriteAgentError(w io.Writer, ae *AgentError) { enc := json.NewEncoder(w) + enc.SetEscapeHTML(false) enc.SetIndent("", " ") _ = enc.Encode(agentErrorEnvelope{Error: ae}) } @@ -72,13 +73,19 @@ func NewMissingFlagsError(missing []string) *AgentError { } // NewMissingOneOfFlagsError: missing lists required flags; at least one of oneOf must also be set. +// The message names both. func NewMissingOneOfFlagsError(missing, oneOf []string) *AgentError { if missing == nil { missing = []string{} } + msg := "required flags not provided: " + if len(missing) > 0 { + msg += strings.Join(missing, ", ") + "; " + } + msg += "set at least one of " + strings.Join(oneOf, ", ") return &AgentError{ Code: "MISSING_REQUIRED_FLAGS", - Message: "required flags not provided: set at least one of " + strings.Join(oneOf, ", "), + Message: msg, Details: map[string]any{"missing": missing, "one_of": oneOf}, ExitCode: ExitBadArgs, } diff --git a/internal/verda-cli/cmd/util/agent_error_test.go b/internal/verda-cli/cmd/util/agent_error_test.go index dcb49bd6..e8df8a5a 100644 --- a/internal/verda-cli/cmd/util/agent_error_test.go +++ b/internal/verda-cli/cmd/util/agent_error_test.go @@ -35,8 +35,11 @@ func TestAgentError_Error(t *testing.T) { func TestWriteAgentError(t *testing.T) { var buf bytes.Buffer - ae := NewMissingFlagsError([]string{"--foo", "--bar"}) + ae := NewMissingFlagsError([]string{"--foo", "--bar", ""}) WriteAgentError(&buf, ae) + if !strings.Contains(buf.String(), `""`) { + t.Errorf("placeholder must not be HTML-escaped: %s", buf.String()) + } var envelope struct { Error struct { @@ -52,8 +55,15 @@ func TestWriteAgentError(t *testing.T) { t.Errorf("code = %q, want MISSING_REQUIRED_FLAGS", envelope.Error.Code) } missing, ok := envelope.Error.Details["missing"].([]any) - if !ok || len(missing) != 2 { - t.Errorf("missing = %v, want [--foo, --bar]", envelope.Error.Details["missing"]) + if !ok || len(missing) != 3 { + t.Errorf("missing = %v, want [--foo, --bar, ]", envelope.Error.Details["missing"]) + } +} + +func TestNewMissingOneOfFlagsError_MessageNamesMissing(t *testing.T) { + got := NewMissingOneOfFlagsError([]string{"--cluster", "--common-name"}, []string{"--a", "--b"}).Message + if want := "required flags not provided: --cluster, --common-name; set at least one of --a, --b"; got != want { + t.Errorf("message = %q, want %q", got, want) } } diff --git a/internal/verda-cli/cmd/util/factory.go b/internal/verda-cli/cmd/util/factory.go index c484c31e..ee41a615 100644 --- a/internal/verda-cli/cmd/util/factory.go +++ b/internal/verda-cli/cmd/util/factory.go @@ -50,9 +50,10 @@ const ( // where verify-live testing saw them leak). Value pattern allows escaped // quotes (\") so values containing them are redacted whole — a bare [^"]* // would stop at the first escaped quote and leak the remainder while emitting -// malformed JSON. +// malformed JSON. An unterminated value (a body cut short) is redacted to the +// end of the text instead of passing through. var sensitiveJSONFieldRe = regexp.MustCompile( - `("(?:client_secret|secret_access_key|service_account_key|value_or_reference_to_secret|base64_content|docker_config_json|jupyter_token|access_token|refresh_token|id_token|password|api_key|bearer|authorization|value)")(\s*:\s*)"(?:[^"\\]|\\.)*"`) + `("(?i:client_secret|secret_access_key|service_account_key|value_or_reference_to_secret|base64_content|docker_config_json|jupyter_token|access_token|refresh_token|id_token|password|api_key|bearer|authorization|kubeconfig|value)")(\s*:\s*)"(?:[^"\\]|\\.)*\\?(?:"|$)`) // sensitiveFormFieldRe matches name=value pairs in form-encoded bodies whose // values are secrets. The SDK retries /oauth2/token form-encoded when the API @@ -138,6 +139,15 @@ func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error return t.base.RoundTrip(req) } +// debugBody is the body text for the debug log. Kubeconfig endpoints carry a +// private key, so their bodies are never printed (the SDK logger skips them too). +func debugBody(path, contentType string, body []byte) string { + if strings.Contains(path, "/kubeconfigs") { + return "" + } + return redactSensitiveBody(contentType, string(body)) +} + // debugTransport logs HTTP request and response wire details to out when // enabled() returns true. The Authorization header value is redacted. type debugTransport struct { @@ -179,7 +189,7 @@ func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) { _, _ = fmt.Fprintf(t.out, "DEBUG: %s: %s\n", k, strings.Join(req.Header[k], ", ")) } if len(reqBody) > 0 { - _, _ = fmt.Fprintf(t.out, "DEBUG: request body: %s\n", redactSensitiveBody(req.Header.Get("Content-Type"), string(reqBody))) + _, _ = fmt.Fprintf(t.out, "DEBUG: request body: %s\n", debugBody(req.URL.Path, req.Header.Get("Content-Type"), reqBody)) } resp, err := t.base.RoundTrip(req) @@ -204,7 +214,7 @@ func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) { } _, _ = fmt.Fprintf(t.out, "DEBUG: HTTP response %s\n", resp.Status) if len(respBody) > 0 { - _, _ = fmt.Fprintf(t.out, "DEBUG: response body: %s\n", redactSensitiveBody(resp.Header.Get("Content-Type"), string(respBody))) + _, _ = fmt.Fprintf(t.out, "DEBUG: response body: %s\n", debugBody(req.URL.Path, resp.Header.Get("Content-Type"), respBody)) } return resp, nil } @@ -288,16 +298,8 @@ func (f *factoryImpl) VerdaClient() (*verda.Client, error) { return nil, err } - // SDK doesn't enable retry by default. Add the exponential-backoff - // middleware so transient failures (429 rate limit, 5xx, 408, 504) - // retry transparently across all CLI commands. Auth/client errors - // (4xx except 408/429) never retry — see shouldRetry in the SDK. - // - // shouldRetry is method-agnostic, so POST creates also retry on 5xx. That's - // safe here: deployment names are unique slugs, so a retry after a partial - // commit gets 409 (4xx, non-retryable) rather than creating a duplicate. If - // a non-idempotent endpoint without a natural unique key is ever added, make - // shouldRetry method-aware in the SDK before relying on this. + // SDK v1.9.1 runs this middleware around a no-op handler, so nothing is retried today. + // A later SDK that retries would also retry POST creates (shouldRetry ignores the method). client.AddRequestMiddleware(verda.ExponentialBackoffRetryMiddleware( retryMaxAttempts, retryInitialDelay, client.Logger, )) diff --git a/internal/verda-cli/cmd/util/output.go b/internal/verda-cli/cmd/util/output.go index 2b1c4710..9b0463f2 100644 --- a/internal/verda-cli/cmd/util/output.go +++ b/internal/verda-cli/cmd/util/output.go @@ -37,6 +37,7 @@ func WriteStructured(w io.Writer, format string, data any) (bool, error) { switch format { case "json": enc := json.NewEncoder(w) + enc.SetEscapeHTML(false) enc.SetIndent("", " ") return true, enc.Encode(data) case "yaml": diff --git a/internal/verda-cli/cmd/util/output_test.go b/internal/verda-cli/cmd/util/output_test.go index 2c6e51d8..456de785 100644 --- a/internal/verda-cli/cmd/util/output_test.go +++ b/internal/verda-cli/cmd/util/output_test.go @@ -24,7 +24,7 @@ import ( func TestWriteStructuredJSON(t *testing.T) { t.Parallel() - data := map[string]string{"name": "test-vol", "id": "abc-123"} + data := map[string]string{"name": "test-vol", "id": "abc-123", "hint": "use & more"} var buf bytes.Buffer wrote, err := WriteStructured(&buf, "json", data) @@ -34,6 +34,9 @@ func TestWriteStructuredJSON(t *testing.T) { if !wrote { t.Fatal("WriteStructured(json) returned false, want true") } + if !strings.Contains(buf.String(), "use & more") { + t.Fatalf("HTML characters must not be escaped: %s", buf.String()) + } var decoded map[string]string if err := json.Unmarshal(buf.Bytes(), &decoded); err != nil { diff --git a/internal/verda-cli/cmd/util/redact_test.go b/internal/verda-cli/cmd/util/redact_test.go index 236a61b2..72c1172b 100644 --- a/internal/verda-cli/cmd/util/redact_test.go +++ b/internal/verda-cli/cmd/util/redact_test.go @@ -15,6 +15,10 @@ package util import ( + "bytes" + "io" + "net/http" + "net/http/httptest" "strings" "testing" ) @@ -29,7 +33,7 @@ func TestRedactSensitiveBody_JSON(t *testing.T) { "client_secret", "secret_access_key", "service_account_key", "value_or_reference_to_secret", "base64_content", "docker_config_json", "jupyter_token", "access_token", "refresh_token", "id_token", - "password", "api_key", "bearer", "authorization", "value", + "password", "api_key", "bearer", "authorization", "kubeconfig", "value", } for _, key := range secretKeys { t.Run(key, func(t *testing.T) { @@ -57,6 +61,44 @@ func TestRedactSensitiveBody_JSON(t *testing.T) { } }) + t.Run("kubeconfig YAML with escaped newlines and quotes redacted whole", func(t *testing.T) { + t.Parallel() + in := `{"id":"c-kc-1","kubeconfig":"apiVersion: v1\nusers:\n- user:\n client-key-data: \"MARKER-KEY\"\n","status":"ready"}` + got := redactSensitiveBody("application/json", in) + if strings.Contains(got, "MARKER-KEY") || strings.Contains(got, "client-key-data") { + t.Errorf("kubeconfig YAML leaked: %s", got) + } + if !strings.Contains(got, `"status":"ready"`) || !strings.Contains(got, `"id":"c-kc-1"`) { + t.Errorf("neighboring fields mangled: %s", got) + } + }) + + t.Run("unterminated value redacted to the end", func(t *testing.T) { + t.Parallel() + for name, in := range map[string]string{ + "kubeconfig": `{"id":"c-kc-1","kubeconfig":"apiVersion: v1\nclient-key-data: MARKER-KEY`, + "trailing escape": `{"kubeconfig":"client-key-data: MARKER-KEY\`, + "client_secret": `{"client_secret": "MARKER-KEY`, + } { + got := redactSensitiveBody("application/json", in) + if strings.Contains(got, "MARKER-KEY") { + t.Errorf("%s: unterminated value leaked: %s", name, got) + } + } + }) + + t.Run("kubeconfig key with spaces and a pretty-printed body", func(t *testing.T) { + t.Parallel() + in := "{\n \"id\": \"c-kc-1\",\n \"kubeconfig\" : \"client-key-data: MARKER-KEY\\n\",\n \"status\": \"ready\"\n}" + got := redactSensitiveBody("application/json", in) + if strings.Contains(got, "MARKER-KEY") { + t.Errorf("spaced kubeconfig key leaked: %s", got) + } + if !strings.Contains(got, `"status": "ready"`) { + t.Errorf("neighboring field mangled: %s", got) + } + }) + t.Run("spacing variants", func(t *testing.T) { t.Parallel() in := "{ \"client_secret\":\"x\", \"refresh_token\"\t: \"y\" }" @@ -136,3 +178,50 @@ func TestRedactSensitiveBody_ContentTypeDispatch(t *testing.T) { } }) } + +// debugTransport must keep a kubeconfig private key out of the debug log: +// bodies of /kubeconfigs endpoints are omitted, any other path is redacted +// whatever the key's case. +func TestDebugTransport_KubeconfigBodies(t *testing.T) { + t.Parallel() + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"id":"x","kubeconfig":"MARKER-KEY","Kubeconfig":"MARKER-KEY"}`) + })) + t.Cleanup(srv.Close) + + tests := []struct { + name, path, wantOut string + }{ + {"kubeconfig endpoint omits bodies", "/v1/kubeconfigs/x", ""}, + {"other endpoint redacts keys in any case", "/v1/other", `"Kubeconfig":""`}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + var out bytes.Buffer + client := &http.Client{Transport: &debugTransport{base: http.DefaultTransport, out: &out, enabled: func() bool { return true }}} + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL+tc.path, strings.NewReader(`{"kubeconfig":"MARKER-KEY"}`)) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + body, _ := io.ReadAll(resp.Body) + _ = resp.Body.Close() + if !bytes.Contains(body, []byte("MARKER-KEY")) { + t.Fatalf("caller must still receive the body: %s", body) + } + if strings.Contains(out.String(), "MARKER-KEY") { + t.Errorf("key leaked into debug output:\n%s", out.String()) + } + if !strings.Contains(out.String(), tc.wantOut) { + t.Errorf("debug output lacks %q:\n%s", tc.wantOut, out.String()) + } + }) + } +} diff --git a/internal/verda-cli/options/paths.go b/internal/verda-cli/options/paths.go index fbde2910..ee3e8a21 100644 --- a/internal/verda-cli/options/paths.go +++ b/internal/verda-cli/options/paths.go @@ -52,7 +52,7 @@ func EnsureVerdaDir() (string, error) { if err != nil { return "", err } - if err := mkdirSecure(dir); err != nil { + if err := MkdirSecure(dir); err != nil { return "", fmt.Errorf("cannot create config directory %s: %w\n\n"+ "Check directory permissions or set VERDA_HOME to an alternative location", dir, err) } @@ -74,7 +74,7 @@ func EnsureVerdaBinDir() (string, error) { if err != nil { return "", err } - if err := mkdirSecure(dir); err != nil { + if err := MkdirSecure(dir); err != nil { return "", fmt.Errorf("cannot create binary directory %s: %w", dir, err) } return dir, nil @@ -84,7 +84,7 @@ func EnsureVerdaBinDir() (string, error) { // On Unix, the file is created with 0600. On Windows, it inherits the // parent directory ACL (Go's os.WriteFile uses default security). func WriteSecureFile(path string, data []byte) error { - if err := mkdirSecure(filepath.Dir(path)); err != nil { + if err := MkdirSecure(filepath.Dir(path)); err != nil { return fmt.Errorf("cannot create directory for %s: %w", path, err) } if err := os.WriteFile(path, data, 0o600); err != nil { @@ -97,8 +97,8 @@ func WriteSecureFile(path string, data []byte) error { return nil } -// mkdirSecure creates a directory with 0700 on Unix. -func mkdirSecure(dir string) error { +// MkdirSecure creates a directory (and parents) and sets 0700 on the leaf on Unix. +func MkdirSecure(dir string) error { if err := os.MkdirAll(dir, 0o700); err != nil { return err } From b4215b07eb7c51bbadaa77b10c56303deeda4aee Mon Sep 17 00:00:00 2001 From: Dmitrii Iutkin Date: Fri, 20 Nov 2026 20:16:04 +0200 Subject: [PATCH 2/2] refactor(mk8s): trim tests and deduplicate helpers - Tests consolidated: table-driven tests replace near-duplicates, and checks repeated across output modes, YAML variants and --debug dumps are gone. The optional refusal tests are dropped. The security guards stay: key never printed, O_EXCL, symlink, path traversal and collisions, 0600/0700, and the debugTransport round trip. - Test helpers merged into fake_test.go; unused fake branches and apiFailure.Times removed. - Helpers deduplicated: poolScope.check/connect, apiRun, runWizard, wizard step builders, poolCmd, checkPoolFlags, cleanGroups for SSH keys. - Dead code and restating comments trimmed; errWaitTimeout removed. - mk8s README.md and CLAUDE.md reconciled with the code and tests. No CLI behavior change: help text, output, errors, exit codes, API calls and file modes are the same as before. Co-Authored-By: Claude Opus 5.5 --- internal/verda-cli/cmd/mk8s/CLAUDE.md | 19 +- internal/verda-cli/cmd/mk8s/README.md | 53 +- internal/verda-cli/cmd/mk8s/cluster_create.go | 15 +- .../verda-cli/cmd/mk8s/cluster_create_test.go | 418 ++---------- internal/verda-cli/cmd/mk8s/cluster_delete.go | 9 +- .../verda-cli/cmd/mk8s/cluster_delete_test.go | 99 +-- .../verda-cli/cmd/mk8s/cluster_describe.go | 10 +- .../cmd/mk8s/cluster_describe_test.go | 259 ++------ internal/verda-cli/cmd/mk8s/cluster_list.go | 5 +- .../verda-cli/cmd/mk8s/cluster_list_test.go | 105 +-- .../verda-cli/cmd/mk8s/cluster_upgrade.go | 10 +- .../cmd/mk8s/cluster_upgrade_test.go | 140 +--- internal/verda-cli/cmd/mk8s/confirm.go | 8 +- internal/verda-cli/cmd/mk8s/errors.go | 29 +- internal/verda-cli/cmd/mk8s/errors_test.go | 277 ++------ internal/verda-cli/cmd/mk8s/fake_test.go | 292 +++++++-- .../verda-cli/cmd/mk8s/kubeconfig_create.go | 32 +- .../cmd/mk8s/kubeconfig_create_test.go | 613 +++++------------- .../verda-cli/cmd/mk8s/kubeconfig_delete.go | 19 +- .../cmd/mk8s/kubeconfig_delete_test.go | 245 +------ .../verda-cli/cmd/mk8s/kubeconfig_describe.go | 8 +- .../cmd/mk8s/kubeconfig_describe_test.go | 325 ++-------- .../verda-cli/cmd/mk8s/kubeconfig_file.go | 47 +- .../cmd/mk8s/kubeconfig_file_test.go | 408 ++---------- .../verda-cli/cmd/mk8s/kubeconfig_list.go | 32 +- .../cmd/mk8s/kubeconfig_list_test.go | 167 +---- .../verda-cli/cmd/mk8s/kubeconfig_prompts.go | 18 +- .../cmd/mk8s/kubeconfig_prompts_test.go | 466 +++---------- .../verda-cli/cmd/mk8s/kubeconfig_save.go | 38 +- .../cmd/mk8s/kubeconfig_save_test.go | 223 ++----- internal/verda-cli/cmd/mk8s/locations.go | 11 +- internal/verda-cli/cmd/mk8s/locations_test.go | 67 +- internal/verda-cli/cmd/mk8s/mk8s.go | 11 +- internal/verda-cli/cmd/mk8s/nodepool.go | 71 +- .../verda-cli/cmd/mk8s/nodepool_create.go | 38 +- .../cmd/mk8s/nodepool_create_test.go | 319 ++------- .../verda-cli/cmd/mk8s/nodepool_delete.go | 19 +- .../cmd/mk8s/nodepool_delete_test.go | 115 +--- .../verda-cli/cmd/mk8s/nodepool_describe.go | 8 +- .../cmd/mk8s/nodepool_describe_test.go | 61 -- internal/verda-cli/cmd/mk8s/nodepool_list.go | 8 +- .../verda-cli/cmd/mk8s/nodepool_list_test.go | 111 +--- internal/verda-cli/cmd/mk8s/nodepool_scale.go | 36 +- .../verda-cli/cmd/mk8s/nodepool_scale_test.go | 444 +++---------- internal/verda-cli/cmd/mk8s/resolve.go | 32 +- internal/verda-cli/cmd/mk8s/resolve_test.go | 206 ++---- .../verda-cli/cmd/mk8s/testhelpers_test.go | 187 ------ internal/verda-cli/cmd/mk8s/views.go | 17 +- internal/verda-cli/cmd/mk8s/wait.go | 68 +- internal/verda-cli/cmd/mk8s/wait_test.go | 138 ++-- internal/verda-cli/cmd/mk8s/wizard_cache.go | 25 +- internal/verda-cli/cmd/mk8s/wizard_cluster.go | 138 ++-- .../verda-cli/cmd/mk8s/wizard_cluster_test.go | 125 +--- .../verda-cli/cmd/mk8s/wizard_nodepool.go | 44 +- .../cmd/mk8s/wizard_nodepool_test.go | 212 +----- .../verda-cli/cmd/util/agent_error_test.go | 7 - internal/verda-cli/cmd/util/redact_test.go | 92 +-- 57 files changed, 1528 insertions(+), 5471 deletions(-) delete mode 100644 internal/verda-cli/cmd/mk8s/testhelpers_test.go diff --git a/internal/verda-cli/cmd/mk8s/CLAUDE.md b/internal/verda-cli/cmd/mk8s/CLAUDE.md index 12e94bf6..ae0727b7 100644 --- a/internal/verda-cli/cmd/mk8s/CLAUDE.md +++ b/internal/verda-cli/cmd/mk8s/CLAUDE.md @@ -6,12 +6,12 @@ - Parent: `verda mk8s` (aliases `k8s`, `kubernetes`), tagged `(experimental)` via `cmdutil.TagAnnotation`, in its own "Kubernetes Commands:" group in `cmd/cmd.go`. No env-var gate: the server's per-project flag controls access. - Verbs: `locations`; `cluster` list/describe/create/upgrade/delete; `node-pool` (`pool`, `np`) list/describe/create/scale/delete; `kubeconfig` list/describe/create/save/delete. One file per verb, plus: - - `resolve.go` -- `clusterTarget`, `resolveCluster`, `apiCall`, pickers' shared constants. - - `nodepool.go` -- `poolScope` (`--cluster` + `-l`, reused by every pool and kubeconfig command), `resolvePool`. + - `resolve.go` -- `clusterTarget`, `resolveCluster`, `apiCall`/`apiRun` (timeout + spinner + `mapErr`), pickers' shared constants. + - `nodepool.go` -- `poolScope` (`--cluster` + `-l`, reused by every pool and kubeconfig command: `missing`/`check` validate, `connect` gets the client and the cluster), `resolvePool`, `poolCmd`. - `confirm.go` -- `checkConfirmed`, `confirmWarning`, `askConfirm`. `errors.go` -- `mapErr`, `createFailed`. - `wait.go` -- `waitFor` and the done conditions. `views.go` -- every JSON/YAML shape. - `kubeconfig_file.go` (file rules), `kubeconfig_save.go` (`--file`/`--force`, recovery command), `kubeconfig_prompts.go` (direct prompts of `kubeconfig create`). - - `wizard_cluster.go`, `wizard_nodepool.go`, `wizard_cache.go` -- the two create wizards. + - `wizard_cluster.go`, `wizard_nodepool.go`, `wizard_cache.go` -- the two create wizards; `runWizard`, the step builders (`textStep`, `selectStep`, `cidrStep`) and `validateName` are in `wizard_cluster.go`. - SDK: `client.ManagedKubernetes` (SDK v1.9.1); the wizards also read `PrivateNetworks`, `InstanceAvailability`, `SSHKeys`. mk8s does not import `cmd/network`. - Help: every `Long` goes through `longDesc`, which puts `experimentalCaution` first. `TestMK8s_HelpStartsWithCaution` checks only the direct children of `mk8s`, not the leaf commands: do not build a `Long` without `longDesc`. Shared help strings (`yesUsage`, `clusterUsage`, `locationUsage`, `waitTimeUsage`) live in `mk8s.go`; reuse them so one rule reads the same everywhere. @@ -19,7 +19,7 @@ - **Names are unique per project.** Cluster and node pool names are unique in the project (pool names too, not only in their cluster). So `resolveCluster` finds a cluster's location with one `List`; with a name and `-l` it makes no call; with no name it shows a picker (filtered by `-l`). No match is a plain `*verda.APIError` 404 (NOT_FOUND via `ClassifyError`); a name in several locations, which cannot happen today, is a `UsageErrorf` asking for `-l`. Every API path needs the location, so all commands go through `clusterTarget`. - **Network and subnet: IDs only, never created.** `cluster create` sends `--network`/`--subnet` as given; the API checks them. The wizard lists networks, then that network's subnets in the chosen location (`subnetLoader` filters by `Location.Code`), and stops with a `stopError` that names `verda network create` / `verda network subnet create` when a list is empty. -- **Creates are sent exactly once.** The API can save an object and still fail the answer (create saves, then reads back; a kubeconfig gets a server-chosen ID), so a retry would give a 409 or a hidden second kubeconfig. `createFailed` + `createMayHaveSucceeded` print "may have been created anyway" (stderr for people, `details.warning` in agent mode) for a 5xx, a 408, a timeout or a network error after the request left. `neverSent` excludes a failed token fetch and a dial error. Some of these are matched on SDK error text (`sdkAuthFailed`, `sdkResponseUnread`, `sdkResponseUnparsed`): an SDK upgrade can change them; `TestCreateMayHaveSucceeded_WhereTheRequestStopped` guards them. +- **Creates are sent exactly once.** The API can save an object and still fail the answer (create saves, then reads back; a kubeconfig gets a server-chosen ID), so a retry would give a 409 or a hidden second kubeconfig. `createFailed` + `createMayHaveSucceeded` print "may have been created anyway" (stderr for people, `details.warning` in agent mode) for a 5xx, a 408, a timeout or a network error after the request left. `neverSent` excludes a failed token fetch and a dial error. Some of these are matched on SDK error text (`sdkAuthFailed`, `sdkResponseUnread`, `sdkResponseUnparsed`): an SDK upgrade can change them; `TestCreateMayHaveSucceeded` guards them. `createFailed` itself has no test. - **SDK retries.** `factory.go` installs `ExponentialBackoffRetryMiddleware`, but in SDK v1.9.1 the request middleware chain wraps a no-op handler and the HTTP call happens after it, so nothing is retried (the comment in `factory.go` says so). An SDK upgrade that makes retries real must keep mk8s creates at one attempt. Nothing guards that today: the unit tests build the client with `newFakeClient`, without the factory's middleware, and there is no mk8s contract test through the real binary. - **One confirmation rule.** Deletes, scale-down and `cluster upgrade`: red warning and confirm on a terminal; elsewhere `--yes` is required, else `CONFIRMATION_REQUIRED`. Creates and scale-up: summary and confirm on a terminal only, never `--yes`. `checkConfirmed` never prompts and runs before `f.VerdaClient()` for deletes and upgrade; scale decides in `nodePoolScaleOptions.confirm`, after it reads the pool, because only then is "down" known. `interactiveTTY` = stdin, stdout and stderr are terminals, not agent mode, table output: `-o json` on a terminal is non-interactive. - **Deletes cascade in the background and have no `--wait`.** The API deletes pools, worker VMs and kubeconfigs after it answers; a cluster or pool GET is 404 at once while VMs are still being deleted, so a wait until 404 would report "completed" while VMs still bill. The CLI does not check for pools before a cluster delete. Named pool and kubeconfig deletes do not read the object first, so one whose read fails stays deletable (`validUntil` then says "until they expire"). @@ -36,7 +36,7 @@ - **Kubeconfig path problems are usage errors.** `kubeconfigTarget` returns an `*inputError` for a taken file, a symlink, a directory, a non-regular file, a missing parent directory and an unwritable directory; the commands convert it with `asUsage` (`UsageErrorf`: VALIDATION_ERROR, exit 2). Failures while creating, writing or renaming stay plain errors. - **Human errors must be plain errors or `cmdutil.UsageErrorf`.** `main` prints any `*AgentError` as a JSON envelope even without `--agent`. Missing flags (`NewMissingFlagsError`) and `CONFIRMATION_REQUIRED` (`confirmationRequired`: one text for scripts, no terminal, agent mode and `-o json|yaml`) are AgentErrors on purpose; everything else returns `*verda.APIError`, `UsageErrorf` or `fmt.Errorf`, which `ClassifyError` still maps for agents. Only `createFailed` and `kubeconfigRecovery` build an AgentError, and only in agent mode. -- **Validate input before `f.VerdaClient()`** (`checkClusterArg`, `checkPoolTarget`, `checkKubeconfigTarget`, `missing()`), so bad input fails the same with or without credentials. A blank `--cluster` or positional is always rejected, even on a terminal. +- **Validate input before `f.VerdaClient()`** (`checkClusterArg`, `poolScope.check`/`missing`, the commands' own `missing`), so bad input fails the same with or without credentials. A blank `--cluster` or positional is always rejected, even on a terminal. - **The kubeconfig YAML holds a client private key.** - `KubeconfigView` has no YAML field; never marshal `verda.ManagedKubernetesKubeconfig` to output or `DebugJSON`. `listKubeconfigViews`/`getKubeconfigView` convert inside the API call so the SDK struct never escapes. The last read of `waitForKubeconfig` holds the YAML: keep it out of every printer. - `cmdutil` `debugBody` prints `` for any `/kubeconfigs` path, and `sensitiveJSONFieldRe` lists `kubeconfig` (`redact_test.go`). Keep both if you touch `factory.go`. @@ -50,18 +50,17 @@ - **`cluster list` width.** The table drops the ENDPOINT column (after stripping `https://`) when a row is wider than `clusterTableWidth` (120); the endpoint holds the project ID, so it usually does. - **Cluster wizard defaults.** The CIDR flags have non-empty defaults, so `podCIDRSet`/`serviceCIDRSet` (from `Flags().Changed`) decide whether the step runs; the steps are not `Required`, so their `Default` applies. - **`cluster describe`** still prints the cluster when `ListNodePools` fails: `NodePools` is a pointer so JSON and YAML both say `null`, with `node_pools_error`. -- **`cluster upgrade`** prints "`describe` may show `degraded` while the rollout runs." on stderr: the API reports the `degraded` phase during a rollout. -- **`make fix.check`** runs `go fix` with the toolchain pinned in the Makefile; use `make fix`, not your local `go fix`. ## Tests -- `fake_test.go` is an in-memory mk8s API on `httptest`: pools and kubeconfigs keyed by cluster name, `failures` ("POST /mk8sclusters" → status/body, `Times` to fail only the first N), `disabled` (403 `feature_not_enabled` everywhere), and hooks `onClusterGet`, `onPoolGet`, `onKubeconfigGet` that move objects through states while a command waits. The hooks run under the fake's lock: read `a.requests` directly, never `requestsMatching`/`paths` inside them. +- `fake_test.go` is an in-memory mk8s API on `httptest` plus every shared helper and fixture (`seededClusters`, `poolAPI`, `setHome`, `readFile`, `assertMode`, `assertNoPrivateKey`, ...): pools and kubeconfigs keyed by cluster name, `failures` ("POST /mk8sclusters" → status/body, every request), `disabled` (403 `feature_not_enabled` everywhere), and hooks `onClusterGet`, `onPoolGet`, `onKubeconfigGet` that move objects through states while a command waits. The hooks run under the fake's lock: read `a.requests` directly, never `requestsMatching`/`paths` inside them. - `TestMain` forces `isInteractiveTerminal` to false, so every test starts off a terminal. `withTerminal`, `fastPoll` (`pollInterval`), `withWizardAnswers` (`newWizardEngine`) and `currentUserName` swap package variables: never call `t.Parallel` in or under a test that uses them. -- Factories: `tableFactory`, `jsonFactory`, `agentFactory`, `yamlFactory`; `runMK8s` runs the command tree. +- Factories: `tableFactory`, `jsonFactory`, `agentFactory`; `runMK8s` runs the command tree. Kubeconfig fixtures (`kcFixtureAPI`, `kcAPI`, `kcPrivateKeyMarker`) are in `kubeconfig_list_test.go` and `kubeconfig_create_test.go`. +- Tests check the API request, the table and JSON output, the `--wait` result and one error per command; the `--yes`/decline refusal paths and the "may have been created" warning are not tested end to end. The security guards are: `TestKubeconfigOutputNeverHasThePrivateKey`, `TestKubeconfigTarget_CreateIsExclusive`, `TestKubeconfigTarget_RejectsSymlink`, `TestNewKubeconfigTarget`, the 0600/0700 checks and `redact_test.go`'s `TestDebugTransport_KubeconfigBodies`. ## Relationships -- `cmdutil` -- `WithSpinner`, `RunWithSpinner`, `Poll`, `WaitOptions`, `DebugJSON`, `WriteStructured`, `ClassifyError`, `NewMissingFlagsError`, `NewMissingOneOfFlagsError`, `NewConfirmationRequiredError`, `UsageErrorf`, `TextColumn`; `factory.go` (`debugBody`, `sensitiveJSONFieldRe`, the retry middleware). +- `cmdutil` -- `WithSpinner`, `Poll`, `WaitOptions`, `DebugJSON`, `WriteStructured`, `ClassifyError`, `NewMissingFlagsError`, `NewMissingOneOfFlagsError`, `NewConfirmationRequiredError`, `UsageErrorf`, `TextColumn`; `factory.go` (`debugBody`, `sensitiveJSONFieldRe`, the retry middleware). - `options.MkdirSecure` -- the 0700 `~/.kube`. - `pkg/tui/wizard` -- the cluster and node pool create flows; `wizardCache` memoizes their lookups. - `cmd/network` -- users create the network and subnet there; no code dependency. diff --git a/internal/verda-cli/cmd/mk8s/README.md b/internal/verda-cli/cmd/mk8s/README.md index c5c233e5..11acbee7 100644 --- a/internal/verda-cli/cmd/mk8s/README.md +++ b/internal/verda-cli/cmd/mk8s/README.md @@ -2,14 +2,7 @@ > Experimental: the managed Kubernetes API may change in incompatible ways. Commands, flags and output may change without a deprecation period. -`verda mk8s` (aliases `k8s`, `kubernetes`) manages clusters, their node pools (the worker VMs) and their kubeconfigs. Flags and all options: `verda mk8s --help`. - -| Command | What it does | -|---|---| -| `locations` | Locations where this project can create clusters | -| `cluster list \| describe \| create \| upgrade \| delete` | Clusters | -| `node-pool list \| describe \| create \| scale \| delete` (aliases `pool`, `np`) | Node pools: workers of one instance type | -| `kubeconfig list \| describe \| create \| save \| delete` | Client certificates for `kubectl` | +`verda mk8s` (aliases `k8s`, `kubernetes`) manages clusters, their node pools (`pool`, `np`: the worker VMs) and their kubeconfigs (client certificates for `kubectl`), plus `locations`. Commands, flags and defaults: `verda mk8s --help`. ## Workflow @@ -33,14 +26,10 @@ verda mk8s node-pool scale workers --cluster prod --replicas 0 --yes verda mk8s cluster delete prod --yes # node pools, worker VMs and kubeconfigs go with it ``` -`cluster list` shows the endpoint without `https://` and drops the ENDPOINT column when a row would be wider than 120 columns; `-o json|yaml` and `cluster describe` always show it in full. - -Cluster and node pool names are unique in the project, so `--cluster prod` is enough: the CLI finds the location with one list call. `-l` gives the location and skips that call. Node pool names must be unique in the project, not only in their cluster. Kubeconfigs are named by an ID that the API chooses (`kubeconfig list`). +Cluster and node pool names are unique in the project (pool names too, not only in their cluster), so `--cluster prod` is enough: the CLI finds the location with one list call; `-l` skips that call. Kubeconfigs are named by an ID that the API chooses. ## Prompts and `--yes` -A command prompts only on a terminal, with table output, without `--agent`. In scripts, agent mode and `-o json|yaml` it never prompts. - | Command | On a terminal | In scripts, agent mode, `-o json\|yaml` | |---|---|---| | `cluster create`, `node-pool create`, `kubeconfig create` | Asks for missing values, shows a summary, asks to confirm | Required flags must be given; no `--yes` needed | @@ -48,46 +37,30 @@ A command prompts only on a terminal, with table output, without `--agent`. In s | `node-pool scale` down (to 0 too), all deletes, `cluster upgrade` | Red warning, asks to confirm (`--yes` skips it) | `--yes` required, else `CONFIRMATION_REQUIRED` | | Any command without its cluster, node pool or kubeconfig | Picker | `--cluster` and the positional target are required | -In the create wizards the instance types are listed smallest first (CPU-only types, then by GPU count), because the availability data has no prices. `kubeconfig create` checks `--duration` (at least `1h`) when you type it. - ## Waiting -Changes run in the background after the API accepts them. Without `--wait` a command reports `accepted`; with `--wait` it polls and reports `completed`. - -| Command | Done when | `--wait-timeout` default | -|---|---|---| -| `cluster create --wait` | phase `running` and an endpoint is set | 15m | -| `node-pool create --wait` | state `active`, all requested workers ready | 15m | -| `node-pool scale --wait` | state `active`, new count reached and ready | 15m | -| `kubeconfig create` (always waits) | status `ready` | 3m | +Without `--wait` a command reports `accepted`; with `--wait` (`kubeconfig create` always waits) it polls and reports `completed`. - A timeout exits non-zero; the change keeps going in the background. Ctrl+C stops only the wait. -- A node pool keeps trying to create workers after a timeout, and workers that exist bill even before they join. Stop a create with `node-pool delete`, a scale-up with `node-pool scale` back to the previous count (the printed advice includes `--yes`, because that is a scale-down). -- `node-pool scale` to the current count sends nothing: "already has N replicas; nothing to do." ("1 replica" for one), status `unchanged` in `-o json|yaml`. With `--wait`, a pool that is not yet active with all workers ready is waited for instead (status `completed`, or a timeout), so a retry after a scale-up timeout does not report success early. -- Deletes and `cluster upgrade` have no `--wait`. A deleted cluster or pool leaves the lists at once while its VMs are still being deleted. The API reports the requested Kubernetes version, not the running one: follow an upgrade with `cluster describe`. +- After a node pool timeout the backend keeps creating workers, which bill even before they join: the error says how to stop (delete the pool, or scale back with `--yes`). +- `node-pool scale` to the current count sends nothing (status `unchanged`); with `--wait` it still waits for a pool that is not yet settled. +- Deletes and `cluster upgrade` have no `--wait`: the VMs of a deleted cluster or pool are deleted in the background, and the API reports the requested version, not the running one. ## Kubeconfigs The kubeconfig YAML holds a client private key. -- It is written only to a file with mode 0600: by default `~/.kube/verda-..yaml` (`~/.kube` is created with mode 0700). Never to `-o json|yaml`, `describe` or `--debug`. -- `kubeconfig create` checks the file before it issues a certificate. An existing file stops it unless `--force`; a symlink or a directory is always refused. -- `--file -` writes only the YAML to stdout, for a pipe. It cannot be combined with `--agent` or `-o json|yaml`. A file you redirect it to gets your umask, not 0600. -- In scripts, give `--common-name` and `--admin` (group `system:masters`, full control) and/or `--group` (repeatable). -- Certificates cannot be revoked before they expire (90 days unless `--duration`, at least `1h`): use a short `--duration` for admin credentials. `kubeconfig delete` does not disable copies already downloaded, which keep working until they expire. Deleting the cluster invalidates them. A copy that does not use `system:masters` can also be cut off by removing its group's RBAC binding in the cluster. -- `kubeconfig create --force` replaces the file but not the earlier kubeconfig on the server, which is a different, still valid certificate: the CLI says so on stderr. Run `kubeconfig list` and delete what you no longer need. `kubeconfig save --force` issues no certificate and prints no such hint. -- If `kubeconfig create` stops before the kubeconfig is ready, or cannot write the file, the error gives the kubeconfig ID and the `verda mk8s kubeconfig save ...` command to run. Running `create` again issues another certificate. +- It is written only to a file with mode 0600 (default `~/.kube/verda-..yaml`, `~/.kube` created 0700), or to stdout with `--file -`; never to `-o json|yaml`, `describe` or `--debug`. The file is checked before a certificate is issued; a symlink or a directory is always refused. +- Certificates cannot be revoked before they expire (90 days unless `--duration`): `kubeconfig delete` does not disable downloaded copies; deleting the cluster does. `kubeconfig create --force` leaves the earlier certificate valid on the server (the CLI says so). +- If `kubeconfig create` stops before the kubeconfig is ready, or cannot write the file, the error gives the ID and the `kubeconfig save` command to run. Running `create` again issues another certificate. ## When a create fails -A create is sent once and never retried. If `cluster create`, `node-pool create` or `kubeconfig create` fails with a 5xx, a 408, a timeout or a network error, the object may exist anyway: the CLI says so on stderr (agent mode: in `error.details.warning`). Check the matching `list` before you run `create` again. A 4xx means nothing was created. +A create is sent once and never retried. After a 5xx, a 408, a timeout or a network error the object may exist anyway: the CLI says so (agent mode: `error.details.warning`). Check `list` before you run `create` again. ## Agent mode -- Missing flag or target: `MISSING_REQUIRED_FLAGS` (exit 2). `kubeconfig create` without `--admin` or `--group` lists them in `details.one_of`. -- Delete, scale-down or upgrade without `--yes`: `CONFIRMATION_REQUIRED` (exit 2). -- Bad flag values or combinations (`--file -` with `--agent`, `--wait --wait-timeout 0`, negative `--replicas`) and a kubeconfig file that cannot be used (exists without `--force`, symlink, directory, missing or read-only directory): `VALIDATION_ERROR` (exit 2). -- Unknown cluster name: `NOT_FOUND` (exit 5). Other API errors show the API message: 400 and 409 are `API_ERROR` (exit 4). -- Managed Kubernetes not enabled for the project: `AUTH_ERROR` (exit 3) with "Managed Kubernetes is not enabled for this project." -- Results: creates `{"status": "accepted"|"completed", "cluster"|"pool": {...}}`; `node-pool scale` adds `previous_replicas` (and can be `"unchanged"`), `cluster upgrade` adds `previous_version`; deletes `{"resource", "name", "location", "action": "delete", "status": "accepted"}` (`cluster` and, for kubeconfigs, `id` too); kubeconfig `create`/`save` print the metadata with `file`, never the YAML. When a `--wait` fails, stdout still has the result, with `wait_error`, and the command exits non-zero. +- `MISSING_REQUIRED_FLAGS` (exit 2) for a missing flag or target (`kubeconfig create` lists `--admin`/`--group` in `details.one_of`); `CONFIRMATION_REQUIRED` (exit 2) without `--yes`; `VALIDATION_ERROR` (exit 2) for bad values and unusable kubeconfig files. +- Unknown cluster: `NOT_FOUND` (exit 5); other API errors keep the API message (`API_ERROR`, exit 4). Not enabled for the project: `AUTH_ERROR` (exit 3). +- Results: creates `{"status", "cluster"|"pool"}`, scale adds `previous_replicas`, upgrade `previous_version`; deletes `{"resource", "name", "location", "action", "status"}`; kubeconfig `create`/`save` print the metadata with `file`, never the YAML. A failed `--wait` still writes the result, with `wait_error`, and exits non-zero. - A failed `kubeconfig create` wait or write adds `kubeconfig_id`, `save_command` and `warning` to `error.details`. diff --git a/internal/verda-cli/cmd/mk8s/cluster_create.go b/internal/verda-cli/cmd/mk8s/cluster_create.go index 1298d445..720c9aef 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_create.go +++ b/internal/verda-cli/cmd/mk8s/cluster_create.go @@ -16,7 +16,6 @@ package mk8s import ( "context" - "errors" "fmt" "strings" "time" @@ -52,7 +51,7 @@ type clusterCreateOptions struct { ServiceCIDR string Wait cmdutil.WaitOptions - // The CIDR flags have defaults, so the wizard needs to know if the user set them. + // The CIDR flags have defaults, so the wizard needs Changed. podCIDRSet, serviceCIDRSet bool } @@ -133,7 +132,6 @@ func (o *clusterCreateOptions) trim() { } } -// missing lists the required flags that are empty. func (o *clusterCreateOptions) missing() []string { var m []string for _, r := range []struct{ flag, value string }{ @@ -176,10 +174,7 @@ func runClusterCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.I } if len(missing) > 0 { flow := buildClusterCreateFlow(opts, newWizardCache(f, client)) - if err := newWizardEngine(f, ioStreams).Run(cmd.Context(), flow); err != nil { - if stop, ok := errors.AsType[*stopError](err); ok { - return stop - } + if err := runWizard(cmd.Context(), f, ioStreams, flow); err != nil { return err } opts.trim() @@ -194,9 +189,7 @@ func runClusterCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.I } cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - cluster, err := cmdutil.WithSpinner(ctx, f.Status(), "Creating cluster...", func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { + cluster, err := apiCall(cmd.Context(), f, "Creating cluster...", func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { return client.ManagedKubernetes.Create(ctx, req) }) if err != nil { @@ -223,8 +216,6 @@ func confirmClusterCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdu return askConfirm(ctx, ioStreams, f.Prompter(), "Create cluster "+req.Name+"?") } -// waitForCluster polls until the cluster is ready and updates result. Its -// error says what was last seen and how to check later. func waitForCluster(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, created *verda.ManagedKubernetesCluster, timeout time.Duration, result *ClusterCreateResult) error { name, location := created.Name, created.Location if f.OutputFormat() == outputTable { diff --git a/internal/verda-cli/cmd/mk8s/cluster_create_test.go b/internal/verda-cli/cmd/mk8s/cluster_create_test.go index 244f4b84..67419700 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_create_test.go +++ b/internal/verda-cli/cmd/mk8s/cluster_create_test.go @@ -17,9 +17,7 @@ package mk8s import ( "slices" "strings" - "sync/atomic" "testing" - "time" "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" @@ -35,64 +33,42 @@ var createArgs = []string{ func withArgs(extra ...string) []string { return append(slices.Clone(createArgs), extra...) } -// fastPoll shortens the wait loop. Never call t.Parallel in or under a test that -// uses it, withTerminal or withWizardAnswers: they change package variables. -func fastPoll(t *testing.T) { - t.Helper() - orig := pollInterval - pollInterval = 5 * time.Millisecond - t.Cleanup(func() { pollInterval = orig }) -} - +// The IDs go through unchecked: no network, subnet or cluster read happens. func TestClusterCreate_FlagsSendExactlyTheGivenIDs(t *testing.T) { t.Parallel() - api := newFakeAPI() - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), createArgs...) - if err != nil { - t.Fatal(err) - } - - got := decodeJSON[map[string]any](t, out) - if got["status"] != "accepted" { - t.Errorf("status = %v", got["status"]) - } - if c, _ := got["cluster"].(map[string]any); c["name"] != "prod" || c["phase"] != "provisioning" { - t.Errorf("cluster = %v", got["cluster"]) - } - - reqs := api.requestsMatching("POST") - if len(reqs) != 1 || reqs[0].Path != "/mk8sclusters" { - t.Fatalf("POST requests = %v", reqs) - } - body := decodeJSON[map[string]string](t, reqs[0].Body) - want := map[string]string{ - "name": "prod", "location": "FIN-03", "kubernetes_version": "1.37.0", - "network": "pn-123", "subnet": "sn-456", "pod_cidr": "10.100.0.0/16", "service_cidr": "10.200.0.0/16", - } - for k, v := range want { - if body[k] != v { - t.Errorf("body[%s] = %q, want %q", k, body[k], v) + for name, tc := range map[string]struct { + extra []string + pod, svc string + }{ + "default ranges": {nil, "10.100.0.0/16", "10.200.0.0/16"}, + "custom ranges": {[]string{"--pod-cidr", "10.50.0.0/16", "--service-cidr", "10.60.0.0/16"}, "10.50.0.0/16", "10.60.0.0/16"}, + } { + api := newFakeAPI() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withArgs(tc.extra...)...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + got := decodeJSON[map[string]any](t, out) + if c, _ := got["cluster"].(map[string]any); got["status"] != "accepted" || c["name"] != "prod" || c["phase"] != "provisioning" { + t.Errorf("%s: result = %v", name, got) + } + reqs := api.requestsMatching("POST") + if len(reqs) != 1 || reqs[0].Path != "/mk8sclusters" { + t.Fatalf("%s: POST requests = %v", name, reqs) + } + body := decodeJSON[map[string]string](t, reqs[0].Body) + want := map[string]string{ + "name": "prod", "location": "FIN-03", "kubernetes_version": "1.37.0", + "network": "pn-123", "subnet": "sn-456", "pod_cidr": tc.pod, "service_cidr": tc.svc, + } + for k, v := range want { + if body[k] != v { + t.Errorf("%s: body[%s] = %q, want %q", name, k, body[k], v) + } + } + if all := api.paths("GET"); len(all) != 0 { + t.Errorf("%s: unexpected reads: %v", name, all) } - } - // IDs go through unchecked: no network, subnet or cluster read happens. - if all := api.paths("GET"); len(all) != 0 { - t.Errorf("unexpected reads: %v", all) - } -} - -func TestClusterCreate_CustomCIDRsAndTrimming(t *testing.T) { - t.Parallel() - api := newFakeAPI() - args := []string{ - "cluster", "create", "--name", " prod ", "-l", "FIN-03", "--kubernetes-version", "1.37.0", - "--network", " pn-123 ", "--subnet", "sn-456", "--pod-cidr", "10.50.0.0/16", "--service-cidr", "10.60.0.0/16", - } - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); err != nil { - t.Fatal(err) - } - body := decodeJSON[map[string]string](t, api.requestsMatching("POST")[0].Body) - if body["name"] != "prod" || body["network"] != "pn-123" || body["pod_cidr"] != "10.50.0.0/16" || body["service_cidr"] != "10.60.0.0/16" { - t.Errorf("body = %v", body) } } @@ -112,162 +88,58 @@ func TestClusterCreate_TableSaysAccepted(t *testing.T) { func TestClusterCreate_MissingFlagsOffTerminal(t *testing.T) { t.Parallel() - for name, f := range map[string]*cmdutil.TestFactory{ - "table": tableFactory(newFakeClient(t, newFakeAPI()), nil), - "agent": agentFactory(newFakeClient(t, newFakeAPI())), - } { - _, _, err := runMK8s(t, f, "cluster", "create") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("%s: err = %v", name, err) - } - missing, _ := ae.Details["missing"].([]string) - want := []string{"--name", "--location", "--kubernetes-version", "--network", "--subnet"} - if !slices.Equal(missing, want) { - t.Errorf("%s: missing = %v, want %v", name, missing, want) - } - } - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, newFakeAPI())), "cluster", "create", "--name", "prod", "-l", "FIN-03") - missing, _ := cmdutil.ClassifyError(err).Details["missing"].([]string) - if !slices.Equal(missing, []string{"--kubernetes-version", "--network", "--subnet"}) { - t.Errorf("only the missing flags are listed: %v", missing) - } -} - -func TestClusterCreate_SDKValidationHappensBeforeAnyRequest(t *testing.T) { - t.Parallel() - api := newFakeAPI() - args := slices.Clone(createArgs) - args[slices.Index(args, "--name")+1] = "Not_A_Label" - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...) - if err == nil || !strings.Contains(strings.ToLower(err.Error()), "name") { - t.Fatalf("err = %v", err) - } - if n := len(api.requestsMatching("POST")); n != 0 { - t.Errorf("%d POST(s) sent for an invalid name", n) - } - if got := createMayHaveSucceeded(err); got { - t.Error("a validation error must not claim the cluster may exist") + ae := cmdutil.ClassifyError(err) + missing, _ := ae.Details["missing"].([]string) + if ae.Code != "MISSING_REQUIRED_FLAGS" || !slices.Equal(missing, []string{"--kubernetes-version", "--network", "--subnet"}) { + t.Errorf("err = %v, missing = %v", err, missing) } } -func TestClusterCreate_FailureWarnings(t *testing.T) { - t.Parallel() - const route = "POST /mk8sclusters" - - t.Run("502 warns, once, in human mode", func(t *testing.T) { - t.Parallel() +func TestClusterCreate_WaitCompletesOnRunningWithEndpoint(t *testing.T) { + fastPoll(t) + for name, mk := range map[string]func(*fakeAPI) *cmdutil.TestFactory{ + "table": func(api *fakeAPI) *cmdutil.TestFactory { return tableFactory(newFakeClient(t, api), nil) }, + "json": func(api *fakeAPI) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, + } { api := newFakeAPI() - api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), createArgs...) - if err == nil || !strings.Contains(err.Error(), "upstream failed") { - t.Fatalf("err = %v", err) + api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + c.ExternalEndpoint = "https://prod.example.test:6443" } - if !strings.Contains(errOut, "The cluster may have been created anyway.") || !strings.Contains(errOut, "verda mk8s cluster list") { - t.Errorf("stderr = %q", errOut) + out, errOut, err := runMK8s(t, mk(api), withArgs("--wait")...) + if err != nil { + t.Fatalf("%s: %v", name, err) } if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("a create must be sent once, got %d POSTs", n) + t.Errorf("%s: POSTs = %d", name, n) } - }) - - t.Run("502 warns in agent details", func(t *testing.T) { - t.Parallel() - api := newFakeAPI() - api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} - _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), createArgs...) - ae := cmdutil.ClassifyError(err) - if w, _ := ae.Details["warning"].(string); !strings.Contains(w, "may have been created anyway") { - t.Errorf("details = %v", ae.Details) - } - if errOut != "" { - t.Errorf("agent mode reports through the error only: %q", errOut) - } - }) - - t.Run("400 does not warn", func(t *testing.T) { - t.Parallel() - api := newFakeAPI() - api.failures[route] = apiFailure{Status: 400, Body: `{"code":"bad_request","message":"quota","details":"cluster_quota_exceeded"}`} - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), createArgs...) - if err == nil || !strings.Contains(err.Error(), "cluster_quota_exceeded") { - t.Fatalf("the API reason must be shown: %v", err) - } - if errOut != "" { - t.Errorf("unexpected warning: %q", errOut) - } - }) - - t.Run("feature not enabled", func(t *testing.T) { - t.Parallel() - api := newFakeAPI() - api.disabled = true - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), createArgs...) - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Fatalf("err = %v", err) + if name == "table" { + if !strings.Contains(out, "Cluster prod completed (FIN-03)") || !strings.Contains(out, "https://prod.example.test:6443") { + t.Errorf("stdout = %q", out) + } + continue } - if errOut != "" { - t.Errorf("a 403 never warns: %q", errOut) + got := decodeJSON[map[string]any](t, out) + if got["status"] != "completed" || got["cluster"].(map[string]any)["external_endpoint"] != "https://prod.example.test:6443" { + t.Errorf("result = %v", got) } - }) -} - -func TestClusterCreate_WaitCompletesOnRunningWithEndpoint(t *testing.T) { - fastPoll(t) - api := newFakeAPI() - var gets atomic.Int32 - api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { - switch gets.Add(1) { - case 1: // still provisioning - case 2: // running, but the endpoint is not published yet: not done - c.Phase = verda.ManagedKubernetesClusterPhaseRunning - default: - c.Phase = verda.ManagedKubernetesClusterPhaseRunning - c.ExternalEndpoint = "https://prod.example.test:6443" + if _, has := got["wait_error"]; has || errOut != "" { + t.Errorf("no wait_error and a quiet stderr off a terminal: %v %q", got, errOut) } } - - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait", "--wait-timeout", "30s")...) - if err != nil { - t.Fatal(err) - } - if !strings.Contains(out, "Cluster prod completed (FIN-03)") || !strings.Contains(out, "https://prod.example.test:6443") { - t.Errorf("stdout = %q", out) - } - if got := gets.Load(); got != 3 { - t.Errorf("reads = %d, want 3: running without an endpoint must not end the wait", got) - } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d", n) - } - - api2 := newFakeAPI() - api2.onClusterGet = func(c *verda.ManagedKubernetesCluster) { - c.Phase = verda.ManagedKubernetesClusterPhaseRunning - c.ExternalEndpoint = "https://prod.example.test:6443" - } - out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api2)), withArgs("--wait")...) - if err != nil { - t.Fatal(err) - } - got := decodeJSON[map[string]any](t, out) - if got["status"] != "completed" || got["cluster"].(map[string]any)["external_endpoint"] != "https://prod.example.test:6443" { - t.Errorf("result = %v", got) - } - if _, has := got["wait_error"]; has { - t.Error("no wait_error on success") - } } func TestClusterCreate_WaitTimeout(t *testing.T) { fastPoll(t) - api := newFakeAPI() // stays provisioning forever + api := newFakeAPI() + api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { c.Phase = verda.ManagedKubernetesClusterPhaseDegraded } out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withArgs("--wait", "--wait-timeout", "300ms")...) if err == nil { t.Fatal("a timeout must fail") } - for _, want := range []string{"timed out after 300ms", "cluster prod in FIN-03", "last phase: provisioning", "verda mk8s cluster describe prod -l FIN-03"} { + // The last phase is the one read, not the one from the create answer. + for _, want := range []string{"timed out after 300ms", "cluster prod in FIN-03", "last phase: degraded", "verda mk8s cluster describe prod -l FIN-03"} { if !strings.Contains(err.Error(), want) { t.Errorf("error misses %q: %v", want, err) } @@ -278,123 +150,11 @@ func TestClusterCreate_WaitTimeout(t *testing.T) { } } -const clusterGetRoute = "GET /mk8sclusters/{loc}/{name}" - -func TestClusterCreate_WaitReadFailures(t *testing.T) { - fastPoll(t) - - t.Run("reads that keep failing end the wait and say the cluster was accepted", func(t *testing.T) { - api := newFakeAPI() - api.failures[clusterGetRoute] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...) - if err == nil || !strings.Contains(err.Error(), "was accepted, but --wait could not read it") || !strings.Contains(err.Error(), "read failed") { - t.Fatalf("err = %v", err) - } - if n := len(api.paths("GET")); n != maxReadFailures { - t.Errorf("reads = %d, want %d in a row before giving up", n, maxReadFailures) - } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d", n) - } - }) - - t.Run("a few failed reads do not fail the wait", func(t *testing.T) { - api := newFakeAPI() - api.failures[clusterGetRoute] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"lb"}`, Times: maxReadFailures - 1} - api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { - c.Phase = verda.ManagedKubernetesClusterPhaseRunning - c.ExternalEndpoint = "https://prod.example.test:6443" - } - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...) - if err != nil || !strings.Contains(out, "completed") { - t.Fatalf("out=%q err=%v", out, err) - } - }) - - t.Run("a read the API refuses ends the wait at once", func(t *testing.T) { - api := newFakeAPI() - api.failures[clusterGetRoute] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access"}`} - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...) - if err == nil || !strings.Contains(err.Error(), "no access") { - t.Fatalf("err = %v", err) - } - if n := len(api.paths("GET")); n != 1 { - t.Errorf("reads = %d, want 1", n) - } - }) - - t.Run("a stale 404 right after the create is tolerated", func(t *testing.T) { - api := newFakeAPI() - api.failures[clusterGetRoute] = apiFailure{Status: 404, Body: `{"code":"not_found","message":"cluster not found"}`, Times: maxReadFailures + 2} - api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { - c.Phase = verda.ManagedKubernetesClusterPhaseRunning - c.ExternalEndpoint = "https://prod.example.test:6443" - } - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait")...); err != nil { - t.Fatal(err) - } - }) -} - -func TestClusterCreate_WaitTimeoutReportsTheLastPhaseSeen(t *testing.T) { - fastPoll(t) - api := newFakeAPI() - api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { c.Phase = verda.ManagedKubernetesClusterPhaseDegraded } - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withArgs("--wait", "--wait-timeout", "300ms")...) - if err == nil || !strings.Contains(err.Error(), "last phase: degraded") { - t.Fatalf("err = %v, want the last phase seen, not the one from the create answer", err) - } -} - -func TestClusterCreate_WaitKeepsStderrQuietOffTerminal(t *testing.T) { - fastPoll(t) - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - } { - api := newFakeAPI() - api.onClusterGet = func(c *verda.ManagedKubernetesCluster) { - c.Phase = verda.ManagedKubernetesClusterPhaseRunning - c.ExternalEndpoint = "https://prod.example.test:6443" - } - _, errOut, err := runMK8s(t, mk(api, t), withArgs("--wait")...) - if err != nil || errOut != "" { - t.Errorf("%s: err=%v stderr=%q", name, err, errOut) - } - } -} - -func TestClusterCreate_FlagValuesAreTrimmed(t *testing.T) { - t.Parallel() - api := newFakeAPI() - args := []string{ - "cluster", "create", "--name", " prod ", "-l", " FIN-03 ", "--kubernetes-version", " 1.37.0 ", - "--network", " pn-123 ", "--subnet", " sn-456 ", "--pod-cidr", " 10.50.0.0/16 ", "--service-cidr", " 10.60.0.0/16 ", - } - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); err != nil { - t.Fatal(err) - } - for k, v := range decodeJSON[map[string]string](t, api.requestsMatching("POST")[0].Body) { - if strings.TrimSpace(v) != v { - t.Errorf("body[%s] = %q was not trimmed", k, v) - } - } -} - -func TestClusterCreate_WaitTimeoutMustBePositive(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, newFakeAPI()), nil), withArgs("--wait", "--wait-timeout", "0s")...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { - t.Fatalf("err = %v: want a usage error (an AgentError would print as JSON for a person)", err) - } -} - func TestClusterCreate_InteractiveSummaryAndConfirm(t *testing.T) { withTerminal(t) - - t.Run("confirmed", func(t *testing.T) { + for _, confirm := range []bool{true, false} { api := newFakeAPI() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), createArgs...) + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(confirm)), createArgs...) if err != nil { t.Fatal(err) } @@ -403,48 +163,8 @@ func TestClusterCreate_InteractiveSummaryAndConfirm(t *testing.T) { t.Errorf("summary misses %q: %s", want, errOut) } } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d", n) - } - }) - - t.Run("declined, Esc and Ctrl+C create nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "declined": {Prompter: tuitest.New().AddConfirm(false)}, - "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, - } { - api := newFakeAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - _, errOut, err := runMK8s(t, f, createArgs...) - if err != nil || !strings.Contains(errOut, canceledMsg) { - t.Errorf("%s: err=%v stderr=%q", name, err, errOut) - } - if n := len(api.requestsMatching("POST")); n != 0 { - t.Errorf("%s: %d POST(s)", name, n) - } - } - }) - - t.Run("agent mode never prompts", func(t *testing.T) { - api := newFakeAPI() - // No prompter: a prompt would fail the test. - if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), createArgs...); err != nil { - t.Fatal(err) - } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d", n) - } - }) - - t.Run("json output never prompts", func(t *testing.T) { - api := newFakeAPI() - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), createArgs...); err != nil { - t.Fatal(err) - } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d", n) + if want, n := map[bool]int{true: 1, false: 0}[confirm], len(api.requestsMatching("POST")); n != want { + t.Errorf("confirm=%v: POSTs = %d, want %d", confirm, n, want) } - }) + } } diff --git a/internal/verda-cli/cmd/mk8s/cluster_delete.go b/internal/verda-cli/cmd/mk8s/cluster_delete.go index 875f9a56..3c9a8d28 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_delete.go +++ b/internal/verda-cli/cmd/mk8s/cluster_delete.go @@ -89,13 +89,10 @@ func runClusterDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.I } cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Delete cluster:", map[string]string{"name": target.Name, "location": target.Location}) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - err = cmdutil.RunWithSpinner(ctx, f.Status(), "Deleting cluster...", func(ctx context.Context) error { + if err := apiRun(cmd.Context(), f, "Deleting cluster...", func(ctx context.Context) error { return client.ManagedKubernetes.Delete(ctx, target.Location, target.Name) - }) - if err != nil { - return mapErr(err) + }); err != nil { + return err } result := ActionResult{Resource: kindCluster, Name: target.Name, Location: target.Location, Action: "delete", Status: statusAccepted} diff --git a/internal/verda-cli/cmd/mk8s/cluster_delete_test.go b/internal/verda-cli/cmd/mk8s/cluster_delete_test.go index 22b5855d..b7fad0f6 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_delete_test.go +++ b/internal/verda-cli/cmd/mk8s/cluster_delete_test.go @@ -69,28 +69,6 @@ func TestClusterDelete_Structured(t *testing.T) { } } -func TestClusterDelete_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { - t.Parallel() - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { - return tableFactory(newFakeClient(t, api), nil) - }, - } { - api := seededClusters() - _, _, err := runMK8s(t, mk(api, t), "cluster", "delete", "prod") - if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { - t.Errorf("%s: err = %v", name, err) - } else if !strings.Contains(ae.Message, "without a terminal, in agent mode or with -o json|yaml") { - t.Errorf("%s: the message must name every cause: %q", name, ae.Message) - } - if n := len(api.requests); n != 0 { - t.Errorf("%s: %d request(s) before the check", name, n) - } - } -} - func TestClusterDelete_MissingCluster(t *testing.T) { t.Parallel() _, _, err := runMK8s(t, agentFactory(newFakeClient(t, seededClusters())), "cluster", "delete", "--yes") @@ -101,76 +79,27 @@ func TestClusterDelete_MissingCluster(t *testing.T) { func TestClusterDelete_Interactive(t *testing.T) { withTerminal(t) - - t.Run("the warning names pools, worker VMs and kubeconfigs", func(t *testing.T) { + for name, tc := range map[string]struct { + prompter *tuitest.Prompter + args []string + warning bool + }{ + "confirmed": {tuitest.New().AddConfirm(true), nil, true}, + // No prompter answers: a confirm would fail the test. + "--yes skips the confirmation": {tuitest.New(), []string{"--yes"}, false}, + } { api := seededClusters() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), "cluster", "delete", "prod") + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tc.prompter), append([]string{"cluster", "delete", "prod"}, tc.args...)...) if err != nil { - t.Fatal(err) + t.Fatalf("%s: %v", name, err) } for _, want := range []string{"node pools", "worker VMs", "kubeconfigs", "Downloaded kubeconfigs stop working", "cannot be undone"} { - if !strings.Contains(errOut, want) { - t.Errorf("warning misses %q: %s", want, errOut) - } - } - if n := len(api.requestsMatching("DELETE")); n != 1 { - t.Errorf("DELETEs = %d", n) - } - }) - - t.Run("picker then confirm deletes the chosen cluster", func(t *testing.T) { - api := seededClusters() - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddSelect(1).AddConfirm(true)), "cluster", "delete"); err != nil { - t.Fatal(err) - } - dels := api.requestsMatching("DELETE") - if len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-01/staging" { - t.Errorf("DELETE requests = %v", dels) - } - }) - - t.Run("declined, Esc and Ctrl+C delete nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "declined": {Prompter: tuitest.New().AddConfirm(false)}, - "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, - } { - api := seededClusters() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - _, errOut, err := runMK8s(t, f, "cluster", "delete", "prod") - if err != nil || !strings.Contains(errOut, canceledMsg) { - t.Errorf("%s: err=%v stderr=%q", name, err, errOut) - } - if n := len(api.requestsMatching("DELETE")); n != 0 { - t.Errorf("%s: %d DELETE(s)", name, n) + if strings.Contains(errOut, want) != tc.warning { + t.Errorf("%s: warning and %q: %s", name, want, errOut) } } - }) - - t.Run("--yes skips the confirmation", func(t *testing.T) { - api := seededClusters() - // No prompter answers: a confirm would fail the test. - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), "cluster", "delete", "prod", "--yes"); err != nil { - t.Fatal(err) - } if n := len(api.requestsMatching("DELETE")); n != 1 { - t.Errorf("DELETEs = %d", n) + t.Errorf("%s: DELETEs = %d", name, n) } - }) -} - -func TestClusterDelete_Errors(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "delete", "ghost", "--yes") - if agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown cluster: %v", err) - } - - api := newFakeAPI() - api.disabled = true - _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "delete", "prod", "-l", "FIN-03", "--yes") - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Errorf("not enabled: %v", err) } } diff --git a/internal/verda-cli/cmd/mk8s/cluster_describe.go b/internal/verda-cli/cmd/mk8s/cluster_describe.go index c8e6b8b8..f6cb3a1c 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_describe.go +++ b/internal/verda-cli/cmd/mk8s/cluster_describe.go @@ -15,13 +15,11 @@ package mk8s import ( - "context" "fmt" "io" "text/tabwriter" "github.com/spf13/cobra" - "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -79,13 +77,9 @@ func runClusterDescribe(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil } view := ClusterDescribeView{ClusterView: newClusterView(cluster)} - poolCtx, poolCancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer poolCancel() - pools, poolErr := cmdutil.WithSpinner(poolCtx, f.Status(), "Loading node pools...", func(ctx context.Context) ([]verda.ManagedKubernetesNodePool, error) { - return client.ManagedKubernetes.ListNodePools(ctx, target.Location, target.Name) - }) + pools, poolErr := listPools(cmd.Context(), f, client, target) if poolErr != nil { - view.NodePoolsError = mapErr(poolErr).Error() + view.NodePoolsError = poolErr.Error() _, _ = fmt.Fprintf(ioStreams.ErrOut, "Warning: could not load the node pools of cluster %s: %s\n", target.Name, view.NodePoolsError) } else { views := newPoolSummaryViews(pools) diff --git a/internal/verda-cli/cmd/mk8s/cluster_describe_test.go b/internal/verda-cli/cmd/mk8s/cluster_describe_test.go index fe5cf110..8c9f760d 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_describe_test.go +++ b/internal/verda-cli/cmd/mk8s/cluster_describe_test.go @@ -15,13 +15,10 @@ package mk8s import ( - "reflect" "regexp" "slices" "strings" "testing" - - "go.yaml.in/yaml/v3" ) func describedAPI() *fakeAPI { @@ -31,6 +28,9 @@ func describedAPI() *fakeAPI { return api } +var describeLine = regexp.MustCompile(`^([A-Za-z][A-Za-z ]*):\s+(\S.*)$`) + +// The name is found with one List, then the cluster and its pools are read. func TestClusterDescribe_Table(t *testing.T) { t.Parallel() api := describedAPI() @@ -38,234 +38,93 @@ func TestClusterDescribe_Table(t *testing.T) { if err != nil { t.Fatal(err) } - for _, want := range []string{ - "Name:", "prod", "Location:", "FIN-03", "Version:", "1.37.0", "Phase:", "running", - "https://prod.example.test:6443", "pn-1", "sn-1", "10.100.0.0/16", "10.200.0.0/16", - "Conditions:", "TYPE", "Ready", "Node pools:", "INSTANCE TYPE", "READY/DESIRED", "workers", "2/3", "active", - } { - if !strings.Contains(out, want) { - t.Errorf("output misses %q:\n%s", want, out) - } - } -} - -func TestClusterDescribe_OneListThenGetThenPools(t *testing.T) { - t.Parallel() - api := describedAPI() - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod"); err != nil { - t.Fatal(err) - } want := []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod", "/mk8sclusters/FIN-03/prod/pools"} if got := api.paths("GET"); !slices.Equal(got, want) { t.Errorf("requests = %v, want %v", got, want) } -} - -func TestClusterDescribe_Structured(t *testing.T) { - t.Parallel() - for name, f := range map[string]func(t *testing.T, api *fakeAPI) (string, error){ - "json": func(t *testing.T, api *fakeAPI) (string, error) { - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "cluster", "describe", "prod") - return out, err - }, - "agent": func(t *testing.T, api *fakeAPI) (string, error) { - out, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "cluster", "describe", "prod") - return out, err - }, - } { - t.Run(name, func(t *testing.T) { - t.Parallel() - out, err := f(t, describedAPI()) - if err != nil { - t.Fatal(err) - } - got := decodeJSON[map[string]any](t, out) - if got["name"] != "prod" || got["phase"] != "running" { - t.Errorf("cluster fields = %v", got) - } - pools, ok := got["node_pools"].([]any) - if !ok || len(pools) != 1 { - t.Fatalf("node_pools = %v", got["node_pools"]) - } - if p := pools[0].(map[string]any); p["name"] != "workers" || p["ready_replicas"] != float64(2) || p["replicas"] != float64(3) { - t.Errorf("pool = %v", p) - } - if _, has := got["node_pools_error"]; has { - t.Error("node_pools_error must be absent when the pool call worked") - } - if conds, _ := got["conditions"].([]any); len(conds) != 1 { - t.Errorf("conditions = %v", got["conditions"]) - } - }) - } -} - -func TestClusterDescribe_PoolCallFailsStillPrintsCluster(t *testing.T) { - t.Parallel() - newAPI := func() *fakeAPI { - api := describedAPI() - api.failures["GET /mk8sclusters/{loc}/{name}/pools"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"pools unavailable"}`} - return api - } - - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, newAPI()), nil), "cluster", "describe", "prod", "-l", "FIN-03") - if err != nil { - t.Fatalf("the cluster must still print: %v", err) - } - if !strings.Contains(out, "prod") || !strings.Contains(out, "Node pools: unavailable") { - t.Errorf("stdout = %q", out) - } - if !strings.Contains(errOut, "Warning: could not load the node pools of cluster prod") { - t.Errorf("stderr = %q", errOut) - } - - out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, newAPI())), "cluster", "describe", "prod", "-l", "FIN-03") - if err != nil { - t.Fatal(err) - } - got := decodeJSON[map[string]any](t, out) - if v, present := got["node_pools"]; !present || v != nil { - t.Errorf("node_pools = %v, want null", v) - } - if msg, _ := got["node_pools_error"].(string); !strings.Contains(msg, "pools unavailable") { - t.Errorf("node_pools_error = %q", msg) - } -} - -func TestClusterDescribe_NoPools(t *testing.T) { - t.Parallel() - api := seededClusters() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "staging") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(out, "Node pools: none") || !strings.Contains(out, "node-pool create --cluster staging") { - t.Errorf("output = %s", out) - } - out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api)), "cluster", "describe", "staging") - if err != nil { - t.Fatal(err) - } - if pools, ok := decodeJSON[map[string]any](t, out)["node_pools"].([]any); !ok || len(pools) != 0 { - t.Errorf("no pools must be [], got %v", pools) - } -} - -func TestClusterDescribe_Aliases(t *testing.T) { - t.Parallel() - client := newFakeClient(t, seededClusters()) - for _, alias := range []string{"get", "show"} { - if _, _, err := runMK8s(t, tableFactory(client, nil), "cluster", alias, "prod"); err != nil { - t.Errorf("alias %s: %v", alias, err) - } - } -} - -// wantDescribed is describedAPI's "prod" cluster. Every field has its own value, so a -// swapped or dropped field fails. -func wantDescribed() ClusterDescribeView { - return ClusterDescribeView{ - ClusterView: ClusterView{ - Name: "prod", Location: "FIN-03", KubernetesVersion: "1.37.0", Phase: "running", - ExternalEndpoint: "https://prod.example.test:6443", NetworkID: "pn-1", SubnetID: "sn-1", - PodCIDR: "10.100.0.0/16", ServiceCIDR: "10.200.0.0/16", DNSSuffix: "", - Conditions: []ConditionView{{Type: "Ready", Status: "True", Reason: "AllGood", Message: "all components ready", LastTransitionTime: testCreated}}, - CreatedAt: testCreated, - }, - NodePools: &[]PoolSummaryView{{Name: "workers", InstanceType: "1V100.6V", ReadyReplicas: 2, Replicas: 3, State: "active"}}, - } -} - -func TestClusterDescribe_JSONHasEveryFieldInItsPlace(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, describedAPI())), "cluster", "describe", "prod") - if err != nil { - t.Fatal(err) - } - got := decodeJSON[ClusterDescribeView](t, out) - if want := wantDescribed(); !reflect.DeepEqual(got, want) { - t.Errorf("got %+v\nwant %+v", got, want) - } -} - -var describeLine = regexp.MustCompile(`^([A-Za-z][A-Za-z ]*):\s+(\S.*)$`) - -func TestClusterDescribe_TableLabelsMatchValues(t *testing.T) { - t.Parallel() - api := describedAPI() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod") - if err != nil { - t.Fatal(err) - } got := map[string]string{} for line := range strings.SplitSeq(out, "\n") { if m := describeLine.FindStringSubmatch(line); m != nil { got[m[1]] = strings.TrimSpace(m[2]) } } - want := map[string]string{ + for k, v := range map[string]string{ "Name": "prod", "Location": "FIN-03", "Version": "1.37.0", "Phase": "running", "Endpoint": "https://prod.example.test:6443", "DNS suffix": "-", "Network": "pn-1", "Subnet": "sn-1", "Pod CIDR": "10.100.0.0/16", "Service CIDR": "10.200.0.0/16", "Created": "2026-11-19 09:30 UTC", - } - for k, v := range want { + } { if got[k] != v { t.Errorf("%s: = %q, want %q", k, got[k], v) } } + for _, want := range []string{"Conditions:", "TYPE", "Ready", "Node pools:", "INSTANCE TYPE", "READY/DESIRED", "workers", "2/3", "active"} { + if !strings.Contains(out, want) { + t.Errorf("output misses %q:\n%s", want, out) + } + } for line := range strings.SplitSeq(out, "\n") { - if strings.Contains(line, "Ready") && strings.Contains(line, "AllGood") { - if strings.Index(line, "AllGood") > strings.Index(line, "all components ready") { - t.Errorf("condition columns are REASON then MESSAGE: %q", line) - } + if strings.Contains(line, "AllGood") && strings.Index(line, "AllGood") > strings.Index(line, "all components ready") { + t.Errorf("condition columns are REASON then MESSAGE: %q", line) } } } -func TestClusterDescribe_YAMLNullVersusEmptyPools(t *testing.T) { +func TestClusterDescribe_Structured(t *testing.T) { t.Parallel() - read := func(api *fakeAPI) map[string]any { - out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, api)), "cluster", "describe", "prod", "-l", "FIN-03") - if err != nil { - t.Fatal(err) - } - var m map[string]any - if err := yaml.Unmarshal([]byte(out), &m); err != nil { - t.Fatal(err) - } - return m + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, describedAPI())), "cluster", "describe", "prod") + if err != nil { + t.Fatal(err) } - - failing := describedAPI() - failing.failures["GET /mk8sclusters/{loc}/{name}/pools"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"down"}`} - got := read(failing) - if v, present := got["node_pools"]; !present || v != nil { - t.Errorf("a failed pool read must be null in YAML too, got %v", v) + got := decodeJSON[map[string]any](t, out) + if got["name"] != "prod" || got["phase"] != "running" || got["pod_cidr"] != "10.100.0.0/16" { + t.Errorf("cluster fields = %v", got) } - if got["node_pools_error"] == nil || got["name"] != "prod" || got["pod_cidr"] != "10.100.0.0/16" { - t.Errorf("YAML = %v", got) + pools, ok := got["node_pools"].([]any) + if !ok || len(pools) != 1 { + t.Fatalf("node_pools = %v", got["node_pools"]) } - - empty := seededClusters() - empty.clusters[0].Name = "prod" - if pools, ok := read(empty)["node_pools"].([]any); !ok || len(pools) != 0 { - t.Errorf("no pools must be [], got %v", read(empty)["node_pools"]) + if p := pools[0].(map[string]any); p["name"] != "workers" || p["ready_replicas"] != float64(2) || p["replicas"] != float64(3) { + t.Errorf("pool = %v", p) } - - if pools, ok := read(describedAPI())["node_pools"].([]any); !ok || len(pools) != 1 { - t.Errorf("pools = %v", pools) + if _, has := got["node_pools_error"]; has { + t.Error("node_pools_error must be absent when the pool call worked") + } + if conds, _ := got["conditions"].([]any); len(conds) != 1 { + t.Errorf("conditions = %v", got["conditions"]) } } -func TestClusterDescribe_Debug(t *testing.T) { +// A failed pool call still prints the cluster: node_pools is null with +// node_pools_error. No pools is []. +func TestClusterDescribe_NodePools(t *testing.T) { t.Parallel() - f := tableFactory(newFakeClient(t, describedAPI()), nil) - f.DebugOverride = true - _, errOut, err := runMK8s(t, f, "cluster", "describe", "prod") - if err != nil { - t.Fatal(err) + failing := func() *fakeAPI { + api := describedAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/pools"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"pools unavailable"}`} + return api } - if !strings.Contains(errOut, "API response: cluster:") || !strings.Contains(errOut, `"pod_cidr": "10.100.0.0/16"`) { - t.Errorf("debug output = %q", errOut) + for name, tc := range map[string]struct { + api func() *fakeAPI + cluster string + table, errOut string + pools, poolErr string + }{ + "failed": {failing, "prod", "Node pools: unavailable", "Warning: could not load the node pools of cluster prod", "null", "pools unavailable"}, + "none": {seededClusters, "staging", "node-pool create --cluster staging", "", "[]", ""}, + } { + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, tc.api()), nil), "cluster", "describe", tc.cluster) + if err != nil || !strings.Contains(out, tc.cluster) || !strings.Contains(out, tc.table) || !strings.Contains(errOut, tc.errOut) { + t.Errorf("%s: out=%q stderr=%q err=%v", name, out, errOut, err) + } + out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, tc.api())), "cluster", "describe", tc.cluster) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + if !strings.Contains(out, `"node_pools": `+tc.pools) { + t.Errorf("%s: node_pools is not %s: %s", name, tc.pools, out) + } + if msg, _ := decodeJSON[map[string]any](t, out)["node_pools_error"].(string); !strings.Contains(msg, tc.poolErr) || (tc.poolErr == "" && msg != "") { + t.Errorf("%s: node_pools_error = %q", name, msg) + } } } diff --git a/internal/verda-cli/cmd/mk8s/cluster_list.go b/internal/verda-cli/cmd/mk8s/cluster_list.go index 0b3a1f83..7b956b93 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_list.go +++ b/internal/verda-cli/cmd/mk8s/cluster_list.go @@ -85,9 +85,8 @@ func runClusterList(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOS return nil } -// clusterTableWidth is the terminal width the table must fit in. The -// endpoint is long (it holds the project ID), so the column is dropped when the -// rows would wrap; list -o json and describe always show it. +// clusterTableWidth: the endpoint holds the project ID, so its column is +// dropped when a row is wider; -o json and describe always show it. const clusterTableWidth = 120 func writeClusterTable(w io.Writer, views []ClusterView, now time.Time, withEndpoint bool) error { diff --git a/internal/verda-cli/cmd/mk8s/cluster_list_test.go b/internal/verda-cli/cmd/mk8s/cluster_list_test.go index c60cb0d7..c33d15bf 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_list_test.go +++ b/internal/verda-cli/cmd/mk8s/cluster_list_test.go @@ -18,19 +18,8 @@ import ( "strings" "testing" "time" - - "go.yaml.in/yaml/v3" - - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -func seededClusters() *fakeAPI { - api := newFakeAPI() - api.addCluster("prod", "FIN-03", "running") - api.addCluster("staging", "FIN-01", "provisioning") - return api -} - func TestClusterList_Table(t *testing.T) { t.Parallel() api := seededClusters() @@ -60,74 +49,27 @@ func TestClusterList_Table(t *testing.T) { } } -func TestClusterList_LongEndpointsDropTheColumnToFit(t *testing.T) { +func TestClusterList_Structured(t *testing.T) { t.Parallel() - api := seededClusters() - api.clusters[0].ExternalEndpoint = "https://prod.project-824e2c70-bfce-4227-9ca4-369fc72d9280.fin03.mk8s.example.test:9443" - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "list") + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, seededClusters())), "cluster", "list") if err != nil { t.Fatal(err) } - if strings.Contains(out, "ENDPOINT") || strings.Contains(out, "mk8s.example.test") { - t.Errorf("the endpoint column must be dropped when rows exceed %d columns:\n%s", clusterTableWidth, out) + got := decodeJSON[[]map[string]any](t, out) + if len(got) != 2 || got[0]["name"] != "prod" || got[1]["phase"] != "provisioning" { + t.Fatalf("got %v", got) } - if got := widestLine(strings.TrimRight(out, "\n")); got > clusterTableWidth { - t.Errorf("widest line = %d", got) - } - jsonOut, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "cluster", "list") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(jsonOut, "https://prod.project-824e2c70") { - t.Errorf("JSON keeps the full endpoint:\n%s", jsonOut) - } -} - -func TestClusterList_Structured(t *testing.T) { - t.Parallel() - wantKeys := []string{ + for _, k := range []string{ "name", "location", "kubernetes_version", "phase", "external_endpoint", "network", "subnet", "pod_cidr", "service_cidr", "dns_suffix", "conditions", "created_at", - } - for name, mk := range map[string]func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory{ - "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, } { - t.Run(name, func(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, mk(seededClusters(), t), "cluster", "list") - if err != nil { - t.Fatal(err) - } - got := decodeJSON[[]map[string]any](t, out) - if len(got) != 2 || got[0]["name"] != "prod" || got[1]["phase"] != "provisioning" { - t.Fatalf("got %v", got) - } - for _, k := range wantKeys { - if _, ok := got[0][k]; !ok { - t.Errorf("JSON misses %q", k) - } - } - if c, ok := got[0]["conditions"].([]any); !ok || len(c) != 0 { - t.Errorf("conditions = %v, want []", got[0]["conditions"]) - } - }) - } - - t.Run("yaml", func(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, seededClusters())), "cluster", "list") - if err != nil { - t.Fatal(err) - } - var got []map[string]any - if err := yaml.Unmarshal([]byte(out), &got); err != nil { - t.Fatal(err) + if _, ok := got[0][k]; !ok { + t.Errorf("JSON misses %q", k) } - if len(got) != 2 || got[0]["kubernetes_version"] != "1.37.0" { - t.Errorf("got %v", got) - } - }) + } + if c, ok := got[0]["conditions"].([]any); !ok || len(c) != 0 { + t.Errorf("conditions = %v, want []", got[0]["conditions"]) + } } func TestClusterList_Empty(t *testing.T) { @@ -151,29 +93,6 @@ func TestClusterList_Empty(t *testing.T) { } } -func TestClusterList_Debug(t *testing.T) { - t.Parallel() - f := tableFactory(newFakeClient(t, seededClusters()), nil) - f.DebugOverride = true - _, errOut, err := runMK8s(t, f, "cluster", "list") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "API response: 2 cluster(s):") || !strings.Contains(errOut, `"kubernetes_version": "1.37.0"`) { - t.Errorf("debug output = %q", errOut) - } -} - -func TestClusterList_NotEnabled(t *testing.T) { - t.Parallel() - api := newFakeAPI() - api.disabled = true - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "list") - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Fatalf("err = %v", err) - } -} - func TestAgeColumn(t *testing.T) { t.Parallel() now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC) diff --git a/internal/verda-cli/cmd/mk8s/cluster_upgrade.go b/internal/verda-cli/cmd/mk8s/cluster_upgrade.go index a92f483f..958e9296 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_upgrade.go +++ b/internal/verda-cli/cmd/mk8s/cluster_upgrade.go @@ -125,13 +125,11 @@ func runClusterUpgrade(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. req := verda.UpdateManagedKubernetesClusterRequest{KubernetesVersion: opts.Version} cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - updated, err := cmdutil.WithSpinner(ctx, f.Status(), "Upgrading cluster...", func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { + updated, err := apiCall(cmd.Context(), f, "Upgrading cluster...", func(ctx context.Context) (*verda.ManagedKubernetesCluster, error) { return client.ManagedKubernetes.Update(ctx, current.Location, current.Name, req) }) if err != nil { - return mapErr(err) + return err } cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response:", newClusterView(updated)) @@ -146,8 +144,6 @@ func runClusterUpgrade(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. return nil } -// upgradeMissing lists the arguments an upgrade needs that were not given; -// without a terminal that includes the version, which a person is asked for. func upgradeMissing(f cmdutil.Factory, name string, given bool, version string) []string { var missing []string if clusterArgMissing(f, name, given) { @@ -159,7 +155,7 @@ func upgradeMissing(f cmdutil.Factory, name string, given bool, version string) return missing } -// askVersion prompts for the new version. ok is false when the user canceled. +// askVersion: ok is false when the user canceled. func askVersion(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, current *verda.ManagedKubernetesCluster) (version string, ok bool, err error) { v, err := f.Prompter().TextInput(ctx, fmt.Sprintf("New Kubernetes version (cluster %s runs %s)", current.Name, current.KubernetesVersion), tui.WithPlaceholder("1.38.0"), diff --git a/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go b/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go index 4f454765..442e0d1a 100644 --- a/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go +++ b/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go @@ -54,57 +54,18 @@ func TestClusterUpgrade_YesSendsTheNewVersion(t *testing.T) { } } -func TestClusterUpgrade_VersionIsTrimmed(t *testing.T) { +func TestClusterUpgrade_Structured(t *testing.T) { t.Parallel() - api := seededClusters() - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "upgrade", "prod", "--kubernetes-version", " 1.38.0 ", "--yes"); err != nil { + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, seededClusters())), "cluster", "upgrade", "prod", "-l", "FIN-03", "--kubernetes-version", "1.38.0", "--yes") + if err != nil { t.Fatal(err) } - if body := decodeJSON[map[string]string](t, api.requestsMatching("PATCH")[0].Body); body["kubernetes_version"] != "1.38.0" { - t.Errorf("body = %v", body) + got := decodeJSON[map[string]any](t, out) + if got["status"] != "accepted" || got["previous_version"] != "1.37.0" { + t.Errorf("result = %v", got) } -} - -func TestClusterUpgrade_Structured(t *testing.T) { - t.Parallel() - for name, f := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - } { - t.Run(name, func(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, f(seededClusters(), t), "cluster", "upgrade", "prod", "-l", "FIN-03", "--kubernetes-version", "1.38.0", "--yes") - if err != nil { - t.Fatal(err) - } - got := decodeJSON[map[string]any](t, out) - if got["status"] != "accepted" || got["previous_version"] != "1.37.0" { - t.Errorf("result = %v", got) - } - if c, _ := got["cluster"].(map[string]any); c["kubernetes_version"] != "1.38.0" { - t.Errorf("cluster = %v", got["cluster"]) - } - }) - } -} - -func TestClusterUpgrade_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { - t.Parallel() - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { - return tableFactory(newFakeClient(t, api), nil) - }, - } { - api := seededClusters() - _, _, err := runMK8s(t, mk(api, t), "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0") - if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { - t.Errorf("%s: err = %v", name, err) - } - if n := len(api.requests); n != 0 { - t.Errorf("%s: %d request(s) before the check", name, n) - } + if c, _ := got["cluster"].(map[string]any); c["kubernetes_version"] != "1.38.0" { + t.Errorf("cluster = %v", got["cluster"]) } } @@ -120,74 +81,22 @@ func TestClusterUpgrade_MissingArguments(t *testing.T) { } } +// Without --kubernetes-version a terminal asks for it, then shows the warning and confirms. func TestClusterUpgrade_Interactive(t *testing.T) { withTerminal(t) - - t.Run("confirm shows the rolling upgrade warning", func(t *testing.T) { - api := seededClusters() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), - "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "Rolling upgrade from 1.37.0 to 1.38.0") { - t.Errorf("stderr = %q", errOut) - } - if n := len(api.requestsMatching("PATCH")); n != 1 { - t.Errorf("PATCHes = %d", n) - } - }) - - t.Run("version is asked when the flag is missing", func(t *testing.T) { - api := seededClusters() - p := tuitest.New().AddTextInput("1.39.0").AddConfirm(true) - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "cluster", "upgrade", "prod"); err != nil { - t.Fatal(err) - } - if body := decodeJSON[map[string]string](t, api.requestsMatching("PATCH")[0].Body); body["kubernetes_version"] != "1.39.0" { - t.Errorf("body = %v", body) - } - }) - - t.Run("picker then confirm", func(t *testing.T) { - api := seededClusters() - p := tuitest.New().AddSelect(1).AddConfirm(true) - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "cluster", "upgrade", "--kubernetes-version", "1.38.0"); err != nil { - t.Fatal(err) - } - if got := api.requestsMatching("PATCH")[0].Path; got != "/mk8sclusters/FIN-01/staging" { - t.Errorf("PATCH path = %s", got) - } - }) - - t.Run("declined, Esc and Ctrl+C change nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "declined": {Prompter: tuitest.New().AddConfirm(false)}, - "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, - } { - api := seededClusters() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - _, errOut, err := runMK8s(t, f, "cluster", "upgrade", "prod", "--kubernetes-version", "1.38.0") - if err != nil || !strings.Contains(errOut, canceledMsg) { - t.Errorf("%s: err=%v stderr=%q", name, err, errOut) - } - if n := len(api.requestsMatching("PATCH")); n != 0 { - t.Errorf("%s: %d PATCH(es)", name, n) - } - } - }) - - t.Run("canceling the version prompt changes nothing", func(t *testing.T) { - api := seededClusters() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = &textCancelPrompter{Prompter: tuitest.New()} - _, errOut, err := runMK8s(t, f, "cluster", "upgrade", "prod") - if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("PATCH")) != 0 { - t.Errorf("err=%v stderr=%q", err, errOut) - } - }) + api := seededClusters() + p := tuitest.New().AddTextInput("1.39.0").AddConfirm(true) + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "cluster", "upgrade", "prod") + if err != nil { + t.Fatal(err) + } + if !strings.Contains(errOut, "Rolling upgrade from 1.37.0 to 1.39.0") { + t.Errorf("stderr = %q", errOut) + } + patches := api.requestsMatching("PATCH") + if len(patches) != 1 || decodeJSON[map[string]string](t, patches[0].Body)["kubernetes_version"] != "1.39.0" { + t.Errorf("PATCHes = %v", patches) + } } func TestClusterUpgrade_APIErrorsShownAsSent(t *testing.T) { @@ -198,9 +107,4 @@ func TestClusterUpgrade_APIErrorsShownAsSent(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "concurrent_modification") { t.Fatalf("err = %v", err) } - - _, _, err = runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "upgrade", "ghost", "--kubernetes-version", "1.38.0", "--yes") - if agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown cluster: %v", err) - } } diff --git a/internal/verda-cli/cmd/mk8s/confirm.go b/internal/verda-cli/cmd/mk8s/confirm.go index 7c23d5e5..f8b8f3b7 100644 --- a/internal/verda-cli/cmd/mk8s/confirm.go +++ b/internal/verda-cli/cmd/mk8s/confirm.go @@ -25,16 +25,14 @@ import ( cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -// confirmationRequired is CONFIRMATION_REQUIRED for every mk8s command. The -// cause is not always agent mode: a script, a non-terminal or -o json|yaml also cannot prompt. +// confirmationRequired: the cause is not always agent mode, so the text names every case. func confirmationRequired(action string) *cmdutil.AgentError { ae := cmdutil.NewConfirmationRequiredError(action) ae.Message = fmt.Sprintf("destructive action %q requires --yes: there is no prompt in scripts, without a terminal, in agent mode or with -o json|yaml", action) return ae } -// checkConfirmed: destructive commands (and cluster upgrade) need --yes unless -// a person at a terminal can confirm. It never prompts. +// checkConfirmed never prompts: --yes is needed unless a person at a terminal can confirm. func checkConfirmed(f cmdutil.Factory, yes bool, action string) error { if !yes && !interactiveTTY(f) { return confirmationRequired(action) @@ -43,8 +41,6 @@ func checkConfirmed(f cmdutil.Factory, yes bool, action string) error { } // confirmWarning prints a red warning with detail lines on stderr, then asks. -// It returns false, nil when the user declines or presses Esc or Ctrl+C, after -// printing "Canceled.". func confirmWarning(ctx context.Context, ioStreams cmdutil.IOStreams, prompter tui.Prompter, heading string, detail []string, prompt string) (bool, error) { warn := lipgloss.NewStyle().Foreground(lipgloss.Color("1")).Bold(true) _, _ = fmt.Fprintf(ioStreams.ErrOut, "\n %s %s\n", warn.Render("⚠"), warn.Render(heading)) diff --git a/internal/verda-cli/cmd/mk8s/errors.go b/internal/verda-cli/cmd/mk8s/errors.go index 0dd6e482..b1bfc38a 100644 --- a/internal/verda-cli/cmd/mk8s/errors.go +++ b/internal/verda-cli/cmd/mk8s/errors.go @@ -35,25 +35,23 @@ const ( notEnabledMessage = "Managed Kubernetes is not enabled for this project." ) -// What a create makes, for the "may have been created" warning. const ( kindCluster = "cluster" kindNodePool = "node pool" kindKubeconfig = "kubeconfig" ) -// SDK error texts that tell where a failed request stopped. The SDK wraps -// these errors as plain strings, so there is no type to match. +// SDK error texts that tell where a failed request stopped: the SDK wraps them +// as plain strings, so there is no type to match. const ( sdkAuthFailed = "failed to get authentication token" sdkResponseUnread = "failed to read response body" sdkResponseUnparsed = "failed to unmarshal response" ) -// mapErr turns a 403 whose reason is feature_not_enabled into the "not enabled" -// message, still AUTH_ERROR (exit 3) through ClassifyError. It returns a plain -// APIError, not an AgentError: main prints AgentErrors as JSON even for humans. -// Every other error is shown as the API sent it. +// mapErr turns a 403 feature_not_enabled into the "not enabled" message, still +// AUTH_ERROR through ClassifyError. It is a plain APIError, not an AgentError: +// main prints AgentErrors as JSON even for humans. func mapErr(err error) error { if apiErr, ok := errors.AsType[*verda.APIError](err); ok && apiErr.StatusCode == http.StatusForbidden && (strings.Contains(apiErr.Code, reasonFeatureNotEnabled) || strings.Contains(apiErr.Details, reasonFeatureNotEnabled)) { @@ -62,11 +60,9 @@ func mapErr(err error) error { return err } -// createMayHaveSucceeded reports whether the API may have saved the object -// although the call failed: the request was sent and the answer is a 5xx, a -// 408, a timeout, a dropped or unreadable response. A 4xx (429 too) means -// nothing was created, and so does an error before the request left (a failed -// token fetch, no connection, a validation error). +// createMayHaveSucceeded: the request was sent and the answer is a 5xx, a 408, +// a timeout, a dropped or unreadable response. A 4xx (429 too) or an error +// before the request left means nothing was created. func createMayHaveSucceeded(err error) bool { if neverSent(err) { return false @@ -84,7 +80,6 @@ func createMayHaveSucceeded(err error) bool { strings.Contains(msg, sdkResponseUnparsed) } -// neverSent: the failure came before the create request reached the server. func neverSent(err error) bool { // A 5xx from the token endpoint is not a 5xx from the create. if strings.Contains(err.Error(), sdkAuthFailed) { @@ -94,7 +89,6 @@ func neverSent(err error) bool { return errors.As(err, &opErr) && opErr.Op == "dial" } -// createWarning is the "may have been created anyway" text. func createWarning(kind, listCmd string) string { w := fmt.Sprintf("The %s may have been created anyway. Before running create again, check `%s`.", kind, listCmd) if kind == kindKubeconfig { @@ -103,10 +97,9 @@ func createWarning(kind, listCmd string) string { return w } -// createFailed returns the error of a failed create. When the create may have -// been saved anyway it adds the warning: in the agent error details, or on -// stderr for humans. Creates are never retried: the API can save an object and -// still fail the answer, and a retry would make a second one. +// createFailed adds the "may have been created" warning to the agent error +// details, or prints it on stderr for humans. Creates are never retried: the +// API can save an object and still fail the answer. func createFailed(f cmdutil.Factory, ioStreams cmdutil.IOStreams, kind, listCmd string, err error) error { err = mapErr(err) if !createMayHaveSucceeded(err) { diff --git a/internal/verda-cli/cmd/mk8s/errors_test.go b/internal/verda-cli/cmd/mk8s/errors_test.go index b5f0498a..f1197f42 100644 --- a/internal/verda-cli/cmd/mk8s/errors_test.go +++ b/internal/verda-cli/cmd/mk8s/errors_test.go @@ -15,15 +15,14 @@ package mk8s import ( - "bytes" "context" "errors" "fmt" "io" "net" "net/http" - "net/http/httptest" "net/url" + "os" "strings" "testing" @@ -32,239 +31,75 @@ import ( cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -func TestMapErr_FeatureNotEnabled(t *testing.T) { +func TestMapErr(t *testing.T) { t.Parallel() - for name, e := range map[string]*verda.APIError{ - "details": {StatusCode: 403, Code: "forbidden", Message: "forbidden", Details: "feature_not_enabled"}, - "code": {StatusCode: 403, Code: "feature_not_enabled", Message: "forbidden"}, + for name, tc := range map[string]struct { + err error + notEnabled bool + }{ + "403 details": {&verda.APIError{StatusCode: 403, Code: "forbidden", Details: "feature_not_enabled"}, true}, + "403 code": {&verda.APIError{StatusCode: 403, Code: "feature_not_enabled"}, true}, + "other 403": {&verda.APIError{StatusCode: 403, Message: "no access", Details: "something_else"}, false}, + "403 text": {&verda.APIError{StatusCode: 403, Message: "reason: feature_not_enabled"}, false}, + "400 reason": {&verda.APIError{StatusCode: 400, Details: "feature_not_enabled"}, false}, + "plain": {errors.New("boom"), false}, } { - t.Run(name, func(t *testing.T) { - t.Parallel() - ae := cmdutil.ClassifyError(mapErr(fmt.Errorf("wrapped: %w", e))) - if ae.Code != "AUTH_ERROR" || ae.ExitCode != cmdutil.ExitAuth { - t.Errorf("got %s exit %d", ae.Code, ae.ExitCode) - } - if ae.Message != notEnabledMessage { - t.Errorf("message = %q", ae.Message) - } - }) - } -} - -func TestMapErr_OtherErrorsUnchanged(t *testing.T) { - t.Parallel() - for name, err := range map[string]error{ - "other 403": &verda.APIError{StatusCode: 403, Message: "no access", Details: "something_else"}, - // Only the code and the details carry the reason, not free text. - "403 text": &verda.APIError{StatusCode: 403, Message: "reason: feature_not_enabled"}, - "404": &verda.APIError{StatusCode: 404, Message: "cluster not found"}, - "400": &verda.APIError{StatusCode: 400, Message: "bad", Details: "cluster_quota_exceeded"}, - "plain": errors.New("boom"), - } { - if got := mapErr(err); !errors.Is(got, err) && got != err { //nolint:errorlint // identity check on purpose + err := fmt.Errorf("wrapped: %w", tc.err) + got := mapErr(err) + if ae := cmdutil.ClassifyError(got); (ae.Message == notEnabledMessage) != tc.notEnabled || (tc.notEnabled && (ae.Code != "AUTH_ERROR" || ae.ExitCode != cmdutil.ExitAuth)) { + t.Errorf("%s: got %s %q", name, ae.Code, ae.Message) + } + if !tc.notEnabled && got != err { //nolint:errorlint // identity check on purpose t.Errorf("%s: mapErr changed the error: %v", name, got) } } - // A 403 with the reason on a non-403 status is not the feature flag. - if got := mapErr(&verda.APIError{StatusCode: 400, Details: "feature_not_enabled"}); strings.Contains(got.Error(), "not enabled for this project") { - t.Error("only a 403 maps to the not-enabled message") - } } -type timeoutNetError struct{} - -func (timeoutNetError) Error() string { return "i/o timeout" } -func (timeoutNetError) Timeout() bool { return true } -func (timeoutNetError) Temporary() bool { return true } - -var _ net.Error = timeoutNetError{} - +// TestCreateMayHaveSucceeded guards the SDK error texts that createMayHaveSucceeded matches. func TestCreateMayHaveSucceeded(t *testing.T) { t.Parallel() - yes := map[string]error{ - "500": &verda.APIError{StatusCode: 500, Message: "x"}, - "502": &verda.APIError{StatusCode: 502, Message: "x"}, - "503": &verda.APIError{StatusCode: 503, Message: "x"}, - "408": &verda.APIError{StatusCode: 408, Message: "x"}, - "deadline": fmt.Errorf("create: %w", context.DeadlineExceeded), - "network": fmt.Errorf("create: %w", timeoutNetError{}), - } - no := map[string]error{ - "400": &verda.APIError{StatusCode: 400, Message: "x"}, - "403": &verda.APIError{StatusCode: 403, Message: "x"}, - "409": &verda.APIError{StatusCode: 409, Message: "x"}, - "429": &verda.APIError{StatusCode: 429, Message: "x"}, - "sdk validation": &verda.ValidationError{Field: "name", Message: "bad"}, - "plain": errors.New("boom"), - } - for name, err := range yes { - if !createMayHaveSucceeded(err) { - t.Errorf("%s: want warning", name) - } - } - for name, err := range no { - if createMayHaveSucceeded(err) { - t.Errorf("%s: want no warning", name) + sent := func(err error) error { + return fmt.Errorf("request failed: %w", &url.Error{Op: "Post", URL: "https://x", Err: err}) + } + for name, want := range map[error]bool{ + &verda.APIError{StatusCode: 500}: true, + &verda.APIError{StatusCode: 503}: true, + &verda.APIError{StatusCode: 408}: true, + fmt.Errorf("create: %w", context.DeadlineExceeded): true, + sent(context.Canceled): true, + sent(io.EOF): true, + io.EOF: true, + io.ErrUnexpectedEOF: true, + &net.OpError{Op: "read", Err: os.ErrDeadlineExceeded}: true, + errors.New("failed to read response body: connection reset by peer"): true, + errors.New("failed to unmarshal response: invalid character '<'"): true, + &verda.APIError{StatusCode: 400}: false, + &verda.APIError{StatusCode: 403}: false, + &verda.APIError{StatusCode: 409}: false, + &verda.APIError{StatusCode: 429}: false, + &verda.ValidationError{Field: "name", Message: "bad"}: false, + errors.New("boom"): false, + fmt.Errorf("failed to get authentication token: %w", &verda.APIError{StatusCode: 502}): false, + sent(&net.OpError{Op: "dial", Err: errors.New("refused")}): false, + } { + if got := createMayHaveSucceeded(name); got != want { + t.Errorf("%v: got %v, want %v", name, got, want) } } -} -// realClientError runs a request the way the SDK does (net/http) and returns -// the error it wraps, so these cases use errors the standard library produces, -// not hand-made ones. -func realClientError(t *testing.T, handler http.HandlerFunc) error { - t.Helper() - srv := httptest.NewServer(handler) - t.Cleanup(srv.Close) - resp, err := http.Post(srv.URL, "application/json", strings.NewReader("{}")) //nolint:noctx // test against a local server + // A real refused connection must look like "never sent": the dial check relies on its shape. + ln, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", "127.0.0.1:0") if err != nil { - return fmt.Errorf("request failed: %w", err) - } - defer func() { _ = resp.Body.Close() }() - if _, err := io.ReadAll(resp.Body); err != nil { - return fmt.Errorf("failed to read response body: %w", err) - } - t.Fatal("the request was expected to fail") - return nil -} - -func TestCreateMayHaveSucceeded_RealNetworkErrors(t *testing.T) { - t.Parallel() - - t.Run("connection refused: nothing was sent", func(t *testing.T) { - t.Parallel() - ln, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - addr := ln.Addr().String() - _ = ln.Close() - resp, err := http.Post("http://"+addr, "application/json", strings.NewReader("{}")) //nolint:noctx // test against a closed port - if err == nil { - _ = resp.Body.Close() - t.Fatal("expected a dial error") - } - if createMayHaveSucceeded(fmt.Errorf("request failed: %w", err)) { - t.Errorf("no request was sent, but the warning would show: %v", err) - } - }) - - t.Run("response cut short after the headers", func(t *testing.T) { - t.Parallel() - err := realClientError(t, func(w http.ResponseWriter, _ *http.Request) { - w.Header().Set("Content-Length", "100") - _, _ = w.Write([]byte(`{"id":`)) - }) - if !createMayHaveSucceeded(err) { - t.Errorf("the server may have saved the object, but no warning: %v", err) - } - if !createMayHaveSucceeded(io.ErrUnexpectedEOF) || !createMayHaveSucceeded(io.EOF) { - t.Error("a bare EOF after the request was sent must warn") - } - }) - - t.Run("connection dropped before any response", func(t *testing.T) { - t.Parallel() - err := realClientError(t, func(w http.ResponseWriter, _ *http.Request) { - conn, _, hijackErr := w.(http.Hijacker).Hijack() - if hijackErr == nil { - _ = conn.Close() - } - }) - if !createMayHaveSucceeded(err) { - t.Errorf("the request reached the server, but no warning: %v", err) - } - }) -} - -func TestCreateMayHaveSucceeded_WhereTheRequestStopped(t *testing.T) { - t.Parallel() - yes := map[string]error{ - "canceled after sending": fmt.Errorf("request failed: %w", &url.Error{Op: "Post", URL: "https://x", Err: context.Canceled}), - "2xx body not readable": errors.New("failed to read response body: connection reset by peer"), - "2xx body not json": errors.New("failed to unmarshal response: invalid character '<'"), - } - no := map[string]error{ - "token endpoint 502": fmt.Errorf("failed to get authentication token: %w", &verda.APIError{StatusCode: 502, Message: "bad gateway"}), - "token endpoint dial": fmt.Errorf("failed to get authentication token: %w", &net.OpError{Op: "dial", Err: errors.New("refused")}), - "validation": errors.New("pod_cidr: must be a valid IPv4 CIDR"), - } - for name, err := range yes { - if !createMayHaveSucceeded(err) { - t.Errorf("%s: want warning", name) - } - } - for name, err := range no { - if createMayHaveSucceeded(err) { - t.Errorf("%s: want no warning", name) - } - } -} - -func TestCreateFailed_HumanWarnsOnStderr(t *testing.T) { - t.Parallel() - var out, errOut bytes.Buffer - io := cmdutil.IOStreams{Out: &out, ErrOut: &errOut} - apiErr := &verda.APIError{StatusCode: 502, Message: "bad gateway"} - - err := createFailed(&cmdutil.TestFactory{}, io, kindCluster, "verda mk8s cluster list", apiErr) - if !errors.Is(err, apiErr) { - t.Fatalf("the API error must be returned: %v", err) + t.Fatal(err) } - if got := errOut.String(); !strings.Contains(got, "The cluster may have been created anyway.") || - !strings.Contains(got, "verda mk8s cluster list") { - t.Errorf("stderr = %q", got) + addr := ln.Addr().String() + _ = ln.Close() + resp, err := http.Post("http://"+addr, "application/json", strings.NewReader("{}")) //nolint:noctx // closed local port + if err == nil { + _ = resp.Body.Close() + t.Fatal("expected a dial error") } - if out.Len() != 0 { - t.Errorf("stdout must stay empty: %q", out.String()) - } -} - -func TestCreateFailed_AgentWarnsInDetails(t *testing.T) { - t.Parallel() - var errOut bytes.Buffer - io := cmdutil.IOStreams{Out: &bytes.Buffer{}, ErrOut: &errOut} - apiErr := &verda.APIError{StatusCode: 500, Message: "boom"} - - err := createFailed(&cmdutil.TestFactory{AgentModeOverride: true}, io, kindKubeconfig, "verda mk8s kubeconfig list --cluster c", apiErr) - ae := cmdutil.ClassifyError(err) - if ae.Code != "API_ERROR" || ae.ExitCode != cmdutil.ExitAPI { - t.Errorf("got %s exit %d", ae.Code, ae.ExitCode) - } - w, _ := ae.Details["warning"].(string) - if !strings.Contains(w, "The kubeconfig may have been created anyway.") || !strings.Contains(w, "kubeconfig delete") { - t.Errorf("warning = %q", w) - } - if ae.Details["status"] != 500 { - t.Errorf("status detail lost: %v", ae.Details) - } - if errOut.Len() != 0 { - t.Errorf("agent mode reports through the error envelope only: %q", errOut.String()) - } -} - -func TestCreateFailed_4xxNoWarning(t *testing.T) { - t.Parallel() - for _, agent := range []bool{false, true} { - var errOut bytes.Buffer - io := cmdutil.IOStreams{Out: &bytes.Buffer{}, ErrOut: &errOut} - apiErr := &verda.APIError{StatusCode: 400, Message: "bad", Details: "cluster_quota_exceeded"} - - err := createFailed(&cmdutil.TestFactory{AgentModeOverride: agent}, io, kindNodePool, "verda mk8s node-pool list", apiErr) - if !errors.Is(err, apiErr) { - t.Errorf("agent=%v: the 4xx must come back as is: %v", agent, err) - } - if errOut.Len() != 0 { - t.Errorf("agent=%v: unexpected warning %q", agent, errOut.String()) - } - } -} - -func TestCreateFailed_FeatureNotEnabledIsMapped(t *testing.T) { - t.Parallel() - io := cmdutil.IOStreams{Out: &bytes.Buffer{}, ErrOut: &bytes.Buffer{}} - err := createFailed(&cmdutil.TestFactory{}, io, kindCluster, "x", &verda.APIError{StatusCode: 403, Details: "feature_not_enabled"}) - if ae := cmdutil.ClassifyError(err); ae.Code != "AUTH_ERROR" || !strings.Contains(ae.Message, "not enabled") { - t.Errorf("got %+v", ae) + if createMayHaveSucceeded(fmt.Errorf("request failed: %w", err)) { + t.Errorf("no request was sent, but the warning would show: %v", err) } } diff --git a/internal/verda-cli/cmd/mk8s/fake_test.go b/internal/verda-cli/cmd/mk8s/fake_test.go index b720a9ea..57e5a069 100644 --- a/internal/verda-cli/cmd/mk8s/fake_test.go +++ b/internal/verda-cli/cmd/mk8s/fake_test.go @@ -22,13 +22,18 @@ import ( "io" "net/http" "net/http/httptest" + "os" + "runtime" + "strings" "sync" "testing" "time" "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + "github.com/verda-cloud/verda-cli/pkg/tui" tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" + "github.com/verda-cloud/verda-cli/pkg/tui/wizard" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -45,8 +50,6 @@ type recordedRequest struct { type apiFailure struct { Status int Body string - // Times is how many requests fail before the route works again; 0 means always. - Times int } // fakeAPI is an in-memory mk8s API. Pools and kubeconfigs are keyed by cluster name. @@ -159,14 +162,6 @@ func (a *fakeAPI) handler() http.Handler { route := func(pattern string, h http.HandlerFunc) { mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { if fl, ok := a.failures[pattern]; ok { - if fl.Times > 0 { - fl.Times-- - if fl.Times == 0 { - delete(a.failures, pattern) - } else { - a.failures[pattern] = fl - } - } w.Header().Set("Content-Type", "application/json") w.WriteHeader(fl.Status) _, _ = io.WriteString(w, fl.Body) @@ -222,9 +217,6 @@ func (a *fakeAPI) handler() http.Handler { }) route("GET /mk8sclusters/{loc}/{name}/pools", func(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } pools := a.pools[r.PathValue("name")] if pools == nil { pools = []verda.ManagedKubernetesNodePool{} @@ -233,9 +225,6 @@ func (a *fakeAPI) handler() http.Handler { }) route("POST /mk8sclusters/{loc}/{name}/pools", a.createPool) route("GET /mk8sclusters/{loc}/{name}/pools/{pool}", func(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } i := a.findPool(r.PathValue("name"), r.PathValue("pool")) if i < 0 { notFound(w, "node pool") @@ -248,9 +237,6 @@ func (a *fakeAPI) handler() http.Handler { }) route("PATCH /mk8sclusters/{loc}/{name}/pools/{pool}", a.updatePool) route("DELETE /mk8sclusters/{loc}/{name}/pools/{pool}", func(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } cluster := r.PathValue("name") i := a.findPool(cluster, r.PathValue("pool")) if i < 0 { @@ -282,10 +268,6 @@ func (a *fakeAPI) handler() http.Handler { func (a *fakeAPI) createCluster(w http.ResponseWriter, r *http.Request) { var req verda.CreateManagedKubernetesClusterRequest _ = json.NewDecoder(r.Body).Decode(&req) - if a.findCluster("", req.Name) >= 0 { - writeJSON(w, http.StatusConflict, map[string]string{"code": "cluster_already_exists", "message": "cluster already exists"}) - return - } c := verda.ManagedKubernetesCluster{ Name: req.Name, KubernetesVersion: req.KubernetesVersion, Location: req.Location, NetworkID: req.NetworkID, SubnetID: req.SubnetID, PodCIDR: req.PodCIDR, ServiceCIDR: req.ServiceCIDR, @@ -296,21 +278,9 @@ func (a *fakeAPI) createCluster(w http.ResponseWriter, r *http.Request) { } func (a *fakeAPI) createPool(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } cluster := r.PathValue("name") var req verda.CreateManagedKubernetesNodePoolRequest _ = json.NewDecoder(r.Body).Decode(&req) - // Pool names are unique in the project, not only in the cluster. - for _, pools := range a.pools { - for i := range pools { - if pools[i].Name == req.Name { - writeJSON(w, http.StatusConflict, map[string]string{"code": "conflict", "message": "node pool already exists", "details": "pool_already_exists"}) - return - } - } - } replicas := 1 if req.Replicas != nil { replicas = *req.Replicas @@ -324,9 +294,6 @@ func (a *fakeAPI) createPool(w http.ResponseWriter, r *http.Request) { } func (a *fakeAPI) updatePool(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } cluster := r.PathValue("name") i := a.findPool(cluster, r.PathValue("pool")) if i < 0 { @@ -343,9 +310,6 @@ func (a *fakeAPI) updatePool(w http.ResponseWriter, r *http.Request) { // createKubeconfig answers pending with a server-chosen ID, as the API does. func (a *fakeAPI) createKubeconfig(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } cluster := r.PathValue("name") var req verda.CreateManagedKubernetesKubeconfigRequest _ = json.NewDecoder(r.Body).Decode(&req) @@ -443,19 +407,12 @@ func (a *fakeAPI) networkRoutes(route routeFunc) { writeJSON(w, http.StatusOK, networks) }) route("GET /private-networks/{id}/subnets", func(w http.ResponseWriter, r *http.Request) { - subnets := a.subnets[r.PathValue("id")] - if subnets == nil { - subnets = []verda.Subnet{} - } - writeJSON(w, http.StatusOK, subnets) + writeJSON(w, http.StatusOK, a.subnets[r.PathValue("id")]) }) } func (a *fakeAPI) kubeconfigRoutes(route routeFunc) { route("GET /mk8sclusters/{loc}/{name}/kubeconfigs", func(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } list := make([]verda.ManagedKubernetesKubeconfig, 0, len(a.kubeconfigs[r.PathValue("name")])) for _, k := range a.kubeconfigs[r.PathValue("name")] { k.Kubeconfig = "" // the list never carries the YAML @@ -465,9 +422,6 @@ func (a *fakeAPI) kubeconfigRoutes(route routeFunc) { }) route("POST /mk8sclusters/{loc}/{name}/kubeconfigs", a.createKubeconfig) route("GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}", func(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } i := a.findKubeconfig(r.PathValue("name"), r.PathValue("id")) if i < 0 { notFound(w, "kubeconfig") @@ -479,9 +433,6 @@ func (a *fakeAPI) kubeconfigRoutes(route routeFunc) { writeJSON(w, http.StatusOK, a.kubeconfigs[r.PathValue("name")][i]) }) route("DELETE /mk8sclusters/{loc}/{name}/kubeconfigs/{id}", func(w http.ResponseWriter, r *http.Request) { - if a.clusterOr404(w, r) < 0 { - return - } cluster := r.PathValue("name") i := a.findKubeconfig(cluster, r.PathValue("id")) if i < 0 { @@ -492,3 +443,234 @@ func (a *fakeAPI) kubeconfigRoutes(route routeFunc) { w.WriteHeader(http.StatusNoContent) }) } + +// TestMain makes every test start off a terminal, whatever runs the tests; withTerminal opts in. +func TestMain(m *testing.M) { + isInteractiveTerminal = func() bool { return false } + os.Exit(m.Run()) +} + +// addCluster seeds a cluster; a running one has an endpoint, like the API. +func (a *fakeAPI) addCluster(name, location, phase string) { + c := verda.ManagedKubernetesCluster{ + Name: name, + KubernetesVersion: "1.37.0", + Location: location, + NetworkID: "pn-1", + SubnetID: "sn-1", + PodCIDR: "10.100.0.0/16", + ServiceCIDR: "10.200.0.0/16", + Phase: phase, + CreatedAt: testCreated, + } + if phase == verda.ManagedKubernetesClusterPhaseRunning { + c.ExternalEndpoint = "https://" + name + ".example.test:6443" + } + a.clusters = append(a.clusters, c) +} + +func (a *fakeAPI) addPool(cluster, name, state string, replicas, ready int) { + a.pools[cluster] = append(a.pools[cluster], verda.ManagedKubernetesNodePool{ + Name: name, + Replicas: replicas, + ReadyReplicas: ready, + InstanceType: "1V100.6V", + SSHKeyIDs: []string{"key-1"}, + State: state, + CreatedAt: testCreated, + }) +} + +// paths returns the path of every recorded request with this method, in order. +func (a *fakeAPI) paths(method string) []string { + reqs := a.requestsMatching(method) + out := make([]string, 0, len(reqs)) + for _, r := range reqs { + out = append(out, r.Path) + } + return out +} + +// cancelPrompter answers Select or Confirm with an error (Esc, Ctrl+C) when set. +type cancelPrompter struct { + *tuitest.Prompter + selectErr error + confirmErr error +} + +func (p *cancelPrompter) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { + if p.selectErr != nil { + return -1, p.selectErr + } + return p.Prompter.Select(ctx, msg, labels, opts...) +} + +func (p *cancelPrompter) Confirm(ctx context.Context, msg string, opts ...tui.ConfirmOption) (bool, error) { + if p.confirmErr != nil { + return false, p.confirmErr + } + return p.Prompter.Confirm(ctx, msg, opts...) +} + +// agentErrorCode is the code main reports, so an API 404 counts as NOT_FOUND. +func agentErrorCode(err error) string { + if ae := cmdutil.ClassifyError(err); ae != nil { + return ae.Code + } + return "" +} + +// isUsageError: VALIDATION_ERROR for agents, and not an AgentError, which main prints as JSON for people too. +func isUsageError(err error) bool { + return agentErrorCode(err) == "VALIDATION_ERROR" && !cmdutil.IsAgentError(err) +} + +func decodeJSON[T any](t *testing.T, s string) T { + t.Helper() + var v T + if err := json.Unmarshal([]byte(s), &v); err != nil { + t.Fatalf("decode %q: %v", s, err) + } + return v +} + +func conditionReady() verda.ManagedKubernetesCondition { + return verda.ManagedKubernetesCondition{Type: "Ready", Status: "True", Reason: "AllGood", Message: "all components ready", LastTransitionTime: testCreated} +} + +func errInterrupted() error { return tui.ErrInterrupted } + +func (a *fakeAPI) addNetwork(id, name string) { + a.networks = append(a.networks, verda.PrivateNetwork{ID: id, Name: name, Mode: verda.NetworkModeCustom, CreatedAt: testCreated}) +} + +func (a *fakeAPI) addSubnet(networkID, id, name, location, cidr string) { + a.subnets[networkID] = append(a.subnets[networkID], verda.Subnet{ + ID: id, Name: name, CIDR: cidr, Location: verda.Location{Code: location}, CreatedAt: testCreated, + }) +} + +func confirmYes() *tuitest.Prompter { return tuitest.New().AddConfirm(true) } + +func confirmNo() *tuitest.Prompter { return tuitest.New().AddConfirm(false) } + +// textCancelPrompter answers TextInput with Esc. +type textCancelPrompter struct{ *tuitest.Prompter } + +func (p *textCancelPrompter) TextInput(context.Context, string, ...tui.TextInputOption) (string, error) { + return "", context.Canceled +} + +// pickerAnswers queues Select answers (indices) for a picker. +func pickerAnswers(indices ...int) *tuitest.Prompter { + p := tuitest.New() + for _, i := range indices { + p.AddSelect(i) + } + return p +} + +// poolWizardAPI has two clusters, instance types per location and two SSH keys. +func poolWizardAPI() *fakeAPI { + api := seededClusters() + api.availability["FIN-03"] = []string{"2A100.80S", "1V100.6V", "CPU.4V.16G"} + api.availability["FIN-01"] = []string{"1A100.22V"} + api.sshKeys = []verda.SSHKey{ + {ID: "key-1", Name: "laptop", CreatedAt: testCreated}, + {ID: "key-2", Name: "ci", CreatedAt: testCreated}, + } + return api +} + +func postBodyAny(t *testing.T, api *fakeAPI) map[string]any { + t.Helper() + reqs := api.requestsMatching("POST") + if len(reqs) != 1 { + t.Fatalf("want one POST, got %v", reqs) + } + return decodeJSON[map[string]any](t, reqs[0].Body) +} + +// addKubeconfig seeds a kubeconfig of a cluster. +func (a *fakeAPI) addKubeconfig(cluster string, k verda.ManagedKubernetesKubeconfig) { + a.kubeconfigs[cluster] = append(a.kubeconfigs[cluster], k) +} + +// setHome points HOME (USERPROFILE on Windows) at a new temp dir and returns it. +func setHome(t *testing.T) string { + t.Helper() + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + return home +} + +func seededClusters() *fakeAPI { + api := newFakeAPI() + api.addCluster("prod", "FIN-03", "running") + api.addCluster("staging", "FIN-01", "provisioning") + return api +} + +// fastPoll shortens the wait loop. Never call t.Parallel in or under a test that +// uses it, withTerminal or withWizardAnswers: they change package variables. +func fastPoll(t *testing.T) { + t.Helper() + orig := pollInterval + pollInterval = 5 * time.Millisecond + t.Cleanup(func() { pollInterval = orig }) +} + +// withWizardAnswers makes the wizard take canned answers instead of a terminal. +func withWizardAnswers(t *testing.T, results ...wizard.TestResult) { + t.Helper() + orig := newWizardEngine + newWizardEngine = func(cmdutil.Factory, cmdutil.IOStreams) *wizard.Engine { + return wizard.NewEngine(nil, nil, wizard.WithOutput(io.Discard), wizard.WithTestResults(results...)) + } + t.Cleanup(func() { newWizardEngine = orig }) +} + +// poolAPI has the clusters of seededClusters and two pools in "prod": workers (3/3, active) and gpu (1/2, degraded). +func poolAPI() *fakeAPI { + api := seededClusters() + api.addPool("prod", "workers", "active", 3, 3) + api.addPool("prod", "gpu", "degraded", 2, 1) + api.pools["prod"][1].Errors = []verda.ManagedKubernetesNodePoolError{ + {Reason: "insufficient_capacity", Message: "no free 1V100.6V", Since: testCreated}, + } + return api +} + +func readFile(t *testing.T, path string) string { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read %s: %v", path, err) + } + return string(b) +} + +// assertMode checks the POSIX mode; Windows has none, so only that check is skipped. +func assertMode(t *testing.T, path string, want os.FileMode) { + t.Helper() + if runtime.GOOS == "windows" { + return + } + info, err := os.Stat(path) + if err != nil { + t.Fatalf("stat %s: %v", path, err) + } + if got := info.Mode().Perm(); got != want { + t.Errorf("%s mode = %o, want %o", path, got, want) + } +} + +func assertNoPrivateKey(t *testing.T, label string, outs ...string) { + t.Helper() + for _, s := range outs { + if strings.Contains(s, kcPrivateKeyMarker) || strings.Contains(s, "client-key-data") || strings.Contains(s, "apiVersion: v1") { + t.Errorf("%s: the private key reached the output:\n%s", label, s) + } + } +} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_create.go b/internal/verda-cli/cmd/mk8s/kubeconfig_create.go index 02c6ae2c..800d8e98 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_create.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_create.go @@ -36,7 +36,6 @@ const ( flagDisplayName = "display-name" flagDuration = "duration" - // groupAdmin is the group that grants cluster-admin. groupAdmin = "system:masters" defaultKubeconfigWaitTimeout = 3 * time.Minute @@ -157,7 +156,7 @@ func cleanGroups(in []string) []string { func (o *kubeconfigCreateOptions) hasAccess() bool { return o.Admin || len(o.Groups) > 0 } -// groups is the access to request: the admin group first, then --group values. +// groups: the admin group first, then --group values. func (o *kubeconfigCreateOptions) groups() []string { if !o.Admin { return slices.Clone(o.Groups) @@ -175,10 +174,9 @@ func (o *kubeconfigCreateOptions) request() verda.CreateManagedKubernetesKubecon } } -// missingError is the error for what a script must give, or nil. A terminal -// asks instead, except for a blank --cluster, which no command accepts. +// missingError: a terminal asks instead, except for a blank --cluster, which no command accepts. func (o *kubeconfigCreateOptions) missingError(f cmdutil.Factory, cmd *cobra.Command) error { - missing := o.missing(f, cmd) + missing := o.missing(f, cmd, nil, "") if !interactiveTTY(f) { if o.CommonName == "" { missing = append(missing, "--"+flagCommonName) @@ -204,8 +202,7 @@ func runKubeconfigCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmduti if err := opts.missingError(f, cmd); err != nil { return err } - // Cheap input checks first, then the file, then whatever prompts or calls the - // API. On a terminal the request and the default path are known only after the prompts. + // Input, then the file, then prompts and calls: no certificate is issued for a path that fails. interactive := interactiveTTY(f) var ( req verda.CreateManagedKubernetesKubeconfigRequest @@ -222,11 +219,7 @@ func runKubeconfigCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmduti } else if req, dest, err = opts.scriptedInput(cmd); err != nil { return err } - client, err := f.VerdaClient() - if err != nil { - return err - } - target, err := opts.resolve(cmd, f, ioStreams, client) + client, target, err := opts.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } @@ -248,8 +241,7 @@ func runKubeconfigCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmduti return createKubeconfig(cmd, f, ioStreams, client, target, opts, &req, dest) } -// scriptedInput validates everything a script gave, before any API call: the -// request, then the file (the cluster name is the one given, names are exact). +// scriptedInput checks the request, then the file, before any API call. func (o *kubeconfigCreateOptions) scriptedInput(cmd *cobra.Command) (verda.CreateManagedKubernetesKubeconfigRequest, *kubeconfigDest, error) { req, err := o.validRequest(cmd) if err != nil { @@ -259,7 +251,6 @@ func (o *kubeconfigCreateOptions) scriptedInput(cmd *cobra.Command) (verda.Creat return req, dest, err } -// validRequest builds the request and rejects what the API would refuse. func (o *kubeconfigCreateOptions) validRequest(cmd *cobra.Command) (verda.CreateManagedKubernetesKubeconfigRequest, error) { req := o.request() if err := req.Validate(); err != nil { @@ -270,9 +261,7 @@ func (o *kubeconfigCreateOptions) validRequest(cmd *cobra.Command) (verda.Create func createKubeconfig(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, target *clusterTarget, opts *kubeconfigCreateOptions, req *verda.CreateManagedKubernetesKubeconfigRequest, dest *kubeconfigDest) error { cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - created, err := cmdutil.WithSpinner(ctx, f.Status(), "Creating kubeconfig...", func(ctx context.Context) (*verda.ManagedKubernetesKubeconfig, error) { + created, err := apiCall(cmd.Context(), f, "Creating kubeconfig...", func(ctx context.Context) (*verda.ManagedKubernetesKubeconfig, error) { return client.ManagedKubernetes.CreateKubeconfig(ctx, target.Location, target.Name, *req) }) if err != nil { @@ -292,8 +281,7 @@ func createKubeconfig(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.I return reportKubeconfigWritten(f, ioStreams, target, dest, ready, dest.replaced) } -// waitForKubeconfig polls until the kubeconfig is ready with its YAML. It -// returns the last read, which holds the YAML: keep it out of every output. +// waitForKubeconfig returns the last read, which holds the YAML: keep it out of every output. func waitForKubeconfig(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, id string, timeout time.Duration) (*verda.ManagedKubernetesKubeconfig, error) { if f.OutputFormat() == outputTable { _, _ = fmt.Fprintf(ioStreams.ErrOut, "Kubeconfig %s created; waiting until it is ready (Ctrl+C stops waiting, the kubeconfig stays).\n", id) @@ -304,7 +292,6 @@ func waitForKubeconfig(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil }, kubeconfigReady) } -// kubeconfigWaitError says what to do after a wait that did not end in a ready kubeconfig. func kubeconfigWaitError(err error, t *clusterTarget, created, last *verda.ManagedKubernetesKubeconfig, timeout time.Duration, saveCmd string) error { subject := fmt.Sprintf("kubeconfig %s of cluster %s (%s)", created.ID, t.Name, t.Location) tail := fmt.Sprintf("Run `%s` once it is ready. %s", saveCmd, kubeconfigAgainWarning) @@ -318,8 +305,7 @@ func kubeconfigWaitError(err error, t *clusterTarget, created, last *verda.Manag return fmt.Errorf("timed out after %s waiting for %s to become ready (last status: %s). It was created and is still being issued. %s", timeout, subject, seen, tail) } -// kubeconfigRecovery adds the kubeconfig ID and the save command to the agent -// error details; people get them in the message. +// kubeconfigRecovery: agents get the ID and the save command in the details, people in the message. func kubeconfigRecovery(f cmdutil.Factory, err error, id, saveCmd string) error { if !f.AgentMode() { return err diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go index f41c471b..7e433a71 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go @@ -20,12 +20,10 @@ import ( "path/filepath" "slices" "strings" - "sync/atomic" "testing" "time" "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - "go.yaml.in/yaml/v3" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -33,8 +31,6 @@ import ( // kcYAML is the YAML the fake serves; kcPrivateKeyMarker stands for the private key. const kcYAML = "apiVersion: v1\nkind: Config\nusers:\n- name: alice\n user:\n client-key-data: " + kcPrivateKeyMarker + "\n" -const kcCreateRoute = "POST /mk8sclusters/{loc}/{name}/kubeconfigs" - // kcReadyOnGet makes every kubeconfig GET answer ready with the YAML. func kcReadyOnGet(api *fakeAPI) { api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { @@ -70,126 +66,74 @@ func kcAssertErrNoKey(t *testing.T, err error) { } } -func kcBody(t *testing.T, api *fakeAPI) map[string]any { - t.Helper() - return postBodyAny(t, api) -} - func TestKubeconfigCreate_WritesFileInEveryOutputMode(t *testing.T) { t.Parallel() for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ "table": func(c *verda.Client) *cmdutil.TestFactory { return tableFactory(c, nil) }, "json": jsonFactory, - "yaml": yamlFactory, - "agent": agentFactory, } { - t.Run(name, func(t *testing.T) { - t.Parallel() - api := kcAPI() - file := filepath.Join(t.TempDir(), "kc.yaml") - out, errOut, err := runMK8s(t, mk(newFakeClient(t, api)), kcArgs(file)...) - if err != nil { - t.Fatal(err) - } - assertNoPrivateKey(t, "stdout", out) - assertNoPrivateKey(t, "stderr", errOut) - if got := readFile(t, file); got != kcYAML { - t.Errorf("file = %q", got) - } - assertMode(t, file, 0o600) - if n := kcPosts(api); n != 1 { - t.Errorf("POSTs = %d, want 1", n) - } - switch name { - case "table": - if !strings.Contains(out, "Kubeconfig prod-kc-1 saved to "+file) { - t.Errorf("stdout = %q", out) - } - if !strings.Contains(errOut, "export KUBECONFIG="+file) || !strings.Contains(errOut, "kubectl get nodes") { - t.Errorf("stderr = %q", errOut) - } - case "yaml": - var v KubeconfigView - if err := yaml.Unmarshal([]byte(out), &v); err != nil || v.File != file || v.ID != "prod-kc-1" || v.Status != "ready" { - t.Errorf("view = %+v err=%v", v, err) - } - default: - v := decodeJSON[KubeconfigView](t, out) - if v.File != file || v.CommonName != "alice" || !slices.Equal(v.Groups, []string{groupAdmin}) || v.NotAfter == nil { - t.Errorf("view = %+v", v) - } - if errOut != "" { - t.Errorf("stderr = %q, want empty", errOut) - } + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + out, errOut, err := runMK8s(t, mk(newFakeClient(t, api)), kcArgs(file)...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + if got := readFile(t, file); got != kcYAML { + t.Errorf("%s: file = %q", name, got) + } + assertMode(t, file, 0o600) + if n := kcPosts(api); n != 1 { + t.Errorf("%s: POSTs = %d, want 1", name, n) + } + if name == "table" { + if !strings.Contains(out, "Kubeconfig prod-kc-1 saved to "+file) || !strings.Contains(errOut, "export KUBECONFIG="+file) { + t.Errorf("out=%q stderr=%q", out, errOut) } - }) + continue + } + v := decodeJSON[KubeconfigView](t, out) + if v.File != file || v.CommonName != "alice" || !slices.Equal(v.Groups, []string{groupAdmin}) || v.NotAfter == nil || errOut != "" { + t.Errorf("view = %+v, stderr = %q", v, errOut) + } } } func TestKubeconfigCreate_RequestBody(t *testing.T) { t.Parallel() - cases := map[string]struct { + for name, tc := range map[string]struct { flags []string - groups []string + groups []any extra map[string]any }{ - "admin": {flags: []string{"--admin"}, groups: []string{groupAdmin}}, - "groups": {flags: []string{"--group", "dev", "--group", "ops"}, groups: []string{"dev", "ops"}}, - "both": {flags: []string{"--admin", "--group", "dev"}, groups: []string{groupAdmin, "dev"}}, - "admin twice": {flags: []string{"--admin", "--group", groupAdmin}, groups: []string{groupAdmin}}, - "duration": {flags: []string{"--admin", "--duration", "720h"}, groups: []string{groupAdmin}, extra: map[string]any{"duration": "720h"}}, - "display name": {flags: []string{"--group", "dev", "--display-name", "Alice laptop"}, groups: []string{"dev"}, extra: map[string]any{"display_name": "Alice laptop"}}, - } - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - t.Parallel() - api := kcAPI() - file := filepath.Join(t.TempDir(), "kc.yaml") - args := []string{"kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--file", file} - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), append(args, tc.flags...)...); err != nil { - t.Fatal(err) - } - body := kcBody(t, api) - got, _ := body["groups"].([]any) - var groups []string - for _, g := range got { - groups = append(groups, g.(string)) - } - if body["common_name"] != "alice" || !slices.Equal(groups, tc.groups) { - t.Errorf("body = %v", body) - } - for _, key := range []string{"duration", "display_name"} { - want, has := tc.extra[key] - if got, ok := body[key]; ok != has || (has && got != want) { - t.Errorf("%s = %v (present %v), want %v (present %v)", key, got, ok, want, has) - } - } - if reqs := api.requestsMatching("POST"); reqs[0].Path != "/mk8sclusters/FIN-03/prod/kubeconfigs" { - t.Errorf("path = %s", reqs[0].Path) + "admin and a group": {flags: []string{"--admin", "--group", "dev", "--group", groupAdmin}, groups: []any{groupAdmin, "dev"}}, + "duration": {flags: []string{"--admin", "--duration", "720h"}, groups: []any{groupAdmin}, extra: map[string]any{"duration": "720h"}}, + "display name": {flags: []string{"--group", "dev", "--display-name", "Alice laptop"}, groups: []any{"dev"}, extra: map[string]any{"display_name": "Alice laptop"}}, + } { + api := kcAPI() + args := []string{"kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--file", filepath.Join(t.TempDir(), "kc.yaml")} + if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), append(args, tc.flags...)...); err != nil { + t.Fatalf("%s: %v", name, err) + } + body := postBodyAny(t, api) + if got, _ := body["groups"].([]any); body["common_name"] != "alice" || !slices.Equal(got, tc.groups) { + t.Errorf("%s: body = %v", name, body) + } + for _, key := range []string{"duration", "display_name"} { + want, has := tc.extra[key] + if got, ok := body[key]; ok != has || (has && got != want) { + t.Errorf("%s: %s = %v (present %v), want %v (present %v)", name, key, got, ok, want, has) } - }) - } -} - -func TestKubeconfigCreate_LocationFlagSkipsLookup(t *testing.T) { - t.Parallel() - api := kcAPI() - file := filepath.Join(t.TempDir(), "kc.yaml") - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "-l", "FIN-03")...); err != nil { - t.Fatal(err) - } - if slices.Contains(api.paths("GET"), "/mk8sclusters") { - t.Errorf("-l must skip the cluster lookup, GETs = %v", api.paths("GET")) + } + if reqs := api.requestsMatching("POST"); reqs[0].Path != "/mk8sclusters/FIN-03/prod/kubeconfigs" { + t.Errorf("%s: path = %s", name, reqs[0].Path) + } } } func TestKubeconfigCreate_DefaultPath(t *testing.T) { - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - api := kcAPI() + home := setHome(t) args := []string{"kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--admin"} - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...) + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, kcAPI()), nil), args...) if err != nil { t.Fatal(err) } @@ -198,83 +142,45 @@ func TestKubeconfigCreate_DefaultPath(t *testing.T) { t.Errorf("file = %q", got) } assertMode(t, want, 0o600) + assertMode(t, filepath.Dir(want), 0o700) if !strings.Contains(out, want) || !strings.Contains(errOut, "export KUBECONFIG="+want) { t.Errorf("out=%q err=%q", out, errOut) } } -func TestKubeconfigCreate_ExportLineQuotesThePath(t *testing.T) { +// A path that cannot be written stops the command before a certificate is issued. +func TestKubeconfigCreate_ExistingTargetStopsBeforeAnyPOST(t *testing.T) { t.Parallel() - file := filepath.Join(t.TempDir(), "my kube's.yaml") - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, kcAPI()), nil), kcArgs(file)...) - if err != nil { + dir := t.TempDir() + file := filepath.Join(dir, "kc.yaml") + if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { t.Fatal(err) } - if want := "export KUBECONFIG=" + shellQuote(file); !strings.Contains(errOut, want) || !strings.Contains(errOut, "'\\''") { - t.Errorf("stderr = %q, want %q", errOut, want) - } -} - -func TestKubeconfigCreate_ExistingTargetStopsBeforeAnyPOST(t *testing.T) { - t.Parallel() - t.Run("file without force", func(t *testing.T) { - t.Parallel() - api := kcAPI() - file := filepath.Join(t.TempDir(), "kc.yaml") - if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { - t.Fatal(err) - } - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file)...) - if err == nil || !strings.Contains(err.Error(), "--force") || !isUsageError(err) { - t.Fatalf("err = %v", err) - } - if n := kcPosts(api); n != 0 { - t.Errorf("%d POST(s): a certificate was issued for an unwritable path", n) - } - if got := readFile(t, file); got != "old" { - t.Errorf("file = %q", got) - } - }) - t.Run("directory", func(t *testing.T) { - t.Parallel() - api := kcAPI() - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(t.TempDir(), "--force")...) - if err == nil || !strings.Contains(err.Error(), "is a directory") || !isUsageError(err) || kcPosts(api) != 0 { - t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) - } - }) - t.Run("symlink", func(t *testing.T) { - t.Parallel() - skipModeTestOnWindows(t) - api := kcAPI() - dir := t.TempDir() - link := filepath.Join(dir, "link.yaml") - if err := os.Symlink(filepath.Join(dir, "elsewhere"), link); err != nil { - t.Fatal(err) - } - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(link, "--force")...) - if err == nil || !strings.Contains(err.Error(), "symlink") || !isUsageError(err) || kcPosts(api) != 0 { - t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) - } - }) - t.Run("missing parent directory", func(t *testing.T) { - t.Parallel() + for name, tc := range map[string]struct { + args []string + want string + }{ + "file without --force": {kcArgs(file), "--force"}, + "directory": {kcArgs(t.TempDir(), "--force"), "is a directory"}, + } { api := kcAPI() - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(filepath.Join(t.TempDir(), "nope", "kc.yaml"))...) - if err == nil || !isUsageError(err) || kcPosts(api) != 0 { - t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) + _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), tc.args...) + if err == nil || !strings.Contains(err.Error(), tc.want) || !isUsageError(err) || kcPosts(api) != 0 { + t.Errorf("%s: err = %v, POSTs = %d", name, err, kcPosts(api)) } - }) + } + if got := readFile(t, file); got != "old" { + t.Errorf("file = %q", got) + } } func TestKubeconfigCreate_ForceReplacesTheFile(t *testing.T) { t.Parallel() - api := kcAPI() file := filepath.Join(t.TempDir(), "kc.yaml") if err := os.WriteFile(file, []byte("old"), 0o644); err != nil { //nolint:gosec // a loose mode proves the replacement is 0600 t.Fatal(err) } - _, errOut, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "--force")...) + _, errOut, err := runMK8s(t, jsonFactory(newFakeClient(t, kcAPI())), kcArgs(file, "--force")...) if err != nil { t.Fatal(err) } @@ -293,12 +199,12 @@ func TestKubeconfigCreate_ForceReplacesTheFile(t *testing.T) { } } -func TestKubeconfigCreate_FileDashWritesOnlyTheYAMLToStdout(t *testing.T) { - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - api := kcAPI() - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs("-")...) +// --file - writes only the YAML to stdout, also with --debug, and no file. +func TestKubeconfigCreate_FileDashWritesOnlyTheYAML(t *testing.T) { + home := setHome(t) + f := tableFactory(newFakeClient(t, kcAPI()), nil) + f.DebugOverride = true + out, errOut, err := runMK8s(t, f, kcArgs("-")...) if err != nil { t.Fatal(err) } @@ -311,64 +217,40 @@ func TestKubeconfigCreate_FileDashWritesOnlyTheYAMLToStdout(t *testing.T) { } } +// --file - would share stdout with the JSON: it fails before any API call. func TestKubeconfigCreate_FileDashRejectedWithStructuredOutput(t *testing.T) { t.Parallel() - for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ - "json": jsonFactory, "yaml": yamlFactory, "agent": agentFactory, + for name, args := range map[string][]string{ + "create": kcArgs("-"), + "save": {"kubeconfig", "save", "prod-kc-1", "--cluster", "prod", "--file", "-"}, } { - t.Run(name, func(t *testing.T) { - t.Parallel() - api := kcAPI() - out, _, err := runMK8s(t, mk(newFakeClient(t, api)), kcArgs("-")...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { - t.Fatalf("err = %v", err) - } - if out != "" || len(api.requests) != 0 { - t.Errorf("stdout = %q, requests = %v: must fail before any API call", out, api.requests) - } - }) + api := kcAPI() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...) + if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) || out != "" || len(api.requests) != 0 { + t.Errorf("%s: err = %v, stdout = %q, requests = %v", name, err, out, api.requests) + } } } func TestKubeconfigCreate_MissingFlags(t *testing.T) { t.Parallel() - for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ - "table": func(c *verda.Client) *cmdutil.TestFactory { return tableFactory(c, nil) }, - "agent": agentFactory, - } { - t.Run(name, func(t *testing.T) { - t.Parallel() - api := kcAPI() - f := mk(newFakeClient(t, api)) - _, _, err := runMK8s(t, f, "kubeconfig", "create") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("err = %v", err) - } - if m, _ := ae.Details["missing"].([]string); !slices.Equal(m, []string{"--cluster", "--common-name"}) { - t.Errorf("missing = %v", ae.Details) - } - if o, _ := ae.Details["one_of"].([]string); !slices.Equal(o, []string{"--admin", "--group"}) { - t.Errorf("one_of = %v", ae.Details) - } - if want := "required flags not provided: --cluster, --common-name; set at least one of --admin, --group"; ae.Message != want { - t.Errorf("message = %q, want %q", ae.Message, want) - } - // With the rest given, a missing access level alone is reported. - _, _, err = runMK8s(t, f, "kubeconfig", "create", "--cluster", "prod", "--common-name", "alice") - ae = cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" || !strings.Contains(ae.Message, "--admin, --group") { - t.Errorf("no access: err = %v", err) - } - // Without a common name only that is missing. - _, _, err = runMK8s(t, f, "kubeconfig", "create", "--cluster", "prod", "--admin") - if m, _ := cmdutil.ClassifyError(err).Details["missing"].([]string); !slices.Equal(m, []string{"--common-name"}) { - t.Errorf("no common name: err = %v", err) - } - if len(api.requests) != 0 { - t.Errorf("requests = %v", api.requests) - } - }) + api := kcAPI() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "kubeconfig", "create") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) + } + if m, _ := ae.Details["missing"].([]string); !slices.Equal(m, []string{"--cluster", "--common-name"}) { + t.Errorf("missing = %v", ae.Details) + } + if o, _ := ae.Details["one_of"].([]string); !slices.Equal(o, []string{"--admin", "--group"}) { + t.Errorf("one_of = %v", ae.Details) + } + if want := "required flags not provided: --cluster, --common-name; set at least one of --admin, --group"; ae.Message != want { + t.Errorf("message = %q, want %q", ae.Message, want) + } + if len(api.requests) != 0 { + t.Errorf("requests = %v", api.requests) } } @@ -376,174 +258,82 @@ func TestKubeconfigCreate_InvalidInput(t *testing.T) { t.Parallel() api := kcAPI() file := filepath.Join(t.TempDir(), "kc.yaml") - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "--wait-timeout", "0s")...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { - t.Errorf("zero wait timeout: %v", err) - } for name, extra := range map[string][]string{ - "duration under 1h": {"--duration", "30m"}, - "system: common name": {"--common-name", "system:admin"}, - "system: group": {"--group", "system:nodes"}, - "newline in the display": {"--display-name", "a\nb"}, + "zero wait timeout": {"--wait-timeout", "0s"}, + "duration under 1h": {"--duration", "30m"}, + "system: group": {"--group", "system:nodes"}, } { _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, extra...)...) if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { t.Errorf("%s: err = %v", name, err) } } - // Input validation comes before the file check. - if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { - t.Fatal(err) - } - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcArgs(file, "--duration", "30m")...); err == nil || !strings.Contains(err.Error(), "invalid kubeconfig") { - t.Errorf("a bad duration must be reported before the taken file: %v", err) - } - if err := os.Remove(file); err != nil { - t.Fatal(err) - } - if usage := NewCmdKubeconfigCreate(nil, cmdutil.IOStreams{}).Flags().Lookup(flagDuration).Usage; !strings.Contains(usage, "cannot be revoked before it expires") { - t.Errorf("--duration help = %q", usage) - } - args := kcArgs(file) - args[slices.Index(args, "prod")] = "ghost" - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown cluster: %v", err) - } - if n := kcPosts(api); n != 0 { - t.Errorf("%d POST(s) for invalid input", n) + if n := len(api.requests); n != 0 { + t.Errorf("%d request(s) for invalid input", n) } } -func TestKubeconfigCreate_FailureSendsOnePOST(t *testing.T) { - t.Parallel() - t.Run("502 warns once", func(t *testing.T) { - t.Parallel() - api := kcAPI() - api.failures[kcCreateRoute] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} - file := filepath.Join(t.TempDir(), "kc.yaml") - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(file)...) - if err == nil || !strings.Contains(err.Error(), "upstream failed") { - t.Fatalf("err = %v", err) - } - if !strings.Contains(errOut, "The kubeconfig may have been created anyway.") || !strings.Contains(errOut, "kubeconfig list --cluster prod -l FIN-03") { - t.Errorf("stderr = %q", errOut) - } - if n := kcPosts(api); n != 1 { - t.Errorf("POSTs = %d, want exactly 1", n) - } - if _, err := os.Stat(file); err == nil { - t.Error("no file may be written when the create failed") - } - }) - t.Run("502 in agent mode puts the warning in the details", func(t *testing.T) { - t.Parallel() - api := kcAPI() - api.failures[kcCreateRoute] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} - _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), kcArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) - ae := cmdutil.ClassifyError(err) - if ae == nil || !strings.Contains(ae.Details["warning"].(string), "may have been created anyway") { - t.Fatalf("err = %v", err) +// After CreateKubeconfig succeeded, a failed wait, read or write returns the +// kubeconfig ID and the exact save command; agents also get them in the details. +func TestKubeconfigCreate_RecoveryAfterCreate(t *testing.T) { + fastPoll(t) + raced := func(file string) func(*verda.ManagedKubernetesKubeconfig) { + // The file appears after the path check passed: the exclusive create refuses it. + return func(k *verda.ManagedKubernetesKubeconfig) { + k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML + _ = os.WriteFile(file, []byte("raced"), 0o600) } - if errOut != "" || kcPosts(api) != 1 { - t.Errorf("stderr = %q, POSTs = %d", errOut, kcPosts(api)) + } + for name, tc := range map[string]struct { + agent bool + base string + extra []string + hook func(file string) func(*verda.ManagedKubernetesKubeconfig) + readFails bool + quoted string // the --file part of the save command + want string + fileLeft string // "" means no file + }{ + "wait timeout": {agent: true, base: "my kc.yaml", extra: []string{"--wait-timeout", "150ms", "--force"}, want: "last status: pending"}, + "write failure": {agent: true, base: "kc.yaml", hook: raced, want: "is ready but writing it failed", fileLeft: "raced"}, + "write failure human": {base: "kc.yaml", hook: raced, want: "is ready but writing it failed", fileLeft: "raced"}, + "read failure": {agent: true, base: "kc.yaml", readFails: true, want: "its status could not be read (API error 403: no access)"}, + } { + api := seededClusters() // never ready unless hook says so + file := filepath.Join(t.TempDir(), tc.base) + if tc.hook != nil { + api.onKubeconfigGet = tc.hook(file) } - }) - t.Run("400 has no warning", func(t *testing.T) { - t.Parallel() - api := kcAPI() - api.failures[kcCreateRoute] = apiFailure{Status: 400, Body: `{"code":"bad_request","message":"bad common name"}`} - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) - if err == nil || strings.Contains(errOut, "may have been created") || kcPosts(api) != 1 { - t.Errorf("err = %v, stderr = %q, POSTs = %d", err, errOut, kcPosts(api)) + if tc.readFails { + api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access"}`} } - }) - t.Run("403 not enabled", func(t *testing.T) { - t.Parallel() - api := kcAPI() - api.disabled = true - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(filepath.Join(t.TempDir(), "kc.yaml"), "-l", "FIN-03")...) - if agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled") { - t.Errorf("err = %v", err) + f := tableFactory(newFakeClient(t, api), nil) + if tc.agent { + f = agentFactory(newFakeClient(t, api)) } - }) -} - -func TestKubeconfigCreate_WaitTimeoutGivesRecovery(t *testing.T) { - fastPoll(t) - for _, agent := range []bool{false, true} { - api := seededClusters() // never ready: the kubeconfig stays pending - file := filepath.Join(t.TempDir(), "my kc.yaml") - runFactory := tableFactory(newFakeClient(t, api), nil) - if agent { - runFactory = agentFactory(newFakeClient(t, api)) + _, errOut, err := runMK8s(t, f, kcArgs(file, tc.extra...)...) + saveCmd := "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file " + file + if strings.Contains(file, " ") { + saveCmd = "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file '" + file + "' --force" } - _, _, err := runMK8s(t, runFactory, kcArgs(file, "--wait-timeout", "150ms", "--force")...) - if err == nil { - t.Fatal("want a timeout") + if err == nil || !strings.Contains(err.Error(), saveCmd) || !strings.Contains(err.Error(), tc.want) || !strings.Contains(err.Error(), kubeconfigAgainWarning) { + t.Errorf("%s: err = %v", name, err) } - msg := err.Error() - saveCmd := "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file '" + file + "' --force" - for _, want := range []string{"prod-kc-1", saveCmd, "running create again issues another certificate", "last status: pending"} { - if !strings.Contains(strings.ToLower(msg), strings.ToLower(want)) { - t.Errorf("agent=%v: message %q lacks %q", agent, msg, want) - } + if ae := cmdutil.ClassifyError(err); tc.agent != cmdutil.IsAgentError(err) || (tc.agent && (ae.Details["kubeconfig_id"] != "prod-kc-1" || ae.Details["save_command"] != saveCmd)) { + t.Errorf("%s: agent error = %v", name, ae) } - if _, statErr := os.Stat(file); statErr == nil { - t.Error("no file may be written before the kubeconfig is ready") + if got, statErr := os.ReadFile(file); string(got) != tc.fileLeft || (tc.fileLeft == "") != (statErr != nil) { + t.Errorf("%s: file = %q (%v)", name, got, statErr) } if n := kcPosts(api); n != 1 { - t.Errorf("POSTs = %d", n) - } - if agent { - ae := cmdutil.ClassifyError(err) - if ae.Details["kubeconfig_id"] != "prod-kc-1" || ae.Details["save_command"] != saveCmd { - t.Errorf("details = %v", ae.Details) - } + t.Errorf("%s: POSTs = %d", name, n) } kcAssertErrNoKey(t, err) + assertNoPrivateKey(t, "stderr", errOut) } } -func TestKubeconfigCreate_WriteFailureAfterCreateGivesRecovery(t *testing.T) { - fastPoll(t) - api := seededClusters() - file := filepath.Join(t.TempDir(), "kc.yaml") - // The file appears after the path check passed: the exclusive create refuses it. - api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { - k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML - _ = os.WriteFile(file, []byte("raced"), 0o600) - } - _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), kcArgs(file)...) - if err == nil { - t.Fatal("want a write error") - } - saveCmd := "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file " + file - if !strings.Contains(err.Error(), saveCmd) || !strings.Contains(err.Error(), "another certificate") { - t.Errorf("err = %v", err) - } - ae := cmdutil.ClassifyError(err) - if ae.Details["kubeconfig_id"] != "prod-kc-1" || ae.Details["save_command"] != saveCmd { - t.Errorf("details = %v", ae.Details) - } - if got := readFile(t, file); got != "raced" { - t.Errorf("the other file was replaced: %q", got) - } - kcAssertErrNoKey(t, err) - assertNoPrivateKey(t, "stderr", errOut) - - // For a person the same text is the plain error. - api = seededClusters() - file = filepath.Join(t.TempDir(), "kc.yaml") - api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { - k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML - _ = os.WriteFile(file, []byte("raced"), 0o600) - } - _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcArgs(file)...) - if err == nil || cmdutil.IsAgentError(err) || !strings.Contains(err.Error(), "prod-kc-1") { - t.Errorf("human err = %v", err) - } -} - +// Pending, then ready without the YAML (a stale read), then ready: the wait ends on the third read. func TestKubeconfigCreate_PendingAndStaleReadsDoNotEndTheWait(t *testing.T) { fastPoll(t) api := seededClusters() @@ -554,7 +344,6 @@ func TestKubeconfigCreate_PendingAndStaleReadsDoNotEndTheWait(t *testing.T) { case 1: k.Status = verda.ManagedKubernetesKubeconfigStatusPending case 2: - // Ready without the YAML is a stale read. k.Status = verda.ManagedKubernetesKubeconfigStatusReady default: k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML @@ -565,102 +354,8 @@ func TestKubeconfigCreate_PendingAndStaleReadsDoNotEndTheWait(t *testing.T) { if err != nil { t.Fatal(err) } - if got := readFile(t, file); got != kcYAML { - t.Errorf("file = %q", got) - } - if reads < 3 { - t.Errorf("reads = %d, the wait ended early", reads) + if got := readFile(t, file); got != kcYAML || reads < 3 { + t.Errorf("file = %q, reads = %d", got, reads) } assertNoPrivateKey(t, "stdout", out) } - -func TestKubeconfigCreate_DebugNeverLogsTheKey(t *testing.T) { - t.Parallel() - api := kcAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.DebugOverride = true - file := filepath.Join(t.TempDir(), "kc.yaml") - out, errOut, err := runMK8s(t, f, kcArgs(file)...) - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "DEBUG: Request payload:") || !strings.Contains(errOut, "DEBUG: API response:") { - t.Errorf("no debug output: %q", errOut) - } - assertNoPrivateKey(t, "stdout", out) - assertNoPrivateKey(t, "debug output", errOut) - - f = agentFactory(newFakeClient(t, api)) - f.DebugOverride = true - out, errOut, err = runMK8s(t, f, kcArgs(filepath.Join(t.TempDir(), "kc2.yaml"))...) - if err != nil { - t.Fatal(err) - } - assertNoPrivateKey(t, "agent stdout", out) - assertNoPrivateKey(t, "agent debug output", errOut) -} - -func TestKubeconfigCreate_StdoutDashDebugKeepsStdoutPure(t *testing.T) { - t.Parallel() - f := tableFactory(newFakeClient(t, kcAPI()), nil) - f.DebugOverride = true - out, errOut, err := runMK8s(t, f, kcArgs("-")...) - if err != nil { - t.Fatal(err) - } - if out != kcYAML { - t.Errorf("stdout = %q", out) - } - assertNoPrivateKey(t, "debug output", errOut) -} - -// A fresh machine has no ~/.kube: the default path passes the prompt check, and -// the recovery command printed after a timed-out create (which leaves out --file) -// creates ~/.kube with 0700. -func TestKubeconfigCreate_FreshHomeDefaultPathAndRecovery(t *testing.T) { - fastPoll(t) - home := kcHome(t) - def := filepath.Join(home, ".kube", "verda-prod.alice.yaml") - if err := kubeconfigFileValidator("prod", "alice", false)(def); err != nil { - t.Errorf("the pre-filled default path must pass the prompt check: %v", err) - } - var ready atomic.Bool - api := seededClusters() - api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { - if ready.Load() { - k.Status, k.Kubeconfig = verda.ManagedKubernetesKubeconfigStatusReady, kcYAML - } - } - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--admin", "--wait-timeout", "100ms") - ae := cmdutil.ClassifyError(err) - saveCmd, _ := ae.Details["save_command"].(string) - if saveCmd == "" || strings.Contains(saveCmd, "--file") { - t.Fatalf("save_command = %q, want one without --file", saveCmd) - } - ready.Store(true) - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), strings.Fields(saveCmd)[2:]...); err != nil { - t.Fatalf("recovery command %q failed: %v", saveCmd, err) - } - if got := readFile(t, def); got != kcYAML { - t.Errorf("file = %q", got) - } - assertMode(t, filepath.Dir(def), 0o700) -} - -// A certificate was issued, so the recovery text leads the error even when the -// classified API message ("no access") is about the failed read. -func TestKubeconfigCreate_ReadFailureKeepsTheRecoveryText(t *testing.T) { - fastPoll(t) - api := seededClusters() - api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access"}`} - file := filepath.Join(t.TempDir(), "kc.yaml") - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), kcArgs(file)...) - ae := cmdutil.ClassifyError(err) - saveCmd := "verda mk8s kubeconfig save prod-kc-1 --cluster prod -l FIN-03 --file " + file - if ae == nil || ae.Code != "AUTH_ERROR" || ae.Details["kubeconfig_id"] != "prod-kc-1" || ae.Details["save_command"] != saveCmd { - t.Fatalf("err = %v, details = %v", err, ae) - } - if !strings.Contains(ae.Message, "Running create again issues another certificate") || !strings.Contains(ae.Message, saveCmd) { - t.Errorf("message = %q, want the recovery text", ae.Message) - } -} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_delete.go b/internal/verda-cli/cmd/mk8s/kubeconfig_delete.go index fe5a1580..aef6ecf3 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_delete.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_delete.go @@ -62,17 +62,13 @@ func NewCmdKubeconfigDelete(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cob } func runKubeconfigDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string, yes bool) error { - if err := checkKubeconfigTarget(f, cmd, scope, args); err != nil { + if err := scope.check(f, cmd, args, argKubeconfigRef); err != nil { return err } if err := checkConfirmed(f, yes, "delete"); err != nil { return err } - client, err := f.VerdaClient() - if err != nil { - return err - } - target, err := scope.resolve(cmd, f, ioStreams, client) + client, target, err := scope.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } @@ -97,13 +93,10 @@ func runKubeconfigDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmduti } cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Delete kubeconfig:", map[string]string{"id": kc.ID, "cluster": target.Name, "location": target.Location}) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - err = cmdutil.RunWithSpinner(ctx, f.Status(), "Deleting kubeconfig...", func(ctx context.Context) error { + if err := apiRun(cmd.Context(), f, "Deleting kubeconfig...", func(ctx context.Context) error { return client.ManagedKubernetes.DeleteKubeconfig(ctx, target.Location, target.Name, kc.ID) - }) - if err != nil { - return mapErr(err) + }); err != nil { + return err } result := ActionResult{Resource: kindKubeconfig, ID: kc.ID, Name: kc.ID, Location: target.Location, Cluster: target.Name, Action: "delete", Status: statusAccepted} @@ -114,7 +107,7 @@ func runKubeconfigDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmduti return nil } -// copiesNote: the CLI cannot revoke a certificate. system:masters is bound to cluster-admin by the control plane itself, so no RBAC change cuts that group off. +// copiesNote: system:masters is bound to cluster-admin by the control plane, so no RBAC change cuts it off. const copiesNote = "Deleting the cluster invalidates them. A copy that does not use system:masters can also be cut off by removing its group's RBAC binding in the cluster." // validUntil: the date is known only when the id came from the picker; a named id is not read first. diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go index 7b543179..28c29e6c 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go @@ -19,8 +19,6 @@ import ( "strings" "testing" - "go.yaml.in/yaml/v3" - tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" @@ -28,6 +26,7 @@ import ( const kcDeletePath = "/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-aaa" +// A named id is not read first (no GET of the kubeconfig, so no YAML either), and there is no --wait. func TestKubeconfigDelete_YesMakesOneDeleteCall(t *testing.T) { t.Parallel() api := kcFixtureAPI() @@ -35,250 +34,56 @@ func TestKubeconfigDelete_YesMakesOneDeleteCall(t *testing.T) { if err != nil { t.Fatal(err) } - if !strings.Contains(out, "Deletion accepted.") || !strings.Contains(out, "not revoked") || !strings.Contains(out, "still work until they expire.") || !strings.Contains(out, "Deleting the cluster invalidates them") || !strings.Contains(out, "does not use system:masters") { - t.Errorf("stdout = %q", out) + for _, want := range []string{"Deletion accepted.", "not revoked", "still work until they expire.", "Deleting the cluster invalidates them", "does not use system:masters"} { + if !strings.Contains(out, want) { + t.Errorf("stdout misses %q: %q", want, out) + } } if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != kcDeletePath { t.Fatalf("DELETE requests = %v", dels) } - if len(api.requestsMatching("POST"))+len(api.requestsMatching("PATCH")) != 0 { - t.Error("delete must not write anything else") - } - // A named id is not read first (no GET of the kubeconfig, so no YAML either), and there is no --wait. if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters"}) { t.Errorf("reads = %v, want only the cluster lookup", got) } - if len(api.kubeconfigs["prod"]) != 1 || api.kubeconfigs["prod"][0].ID != "prod-kc-bbb" { - t.Errorf("kubeconfigs left = %+v", api.kubeconfigs["prod"]) - } } -func TestKubeconfigDelete_AKubeconfigThatCannotBeReadCanStillBeDeleted(t *testing.T) { +func TestKubeconfigDelete_Structured(t *testing.T) { t.Parallel() - api := kcFixtureAPI() - api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "rm", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes"); err != nil { + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes") + if err != nil { t.Fatal(err) } - if got := api.paths("GET"); len(got) != 0 { - t.Errorf("reads = %v, want none with -l", got) - } - if n := len(api.requestsMatching("DELETE")); n != 1 { - t.Errorf("DELETEs = %d", n) - } -} - -func TestKubeconfigDelete_Structured(t *testing.T) { - t.Parallel() want := ActionResult{Resource: "kubeconfig", ID: "prod-kc-aaa", Name: "prod-kc-aaa", Location: "FIN-03", Cluster: "prod", Action: "delete", Status: "accepted"} - wantKeys := []string{"action", "cluster", "id", "location", "name", "resource", "status"} - for name, f := range map[string]*cmdutil.TestFactory{ - "json": jsonFactory(newFakeClient(t, kcFixtureAPI())), - "agent": agentFactory(newFakeClient(t, kcFixtureAPI())), - } { - out, _, err := runMK8s(t, f, "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes") - if err != nil { - t.Fatal(err) - } - if got := decodeJSON[ActionResult](t, out); got != want { - t.Errorf("%s: got %+v, want %+v", name, got, want) - } - if got := sortedKeys(decodeJSON[map[string]any](t, out)); !slices.Equal(got, wantKeys) { - t.Errorf("%s: keys = %v", name, got) - } + if got := decodeJSON[ActionResult](t, out); got != want { + t.Errorf("got %+v, want %+v", got, want) } +} - out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes") +func TestKubeconfigDelete_Interactive(t *testing.T) { + withTerminal(t) + api := kcFixtureAPI() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod") if err != nil { t.Fatal(err) } - var got ActionResult - if err := yaml.Unmarshal([]byte(out), &got); err != nil || got != want { - t.Errorf("yaml: got %+v (%v), want %+v", got, err, want) - } -} - -func TestKubeconfigDelete_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { - t.Parallel() - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { - return tableFactory(newFakeClient(t, api), nil) - }, - } { - api := kcFixtureAPI() - _, _, err := runMK8s(t, mk(api, t), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod") - if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { - t.Errorf("%s: err = %v", name, err) + for _, s := range []string{"Deletes kubeconfig prod-kc-aaa.", "Copies already downloaded are NOT revoked", "keep working until they expire.", "cannot be undone"} { + if !strings.Contains(errOut, s) { + t.Errorf("warning misses %q:\n%s", s, errOut) } - if n := len(api.requests); n != 0 { - t.Errorf("%s: %d request(s) before the check", name, n) - } - } -} - -func TestKubeconfigDelete_MissingTargets(t *testing.T) { - t.Parallel() - client := newFakeClient(t, kcFixtureAPI()) - _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "delete", "--yes") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("err = %v", err) - } - if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { - t.Errorf("missing = %v", missing) - } - // Missing targets are reported before the missing --yes, as in node-pool delete. - if _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "delete"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Errorf("no args, no --yes: %v", err) } - if _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "delete", " ", "--cluster", "prod", "--yes"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Errorf("blank id: %v", err) + if !strings.Contains(out, "still work until they expire.") || len(api.requestsMatching("DELETE")) != 1 { + t.Errorf("stdout = %q, DELETEs = %d", out, len(api.requestsMatching("DELETE"))) } } -func TestKubeconfigDelete_Interactive(t *testing.T) { - withTerminal(t) - - t.Run("the warning says copies are not revoked", func(t *testing.T) { - api := kcFixtureAPI() - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - for _, s := range []string{"Deletes kubeconfig prod-kc-aaa.", "Copies already downloaded are NOT revoked", "keep working until they expire.", "Deleting the cluster invalidates them", "does not use system:masters", "cannot be undone"} { - if !strings.Contains(errOut, s) { - t.Errorf("warning misses %q:\n%s", s, errOut) - } - } - if !strings.Contains(out, "still work until they expire.") { - t.Errorf("stdout = %q", out) - } - if n := len(api.requestsMatching("DELETE")); n != 1 { - t.Errorf("DELETEs = %d", n) - } - }) -} - -func TestKubeconfigDelete_InteractivePickers(t *testing.T) { - withTerminal(t) - - t.Run("a picked kubeconfig names its expiry", func(t *testing.T) { - api := kcFixtureAPI() - p := &kcSelectRecorder{Prompter: pickerAnswers(0, 0).AddConfirm(true)} - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - out, errOut, err := runMK8s(t, f, "kubeconfig", "delete") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "keep working until 2027-02-17 09:30 UTC.") || !strings.Contains(out, "still work until 2027-02-17 09:30 UTC.") { - t.Errorf("stdout = %q\nstderr = %q", out, errOut) - } - if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != kcDeletePath { - t.Errorf("DELETE requests = %v", dels) - } - if len(p.calls) != 2 || p.calls[1].Message != "Select kubeconfig" || !p.calls[1].Hints || p.calls[1].Labels[len(p.calls[1].Labels)-1] != cancelLabel { - t.Errorf("picker calls = %+v", p.calls) - } - }) - - t.Run("a picked pending kubeconfig has no expiry yet", func(t *testing.T) { - api := kcFixtureAPI() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 1).AddConfirm(true)), "kubeconfig", "delete") - if err != nil || !strings.Contains(errOut, "keep working until they expire.") { - t.Fatalf("err=%v stderr=%q", err, errOut) - } - if dels := api.requestsMatching("DELETE"); len(dels) != 1 || !strings.HasSuffix(dels[0].Path, "/prod-kc-bbb") { - t.Errorf("DELETE requests = %v", dels) - } - }) - - t.Run("--yes skips the confirmation but not the pickers", func(t *testing.T) { - api := kcFixtureAPI() - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 0)), "kubeconfig", "delete", "--yes"); err != nil { - t.Fatal(err) - } - if n := len(api.requestsMatching("DELETE")); n != 1 { - t.Errorf("DELETEs = %d", n) - } - }) -} - -func TestKubeconfigDelete_InteractiveDeclined(t *testing.T) { - withTerminal(t) - - t.Run("declined, Esc and Ctrl+C delete nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "declined": {Prompter: confirmNo()}, - "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, - } { - api := kcFixtureAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - out, errOut, err := runMK8s(t, f, "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod") - if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("DELETE")) != 0 { - t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) - } - } - }) -} - -func TestKubeconfigDelete_InteractivePickerCancelAndEmpty(t *testing.T) { - withTerminal(t) - - t.Run("picker cancel paths delete nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "cancel row": {Prompter: pickerAnswers(2)}, - "esc": {Prompter: tuitest.New(), selectErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), selectErr: errInterrupted()}, - } { - api := kcFixtureAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - out, errOut, err := runMK8s(t, f, "kubeconfig", "delete", "--cluster", "prod") - if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("DELETE")) != 0 { - t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) - } - } - }) - - t.Run("no kubeconfigs", func(t *testing.T) { - api := seededClusters() - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), "kubeconfig", "delete", "--cluster", "prod") - if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "kubeconfig create --cluster prod") || len(api.requestsMatching("DELETE")) != 0 { - t.Errorf("out=%q err=%q error=%v", out, errOut, err) - } - }) -} - func TestKubeconfigDelete_Errors(t *testing.T) { t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, kcFixtureAPI()), nil), "kubeconfig", "delete", "ghost", "--cluster", "prod", "--yes") - if agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown id: %v", err) + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "delete", "--yes") + if m, _ := cmdutil.ClassifyError(err).Details["missing"].([]string); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" || !slices.Equal(m, []string{"--cluster", ""}) { + t.Errorf("err = %v, missing = %v", err, m) } - _, _, err = runMK8s(t, tableFactory(newFakeClient(t, kcFixtureAPI()), nil), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "ghost", "--yes") + _, _, err = runMK8s(t, tableFactory(newFakeClient(t, kcFixtureAPI()), nil), "kubeconfig", "delete", "ghost", "--cluster", "prod", "--yes") if agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown cluster: %v", err) - } - api := newFakeAPI() - api.disabled = true - _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "delete", "x", "--cluster", "prod", "-l", "FIN-03", "--yes") - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Errorf("not enabled: %v", err) - } -} - -func TestKubeconfigDelete_Debug(t *testing.T) { - t.Parallel() - f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) - f.DebugOverride = true - _, errOut, err := runMK8s(t, f, "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03", "--yes") - if err != nil || !strings.Contains(errOut, "Delete kubeconfig:") || !strings.Contains(errOut, `"id": "prod-kc-aaa"`) { - t.Errorf("err=%v debug=%q", err, errOut) + t.Errorf("unknown id: %v", err) } - assertNoPrivateKey(t, "delete debug", errOut) } diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_describe.go b/internal/verda-cli/cmd/mk8s/kubeconfig_describe.go index 077653e1..5225c767 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_describe.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_describe.go @@ -55,14 +55,10 @@ func NewCmdKubeconfigDescribe(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *c } func runKubeconfigDescribe(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string) error { - if err := checkKubeconfigTarget(f, cmd, scope, args); err != nil { + if err := scope.check(f, cmd, args, argKubeconfigRef); err != nil { return err } - client, err := f.VerdaClient() - if err != nil { - return err - } - target, err := scope.resolve(cmd, f, ioStreams, client) + client, target, err := scope.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go index 42c182ec..c4b35acd 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go @@ -15,6 +15,7 @@ package mk8s import ( + "path/filepath" "reflect" "regexp" "slices" @@ -22,58 +23,21 @@ import ( "testing" "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - "go.yaml.in/yaml/v3" - - tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -func TestKubeconfigDescribe_JSONHasEveryFieldInItsPlace(t *testing.T) { +// The view has no YAML and no file; a pending kubeconfig has groups [] and not_after null. +func TestKubeconfigDescribe_JSONHasEveryField(t *testing.T) { t.Parallel() - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - if got := decodeJSON[KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()[0]) { - t.Errorf("got %+v\nwant %+v", got, kcWantViews()[0]) - } - if got := sortedKeys(decodeJSON[map[string]any](t, out)); !slices.Equal(got, []string{"common_name", "display_name", "groups", "id", "not_after", "status"}) { - t.Errorf("keys = %v: the view has no YAML and no file", got) - } - - out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "describe", "prod-kc-bbb", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - if got := decodeJSON[KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()[1]) { - t.Errorf("pending: got %+v, want %+v", got, kcWantViews()[1]) - } - raw := decodeJSON[map[string]any](t, out) - if groups, ok := raw["groups"].([]any); !ok || len(groups) != 0 { - t.Errorf("groups = %#v, want []", raw["groups"]) - } - if v, ok := raw["not_after"]; !ok || v != nil { - t.Errorf("not_after = %#v, want null", v) - } -} - -func TestKubeconfigDescribe_YAMLShape(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "show", "prod-kc-aaa", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - var got map[string]any - if err := yaml.Unmarshal([]byte(out), &got); err != nil { - t.Fatal(err) - } - want := map[string]any{ - "id": "prod-kc-aaa", "display_name": "alice-laptop", "common_name": "alice", - "groups": []any{"system:masters", "dev"}, "status": "ready", "not_after": kcExpiry, - } - if !reflect.DeepEqual(got, want) { - t.Errorf("got %v\nwant %v", got, want) + for i, id := range []string{"prod-kc-aaa", "prod-kc-bbb"} { + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "describe", id, "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + if got := decodeJSON[KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()[i]) || len(decodeJSON[map[string]any](t, out)) != 6 { + t.Errorf("got %s\nwant %+v", out, kcWantViews()[i]) + } } } @@ -82,250 +46,87 @@ var kcDescribeLine = regexp.MustCompile(`^([A-Za-z][A-Za-z ]*):\s+(\S.*)$`) func TestKubeconfigDescribe_Table(t *testing.T) { t.Parallel() api := kcFixtureAPI() - parse := func(id string) map[string]string { - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "describe", id, "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - got := map[string]string{} - for line := range strings.SplitSeq(out, "\n") { - if m := kcDescribeLine.FindStringSubmatch(line); m != nil { - got[m[1]] = strings.TrimSpace(m[2]) - } + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + got := map[string]string{} + for line := range strings.SplitSeq(out, "\n") { + if m := kcDescribeLine.FindStringSubmatch(line); m != nil { + got[m[1]] = strings.TrimSpace(m[2]) } - return got } want := map[string]string{ "ID": "prod-kc-aaa", "Display name": "alice-laptop", "Common name": "alice", "Groups": "system:masters,dev", "Status": "ready", "Expires": "2027-02-17 09:30 UTC", "Cluster": "prod", "Location": "FIN-03", } - if got := parse("prod-kc-aaa"); !reflect.DeepEqual(got, want) { + if !reflect.DeepEqual(got, want) { t.Errorf("got %v\nwant %v", got, want) } - want = map[string]string{ - "ID": "prod-kc-bbb", "Display name": "-", "Common name": "ci-bot", "Groups": "-", - "Status": "pending", "Expires": "-", "Cluster": "prod", "Location": "FIN-03", - } - if got := parse("prod-kc-bbb"); !reflect.DeepEqual(got, want) { - t.Errorf("pending: got %v\nwant %v", got, want) - } - wantReqs := []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-aaa", "/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-bbb"} - if got := api.paths("GET"); !slices.Equal(got, wantReqs) { - t.Errorf("requests = %v, want %v", got, wantReqs) - } -} - -func TestKubeconfigDescribe_AgentAndLocation(t *testing.T) { - t.Parallel() - api := kcFixtureAPI() - out, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "kubeconfig", "get", "prod-kc-aaa", "--cluster", "prod", "-l", "FIN-03") - if err != nil { - t.Fatal(err) - } - if v := decodeJSON[KubeconfigView](t, out); !reflect.DeepEqual(v, kcWantViews()[0]) { - t.Errorf("view = %+v", v) - } - if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-aaa"}) { - t.Errorf("-l must skip the cluster lookup, got %v", got) + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs/prod-kc-aaa"}) { + t.Errorf("requests = %v", got) } } func TestKubeconfigDescribe_MissingTargetsAndErrors(t *testing.T) { t.Parallel() client := newFakeClient(t, kcFixtureAPI()) - for _, f := range []*cmdutil.TestFactory{agentFactory(client), tableFactory(client, nil)} { - _, _, err := runMK8s(t, f, "kubeconfig", "describe") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("err = %v", err) - } - if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { - t.Errorf("missing = %v", missing) - } - } - _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "describe", "prod-kc-aaa") - if ae := cmdutil.ClassifyError(err); ae == nil || !slices.Equal(ae.Details["missing"].([]string), []string{"--cluster"}) { - t.Errorf("only --cluster missing: %v", err) - } - _, _, err = runMK8s(t, agentFactory(client), "kubeconfig", "describe", "--cluster", "prod") - if ae := cmdutil.ClassifyError(err); ae == nil || !slices.Equal(ae.Details["missing"].([]string), []string{""}) { - t.Errorf("only missing: %v", err) - } - if _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "describe", " ", "--cluster", "prod"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Errorf("blank id: %v", err) + _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "describe") + if m, _ := cmdutil.ClassifyError(err).Details["missing"].([]string); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" || !slices.Equal(m, []string{"--cluster", ""}) { + t.Errorf("err = %v, missing = %v", err, m) } if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "describe", "ghost", "--cluster", "prod"); agentErrorCode(err) != "NOT_FOUND" { t.Errorf("unknown id: %v", err) } - if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "ghost"); agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown cluster: %v", err) - } - - api := newFakeAPI() - api.disabled = true - _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "describe", "x", "--cluster", "prod", "-l", "FIN-03") - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Errorf("not enabled: %v", err) - } -} - -func TestKubeconfigDescribe_Debug(t *testing.T) { - t.Parallel() - f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) - f.DebugOverride = true - _, errOut, err := runMK8s(t, f, "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - for _, s := range []string{"API response: kubeconfig:", `"id": "prod-kc-aaa"`, `"common_name": "alice"`, `"status": "ready"`} { - if !strings.Contains(errOut, s) { - t.Errorf("debug output misses %q:\n%s", s, errOut) - } - } - if strings.Contains(errOut, `"kubeconfig"`) || strings.Contains(errOut, "client-key-data") { - t.Errorf("debug output has the kubeconfig YAML:\n%s", errOut) - } } -func TestKubeconfigDescribe_Pickers(t *testing.T) { +// Without --cluster and , a terminal picks both; the list already has every +// field of the view, so the kubeconfig is not read again. +func TestKubeconfigDescribe_Picker(t *testing.T) { withTerminal(t) - - t.Run("cluster picker then kubeconfig picker", func(t *testing.T) { - api := kcFixtureAPI() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 1)), "kubeconfig", "describe") - if err != nil || !strings.Contains(out, "prod-kc-bbb") || !strings.Contains(out, "ci-bot") { - t.Fatalf("out=%q err=%v", out, err) - } - // The list has every field of the view and never the YAML: no second read. - if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs"}) { - t.Errorf("requests = %v", got) - } - }) - - t.Run("kubeconfig picker only", func(t *testing.T) { - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, kcFixtureAPI()), pickerAnswers(0)), "kubeconfig", "describe", "--cluster", "prod") - if err != nil || !strings.Contains(out, "prod-kc-aaa") { - t.Fatalf("out=%q err=%v", out, err) - } - }) - - t.Run("the picker offers every kubeconfig and Cancel", func(t *testing.T) { - p := &kcSelectRecorder{Prompter: pickerAnswers(2)} - f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) - f.PrompterOverride = p - if _, _, err := runMK8s(t, f, "kubeconfig", "describe", "--cluster", "prod"); err != nil { - t.Fatal(err) - } - calls := p.calls - if len(calls) != 1 || calls[0].Message != "Select kubeconfig" || !calls[0].Hints || len(calls[0].Labels) != 3 || calls[0].Labels[2] != cancelLabel { - t.Fatalf("calls = %+v", calls) - } - if !strings.Contains(calls[0].Labels[0], "prod-kc-aaa") || !strings.Contains(calls[0].Labels[1], "prod-kc-bbb") { - t.Errorf("labels = %v", calls[0].Labels) - } - }) -} - -func TestKubeconfigDescribe_PickerCancelAndEmpty(t *testing.T) { - withTerminal(t) - - t.Run("cancel row, Esc and Ctrl+C end cleanly", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "cancel row": {Prompter: pickerAnswers(2)}, - "esc": {Prompter: tuitest.New(), selectErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), selectErr: errInterrupted()}, - } { - f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) - f.PrompterOverride = p - out, errOut, err := runMK8s(t, f, "kubeconfig", "describe", "--cluster", "prod") - if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { - t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) - } - } - }) - - t.Run("no kubeconfigs", func(t *testing.T) { - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), tuitest.New()), "kubeconfig", "describe", "--cluster", "prod") - if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "kubeconfig create --cluster prod") { - t.Errorf("out=%q err=%q error=%v", out, errOut, err) - } - }) -} - -// kcOutputFormats runs one command in each output mode, with --debug on. -func kcOutputFormats(client *verda.Client) map[string]*cmdutil.TestFactory { - return map[string]*cmdutil.TestFactory{ - "table": tableFactory(client, nil), - "json": jsonFactory(client), - "yaml": yamlFactory(client), - "agent": agentFactory(client), + api := kcFixtureAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 1)), "kubeconfig", "describe") + if err != nil || !strings.Contains(out, "prod-kc-bbb") || !strings.Contains(out, "ci-bot") { + t.Fatalf("out=%q err=%v", out, err) } -} - -func assertNoPrivateKey(t *testing.T, label string, outs ...string) { - t.Helper() - for _, s := range outs { - if strings.Contains(s, kcPrivateKeyMarker) || strings.Contains(s, "client-key-data") || strings.Contains(s, "apiVersion: v1") { - t.Errorf("%s: the private key reached the output:\n%s", label, s) - } + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs"}) { + t.Errorf("requests = %v", got) } } -// Security: the YAML (with the client key) never reaches stdout, stderr, --debug or an error, in any format. +// S1: the YAML (with the client key) never reaches stdout, stderr, --debug or +// an error, for a person or an agent. func TestKubeconfigOutputNeverHasThePrivateKey(t *testing.T) { t.Parallel() - commands := map[string][]string{ - "describe": {"kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod"}, - "describe other": {"kubeconfig", "describe", "prod-kc-bbb", "--cluster", "prod", "-l", "FIN-03"}, - "list": {"kubeconfig", "list", "--cluster", "prod"}, - "delete": {"kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "--yes"}, - "describe 404": {"kubeconfig", "describe", "ghost", "--cluster", "prod"}, - } - for cname, args := range commands { - for fname := range kcOutputFormats(nil) { - t.Run(cname+"/"+fname, func(t *testing.T) { - t.Parallel() - f := kcOutputFormats(newFakeClient(t, kcFixtureAPI()))[fname] - f.DebugOverride = true - out, errOut, err := runMK8s(t, f, args...) - errText := "" - if err != nil { - errText = err.Error() - } - assertNoPrivateKey(t, cname+"/"+fname, out, errOut, errText) - }) - } - } - - t.Run("API errors", func(t *testing.T) { - t.Parallel() + failingRead := func() *fakeAPI { api := kcFixtureAPI() api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} - f := agentFactory(newFakeClient(t, api)) - f.DebugOverride = true - out, errOut, err := runMK8s(t, f, "kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod") - if err == nil { - t.Fatal("want an error") + return api + } + for cname, tc := range map[string]struct { + api func() *fakeAPI + args func(file string) []string + }{ + "create": {kcAPI, func(file string) []string { return kcArgs(file) }}, + "save": {func() *fakeAPI { return kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) }, func(file string) []string { return kcSaveArgs(file) }}, + "describe": {kcFixtureAPI, func(string) []string { return []string{"kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod"} }}, + "describe 500": {failingRead, func(string) []string { return []string{"kubeconfig", "describe", "prod-kc-aaa", "--cluster", "prod"} }}, + } { + for fname, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ + "table": func(c *verda.Client) *cmdutil.TestFactory { return tableFactory(c, nil) }, + "agent": agentFactory, + } { + f := mk(newFakeClient(t, tc.api())) + f.DebugOverride = true + out, errOut, err := runMK8s(t, f, tc.args(filepath.Join(t.TempDir(), "kc.yaml"))...) + if (err != nil) != (cname == "describe 500") { + t.Errorf("%s/%s: err = %v", cname, fname, err) + } + if err == nil && !strings.Contains(errOut, "DEBUG:") { + t.Errorf("%s/%s: no debug output", cname, fname) + } + assertNoPrivateKey(t, cname+"/"+fname, out, errOut) + kcAssertErrNoKey(t, err) } - assertNoPrivateKey(t, "500", out, errOut, err.Error()) - }) -} - -func TestKubeconfigPickerAndConfirmPathsNeverPrintTheKey(t *testing.T) { - withTerminal(t) - f := tableFactory(newFakeClient(t, kcFixtureAPI()), pickerAnswers(0, 0)) - f.DebugOverride = true - out, errOut, err := runMK8s(t, f, "kubeconfig", "describe") - if err != nil { - t.Fatal(err) - } - assertNoPrivateKey(t, "describe pickers", out, errOut) - - f = tableFactory(newFakeClient(t, kcFixtureAPI()), pickerAnswers(0, 0).AddConfirm(true)) - f.DebugOverride = true - out, errOut, err = runMK8s(t, f, "kubeconfig", "delete") - if err != nil { - t.Fatal(err) } - assertNoPrivateKey(t, "delete pickers", out, errOut) } diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_file.go b/internal/verda-cli/cmd/mk8s/kubeconfig_file.go index 8e09d982..389caf4b 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_file.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_file.go @@ -30,14 +30,12 @@ import ( var nonFileNameChars = regexp.MustCompile(`[^A-Za-z0-9_-]`) -// inputError is a problem with the file the user chose (taken, a symlink, a -// missing or read-only directory), as opposed to an I/O failure while writing. +// inputError is a problem with the chosen file, not an I/O failure while writing. type inputError struct{ msg string } func (e *inputError) Error() string { return e.msg } -// asUsage turns an inputError into a usage error (VALIDATION_ERROR, exit 2 in -// agent mode); any other error is returned as it is. +// asUsage turns an inputError into a usage error (VALIDATION_ERROR, exit 2). func asUsage(cmd *cobra.Command, err error) error { if ie, ok := errors.AsType[*inputError](err); ok { return cmdutil.UsageErrorf(cmd, "%s", ie.msg) @@ -49,18 +47,15 @@ func existsMessage(path string) string { return path + " already exists; use --force to replace it or --file to choose another path" } -// kubeconfigTarget is where a kubeconfig YAML is written. The YAML holds a -// client private key: the file is 0600, never created over an existing file -// without force, and never written through a symlink. +// kubeconfigTarget: the YAML holds a client private key, so the file is 0600, +// never created over an existing file without force, never written through a symlink. type kubeconfigTarget struct { path string - // defaultDir is true for the default path: its parent (~/.kube) is created - // with 0700 if missing. A custom --file parent must already exist. + // defaultDir: ~/.kube is created 0700 if missing; a custom --file parent must exist. defaultDir bool } -// newKubeconfigTarget resolves --file: empty, or equal to the default path, -// gives ~/.kube/verda-..yaml. +// newKubeconfigTarget: --file empty or equal to the default path gives ~/.kube/verda-..yaml. func newKubeconfigTarget(file, cluster, commonName string) (kubeconfigTarget, error) { home, err := os.UserHomeDir() if err != nil { @@ -69,9 +64,8 @@ func newKubeconfigTarget(file, cluster, commonName string) (kubeconfigTarget, er } return kubeconfigTarget{}, fmt.Errorf("find home directory for the default kubeconfig path (use --file): %w", err) } - // '.' is not a sanitized character, so it keeps (cluster "a-b", cn "c") and - // (cluster "a", cn "b-c") apart: with '-' both would be verda-a-b-c.yaml and - // --force would overwrite the other cluster's key. + // '.' cannot come from a sanitized part, so (a-b, c) and (a, b-c) never share + // a file that --force would overwrite. name := "verda-" + sanitizeFileName(cluster) + "." + sanitizeFileName(commonName) + ".yaml" def := filepath.Join(home, ".kube", name) if file != "" && filepath.Clean(file) != def { @@ -80,22 +74,19 @@ func newKubeconfigTarget(file, cluster, commonName string) (kubeconfigTarget, er return kubeconfigTarget{path: def, defaultDir: true}, nil } -// sanitizeFileName replaces every character other than letters, digits, '-' and '_' with '-'. func sanitizeFileName(s string) string { return nonFileNameChars.ReplaceAllString(s, "-") } -// Check validates the target before any API call that would issue a -// certificate: the path must be free (or replaceable with force) and its -// directory writable. It leaves nothing behind. +// Check runs before a certificate is issued: the path is free (or replaceable +// with force) and its directory writable. It leaves nothing behind. func (t kubeconfigTarget) Check(force bool) error { if err := t.checkFree(force); err != nil { return err } dir := filepath.Dir(t.path) if t.defaultDir { - // ~/.kube may not exist yet: it is created on write, so probe the - // nearest directory that does. + // ~/.kube is created on write, so probe the nearest directory that exists. dir = nearestExistingDir(dir) } else if _, err := os.Stat(dir); err != nil { return &inputError{msg: fmt.Sprintf("directory %s for the kubeconfig file does not exist or is not readable: %v", dir, err)} @@ -103,7 +94,6 @@ func (t kubeconfigTarget) Check(force bool) error { return probeWritable(dir) } -// checkFree: nothing is at the path, or force may replace a regular file. func (t kubeconfigTarget) checkFree(force bool) error { info, err := os.Lstat(t.path) if errors.Is(err, fs.ErrNotExist) { @@ -138,8 +128,7 @@ func nearestExistingDir(dir string) string { } } -// probeWritable fails now, before a certificate is issued, when the key could -// not be saved afterwards (read-only mount, root-owned ~/.kube after sudo). +// probeWritable catches a read-only mount or a root-owned ~/.kube before a certificate is issued. func probeWritable(dir string) error { f, err := os.CreateTemp(dir, ".verda-write-check-*") if err != nil { @@ -151,10 +140,8 @@ func probeWritable(dir string) error { return nil } -// Write stores data in the target with mode 0600. Without force the file is -// created exclusively (O_EXCL decides, not an earlier check, so a file that -// appears after Check is never overwritten). Force replaces an existing -// regular file through a temp file in the same directory and a rename. +// Write: without force O_EXCL decides, not Check, so a file that appears after +// Check is never overwritten. Force replaces through a temp file and a rename. func (t kubeconfigTarget) Write(data []byte, force bool) error { if err := t.Check(force); err != nil { return err @@ -176,8 +163,7 @@ func (t kubeconfigTarget) Write(data []byte, force bool) error { return t.replace(data) } -// ensureDefaultDir creates ~/.kube with 0700 only when it is missing; an -// existing directory keeps its mode. +// ensureDefaultDir: an existing ~/.kube keeps its mode. func (t kubeconfigTarget) ensureDefaultDir() error { dir := filepath.Dir(t.path) if _, err := os.Stat(dir); err == nil { @@ -189,8 +175,7 @@ func (t kubeconfigTarget) ensureDefaultDir() error { return nil } -// create makes a new file; the 0600 mode is already the most permissive result -// of any umask, so no chmod follows. +// create needs no chmod: 0600 is already the most permissive result of any umask. func (t kubeconfigTarget) create(data []byte) error { f, err := os.OpenFile(t.path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) //nolint:gosec // path is the user's --file or the default kubeconfig path if err != nil { diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go index 1199b966..57c02076 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go @@ -15,16 +15,11 @@ package mk8s import ( - "encoding/json" "os" "path/filepath" "runtime" "strings" "testing" - "time" - - "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - "go.yaml.in/yaml/v3" ) func skipModeTestOnWindows(t *testing.T) { @@ -34,112 +29,76 @@ func skipModeTestOnWindows(t *testing.T) { } } -func readFile(t *testing.T, path string) string { - t.Helper() - b, err := os.ReadFile(path) - if err != nil { - t.Fatalf("read %s: %v", path, err) - } - return string(b) -} - -// assertMode checks the POSIX mode; Windows has none, so only that check is skipped. -func assertMode(t *testing.T, path string, want os.FileMode) { - t.Helper() - if runtime.GOOS == "windows" { - return - } - info, err := os.Stat(path) - if err != nil { - t.Fatalf("stat %s: %v", path, err) - } - if got := info.Mode().Perm(); got != want { - t.Errorf("%s mode = %o, want %o", path, got, want) +// The default name keeps every part inside ~/.kube (S5) and never lets two +// clusters share a file: '.' cannot come from a sanitized part. +func TestNewKubeconfigTarget(t *testing.T) { + home := setHome(t) + kube := filepath.Join(home, ".kube") + for name, tc := range map[string]struct { + file, cluster, cn string + want string + defaultDir bool + }{ + "default": {"", "prod", "alice@corp.com", filepath.Join(kube, "verda-prod.alice-corp-com.yaml"), true}, + "path traversal": {"", "../etc", `a b/c\d`, filepath.Join(kube, "verda----etc.a-b-c-d.yaml"), true}, + "custom": {"/x/y.yaml", "prod", "alice", "/x/y.yaml", false}, + "same as default": {filepath.Join(kube, "verda-prod.alice.yaml"), "prod", "alice", filepath.Join(kube, "verda-prod.alice.yaml"), true}, + } { + tgt, err := newKubeconfigTarget(tc.file, tc.cluster, tc.cn) + if err != nil || tgt.path != tc.want || tgt.defaultDir != tc.defaultDir { + t.Errorf("%s: target = %+v, err = %v, want %s", name, tgt, err, tc.want) + } } -} -func TestSanitizeFileName(t *testing.T) { - t.Parallel() - cases := map[string]string{ - "alice": "alice", - "a_b-c9": "a_b-c9", - "alice@corp.com": "alice-corp-com", - "a b/c\\d": "a-b-c-d", - "../etc": "---etc", - } - for in, want := range cases { - if got := sanitizeFileName(in); got != want { - t.Errorf("sanitizeFileName(%q) = %q, want %q", in, got, want) + // With '-' as the separator, the first two pairs would both be verda-a-b-c.yaml. + seen := map[string][2]string{} + for _, p := range [][2]string{{"a-b", "c"}, {"a", "b-c"}, {"a-b-c", ""}, {"", "a-b-c"}, {"a", ""}, {"", "a"}} { + tgt, err := newKubeconfigTarget("", p[0], p[1]) + if err != nil { + t.Fatal(err) + } + if prev, dup := seen[tgt.path]; dup { + t.Errorf("%v and %v both map to %s", prev, p, tgt.path) } + seen[tgt.path] = p } } -func TestNewKubeconfigTarget_DefaultPath(t *testing.T) { - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - - tgt, err := newKubeconfigTarget("", "prod", "alice@corp.com") - if err != nil { - t.Fatal(err) - } - want := filepath.Join(home, ".kube", "verda-prod.alice-corp-com.yaml") - if tgt.path != want { - t.Errorf("path = %q, want %q", tgt.path, want) +// Check fails, before a certificate is issued, where the write would fail later; +// an existing ~/.kube keeps its mode. +func TestKubeconfigTarget_CheckRefuses(t *testing.T) { + skipModeTestOnWindows(t) + if os.Geteuid() == 0 { + t.Skip("root can write anywhere") } - if !tgt.defaultDir { - t.Error("default path must create its parent") + readOnly := func(dir string) string { + if err := os.Chmod(dir, 0o500); err != nil { //nolint:gosec // test: a read-only directory + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chmod(dir, 0o700) }) //nolint:gosec // test cleanup + return dir } - other, _ := newKubeconfigTarget("", "prod", "bob") - if other.path == tgt.path { - t.Error("different common names must give different files") + dir := readOnly(t.TempDir()) + if err := (kubeconfigTarget{path: filepath.Join(dir, "kc.yaml")}).Check(false); err == nil || !strings.Contains(err.Error(), "cannot write") { + t.Errorf("read-only directory: %v", err) } - - custom, _ := newKubeconfigTarget("/x/y.yaml", "prod", "alice") - if custom.path != "/x/y.yaml" || custom.defaultDir { - t.Errorf("custom target = %+v", custom) + if entries, _ := os.ReadDir(dir); len(entries) != 0 { + t.Errorf("Check left files behind: %v", entries) } -} - -func TestKubeconfigTarget_DefaultCreatesKubeDir0700(t *testing.T) { - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - tgt, err := newKubeconfigTarget("", "prod", "alice") - if err != nil { - t.Fatal(err) - } - if err := tgt.Check(false); err != nil { - t.Fatalf("Check with missing ~/.kube: %v", err) - } - if _, err := os.Stat(filepath.Join(home, ".kube")); err == nil { - t.Fatal("Check must not create ~/.kube") - } - if err := tgt.Write([]byte(kcPrivateKeyMarker), false); err != nil { - t.Fatal(err) + readOnly(setHome(t)) + if tgt, err := newKubeconfigTarget("", "prod", "alice"); err != nil || tgt.Check(false) == nil { + t.Errorf("no ~/.kube and a read-only home: Check must fail (%v)", err) } - assertMode(t, filepath.Join(home, ".kube"), 0o700) - assertMode(t, tgt.path, 0o600) - if got := readFile(t, tgt.path); got != kcPrivateKeyMarker { - t.Errorf("content = %q", got) - } -} -func TestKubeconfigTarget_ExistingKubeDirKeepsMode(t *testing.T) { - skipModeTestOnWindows(t) - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - kube := filepath.Join(home, ".kube") - if err := os.Mkdir(kube, 0o755); err != nil { + kube := filepath.Join(setHome(t), ".kube") + if err := os.Mkdir(kube, 0o755); err != nil { //nolint:gosec // test: a loose existing ~/.kube t.Fatal(err) } - if err := os.Chmod(kube, 0o755); err != nil { + if err := os.Chmod(kube, 0o755); err != nil { //nolint:gosec // test: a loose existing ~/.kube t.Fatal(err) } - tgt, _ := newKubeconfigTarget("", "prod", "alice") if err := tgt.Write([]byte("x"), false); err != nil { t.Fatal(err) @@ -148,98 +107,6 @@ func TestKubeconfigTarget_ExistingKubeDirKeepsMode(t *testing.T) { assertMode(t, tgt.path, 0o600) } -func TestKubeconfigTarget_CustomParentMustExist(t *testing.T) { - t.Parallel() - tgt := kubeconfigTarget{path: filepath.Join(t.TempDir(), "missing", "kc.yaml")} - if err := tgt.Check(false); err == nil { - t.Fatal("Check must fail when the parent directory is missing") - } - if err := tgt.Write([]byte("x"), false); err == nil { - t.Fatal("Write must fail when the parent directory is missing") - } - if _, err := os.Stat(filepath.Dir(tgt.path)); err == nil { - t.Error("a custom --file parent must not be created") - } -} - -func TestKubeconfigTarget_ParentIsFile(t *testing.T) { - t.Parallel() - dir := t.TempDir() - parent := filepath.Join(dir, "afile") - if err := os.WriteFile(parent, []byte("x"), 0o600); err != nil { - t.Fatal(err) - } - if err := (kubeconfigTarget{path: filepath.Join(parent, "kc.yaml")}).Check(false); err == nil { - t.Fatal("a parent that is a file must be rejected") - } -} - -func TestKubeconfigTarget_NewFile0600(t *testing.T) { - t.Parallel() - tgt := kubeconfigTarget{path: filepath.Join(t.TempDir(), "kc.yaml")} - if err := tgt.Write([]byte(kcPrivateKeyMarker), false); err != nil { - t.Fatal(err) - } - assertMode(t, tgt.path, 0o600) - if got := readFile(t, tgt.path); got != kcPrivateKeyMarker { - t.Errorf("content = %q", got) - } -} - -func TestKubeconfigTarget_NoOverwriteWithoutForce(t *testing.T) { - t.Parallel() - path := filepath.Join(t.TempDir(), "kc.yaml") - if err := os.WriteFile(path, []byte("original"), 0o600); err != nil { - t.Fatal(err) - } - tgt := kubeconfigTarget{path: path} - - err := tgt.Check(false) - if err == nil || !strings.Contains(err.Error(), "--force") { - t.Fatalf("Check error = %v, want mention of --force", err) - } - if err := tgt.Write([]byte("new"), false); err == nil { - t.Fatal("Write must not overwrite without force") - } - if got := readFile(t, path); got != "original" { - t.Errorf("file changed to %q", got) - } -} - -func TestKubeconfigTarget_ForceReplacesAtomically(t *testing.T) { - t.Parallel() - dir := t.TempDir() - path := filepath.Join(dir, "kc.yaml") - if err := os.WriteFile(path, []byte("original"), 0o644); err != nil { //nolint:gosec // test: a permissive old file must end up 0600 - t.Fatal(err) - } - tgt := kubeconfigTarget{path: path} - if err := tgt.Check(true); err != nil { - t.Fatalf("Check with force: %v", err) - } - if err := tgt.Write([]byte("new"), true); err != nil { - t.Fatal(err) - } - if got := readFile(t, path); got != "new" { - t.Errorf("content = %q", got) - } - assertMode(t, path, 0o600) - - entries, _ := os.ReadDir(dir) - if len(entries) != 1 { - t.Errorf("temp file left behind: %v", entries) - } -} - -func TestKubeconfigTarget_ForceOnNewFileCreates(t *testing.T) { - t.Parallel() - tgt := kubeconfigTarget{path: filepath.Join(t.TempDir(), "kc.yaml")} - if err := tgt.Write([]byte("x"), true); err != nil { - t.Fatal(err) - } - assertMode(t, tgt.path, 0o600) -} - func TestKubeconfigTarget_RejectsSymlink(t *testing.T) { t.Parallel() if runtime.GOOS == "windows" { @@ -268,28 +135,6 @@ func TestKubeconfigTarget_RejectsSymlink(t *testing.T) { } } -func TestNewKubeconfigTarget_NoCollisions(t *testing.T) { - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - - // With '-' as the separator, the first two pairs would both be verda-a-b-c.yaml. - // Names that differ only in replaced characters (b.c vs b-c) still share a - // file for one cluster; only the cluster/common-name boundary is guaranteed. - pairs := [][2]string{{"a-b", "c"}, {"a", "b-c"}, {"a-b-c", ""}, {"", "a-b-c"}, {"a", ""}, {"", "a"}} - seen := map[string][2]string{} - for _, p := range pairs { - tgt, err := newKubeconfigTarget("", p[0], p[1]) - if err != nil { - t.Fatal(err) - } - if prev, dup := seen[tgt.path]; dup { - t.Errorf("%v and %v both map to %s", prev, p, tgt.path) - } - seen[tgt.path] = p - } -} - func TestKubeconfigTarget_CreateIsExclusive(t *testing.T) { t.Parallel() dir := t.TempDir() @@ -311,152 +156,3 @@ func TestKubeconfigTarget_CreateIsExclusive(t *testing.T) { t.Errorf("file changed to %q", got) } } - -func TestKubeconfigTarget_CreateRefusesPlantedSymlink(t *testing.T) { - t.Parallel() - if runtime.GOOS == "windows" { - t.Skip("symlinks need privileges on Windows") - } - dir := t.TempDir() - victim := filepath.Join(dir, "victim") - link := filepath.Join(dir, "kc.yaml") - if err := os.Symlink(victim, link); err != nil { // dangling: O_EXCL must still refuse - t.Fatal(err) - } - tgt := kubeconfigTarget{path: link} - if err := tgt.Check(true); err == nil || !strings.Contains(err.Error(), "symlink") { - t.Errorf("Check on a dangling symlink = %v, want symlink error", err) - } - if err := tgt.create([]byte(kcPrivateKeyMarker)); err == nil { - t.Error("create must refuse a symlink planted after Check") - } - if _, err := os.Stat(victim); err == nil { - t.Error("the symlink target was created") - } -} - -func TestKubeconfigTarget_UnwritableDirFailsCheck(t *testing.T) { - t.Parallel() - skipModeTestOnWindows(t) - if os.Geteuid() == 0 { - t.Skip("root can write anywhere") - } - dir := t.TempDir() - if err := os.Chmod(dir, 0o500); err != nil { //nolint:gosec // test: a read-only directory - t.Fatal(err) - } - t.Cleanup(func() { _ = os.Chmod(dir, 0o700) }) //nolint:gosec // test cleanup - - tgt := kubeconfigTarget{path: filepath.Join(dir, "kc.yaml")} - if err := tgt.Check(false); err == nil || !strings.Contains(err.Error(), "cannot write") { - t.Errorf("Check in a read-only directory = %v, want a cannot-write error", err) - } - if entries, _ := os.ReadDir(dir); len(entries) != 0 { - t.Errorf("Check left files behind: %v", entries) - } -} - -func TestKubeconfigTarget_UnwritableHomeFailsCheckWithoutKubeDir(t *testing.T) { - skipModeTestOnWindows(t) - if os.Geteuid() == 0 { - t.Skip("root can write anywhere") - } - home := t.TempDir() - if err := os.Chmod(home, 0o500); err != nil { //nolint:gosec // test: a read-only home - t.Fatal(err) - } - t.Cleanup(func() { _ = os.Chmod(home, 0o700) }) //nolint:gosec // test cleanup - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) - - tgt, err := newKubeconfigTarget("", "prod", "alice") - if err != nil { - t.Fatal(err) - } - if err := tgt.Check(false); err == nil { - t.Fatal("Check must fail when ~/.kube is missing and the home directory is not writable") - } -} - -func TestKubeconfigTarget_RejectsDirectory(t *testing.T) { - t.Parallel() - tgt := kubeconfigTarget{path: t.TempDir()} - for _, force := range []bool{false, true} { - if err := tgt.Check(force); err == nil || !strings.Contains(err.Error(), "directory") { - t.Errorf("Check(force=%v) = %v, want directory error", force, err) - } - } -} - -func TestKubeconfigView_NeverContainsYAML(t *testing.T) { - t.Parallel() - notAfter := time.Date(2027, 2, 1, 0, 0, 0, 0, time.UTC) - k := &verda.ManagedKubernetesKubeconfig{ - ID: "prod-kc-abc", - DisplayName: "ci", - CommonName: "alice", - Groups: []string{"system:masters"}, - Status: verda.ManagedKubernetesKubeconfigStatusReady, - NotAfter: ¬After, - Kubeconfig: "client-key-data: " + kcPrivateKeyMarker, - } - v := newKubeconfigView(k, "/home/a/.kube/kc.yaml") - - j, err := json.Marshal(v) - if err != nil { - t.Fatal(err) - } - y, err := yaml.Marshal(v) - if err != nil { - t.Fatal(err) - } - for name, out := range map[string]string{"json": string(j), "yaml": string(y)} { - if strings.Contains(out, kcPrivateKeyMarker) || strings.Contains(out, "client-key-data") { - t.Errorf("%s leaks the kubeconfig YAML: %s", name, out) - } - if !strings.Contains(out, "prod-kc-abc") || !strings.Contains(out, "/home/a/.kube/kc.yaml") { - t.Errorf("%s misses metadata: %s", name, out) - } - } - - var m map[string]any - if err := json.Unmarshal(j, &m); err != nil { - t.Fatal(err) - } - for _, key := range []string{"id", "display_name", "common_name", "groups", "status", "not_after", "file"} { - if _, ok := m[key]; !ok { - t.Errorf("view JSON misses %q", key) - } - } - if len(m) != 7 { - t.Errorf("view has %d fields, want exactly 7: %v", len(m), m) - } -} - -func TestKubeconfigView_StableShape(t *testing.T) { - t.Parallel() - // A pending kubeconfig: no groups, no expiry, nothing written yet. - v := newKubeconfigView(&verda.ManagedKubernetesKubeconfig{ID: "c-kc-1", CommonName: "alice", Status: "pending"}, "") - j, err := json.Marshal(v) - if err != nil { - t.Fatal(err) - } - var m map[string]any - if err := json.Unmarshal(j, &m); err != nil { - t.Fatal(err) - } - for _, key := range []string{"id", "display_name", "common_name", "groups", "status", "not_after"} { - if _, ok := m[key]; !ok { - t.Errorf("pending view JSON misses %q: %s", key, j) - } - } - if g, ok := m["groups"].([]any); !ok || len(g) != 0 { - t.Errorf("groups = %v, want []", m["groups"]) - } - if m["not_after"] != nil { - t.Errorf("not_after = %v, want null", m["not_after"]) - } - if _, ok := m["file"]; ok { - t.Errorf("file must be absent before a write: %s", j) - } -} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_list.go b/internal/verda-cli/cmd/mk8s/kubeconfig_list.go index b3d03903..a4323e8d 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_list.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_list.go @@ -59,14 +59,10 @@ func NewCmdKubeconfigList(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra } func runKubeconfigList(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope) error { - if missing := scope.missing(f, cmd); len(missing) > 0 { - return cmdutil.NewMissingFlagsError(missing) - } - client, err := f.VerdaClient() - if err != nil { + if err := scope.check(f, cmd, nil, ""); err != nil { return err } - target, err := scope.resolve(cmd, f, ioStreams, client) + client, target, err := scope.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } @@ -93,8 +89,8 @@ func runKubeconfigList(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. return tw.Flush() } -// listKubeconfigViews and getKubeconfigView convert inside the call, so an SDK -// struct (with the YAML and its private key) never reaches a printer or the debug log. +// listKubeconfigViews and getKubeconfigView convert inside the call, so the SDK +// struct (with the private key) never reaches a printer or the debug log. func listKubeconfigViews(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget) ([]KubeconfigView, error) { return apiCall(ctx, f, "Loading kubeconfigs...", func(ctx context.Context) ([]KubeconfigView, error) { list, err := client.ManagedKubernetes.ListKubeconfigs(ctx, t.Location, t.Name) @@ -120,22 +116,8 @@ func getKubeconfigView(ctx context.Context, f cmdutil.Factory, client *verda.Cli }) } -// checkKubeconfigTarget fails before any call when --cluster or cannot be -// asked for. It lists everything missing at once. -func checkKubeconfigTarget(f cmdutil.Factory, cmd *cobra.Command, scope *poolScope, args []string) error { - missing := scope.missing(f, cmd) - if poolArgMissing(f, args) { - missing = append(missing, argKubeconfigRef) - } - if len(missing) > 0 { - return cmdutil.NewMissingFlagsError(missing) - } - return nil -} - -// resolveKubeconfig: a named id is read when read is set and used as given -// otherwise; with no id a picker lists the cluster's kubeconfigs. A nil result -// with a nil error means the user canceled or the cluster has none. +// resolveKubeconfig: a named id is read only when read is set; with no id a +// picker. A nil result with a nil error means canceled or none. func resolveKubeconfig(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, args []string, read bool) (*KubeconfigView, error) { if len(args) > 0 { id := strings.TrimSpace(args[0]) @@ -182,7 +164,7 @@ func groupsColumn(groups []string) string { return cmdutil.TextColumn(strings.Join(groups, ",")) } -// expiresColumn is "-" while the kubeconfig is pending: the API sets not_after when it issues the certificate. +// expiresColumn is "-" while pending: the API sets not_after when it issues the certificate. func expiresColumn(t *time.Time) string { if t == nil { return "-" diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go index cca402bf..17b20ef6 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go @@ -15,7 +15,6 @@ package mk8s import ( - "context" "reflect" "slices" "strings" @@ -23,10 +22,6 @@ import ( "time" "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - "go.yaml.in/yaml/v3" - - "github.com/verda-cloud/verda-cli/pkg/tui" - tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -88,75 +83,26 @@ func TestKubeconfigList_Table(t *testing.T) { } } -func TestKubeconfigList_LocationSkipsTheClusterLookup(t *testing.T) { +// Every key but file is always present: a pending kubeconfig has groups [] and not_after null. +func TestKubeconfigList_Structured(t *testing.T) { t.Parallel() - api := kcFixtureAPI() - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "ls", "--cluster", "prod", "-l", "FIN-03"); err != nil { + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "list", "--cluster", "prod") + if err != nil { t.Fatal(err) } - if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters/FIN-03/prod/kubeconfigs"}) { - t.Errorf("requests = %v", got) + if got := decodeJSON[[]KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()) { + t.Errorf("got %+v\nwant %+v", got, kcWantViews()) } -} - -func TestKubeconfigList_Structured(t *testing.T) { - t.Parallel() - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - } { - t.Run(name, func(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, mk(kcFixtureAPI(), t), "kubeconfig", "list", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - if got := decodeJSON[[]KubeconfigView](t, out); !reflect.DeepEqual(got, kcWantViews()) { - t.Errorf("got %+v\nwant %+v", got, kcWantViews()) - } - for i, m := range decodeJSON[[]map[string]any](t, out) { - if got := sortedKeys(m); !slices.Equal(got, []string{"common_name", "display_name", "groups", "id", "not_after", "status"}) { - t.Errorf("item %d keys = %v", i, got) - } - } - }) - } - - t.Run("yaml", func(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, kcFixtureAPI())), "kubeconfig", "list", "--cluster", "prod") - if err != nil { - t.Fatal(err) + for i, m := range decodeJSON[[]map[string]any](t, out) { + if len(m) != 6 { + t.Errorf("item %d = %v, want the 6 keys of the view", i, m) } - var got []map[string]any - if err := yaml.Unmarshal([]byte(out), &got); err != nil { - t.Fatal(err) - } - if len(got) != 2 || got[0]["id"] != "prod-kc-aaa" || got[1]["status"] != "pending" { - t.Fatalf("got %v", got) - } - if groups, _ := got[0]["groups"].([]any); !reflect.DeepEqual(groups, []any{"system:masters", "dev"}) { - t.Errorf("groups = %v", got[0]["groups"]) - } - if groups, ok := got[1]["groups"].([]any); !ok || len(groups) != 0 { - t.Errorf("pending groups = %#v, want an empty list", got[1]["groups"]) - } - if v, ok := got[1]["not_after"]; !ok || v != nil { - t.Errorf("pending not_after = %#v, want null", v) - } - if _, ok := got[0]["kubeconfig"]; ok { - t.Error("the YAML key must not be in the output") + for _, k := range []string{"common_name", "display_name", "groups", "id", "not_after", "status"} { + if _, ok := m[k]; !ok { + t.Errorf("item %d misses %q", i, k) + } } - }) -} - -func sortedKeys(m map[string]any) []string { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) } - slices.Sort(keys) - return keys } func TestKubeconfigList_Empty(t *testing.T) { @@ -166,97 +112,20 @@ func TestKubeconfigList_Empty(t *testing.T) { if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "verda mk8s kubeconfig create --cluster prod") { t.Errorf("out=%q err=%q error=%v", out, errOut, err) } - for name, f := range map[string]*cmdutil.TestFactory{"json": jsonFactory(client), "agent": agentFactory(client)} { - out, _, err = runMK8s(t, f, "kubeconfig", "list", "--cluster", "prod") - if err != nil || strings.TrimSpace(out) != "[]" { - t.Errorf("%s: empty output = %q, error=%v", name, out, err) - } - } - out, _, err = runMK8s(t, yamlFactory(client), "kubeconfig", "list", "--cluster", "prod") + out, _, err = runMK8s(t, jsonFactory(client), "kubeconfig", "list", "--cluster", "prod") if err != nil || strings.TrimSpace(out) != "[]" { - t.Errorf("yaml: empty output = %q, error=%v", out, err) + t.Errorf("json: empty output = %q, error=%v", out, err) } } func TestKubeconfigList_ClusterErrors(t *testing.T) { t.Parallel() client := newFakeClient(t, kcFixtureAPI()) - for _, f := range []*cmdutil.TestFactory{tableFactory(client, nil), agentFactory(client), jsonFactory(client)} { - _, _, err := runMK8s(t, f, "kubeconfig", "list") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("err = %v", err) - } - if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster"}) { - t.Errorf("missing = %v", missing) - } + _, _, err := runMK8s(t, agentFactory(client), "kubeconfig", "list") + if m, _ := cmdutil.ClassifyError(err).Details["missing"].([]string); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" || !slices.Equal(m, []string{"--cluster"}) { + t.Errorf("err = %v, missing = %v", err, m) } if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "list", "--cluster", "ghost"); agentErrorCode(err) != "NOT_FOUND" { t.Errorf("unknown cluster: %v", err) } - if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "list", "--cluster", " "); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Errorf("blank cluster: %v", err) - } - if _, _, err := runMK8s(t, tableFactory(client, nil), "kubeconfig", "list", "extra", "--cluster", "prod"); err == nil { - t.Error("a positional argument must be rejected") - } -} - -func TestKubeconfigList_Debug(t *testing.T) { - t.Parallel() - f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) - f.DebugOverride = true - _, errOut, err := runMK8s(t, f, "kubeconfig", "list", "--cluster", "prod") - if err != nil || !strings.Contains(errOut, "API response: 2 kubeconfig(s):") || !strings.Contains(errOut, `"common_name": "alice"`) { - t.Errorf("err=%v debug=%q", err, errOut) - } - if strings.Contains(errOut, `"kubeconfig"`) { - t.Errorf("debug output has a kubeconfig key: %q", errOut) - } -} - -func TestKubeconfigList_ClusterPicker(t *testing.T) { - withTerminal(t) - api := kcFixtureAPI() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0)), "kubeconfig", "list") - if err != nil || !strings.Contains(out, "prod-kc-aaa") { - t.Fatalf("out=%q err=%v", out, err) - } - if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs"}) { - t.Errorf("requests = %v", got) - } - - f := tableFactory(newFakeClient(t, kcFixtureAPI()), nil) - f.PrompterOverride = &cancelPrompter{Prompter: pickerAnswers(), selectErr: contextCanceled()} - out, errOut, err := runMK8s(t, f, "kubeconfig", "list") - if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { - t.Errorf("esc: out=%q err=%q error=%v", out, errOut, err) - } -} - -func TestKubeconfigList_NotEnabled(t *testing.T) { - t.Parallel() - api := newFakeAPI() - api.disabled = true - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "list", "--cluster", "prod", "-l", "FIN-03") - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Fatalf("err = %v", err) - } -} - -type kcSelectCall struct { - Message string - Labels []string - Hints bool -} - -// kcSelectRecorder answers like the wrapped prompter and keeps what each Select showed. -type kcSelectRecorder struct { - *tuitest.Prompter - calls []kcSelectCall -} - -func (p *kcSelectRecorder) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { - p.calls = append(p.calls, kcSelectCall{Message: msg, Labels: labels, Hints: tui.ResolveSelectConfig(opts).ShowHints}) - return p.Prompter.Select(ctx, msg, labels, opts...) } diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go index 72bff5d3..77a2ad85 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go @@ -36,7 +36,7 @@ const ( revokeNote = "A downloaded certificate cannot be revoked before it expires; deleting the kubeconfig does not disable it." ) -// currentUserName is the OS user name that pre-fills the common name; a var so tests can set it. +// currentUserName pre-fills the common name; a var so tests can set it. var currentUserName = func() string { if u, err := user.Current(); err == nil && u.Username != "" { // Windows gives DOMAIN\user. @@ -45,8 +45,7 @@ var currentUserName = func() string { return os.Getenv("USER") } -// kubeconfigAsker asks the prompts of `kubeconfig create`. Each ask reports -// ok=false after printing "Canceled." when the user pressed Esc or Ctrl+C. +// kubeconfigAsker: each ask reports ok=false after "Canceled." on Esc or Ctrl+C. type kubeconfigAsker struct { ctx context.Context ioStreams cmdutil.IOStreams @@ -71,7 +70,6 @@ func (a *kubeconfigAsker) selectOne(msg string, labels []string) (idx int, ok bo return idx, true, nil } -// canceled turns a prompt error into (false, nil) for Esc and Ctrl+C, else (false, err). func (a *kubeconfigAsker) canceled(err error) (ok bool, retErr error) { if !cmdutil.IsPromptCancel(err) { return false, err @@ -80,8 +78,7 @@ func (a *kubeconfigAsker) canceled(err error) (ok bool, retErr error) { return false, nil } -// promptKubeconfigCreate asks, in prompt order, for each value its flag did not -// give. It reports false when the user canceled. +// promptKubeconfigCreate asks for each value its flag did not give; false means canceled. func promptKubeconfigCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *kubeconfigCreateOptions, t *clusterTarget) (bool, error) { a := &kubeconfigAsker{ctx: ctx, ioStreams: ioStreams, prompter: f.Prompter()} steps := []func() (bool, error){ @@ -155,7 +152,7 @@ func (a *kubeconfigAsker) askDisplayName(opts *kubeconfigCreateOptions) (bool, e return ok, err } -// askDuration shows the API default as a hint only: an empty answer leaves duration out. +// askDuration: the API default is a hint only; an empty answer leaves duration out. func (a *kubeconfigAsker) askDuration(opts *kubeconfigCreateOptions) (bool, error) { if opts.durationSet { return true, nil @@ -167,7 +164,6 @@ func (a *kubeconfigAsker) askDuration(opts *kubeconfigCreateOptions) (bool, erro return ok, err } -// validateDurationInput applies the request's duration rule; empty leaves the API default. func validateDurationInput(s string) error { req := verda.CreateManagedKubernetesKubeconfigRequest{CommonName: "x", Duration: strings.TrimSpace(s)} if err := req.Validate(); err != nil { @@ -192,8 +188,7 @@ func (a *kubeconfigAsker) askFile(opts *kubeconfigCreateOptions, t *clusterTarge return true, nil } -// kubeconfigFileValidator checks the typed path with the same rules as the write. -// "-" is refused: it would print the private key on the terminal. +// kubeconfigFileValidator refuses "-": it would print the private key on the terminal. func kubeconfigFileValidator(cluster, commonName string, force bool) func(string) error { return func(s string) error { s = strings.TrimSpace(s) @@ -208,8 +203,7 @@ func kubeconfigFileValidator(cluster, commonName string, force bool) func(string } } -// confirmKubeconfigCreate prints the summary and asks. adminFlag: --admin was -// given, so the access prompt (which prints the note itself) did not run. +// confirmKubeconfigCreate: adminFlag means the access prompt, which prints the note, did not run. func confirmKubeconfigCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, req *verda.CreateManagedKubernetesKubeconfigRequest, dest *kubeconfigDest, adminFlag bool) (bool, error) { display, duration, file := req.DisplayName, req.Duration, dest.path() if display == "" { diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go index 2e5a5e78..d3f98196 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go @@ -15,74 +15,20 @@ package mk8s import ( - "context" "os" "path/filepath" "slices" "strings" "testing" - "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - "github.com/verda-cloud/verda-cli/pkg/tui" tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" - - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -type kcPrompt struct { - msg string - def string - placeholder string - hints bool - escLabel string -} - -// kcRecorder wraps a prompter and records what each prompt asked and how. -type kcRecorder struct { - tui.Prompter - texts []kcPrompt - selects []kcPrompt -} - -func (r *kcRecorder) TextInput(ctx context.Context, msg string, opts ...tui.TextInputOption) (string, error) { - var c tui.TextInputConfig - for _, o := range opts { - o(&c) - } - r.texts = append(r.texts, kcPrompt{msg: msg, def: c.Default, placeholder: c.Placeholder}) - return r.Prompter.TextInput(ctx, msg, opts...) -} - -func (r *kcRecorder) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { - var c tui.SelectConfig - for _, o := range opts { - o(&c) - } - r.selects = append(r.selects, kcPrompt{msg: msg, hints: c.ShowHints, escLabel: c.RelabelByID["esc"]}) - return r.Prompter.Select(ctx, msg, labels, opts...) -} - -// asked reports whether a text or select prompt started with prefix. -func (r *kcRecorder) asked(prefix string) bool { - for _, p := range slices.Concat(r.texts, r.selects) { - if strings.HasPrefix(p.msg, prefix) { - return true - } - } - return false -} - -func kcTable(client *verda.Client, p tui.Prompter) *cmdutil.TestFactory { - return &cmdutil.TestFactory{ClientOverride: client, OutputFormatOverride: outputTable, PrompterOverride: p} -} - // kcHome points the default kubeconfig path at a temp dir and fixes the OS user name. func kcHome(t *testing.T) string { t.Helper() - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) + home := setHome(t) orig := currentUserName currentUserName = func() string { return "tester" } t.Cleanup(func() { currentUserName = orig }) @@ -91,349 +37,125 @@ func kcHome(t *testing.T) string { var kcInteractiveBase = []string{"kubeconfig", "create", "--cluster", "prod"} -// kcAssertFullFlowPrompts checks the order, defaults and hints of the prompts of a full interactive create. -func kcAssertFullFlowPrompts(t *testing.T, rec *kcRecorder, home string) { - t.Helper() - msgs := make([]string, 0, len(rec.texts)) - for _, p := range rec.texts { - msgs = append(msgs, p.msg) - } - wantOrder := []string{"Common name", "Display name", "Duration", "File to write"} - if len(msgs) != len(wantOrder) { - t.Fatalf("text prompts = %v", msgs) - } - for i, prefix := range wantOrder { - if !strings.HasPrefix(msgs[i], prefix) { - t.Errorf("prompt %d = %q, want prefix %q", i, msgs[i], prefix) - } - } - if rec.texts[0].def != "tester" { - t.Errorf("common name default = %q, want the OS user name", rec.texts[0].def) - } - if rec.texts[2].def != "" || !strings.Contains(rec.texts[2].placeholder, "90 days") { - t.Errorf("duration prompt = %+v: the 90 day default is a hint only", rec.texts[2]) - } - if want := filepath.Join(home, ".kube", "verda-prod.alice.yaml"); rec.texts[3].def != want { - t.Errorf("file default = %q, want %q", rec.texts[3].def, want) - } - if len(rec.selects) != 1 || rec.selects[0].msg != "Access" || !rec.selects[0].hints || rec.selects[0].escLabel != "esc cancel" { - t.Errorf("selects = %+v, want the access select with hints", rec.selects) - } -} - -func TestKubeconfigCreateInteractive_FullFlow(t *testing.T) { +// Each flag skips its prompt; an empty display name or duration is left out of the request. +func TestKubeconfigCreateInteractive(t *testing.T) { withTerminal(t) - home := kcHome(t) - file := filepath.Join(t.TempDir(), "kc.yaml") - api := kcAPI() - rec := &kcRecorder{Prompter: tuitest.New(). - AddTextInput("alice").AddTextInput("Alice laptop").AddTextInput("720h").AddTextInput(file). - AddSelect(0).AddConfirm(true)} - out, errOut, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), kcInteractiveBase...) - if err != nil { - t.Fatal(err) - } - kcAssertFullFlowPrompts(t, rec, home) - body := kcBody(t, api) - if body["common_name"] != "alice" || body["display_name"] != "Alice laptop" || body["duration"] != "720h" { - t.Errorf("body = %v", body) - } - if g, _ := body["groups"].([]any); len(g) != 1 || g[0] != groupAdmin { - t.Errorf("groups = %v", body["groups"]) - } - if got := readFile(t, file); got != kcYAML { - t.Errorf("file = %q", got) - } - assertMode(t, file, 0o600) - for _, want := range []string{"Create kubeconfig", "alice", "cannot be revoked", "Canceled."} { - if want == "Canceled." { - if strings.Contains(errOut, want) { - t.Errorf("stderr has %q", want) - } - continue + for name, tc := range map[string]struct { + prompter func(file string) *tuitest.Prompter + flags func(file string) []string + file func(home, dir string) string + groups []any + extra map[string]any + }{ + "admin by prompt": { + prompter: func(file string) *tuitest.Prompter { + return tuitest.New().AddTextInput("alice").AddSelect(0).AddTextInput("Alice laptop").AddTextInput("720h").AddTextInput(file).AddConfirm(true) + }, + file: func(_, dir string) string { return filepath.Join(dir, "kc.yaml") }, + groups: []any{groupAdmin}, + extra: map[string]any{"display_name": "Alice laptop", "duration": "720h"}, + }, + "custom groups, default path": { + prompter: func(file string) *tuitest.Prompter { + return tuitest.New().AddTextInput("alice").AddSelect(1).AddTextInput("dev, ops, dev").AddTextInput("").AddTextInput("").AddTextInput(file).AddConfirm(true) + }, + file: func(home, _ string) string { return filepath.Join(home, ".kube", "verda-prod.alice.yaml") }, + groups: []any{"dev", "ops"}, + }, + "all flags: only the confirm": { + prompter: func(string) *tuitest.Prompter { return tuitest.New().AddConfirm(true) }, + flags: func(file string) []string { + return []string{"--common-name", "alice", "--group", "dev", "--display-name", "x", "--duration", "2h", "--file", file} + }, + file: func(_, dir string) string { return filepath.Join(dir, "kc.yaml") }, + groups: []any{"dev"}, + extra: map[string]any{"display_name": "x", "duration": "2h"}, + }, + } { + home := kcHome(t) + file := tc.file(home, t.TempDir()) + args := slices.Clone(kcInteractiveBase) + if tc.flags != nil { + args = append(args, tc.flags(file)...) } - if !strings.Contains(errOut, want) { - t.Errorf("stderr lacks %q: %q", want, errOut) + api := kcAPI() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tc.prompter(file)), args...) + if err != nil { + t.Fatalf("%s: %v", name, err) } - } - assertNoPrivateKey(t, "stdout", out) - assertNoPrivateKey(t, "stderr", errOut) -} - -func TestKubeconfigCreateInteractive_CustomGroupsAndDefaultPath(t *testing.T) { - withTerminal(t) - home := kcHome(t) - api := kcAPI() - def := filepath.Join(home, ".kube", "verda-prod.alice.yaml") - rec := &kcRecorder{Prompter: tuitest.New(). - AddTextInput("alice").AddTextInput("dev, ops, dev").AddTextInput("").AddTextInput("").AddTextInput(def). - AddSelect(1).AddConfirm(true)} - if _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), kcInteractiveBase...); err != nil { - t.Fatal(err) - } - body := kcBody(t, api) - if g, _ := body["groups"].([]any); len(g) != 2 || g[0] != "dev" || g[1] != "ops" { - t.Errorf("groups = %v", body["groups"]) - } - for _, key := range []string{"duration", "display_name"} { - if _, ok := body[key]; ok { - t.Errorf("%s sent although the answer was empty: %v", key, body) + body := postBodyAny(t, api) + if g, _ := body["groups"].([]any); body["common_name"] != "alice" || !slices.Equal(g, tc.groups) { + t.Errorf("%s: body = %v", name, body) } - } - // The default path keeps default-path behavior: ~/.kube is created with 0700. - if got := readFile(t, def); got != kcYAML { - t.Errorf("file = %q", got) - } - assertMode(t, filepath.Dir(def), 0o700) -} - -func TestKubeconfigCreateInteractive_EachFlagSkipsItsPrompt(t *testing.T) { - withTerminal(t) - file := filepath.Join(t.TempDir(), "flag.yaml") - cases := []struct { - name string - flags []string - prefix string - }{ - {"common name", []string{"--common-name", "bob"}, "Common name"}, - {"admin", []string{"--admin"}, "Access"}, - {"group", []string{"--group", "dev"}, "Access"}, - {"display name", []string{"--display-name", "x"}, "Display name"}, - {"duration", []string{"--duration", "720h"}, "Duration"}, - {"file", []string{"--file", file}, "File to write"}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - home := kcHome(t) - def := filepath.Join(home, ".kube", "verda-prod.bob.yaml") - // Answers in prompt order, minus the prompt whose flag is set. - p := tuitest.New().AddSelect(0).AddConfirm(true) - for _, a := range []struct{ prefix, answer string }{{"Common name", "bob"}, {"Display name", ""}, {"Duration", ""}, {"File to write", def}} { - if a.prefix != tc.prefix { - p.AddTextInput(a.answer) - } - } - rec := &kcRecorder{Prompter: p} - args := append(slices.Clone(kcInteractiveBase), tc.flags...) - if _, _, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), rec), args...); err != nil { - t.Fatal(err) + for _, key := range []string{"duration", "display_name"} { + if got, ok := body[key]; got != tc.extra[key] || ok != (tc.extra[key] != nil) { + t.Errorf("%s: %s = %v", name, key, got) } - if rec.asked(tc.prefix) { - t.Errorf("%q was asked although its flag is set", tc.prefix) - } - }) - } -} - -func TestKubeconfigCreateInteractive_AllFlagsOnlyConfirm(t *testing.T) { - withTerminal(t) - kcHome(t) - api := kcAPI() - file := filepath.Join(t.TempDir(), "kc.yaml") - rec := &kcRecorder{Prompter: tuitest.New().AddConfirm(true)} - args := append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--group", "dev", "--display-name", "x", "--duration", "2h", "--file", file) - if _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), args...); err != nil { - t.Fatal(err) - } - if len(rec.texts) != 0 || len(rec.selects) != 0 { - t.Errorf("prompts asked: texts=%v selects=%v", rec.texts, rec.selects) - } - if body := kcBody(t, api); body["duration"] != "2h" || body["display_name"] != "x" { - t.Errorf("body = %v", body) - } -} - -func TestKubeconfigCreateInteractive_FileDashSkipsFilePromptAndPrintsYAML(t *testing.T) { - withTerminal(t) - kcHome(t) - rec := &kcRecorder{Prompter: tuitest.New().AddConfirm(true)} - args := append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--admin", "--display-name", "", "--duration", "", "--file", "-") - out, errOut, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), rec), args...) - if err != nil { - t.Fatal(err) - } - if out != kcYAML || len(rec.texts) != 0 { - t.Errorf("stdout = %q, text prompts = %v", out, rec.texts) - } - assertNoPrivateKey(t, "stderr", errOut) - if !strings.Contains(errOut, "File: stdout") { - t.Errorf("summary = %q", errOut) - } -} - -func TestKubeconfigCreateInteractive_PicksTheClusterWhenOmitted(t *testing.T) { - withTerminal(t) - kcHome(t) - api := kcAPI() - file := filepath.Join(t.TempDir(), "kc.yaml") - rec := &kcRecorder{Prompter: tuitest.New().AddSelect(0).AddConfirm(true)} - args := []string{"kubeconfig", "create", "--common-name", "bob", "--admin", "--display-name", "", "--duration", "", "--file", file} - if _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), args...); err != nil { - t.Fatal(err) - } - if len(rec.selects) != 1 || rec.selects[0].msg != "Select cluster" || !rec.selects[0].hints || rec.selects[0].escLabel != "esc cancel" { - t.Errorf("selects = %+v", rec.selects) - } - if reqs := api.requestsMatching("POST"); len(reqs) != 1 || reqs[0].Path != "/mk8sclusters/FIN-03/prod/kubeconfigs" { - t.Errorf("POSTs = %v", reqs) + } + if got := readFile(t, file); got != kcYAML { + t.Errorf("%s: file = %q", name, got) + } + assertMode(t, file, 0o600) + // The access prompt prints the revoke note; --group skips it. + if !strings.Contains(errOut, "Create kubeconfig") || strings.Contains(errOut, "cannot be revoked") != (tc.flags == nil) { + t.Errorf("%s: stderr = %q", name, errOut) + } + assertNoPrivateKey(t, "stdout", out) + assertNoPrivateKey(t, "stderr", errOut) } } func TestKubeconfigCreateInteractive_CancelAndDecline(t *testing.T) { withTerminal(t) kcHome(t) - full := func() []string { - return append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--admin", "--display-name", "", "--duration", "", "--file", filepath.Join(t.TempDir(), "kc.yaml")) - } - cases := map[string]struct { + full := append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--admin", "--display-name", "", "--duration", "", "--file", filepath.Join(t.TempDir(), "kc.yaml")) + for name, tc := range map[string]struct { prompter tui.Prompter args []string + err string // a prompt failure that is not a cancel is returned }{ - "Esc at a text prompt": {&textCancelPrompter{Prompter: tuitest.New()}, kcInteractiveBase}, - "Ctrl+C at the select": {&cancelPrompter{Prompter: tuitest.New(), selectErr: errInterrupted()}, append(slices.Clone(kcInteractiveBase), "--common-name", "bob")}, - "Esc at the confirm": {&cancelPrompter{Prompter: tuitest.New(), confirmErr: contextCanceled()}, full()}, - "Ctrl+C at the confirm": {&cancelPrompter{Prompter: tuitest.New(), confirmErr: errInterrupted()}, full()}, - "decline the summary": {confirmNo(), full()}, - "Esc at the cluster pick": {&cancelPrompter{Prompter: tuitest.New(), selectErr: contextCanceled()}, []string{"kubeconfig", "create", "--common-name", "bob", "--admin"}}, - } - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - api := kcAPI() - out, errOut, err := runMK8s(t, kcTable(newFakeClient(t, api), tc.prompter), tc.args...) - if err != nil { - t.Fatalf("a cancel is a clean exit, got %v", err) - } - if !strings.Contains(errOut, "Canceled.") || out != "" { - t.Errorf("stdout = %q, stderr = %q", out, errOut) - } - if n := kcPosts(api); n != 0 { - t.Errorf("%d POST(s) after a cancel", n) - } - }) - } -} - -func TestKubeconfigCreateInteractive_PromptFailureIsReturned(t *testing.T) { - withTerminal(t) - kcHome(t) - // No answers queued: the fake prompter fails, which is not a cancel. - _, _, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), tuitest.New()), kcInteractiveBase...) - if err == nil || strings.Contains(err.Error(), "Canceled") { - t.Errorf("err = %v", err) - } -} - -func TestKubeconfigCreateInteractive_ExistingFileStopsBeforeConfirmAndPOST(t *testing.T) { - withTerminal(t) - home := kcHome(t) - def := filepath.Join(home, ".kube", "verda-prod.bob.yaml") - if err := os.MkdirAll(filepath.Dir(def), 0o700); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(def, []byte("old"), 0o600); err != nil { - t.Fatal(err) - } - api := kcAPI() - // No confirm queued: reaching it would fail with a different error. - p := tuitest.New().AddTextInput("").AddTextInput("").AddTextInput(def) - args := append(slices.Clone(kcInteractiveBase), "--common-name", "bob", "--admin") - _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), p), args...) - if err == nil || !strings.Contains(err.Error(), "--force") || kcPosts(api) != 0 { - t.Errorf("err = %v, POSTs = %d", err, kcPosts(api)) - } - if got := readFile(t, def); got != "old" { - t.Errorf("file = %q", got) - } -} - -// A named --file that is taken stops the command before any prompt or API call. -func TestKubeconfigCreateInteractive_ExistingFlagFileStopsBeforePrompts(t *testing.T) { - withTerminal(t) - file := filepath.Join(t.TempDir(), "kc.yaml") - if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { - t.Fatal(err) - } - api := kcAPI() - rec := &kcRecorder{Prompter: tuitest.New()} - _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), "kubeconfig", "create", "--cluster", "prod", "--file", file) - if err == nil || !strings.Contains(err.Error(), "--force") { - t.Errorf("err = %v", err) - } - if len(rec.texts)+len(rec.selects) != 0 || len(api.requests) != 0 { - t.Errorf("prompts = %v %v, requests = %v: want none", rec.texts, rec.selects, api.requests) - } -} - -// A blank --cluster is an error in create and save, as in list and describe: no picker. -func TestKubeconfigCreateAndSave_BlankClusterNeverPicks(t *testing.T) { - withTerminal(t) - for _, args := range [][]string{{"kubeconfig", "create", "--cluster", ""}, {"kubeconfig", "save", "prod-kc-1", "--cluster", ""}} { - rec := &kcRecorder{Prompter: tuitest.New()} - _, _, err := runMK8s(t, kcTable(newFakeClient(t, kcAPI()), rec), args...) - if agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" || len(rec.texts)+len(rec.selects) != 0 { - t.Errorf("%v: err = %v, prompts = %v %v", args, err, rec.texts, rec.selects) - } - } -} - -func TestKubeconfigCreate_NeverPromptsOffTheTable(t *testing.T) { - withTerminal(t) - for name, f := range map[string]*cmdutil.TestFactory{ - "json": jsonFactory(newFakeClient(t, kcAPI())), - "agent": agentFactory(newFakeClient(t, kcAPI())), + "Esc at a text prompt": {&textCancelPrompter{Prompter: tuitest.New()}, kcInteractiveBase, ""}, + "Ctrl+C at the select": {&cancelPrompter{Prompter: tuitest.New(), selectErr: errInterrupted()}, append(slices.Clone(kcInteractiveBase), "--common-name", "bob"), ""}, + "decline the summary": {confirmNo(), full, ""}, + "prompt failure": {tuitest.New(), kcInteractiveBase, "no text input responses queued"}, } { - rec := &kcRecorder{Prompter: tuitest.New()} - f.PrompterOverride = rec - _, _, err := runMK8s(t, f, "kubeconfig", "create") - if agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Errorf("%s: err = %v", name, err) + api := kcAPI() + f := tableFactory(newFakeClient(t, api), nil) + f.PrompterOverride = tc.prompter + out, errOut, err := runMK8s(t, f, tc.args...) + if tc.err != "" { + if err == nil || !strings.Contains(err.Error(), tc.err) || strings.Contains(errOut, canceledMsg) || kcPosts(api) != 0 { + t.Errorf("%s: err = %v, stderr = %q", name, err, errOut) + } + continue } - if len(rec.texts)+len(rec.selects) != 0 { - t.Errorf("%s prompted", name) + if err != nil || !strings.Contains(errOut, canceledMsg) || out != "" || kcPosts(api) != 0 { + t.Errorf("%s: err = %v, stdout = %q, stderr = %q, POSTs = %d", name, err, out, errOut, kcPosts(api)) } } } -func TestKubeconfigFileValidator(t *testing.T) { +func TestKubeconfigPromptValidators(t *testing.T) { t.Parallel() dir := t.TempDir() existing := filepath.Join(dir, "exists.yaml") if err := os.WriteFile(existing, []byte("x"), 0o600); err != nil { t.Fatal(err) } - if err := kubeconfigFileValidator("prod", "alice", false)("-"); err == nil { - t.Error("- must be refused: it prints the key on the terminal") - } - if err := kubeconfigFileValidator("prod", "alice", false)(existing); err == nil || !strings.Contains(err.Error(), "--force") { - t.Errorf("existing file without force: %v", err) - } - if err := kubeconfigFileValidator("prod", "alice", true)(existing); err != nil { - t.Errorf("existing file with force: %v", err) - } - if err := kubeconfigFileValidator("prod", "alice", false)(filepath.Join(dir, "new.yaml")); err != nil { - t.Errorf("new file: %v", err) - } - if err := kubeconfigFileValidator("prod", "alice", false)(filepath.Join(dir, "no", "such", "new.yaml")); err == nil { - t.Error("a missing parent directory must be refused") - } -} - -func TestValidateDurationInput(t *testing.T) { - t.Parallel() - for in, ok := range map[string]bool{"": true, " ": true, "720h": true, "1h": true, "5m": false, "30m": false, "abc": false, "-1h": false} { - err := validateDurationInput(in) - if (err == nil) != ok { - t.Errorf("validateDurationInput(%q) = %v, ok=%v", in, err, ok) - } - if err != nil && strings.HasSuffix(err.Error(), ".") { - t.Errorf("validateDurationInput(%q) = %q, trailing dot", in, err) + for name, tc := range map[string]struct { + err error + want string // "" means accepted + }{ + "- prints the key on the terminal": {kubeconfigFileValidator("prod", "alice", false)("-"), "enter a file path"}, + "existing file": {kubeconfigFileValidator("prod", "alice", false)(existing), "--force"}, + "existing file with --force": {kubeconfigFileValidator("prod", "alice", true)(existing), ""}, + "missing parent": {kubeconfigFileValidator("prod", "alice", false)(filepath.Join(dir, "no", "new.yaml")), "does not exist"}, + "empty duration: API default": {validateDurationInput(" "), ""}, + "duration": {validateDurationInput("720h"), ""}, + "duration under 1h": {validateDurationInput("30m"), "1h"}, + } { + if (tc.want == "") != (tc.err == nil) || (tc.err != nil && (!strings.Contains(tc.err.Error(), tc.want) || strings.HasSuffix(tc.err.Error(), "."))) { + t.Errorf("%s: err = %v, want %q", name, tc.err, tc.want) } } } - -func TestCurrentUserName(t *testing.T) { - t.Parallel() - if currentUserName() == "" { - t.Skip("no OS user name in this environment") - } -} diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_save.go b/internal/verda-cli/cmd/mk8s/kubeconfig_save.go index 436c229f..480cfe98 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_save.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_save.go @@ -32,14 +32,12 @@ const ( flagFile = "file" flagForce = "force" - // kubeconfigStdout as --file writes the YAML to stdout. kubeconfigStdout = "-" ) // shellSafe leaves out ':', the separator of a KUBECONFIG list. var shellSafe = regexp.MustCompile(`^[A-Za-z0-9_@%+=,./-]+$`) -// shellQuote quotes s for a POSIX shell only when it has characters the shell would interpret. func shellQuote(s string) string { if shellSafe.MatchString(s) { return s @@ -47,7 +45,6 @@ func shellQuote(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" } -// kubeconfigFileFlags are --file and --force, shared by create and save. type kubeconfigFileFlags struct { File string Force bool @@ -70,17 +67,13 @@ func (k *kubeconfigFileFlags) validate(cmd *cobra.Command, f cmdutil.Factory) er return nil } -// kubeconfigDest is where the YAML goes: a checked file, or stdout. type kubeconfigDest struct { - stdout bool - target kubeconfigTarget - // replaced is set by write when an existing file was replaced. - replaced bool + stdout bool + target kubeconfigTarget + replaced bool // set by write } -// destination resolves --file for a cluster and common name and checks the -// target, so a path that cannot be written fails before a certificate is -// issued. A bad path is a usage error. +// destination checks the target before a certificate is issued; a bad path is a usage error. func (k *kubeconfigFileFlags) destination(cmd *cobra.Command, cluster, commonName string) (*kubeconfigDest, error) { if k.File == kubeconfigStdout { return &kubeconfigDest{stdout: true}, nil @@ -95,7 +88,6 @@ func (k *kubeconfigFileFlags) destination(cmd *cobra.Command, cluster, commonNam return &kubeconfigDest{target: target}, nil } -// path is the file written, or - for stdout. func (d *kubeconfigDest) path() string { if d.stdout { return kubeconfigStdout @@ -121,9 +113,8 @@ func kubeconfigListCmd(t *clusterTarget) string { return fmt.Sprintf("verda mk8s kubeconfig list --cluster %s -l %s", shellQuote(t.Name), shellQuote(t.Location)) } -// kubeconfigSaveCmd is the command that writes a ready kubeconfig, for recovery -// messages. The default path is left out (save derives it and creates ~/.kube), -// and so is stdout: a recovery command must not print the key. +// kubeconfigSaveCmd leaves out the default path (save derives it) and stdout: +// a recovery command must not print the key. func kubeconfigSaveCmd(t *clusterTarget, id string, d *kubeconfigDest, force bool) string { s := fmt.Sprintf("verda mk8s kubeconfig save %s --cluster %s -l %s", shellQuote(id), shellQuote(t.Name), shellQuote(t.Location)) if !d.stdout && !d.target.defaultDir { @@ -135,11 +126,8 @@ func kubeconfigSaveCmd(t *clusterTarget, id string, d *kubeconfigDest, force boo return s } -// reportKubeconfigWritten prints the result after the YAML was written: a -// KubeconfigView for structured output (never the YAML), a short text and the -// KUBECONFIG hint for people. earlierStillValid adds the hint that a file's -// earlier kubeconfig is a different credential that still works: only create -// issues a new certificate, save rewrites the one it was asked for. +// reportKubeconfigWritten never prints the YAML. earlierStillValid: only create +// replaces a file's kubeconfig with a different credential. func reportKubeconfigWritten(f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, d *kubeconfigDest, k *verda.ManagedKubernetesKubeconfig, earlierStillValid bool) error { file := "" if !d.stdout { @@ -225,15 +213,10 @@ func runKubeconfigSave(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. if err := opts.validate(cmd, f); err != nil { return err } - opts.Cluster, opts.Location = strings.TrimSpace(opts.Cluster), strings.TrimSpace(opts.Location) - if err := checkKubeconfigTarget(f, cmd, &opts.poolScope, args); err != nil { + if err := opts.check(f, cmd, args, argKubeconfigRef); err != nil { return err } - client, err := f.VerdaClient() - if err != nil { - return err - } - target, err := opts.resolve(cmd, f, ioStreams, client) + client, target, err := opts.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } @@ -261,7 +244,6 @@ func runKubeconfigSave(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. return reportKubeconfigWritten(f, ioStreams, target, dest, k, false) } -// notReadyError is the one "not ready" message of save: not ready, or ready without its content yet. func notReadyError(k *verda.ManagedKubernetesKubeconfig) error { detail := "status " + k.Status if k.Status == verda.ManagedKubernetesKubeconfigStatusReady { diff --git a/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go b/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go index e31e9279..a5f6ad9d 100644 --- a/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go @@ -17,13 +17,11 @@ package mk8s import ( "os" "path/filepath" + "slices" "strings" "testing" "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - "go.yaml.in/yaml/v3" - - tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -45,133 +43,59 @@ func kcSaveArgs(file string, extra ...string) []string { return append(args, extra...) } -func TestKubeconfigSave_WritesFileInEveryOutputMode(t *testing.T) { +// save issues no certificate: it reads the kubeconfig and writes the file. +func TestKubeconfigSave_WritesFile(t *testing.T) { t.Parallel() for name, mk := range map[string]func(*verda.Client) *cmdutil.TestFactory{ "table": func(c *verda.Client) *cmdutil.TestFactory { return tableFactory(c, nil) }, "json": jsonFactory, - "yaml": yamlFactory, - "agent": agentFactory, } { - t.Run(name, func(t *testing.T) { - t.Parallel() - api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - file := filepath.Join(t.TempDir(), "kc.yaml") - out, errOut, err := runMK8s(t, mk(newFakeClient(t, api)), kcSaveArgs(file)...) - if err != nil { - t.Fatal(err) - } - assertNoPrivateKey(t, "stdout", out) - assertNoPrivateKey(t, "stderr", errOut) - if got := readFile(t, file); got != kcYAML { - t.Errorf("file = %q", got) - } - assertMode(t, file, 0o600) - if n := kcPosts(api); n != 0 { - t.Errorf("save made %d POST(s)", n) - } - switch name { - case "table": - if !strings.Contains(errOut, "export KUBECONFIG="+file) || !strings.Contains(errOut, "kubectl get nodes") { - t.Errorf("stderr = %q", errOut) - } - case "yaml": - var v KubeconfigView - if err := yaml.Unmarshal([]byte(out), &v); err != nil || v.File != file || v.ID != "prod-kc-abc" { - t.Errorf("view = %+v err=%v", v, err) - } - default: - if v := decodeJSON[KubeconfigView](t, out); v.File != file || v.ID != "prod-kc-abc" || v.Status != "ready" { - t.Errorf("view = %+v", v) - } + api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) + file := filepath.Join(t.TempDir(), "kc.yaml") + out, errOut, err := runMK8s(t, mk(newFakeClient(t, api)), kcSaveArgs(file)...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + if got := readFile(t, file); got != kcYAML || kcPosts(api) != 0 { + t.Errorf("%s: file = %q, POSTs = %d", name, got, kcPosts(api)) + } + assertMode(t, file, 0o600) + if name == "table" { + if !strings.Contains(errOut, "export KUBECONFIG="+file) { + t.Errorf("stderr = %q", errOut) } - }) + continue + } + if v := decodeJSON[KubeconfigView](t, out); v.File != file || v.ID != "prod-kc-abc" || v.Status != "ready" { + t.Errorf("view = %+v", v) + } } } func TestKubeconfigSave_NotReady(t *testing.T) { t.Parallel() - api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusPending) - file := filepath.Join(t.TempDir(), "kc.yaml") - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcSaveArgs(file)...) - if err == nil || err.Error() != "kubeconfig prod-kc-abc is not ready yet (status pending); run save again in a moment" { - t.Fatalf("err = %v", err) - } - if out != "" { - t.Errorf("stdout = %q", out) - } - if _, statErr := os.Stat(file); statErr == nil { - t.Error("no file may be written for a kubeconfig that is not ready") - } -} - -func TestKubeconfigSave_ReadyWithoutYAMLIsNotReady(t *testing.T) { - t.Parallel() - api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - api.onKubeconfigGet = nil // ready, but the fake serves no YAML - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcSaveArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) - if err == nil || err.Error() != "kubeconfig prod-kc-abc is not ready yet (status ready, content not available yet); run save again in a moment" { - t.Errorf("err = %v", err) - } -} - -func TestKubeconfigSave_FileDash(t *testing.T) { - t.Parallel() - api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcSaveArgs("-")...) - if err != nil { - t.Fatal(err) - } - if out != kcYAML { - t.Errorf("stdout = %q, want exactly the YAML", out) - } - assertNoPrivateKey(t, "stderr", errOut) - - for name, f := range map[string]*cmdutil.TestFactory{ - "json": jsonFactory(newFakeClient(t, api)), - "yaml": yamlFactory(newFakeClient(t, api)), - "agent": agentFactory(newFakeClient(t, api)), + for name, tc := range map[string]struct { + status string + want string + }{ + "pending": {verda.ManagedKubernetesKubeconfigStatusPending, "status pending"}, + "ready without YAML": {verda.ManagedKubernetesKubeconfigStatusReady, "status ready, content not available yet"}, } { - before := len(api.requests) - out, _, err := runMK8s(t, f, kcSaveArgs("-")...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) || out != "" { - t.Errorf("%s: out=%q err=%v", name, out, err) + api := kcSaveAPI(tc.status) + api.onKubeconfigGet = nil // the fake serves no YAML + file := filepath.Join(t.TempDir(), "kc.yaml") + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), kcSaveArgs(file)...) + if want := "kubeconfig prod-kc-abc is not ready yet (" + tc.want + "); run save again in a moment"; err == nil || err.Error() != want || out != "" { + t.Errorf("%s: err = %v, stdout = %q", name, err, out) } - if len(api.requests) != before { - t.Errorf("%s: --file - with structured output reached the API", name) + if _, statErr := os.Stat(file); statErr == nil { + t.Errorf("%s: no file may be written", name) } } } -func TestKubeconfigSave_ExistingFile(t *testing.T) { - t.Parallel() - api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - file := filepath.Join(t.TempDir(), "kc.yaml") - if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { - t.Fatal(err) - } - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcSaveArgs(file)...) - if err == nil || !strings.Contains(err.Error(), "--force") || !isUsageError(err) { - t.Fatalf("err = %v", err) - } - kcAssertErrNoKey(t, err) - if got := readFile(t, file); got != "old" { - t.Errorf("file = %q", got) - } - _, errOut, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), kcSaveArgs(file, "--force")...) - if err != nil || strings.Contains(errOut, "still valid") || strings.Contains(errOut, "kubeconfig list") { - t.Fatalf("save issues no new certificate, so it must not advise deleting one: err=%v stderr=%q", err, errOut) - } - if got := readFile(t, file); got != kcYAML { - t.Errorf("file = %q", got) - } - assertMode(t, file, 0o600) -} - func TestKubeconfigSave_DefaultPathUsesTheCommonName(t *testing.T) { - home := t.TempDir() - t.Setenv("HOME", home) - t.Setenv("USERPROFILE", home) + home := setHome(t) api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "kubeconfig", "save", "prod-kc-abc", "--cluster", "prod") if err != nil { @@ -185,83 +109,16 @@ func TestKubeconfigSave_DefaultPathUsesTheCommonName(t *testing.T) { func TestKubeconfigSave_MissingAndUnknown(t *testing.T) { t.Parallel() api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - for name, f := range map[string]*cmdutil.TestFactory{ - "table": tableFactory(newFakeClient(t, api), nil), - "agent": agentFactory(newFakeClient(t, api)), - } { - _, _, err := runMK8s(t, f, "kubeconfig", "save") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("%s: err = %v", name, err) - } - if m, _ := ae.Details["missing"].([]string); len(m) != 2 || m[0] != "--cluster" || m[1] != "" { - t.Errorf("%s: missing = %v", name, ae.Details) - } + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "kubeconfig", "save") + if m, _ := cmdutil.ClassifyError(err).Details["missing"].([]string); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" || !slices.Equal(m, []string{"--cluster", ""}) { + t.Errorf("err = %v, missing = %v", err, m) } - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), "kubeconfig", "save", "ghost", "--cluster", "prod", "--file", filepath.Join(t.TempDir(), "x")) + _, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api)), "kubeconfig", "save", "ghost", "--cluster", "prod", "--file", filepath.Join(t.TempDir(), "x")) if agentErrorCode(err) != "NOT_FOUND" { t.Errorf("unknown id: %v", err) } } -func TestKubeconfigSave_DebugNeverLogsTheKey(t *testing.T) { - t.Parallel() - f := tableFactory(newFakeClient(t, kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady)), nil) - f.DebugOverride = true - out, errOut, err := runMK8s(t, f, kcSaveArgs(filepath.Join(t.TempDir(), "kc.yaml"))...) - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "DEBUG: Kubeconfig:") { - t.Errorf("no debug output: %q", errOut) - } - assertNoPrivateKey(t, "stdout", out) - assertNoPrivateKey(t, "debug output", errOut) -} - -func TestKubeconfigSave_PickerWhenIDOmitted(t *testing.T) { - withTerminal(t) - t.Run("picks and writes", func(t *testing.T) { - api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - file := filepath.Join(t.TempDir(), "kc.yaml") - rec := &kcRecorder{Prompter: pickerAnswers(0)} - _, _, err := runMK8s(t, kcTable(newFakeClient(t, api), rec), "kubeconfig", "save", "--cluster", "prod", "--file", file) - if err != nil { - t.Fatal(err) - } - if got := readFile(t, file); got != kcYAML { - t.Errorf("file = %q", got) - } - if len(rec.selects) != 1 || !rec.selects[0].hints { - t.Errorf("selects = %+v, want one with hints", rec.selects) - } - }) - t.Run("cancel row and Ctrl+C", func(t *testing.T) { - api := kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - file := filepath.Join(t.TempDir(), "kc.yaml") - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(1)), "kubeconfig", "save", "--cluster", "prod", "--file", file) - if err != nil || !strings.Contains(errOut, "Canceled.") { - t.Errorf("Cancel row: err=%v stderr=%q", err, errOut) - } - if _, statErr := os.Stat(file); statErr == nil { - t.Error("the Cancel row must not write a file") - } - api = kcSaveAPI(verda.ManagedKubernetesKubeconfigStatusReady) - p := &cancelPrompter{Prompter: tuitest.New(), selectErr: errInterrupted()} - _, errOut, err = runMK8s(t, kcTable(newFakeClient(t, api), p), "kubeconfig", "save", "--cluster", "prod") - if err != nil || !strings.Contains(errOut, "Canceled.") { - t.Errorf("Ctrl+C: err=%v stderr=%q", err, errOut) - } - }) - t.Run("no kubeconfigs", func(t *testing.T) { - api := seededClusters() - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), "kubeconfig", "save", "--cluster", "prod") - if err != nil || !strings.Contains(out, "No kubeconfigs in cluster prod.") || !strings.Contains(errOut, "kubeconfig create --cluster prod") { - t.Errorf("out=%q err=%q %v", out, errOut, err) - } - }) -} - func TestShellQuote(t *testing.T) { t.Parallel() for in, want := range map[string]string{ diff --git a/internal/verda-cli/cmd/mk8s/locations.go b/internal/verda-cli/cmd/mk8s/locations.go index 98e3dc47..78b48391 100644 --- a/internal/verda-cli/cmd/mk8s/locations.go +++ b/internal/verda-cli/cmd/mk8s/locations.go @@ -15,11 +15,9 @@ package mk8s import ( - "context" "fmt" "github.com/spf13/cobra" - "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -54,14 +52,9 @@ func runLocations(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStr if err != nil { return err } - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - - res, err := cmdutil.WithSpinner(ctx, f.Status(), "Loading locations...", func(ctx context.Context) (*verda.ManagedKubernetesEnabledLocations, error) { - return client.ManagedKubernetes.GetEnabledLocations(ctx) - }) + res, err := apiCall(cmd.Context(), f, "Loading locations...", client.ManagedKubernetes.GetEnabledLocations) if err != nil { - return mapErr(err) + return err } codes := res.Locations if codes == nil { diff --git a/internal/verda-cli/cmd/mk8s/locations_test.go b/internal/verda-cli/cmd/mk8s/locations_test.go index fb928269..83cea7e6 100644 --- a/internal/verda-cli/cmd/mk8s/locations_test.go +++ b/internal/verda-cli/cmd/mk8s/locations_test.go @@ -19,8 +19,6 @@ import ( "strings" "testing" - "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -43,24 +41,17 @@ func TestLocations_Table(t *testing.T) { } } -func TestLocations_JSONAndAgent(t *testing.T) { - for name, newFactory := range map[string]func(*verda.Client) *cmdutil.TestFactory{ - "json": jsonFactory, - "agent": agentFactory, - } { - t.Run(name, func(t *testing.T) { - out, _, err := runMK8s(t, newFactory(newFakeClient(t, newFakeAPI())), "locations") - if err != nil { - t.Fatal(err) - } - var got []string - if err := json.Unmarshal([]byte(out), &got); err != nil { - t.Fatalf("output is not a JSON string array: %v\n%s", err, out) - } - if len(got) != 2 || got[0] != "FIN-01" { - t.Errorf("locations = %v", got) - } - }) +func TestLocations_JSON(t *testing.T) { + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, newFakeAPI())), "locations") + if err != nil { + t.Fatal(err) + } + var got []string + if err := json.Unmarshal([]byte(out), &got); err != nil { + t.Fatalf("output is not a JSON string array: %v\n%s", err, out) + } + if len(got) != 2 || got[0] != "FIN-01" { + t.Errorf("locations = %v", got) } } @@ -105,33 +96,6 @@ func TestLocations_FeatureNotEnabled(t *testing.T) { } } -func TestLocations_Other403StaysPlain(t *testing.T) { - api := newFakeAPI() - api.failures["GET /mk8sclusters/enabled_locations"] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access","details":"some_other_reason"}`} - - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), "locations") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "AUTH_ERROR" { - t.Fatalf("classified = %+v", ae) - } - if strings.Contains(ae.Message, "not enabled") { - t.Errorf("only feature_not_enabled gets the not-enabled message: %q", ae.Message) - } -} - -func TestLocations_Debug(t *testing.T) { - api := newFakeAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.DebugOverride = true - _, errOut, err := runMK8s(t, f, "locations") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "FIN-03") { - t.Errorf("debug output misses the API response: %q", errOut) - } -} - func TestMK8s_HelpStartsWithCaution(t *testing.T) { f := tableFactory(nil, nil) cmd := NewCmdMK8s(f, cmdutil.IOStreams{}) @@ -141,15 +105,6 @@ func TestMK8s_HelpStartsWithCaution(t *testing.T) { if cmd.Annotations[cmdutil.TagAnnotation] != "experimental" { t.Errorf("tag = %q", cmd.Annotations[cmdutil.TagAnnotation]) } - for _, alias := range []string{"k8s", "kubernetes"} { - found := false - for _, a := range cmd.Aliases { - found = found || a == alias - } - if !found { - t.Errorf("missing alias %q", alias) - } - } for _, sub := range cmd.Commands() { if !strings.HasPrefix(sub.Long, "Experimental:") { t.Errorf("%s: Long must start with the caution", sub.Name()) diff --git a/internal/verda-cli/cmd/mk8s/mk8s.go b/internal/verda-cli/cmd/mk8s/mk8s.go index f7a702fa..5d8a9a2b 100644 --- a/internal/verda-cli/cmd/mk8s/mk8s.go +++ b/internal/verda-cli/cmd/mk8s/mk8s.go @@ -28,16 +28,13 @@ import ( const ( outputTable = "table" - // Results of a command that only accepts a request, or that waited for it. statusAccepted = "accepted" statusCompleted = "completed" - // statusUnchanged: the resource already had the requested state, nothing was sent. - statusUnchanged = "unchanged" + statusUnchanged = "unchanged" // nothing was sent timeLayout = "2006-01-02 15:04" - // experimentalCaution starts the Long help of every mk8s command; the line - // break keeps it within 80 columns like the rest of the help. + // experimentalCaution starts every Long help; the line break keeps 80 columns. experimentalCaution = "Experimental: the managed Kubernetes API may change in incompatible ways.\n" + "Commands, flags and output may change without a deprecation period." @@ -48,7 +45,6 @@ const ( waitTimeUsage = "Maximum time to wait with --wait" ) -// longDesc prefixes the experimental caution to a command's long description. func longDesc(s string) string { return experimentalCaution + "\n\n" + cmdutil.LongDesc(s) } @@ -108,12 +104,11 @@ func NewCmdMK8s(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { return cmd } -// isInteractiveTerminal: prompts need stdin, stdout and stderr on a TTY; a var so tests can fake one. +// isInteractiveTerminal: stdin, stdout and stderr on a TTY; a var so tests can fake one. var isInteractiveTerminal = func() bool { return term.IsTerminal(os.Stdin.Fd()) && cmdutil.IsStdoutTerminal() && cmdutil.IsStderrTerminal() } -// interactiveTTY: prompts only on a terminal, outside agent mode, with table output. func interactiveTTY(f cmdutil.Factory) bool { return isInteractiveTerminal() && !f.AgentMode() && f.OutputFormat() == outputTable } diff --git a/internal/verda-cli/cmd/mk8s/nodepool.go b/internal/verda-cli/cmd/mk8s/nodepool.go index 6292653b..ec6ef1ef 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool.go +++ b/internal/verda-cli/cmd/mk8s/nodepool.go @@ -63,7 +63,7 @@ func NewCmdNodePool(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Comma return cmd } -// poolScope is the cluster a node-pool command acts on: --cluster and -l. +// poolScope is --cluster and -l, shared by the node-pool and kubeconfig commands. type poolScope struct { Cluster string Location string @@ -74,42 +74,45 @@ func (s *poolScope) addFlags(cmd *cobra.Command) { addLocationFlag(cmd, &s.Location) } -// missing lists --cluster when a terminal cannot ask for it (or it is blank). -func (s *poolScope) missing(f cmdutil.Factory, cmd *cobra.Command) []string { +// missing lists --cluster, and arg ( or ) unless it is "", when a +// terminal cannot ask for them or they are blank. +func (s *poolScope) missing(f cmdutil.Factory, cmd *cobra.Command, args []string, arg string) []string { + var m []string if clusterArgMissing(f, s.Cluster, cmd.Flags().Changed(flagCluster)) { - return []string{"--" + flagCluster} + m = append(m, "--"+flagCluster) } - return nil -} - -func (s *poolScope) resolve(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client) (*clusterTarget, error) { - return resolveCluster(cmd, f, ioStreams, client, s.Cluster, s.Location) + if arg != "" && clusterArgMissing(f, argOrEmpty(args), len(args) > 0) { + m = append(m, arg) + } + return m } -// poolArgMissing: the pool was not given where it has to be, or was blank. -func poolArgMissing(f cmdutil.Factory, args []string) bool { - blank := len(args) > 0 && strings.TrimSpace(args[0]) == "" - return blank || (len(args) == 0 && !interactiveTTY(f)) +// check fails before any call, listing everything missing at once. +func (s *poolScope) check(f cmdutil.Factory, cmd *cobra.Command, args []string, arg string) error { + if m := s.missing(f, cmd, args, arg); len(m) > 0 { + return cmdutil.NewMissingFlagsError(m) + } + return nil } -// checkPoolTarget fails before any call when --cluster or cannot be -// asked for. It lists everything missing at once. -func checkPoolTarget(f cmdutil.Factory, cmd *cobra.Command, scope *poolScope, args []string) error { - missing := scope.missing(f, cmd) - if poolArgMissing(f, args) { - missing = append(missing, argPool) - } - if len(missing) > 0 { - return cmdutil.NewMissingFlagsError(missing) +// connect returns a nil target with a nil error when the user canceled or there is nothing to pick. +func (s *poolScope) connect(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams) (*verda.Client, *clusterTarget, error) { + client, err := f.VerdaClient() + if err != nil { + return nil, nil, err } - return nil + t, err := resolveCluster(cmd, f, ioStreams, client, s.Cluster, s.Location) + return client, t, err } // resolvePool reads the pool named by args[0], or asks for one. A nil pool with // a nil error means the user canceled or the cluster has no pool. func resolvePool(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, args []string) (*verda.ManagedKubernetesNodePool, error) { if len(args) > 0 { - return getPool(cmd.Context(), f, client, t, strings.TrimSpace(args[0])) + name := strings.TrimSpace(args[0]) + return apiCall(cmd.Context(), f, "Loading node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { + return client.ManagedKubernetes.GetNodePool(ctx, t.Location, t.Name, name) + }) } pools, err := listPools(cmd.Context(), f, client, t) if err != nil { @@ -118,30 +121,18 @@ func resolvePool(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStre return pickPool(cmd.Context(), f, ioStreams, t, pools) } -func getPool(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget, name string) (*verda.ManagedKubernetesNodePool, error) { - return apiCall(ctx, f, "Loading node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { - return client.ManagedKubernetes.GetNodePool(ctx, t.Location, t.Name, name) - }) -} - func listPools(ctx context.Context, f cmdutil.Factory, client *verda.Client, t *clusterTarget) ([]verda.ManagedKubernetesNodePool, error) { return apiCall(ctx, f, "Loading node pools...", func(ctx context.Context) ([]verda.ManagedKubernetesNodePool, error) { return client.ManagedKubernetes.ListNodePools(ctx, t.Location, t.Name) }) } -// poolDescribeCmd, poolDeleteCmd and poolScaleCmd are the commands named in "check with" and "to stop" hints. -func poolDescribeCmd(t *clusterTarget, pool string) string { - return fmt.Sprintf("verda mk8s node-pool describe %s --cluster %s -l %s", pool, t.Name, t.Location) +// poolCmd is a node-pool command for "check with" and "to stop" hints. +func poolCmd(verb string, t *clusterTarget, pool string) string { + return fmt.Sprintf("verda mk8s node-pool %s %s --cluster %s -l %s", verb, pool, t.Name, t.Location) } -func poolDeleteCmd(t *clusterTarget, pool string) string { - return fmt.Sprintf("verda mk8s node-pool delete %s --cluster %s -l %s", pool, t.Name, t.Location) -} - -func poolScaleCmd(t *clusterTarget, pool string, replicas int) string { - return fmt.Sprintf("verda mk8s node-pool scale %s --cluster %s -l %s --replicas %d", pool, t.Name, t.Location, replicas) -} +func poolDescribeCmd(t *clusterTarget, pool string) string { return poolCmd("describe", t, pool) } func pickPool(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, pools []verda.ManagedKubernetesNodePool) (*verda.ManagedKubernetesNodePool, error) { if len(pools) == 0 { diff --git a/internal/verda-cli/cmd/mk8s/nodepool_create.go b/internal/verda-cli/cmd/mk8s/nodepool_create.go index c57cab48..7cda0ef0 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_create.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_create.go @@ -16,9 +16,7 @@ package mk8s import ( "context" - "errors" "fmt" - "slices" "strings" "time" @@ -126,28 +124,21 @@ func NewCmdNodePoolCreate(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra func (o *nodePoolCreateOptions) trim() { o.Cluster, o.Location = strings.TrimSpace(o.Cluster), strings.TrimSpace(o.Location) o.Name, o.InstanceType = strings.TrimSpace(o.Name), strings.TrimSpace(o.InstanceType) - keys := make([]string, 0, len(o.SSHKeys)) - for _, k := range o.SSHKeys { - if k = strings.TrimSpace(k); k != "" && !slices.Contains(keys, k) { - keys = append(keys, k) - } - } - o.SSHKeys = keys + o.SSHKeys = cleanGroups(o.SSHKeys) } -// validate rejects values that cannot work before any prompt or call. The errors -// are UsageErrors: main prints an AgentError as JSON even for a person. -func (o *nodePoolCreateOptions) validate(cmd *cobra.Command) error { - if o.Wait.Wait && o.Wait.Timeout <= 0 { +// checkPoolFlags runs before any prompt or call. The errors are UsageErrors: +// main prints an AgentError as JSON even for a person. +func checkPoolFlags(cmd *cobra.Command, w cmdutil.WaitOptions, replicas int) error { + if w.Wait && w.Timeout <= 0 { return cmdutil.UsageErrorf(cmd, "--%s must be greater than 0", flagWaitTimeout) } - if o.Replicas < 0 { + if replicas < 0 { return cmdutil.UsageErrorf(cmd, "--%s must be 0 or more", flagReplicas) } return nil } -// missingValues lists the required values that are empty. func (o *nodePoolCreateOptions) missingValues() []string { var m []string if o.Cluster == "" { @@ -177,7 +168,7 @@ func (o *nodePoolCreateOptions) request() verda.CreateManagedKubernetesNodePoolR func runNodePoolCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, opts *nodePoolCreateOptions) error { opts.trim() - if err := opts.validate(cmd); err != nil { + if err := checkPoolFlags(cmd, opts.Wait, opts.Replicas); err != nil { return err } missing := opts.missingValues() @@ -200,10 +191,7 @@ func runNodePoolCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. } if len(missing) > 0 { flow := buildNodePoolCreateFlow(opts, newWizardCache(f, client)) - if err := newWizardEngine(f, ioStreams).Run(cmd.Context(), flow); err != nil { - if stop, ok := errors.AsType[*stopError](err); ok { - return stop - } + if err := runWizard(cmd.Context(), f, ioStreams, flow); err != nil { return err } opts.trim() @@ -219,9 +207,7 @@ func runNodePoolCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. } cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - pool, err := cmdutil.WithSpinner(ctx, f.Status(), "Creating node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { + pool, err := apiCall(cmd.Context(), f, "Creating node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { return client.ManagedKubernetes.CreateNodePool(ctx, target.Location, target.Name, req) }) if err != nil { @@ -252,12 +238,8 @@ func runNodePoolCreate(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. } func confirmNodePoolCreate(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, req *verda.CreateManagedKubernetesNodePoolRequest) (bool, error) { - replicas := 0 - if req.Replicas != nil { - replicas = *req.Replicas - } _, _ = fmt.Fprintf(ioStreams.ErrOut, "\nCreate node pool %s\n Cluster: %s (%s)\n Instance type: %s\n Workers: %d\n SSH keys: %s\n\n", - req.Name, t.Name, t.Location, req.InstanceType, replicas, strings.Join(req.SSHKeyIDs, ", ")) + req.Name, t.Name, t.Location, req.InstanceType, *req.Replicas, strings.Join(req.SSHKeyIDs, ", ")) return askConfirm(ctx, ioStreams, f.Prompter(), "Create node pool "+req.Name+"?") } diff --git a/internal/verda-cli/cmd/mk8s/nodepool_create_test.go b/internal/verda-cli/cmd/mk8s/nodepool_create_test.go index 1c90adc2..4c8d393f 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_create_test.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_create_test.go @@ -17,7 +17,6 @@ package mk8s import ( "slices" "strings" - "sync/atomic" "testing" "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" @@ -36,44 +35,32 @@ func withPoolArgs(extra ...string) []string { return append(slices.Clone(poolCre func TestNodePoolCreate_FlagsSendOneRequest(t *testing.T) { t.Parallel() - api := poolWizardAPI() - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), poolCreateArgs...) - if err != nil { - t.Fatal(err) - } - result := decodeJSON[PoolResult](t, out) - if result.Status != "accepted" || result.Pool.Name != "workers" || result.Pool.State != "creating" { - t.Errorf("result = %+v", result) - } - - posts := api.requestsMatching("POST") - if len(posts) != 1 || posts[0].Path != "/mk8sclusters/FIN-03/prod/pools" { - t.Fatalf("POST requests = %v", posts) - } - body := postBodyAny(t, api) - if body["name"] != "workers" || body["instance_type"] != "1V100.6V" || body["replicas"] != float64(1) { - t.Errorf("body = %v", body) - } - if keys, _ := body["ssh_key_ids"].([]any); len(keys) != 2 || keys[0] != "key-1" || keys[1] != "key-2" { - t.Errorf("ssh_key_ids = %v", body["ssh_key_ids"]) - } - // The instance type is not checked: no availability or SSH key lookup. - if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters"}) { - t.Errorf("reads = %v, want only the cluster lookup", got) - } -} - -func TestNodePoolCreate_ReplicasAndLocation(t *testing.T) { - t.Parallel() - api := poolWizardAPI() - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--replicas", "0", "-l", "FIN-03")...); err != nil { - t.Fatal(err) - } - if body := postBodyAny(t, api); body["replicas"] != float64(0) { - t.Errorf("an explicit 0 must be sent, body = %v", body) - } - if got := api.paths("GET"); len(got) != 0 { - t.Errorf("-l skips the cluster lookup, reads = %v", got) + for name, tc := range map[string]struct { + extra []string + replicas float64 + reads []string + }{ + "defaults": {replicas: 1, reads: []string{"/mk8sclusters"}}, + "an explicit 0, -l skips GETs": {extra: []string{"--replicas", "0", "-l", "FIN-03"}, replicas: 0}, + } { + api := poolWizardAPI() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs(tc.extra...)...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + if r := decodeJSON[PoolResult](t, out); r.Status != "accepted" || r.Pool.Name != "workers" || r.Pool.State != "creating" { + t.Errorf("%s: result = %+v", name, r) + } + if posts := api.requestsMatching("POST"); len(posts) != 1 || posts[0].Path != "/mk8sclusters/FIN-03/prod/pools" { + t.Fatalf("%s: POST requests = %v", name, posts) + } + body := postBodyAny(t, api) + if keys, _ := body["ssh_key_ids"].([]any); body["name"] != "workers" || body["instance_type"] != "1V100.6V" || body["replicas"] != tc.replicas || !slices.Equal(keys, []any{"key-1", "key-2"}) { + t.Errorf("%s: body = %v", name, body) + } + if got := api.paths("GET"); !slices.Equal(got, tc.reads) { + t.Errorf("%s: reads = %v, want %v", name, got, tc.reads) + } } } @@ -90,123 +77,44 @@ func TestNodePoolCreate_TableSaysAccepted(t *testing.T) { func TestNodePoolCreate_MissingFlagsOffTerminal(t *testing.T) { t.Parallel() - for name, f := range map[string]*cmdutil.TestFactory{ - "table": tableFactory(newFakeClient(t, poolWizardAPI()), nil), - "agent": agentFactory(newFakeClient(t, poolWizardAPI())), - } { - _, _, err := runMK8s(t, f, "node-pool", "create") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("%s: err = %v", name, err) - } - if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "--name", "--instance-type", "--ssh-key"}) { - t.Errorf("%s: missing = %v", name, missing) - } - } -} - -func TestNodePoolCreate_UnknownClusterAndInvalidInput(t *testing.T) { - t.Parallel() - api := poolWizardAPI() - args := slices.Clone(poolCreateArgs) - args[slices.Index(args, "--cluster")+1] = "ghost" - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...); agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown cluster: %v", err) - } - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs("--replicas", "-1")...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { - t.Errorf("a negative count is a usage error, got %v", err) + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolWizardAPI())), "node-pool", "create") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) } - if n := len(api.requestsMatching("POST")); n != 0 { - t.Errorf("%d POST(s) for invalid input", n) + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "--name", "--instance-type", "--ssh-key"}) { + t.Errorf("missing = %v", missing) } } -func TestNodePoolCreate_FailureWarnings(t *testing.T) { +func TestNodePoolCreate_InvalidFlagsSendNothing(t *testing.T) { t.Parallel() - const route = "POST /mk8sclusters/{loc}/{name}/pools" - - t.Run("502 warns once", func(t *testing.T) { - t.Parallel() - api := poolWizardAPI() - api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), poolCreateArgs...) - if err == nil || !strings.Contains(err.Error(), "upstream failed") { - t.Fatalf("err = %v", err) - } - if !strings.Contains(errOut, "The node pool may have been created anyway.") || !strings.Contains(errOut, "node-pool list --cluster prod -l FIN-03") { - t.Errorf("stderr = %q", errOut) - } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d, a create is sent once", n) - } - }) - - t.Run("502 in agent details", func(t *testing.T) { - t.Parallel() + for _, extra := range [][]string{{"--replicas", "-1"}, {"--wait", "--wait-timeout", "0s"}} { api := poolWizardAPI() - api.failures[route] = apiFailure{Status: 502, Body: `{"code":"bad_gateway","message":"upstream failed"}`} - _, errOut, err := runMK8s(t, agentFactory(newFakeClient(t, api)), poolCreateArgs...) - if w, _ := cmdutil.ClassifyError(err).Details["warning"].(string); !strings.Contains(w, "node pool may have been created anyway") || errOut != "" { - t.Errorf("details=%v stderr=%q", cmdutil.ClassifyError(err).Details, errOut) + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs(extra...)...); !isUsageError(err) || len(api.requests) != 0 { + t.Errorf("%v: err = %v, %d request(s)", extra, err, len(api.requests)) } - }) - - t.Run("400 and 409 do not warn", func(t *testing.T) { - t.Parallel() - api := poolWizardAPI() - api.addPool("staging", "workers", "active", 1, 1) // the name is taken in another cluster - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), poolCreateArgs...) - if err == nil || !strings.Contains(err.Error(), "pool_already_exists") { - t.Fatalf("the API reason must be shown: %v", err) - } - if errOut != "" { - t.Errorf("unexpected warning %q", errOut) - } - }) + } } +// A read at the count but not yet active must not end the wait. func TestNodePoolCreate_WaitCompletes(t *testing.T) { fastPoll(t) api := poolWizardAPI() - var reads atomic.Int32 + reads := 0 api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { - switch reads.Add(1) { - case 1: // stale: active with no worker yet, not what was asked for - p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 0 - case 2: - p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 2 - default: - p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 3 + reads++ + p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateCreating, p.Replicas + if reads > 1 { + p.State = verda.ManagedKubernetesNodePoolStateActive } } - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs("--replicas", "3", "--wait", "--wait-timeout", "30s")...) + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs("--replicas", "3", "--wait")...) if err != nil || !strings.Contains(out, "Node pool workers completed (cluster prod, FIN-03): 3/3 workers ready") { t.Fatalf("out=%q err=%v", out, err) } - if got := reads.Load(); got != 3 { - t.Errorf("reads = %d, want 3: a stale or partial read must not end the wait", got) - } -} - -func TestNodePoolCreate_WaitKeepsGoingWhileDegraded(t *testing.T) { - fastPoll(t) - api := poolWizardAPI() - var reads atomic.Int32 - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { - if reads.Add(1) < 3 { - p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateDegraded, 0 - p.Errors = []verda.ManagedKubernetesNodePoolError{{Reason: "insufficient_capacity", Since: testCreated}} - return - } - p.State, p.ReadyReplicas, p.Errors = verda.ManagedKubernetesNodePoolStateActive, 1, nil - } - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--wait")...) - if err != nil { - t.Fatal(err) - } - if r := decodeJSON[PoolResult](t, out); r.Status != "completed" || reads.Load() != 3 { - t.Errorf("result=%+v reads=%d", r, reads.Load()) + if reads != 2 { + t.Errorf("reads = %d, want 2", reads) } } @@ -215,17 +123,12 @@ func TestNodePoolCreate_WaitTimeout(t *testing.T) { api := poolWizardAPI() api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateDegraded, 1 - p.Errors = []verda.ManagedKubernetesNodePoolError{{Reason: "insufficient_capacity", Since: testCreated}} } out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--replicas", "3", "--wait", "--wait-timeout", "300ms")...) if err == nil { t.Fatal("a timeout must fail") } - for _, want := range []string{ - "timed out after 300ms", "node pool workers in cluster prod (FIN-03)", "last state: degraded, 1/3 workers ready", - "errors: insufficient_capacity", "The backend keeps trying to create workers.", "keep billing", - "verda mk8s node-pool delete workers --cluster prod -l FIN-03", - } { + for _, want := range []string{"keep billing", "verda mk8s node-pool delete workers --cluster prod -l FIN-03"} { if !strings.Contains(err.Error(), want) { t.Errorf("error misses %q: %v", want, err) } @@ -235,133 +138,21 @@ func TestNodePoolCreate_WaitTimeout(t *testing.T) { } } -func TestNodePoolCreate_WaitReadFailure(t *testing.T) { - fastPoll(t) - api := poolWizardAPI() - api.failures["GET /mk8sclusters/{loc}/{name}/pools/{pool}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), withPoolArgs("--wait")...) - if err == nil || !strings.Contains(err.Error(), "node pool workers in cluster prod (FIN-03) was accepted, but --wait could not read it") { - t.Fatalf("err = %v", err) - } - // The pool exists and may keep creating workers, so this ending carries the billing note too. - if !strings.Contains(err.Error(), "keep billing") { - t.Errorf("err = %v, want the billing note", err) - } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d", n) - } -} - -func TestNodePoolCreate_WaitTimeoutMustBePositive(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolWizardAPI()), nil), withPoolArgs("--wait", "--wait-timeout", "0s")...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { - t.Fatalf("err = %v: want a usage error", err) - } -} - -func TestNodePoolCreate_ValuesAreTrimmed(t *testing.T) { - t.Parallel() - api := poolWizardAPI() - args := []string{ - "node-pool", "create", "--cluster", " prod ", "--name", " workers ", "--instance-type", " 1V100.6V ", - "--ssh-key", " key-1 ", "--ssh-key", " ", "--ssh-key", "key-1", - } - if _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...); err != nil { - t.Fatal(err) - } - body := postBodyAny(t, api) - if body["name"] != "workers" || body["instance_type"] != "1V100.6V" { - t.Errorf("body = %v", body) - } - if keys, _ := body["ssh_key_ids"].([]any); len(keys) != 1 || keys[0] != "key-1" { - t.Errorf("a blank key is dropped and the others trimmed: %v", body["ssh_key_ids"]) - } -} - -func TestNodePoolCreate_NegativeReplicasNeverReachesTheSummary(t *testing.T) { - withTerminal(t) - api := poolWizardAPI() - // No confirm queued: reaching the summary would fail the test. - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), withPoolArgs("--replicas", "-1")...) - if err == nil || strings.Contains(errOut, "Create node pool") { - t.Errorf("err=%v stderr=%q", err, errOut) - } -} - -func TestNodePoolCreate_WaitUntilActiveAndNotJustReady(t *testing.T) { - fastPoll(t) - // A pool that is degraded or still creating is not done, even when ready == requested. - for name, replicas := range map[string]string{"zero workers": "0", "three workers": "3"} { - api := poolWizardAPI() - want := 0 - if replicas == "3" { - want = 3 - } - var reads atomic.Int32 - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { - state := verda.ManagedKubernetesNodePoolStateCreating - switch reads.Add(1) { - case 2: - state = verda.ManagedKubernetesNodePoolStateDegraded - case 3: - state = verda.ManagedKubernetesNodePoolStateActive - } - p.State, p.ReadyReplicas = state, want - } - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), withPoolArgs("--replicas", replicas, "--wait")...) - if err != nil { - t.Fatalf("%s: %v", name, err) - } - if r := decodeJSON[PoolResult](t, out); r.Status != "completed" || reads.Load() != 3 { - t.Errorf("%s: status=%s reads=%d, want completed after the third read", name, r.Status, reads.Load()) - } - } -} - func TestNodePoolCreate_InteractiveSummaryAndConfirm(t *testing.T) { withTerminal(t) - - t.Run("confirmed", func(t *testing.T) { + for _, tc := range []struct { + p *tuitest.Prompter + posts int + }{{confirmYes(), 1}, {confirmNo(), 0}} { api := poolWizardAPI() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), poolCreateArgs...) - if err != nil { - t.Fatal(err) + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tc.p), poolCreateArgs...) + if err != nil || len(api.requestsMatching("POST")) != tc.posts { + t.Errorf("want %d POST(s): err=%v requests=%v", tc.posts, err, api.requestsMatching("POST")) } for _, want := range []string{"Create node pool workers", "prod (FIN-03)", "1V100.6V", "key-1, key-2"} { if !strings.Contains(errOut, want) { t.Errorf("summary misses %q: %s", want, errOut) } } - if n := len(api.requestsMatching("POST")); n != 1 { - t.Errorf("POSTs = %d", n) - } - }) - - t.Run("declined, Esc and Ctrl+C create nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "declined": {Prompter: tuitest.New().AddConfirm(false)}, - "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, - } { - api := poolWizardAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - _, errOut, err := runMK8s(t, f, poolCreateArgs...) - if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("POST")) != 0 { - t.Errorf("%s: err=%v stderr=%q", name, err, errOut) - } - } - }) - - t.Run("agent and json never prompt", func(t *testing.T) { - for name, f := range map[string]*cmdutil.TestFactory{ - "agent": agentFactory(newFakeClient(t, poolWizardAPI())), - "json": jsonFactory(newFakeClient(t, poolWizardAPI())), - } { - if _, _, err := runMK8s(t, f, poolCreateArgs...); err != nil { - t.Errorf("%s: %v", name, err) - } - } - }) + } } diff --git a/internal/verda-cli/cmd/mk8s/nodepool_delete.go b/internal/verda-cli/cmd/mk8s/nodepool_delete.go index e5ab5ce2..7e17e8e4 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_delete.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_delete.go @@ -62,17 +62,13 @@ func NewCmdNodePoolDelete(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra } func runNodePoolDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string, yes bool) error { - if err := checkPoolTarget(f, cmd, scope, args); err != nil { + if err := scope.check(f, cmd, args, argPool); err != nil { return err } if err := checkConfirmed(f, yes, "delete"); err != nil { return err } - client, err := f.VerdaClient() - if err != nil { - return err - } - target, err := scope.resolve(cmd, f, ioStreams, client) + client, target, err := scope.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } @@ -93,13 +89,10 @@ func runNodePoolDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. } cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Delete node pool:", map[string]string{"name": name, "cluster": target.Name, "location": target.Location}) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - err = cmdutil.RunWithSpinner(ctx, f.Status(), "Deleting node pool...", func(ctx context.Context) error { + if err := apiRun(cmd.Context(), f, "Deleting node pool...", func(ctx context.Context) error { return client.ManagedKubernetes.DeleteNodePool(ctx, target.Location, target.Name, name) - }) - if err != nil { - return mapErr(err) + }); err != nil { + return err } result := ActionResult{Resource: kindNodePool, Name: name, Location: target.Location, Cluster: target.Name, Action: "delete", Status: statusAccepted} @@ -110,7 +103,7 @@ func runNodePoolDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil. return nil } -// poolToDelete is args[0], or the pool the user picks; "" means canceled or no pool. +// poolToDelete: "" means canceled or no pool. func poolToDelete(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, args []string) (string, error) { if len(args) > 0 { return strings.TrimSpace(args[0]), nil diff --git a/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go b/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go index 1c89a7be..064041ed 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go @@ -19,52 +19,28 @@ import ( "strings" "testing" - tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) func TestNodePoolDelete_YesMakesOneDeleteCall(t *testing.T) { t.Parallel() - api := scaleAPI() + api := poolAPI() out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "delete", "workers", "--cluster", "prod", "--yes") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(out, "Deletion accepted. Worker VMs are deleted in the background.") { - t.Errorf("stdout = %q", out) + if err != nil || !strings.Contains(out, "Deletion accepted. Worker VMs are deleted in the background.") { + t.Fatalf("out=%q err=%v", out, err) } - dels := api.requestsMatching("DELETE") - if len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-03/prod/pools/workers" { + if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-03/prod/pools/workers" { t.Fatalf("DELETE requests = %v", dels) } - if len(api.requestsMatching("POST"))+len(api.requestsMatching("PATCH")) != 0 { - t.Error("delete must not write anything else") - } - // A named pool is not read first (a pool whose read fails must stay deletable), and there is no --wait. + // A named pool is not read first: a pool whose read fails must stay deletable. if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters"}) { t.Errorf("reads = %v, want only the cluster lookup", got) } } -func TestNodePoolDelete_APoolThatCannotBeReadCanStillBeDeleted(t *testing.T) { - t.Parallel() - api := scaleAPI() - api.failures["GET /mk8sclusters/{loc}/{name}/pools/{pool}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "delete", "workers", "--cluster", "prod", "-l", "FIN-03", "--yes"); err != nil { - t.Fatal(err) - } - if got := api.paths("GET"); len(got) != 0 { - t.Errorf("reads = %v, want none with -l", got) - } - if n := len(api.requestsMatching("DELETE")); n != 1 { - t.Errorf("DELETEs = %d", n) - } -} - func TestNodePoolDelete_Structured(t *testing.T) { t.Parallel() - out, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "np", "rm", "workers", "--cluster", "prod", "-l", "FIN-03", "--yes") + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "np", "rm", "workers", "--cluster", "prod", "-l", "FIN-03", "--yes") if err != nil { t.Fatal(err) } @@ -77,29 +53,9 @@ func TestNodePoolDelete_Structured(t *testing.T) { } } -func TestNodePoolDelete_NeedsYesOffTerminalBeforeAnyCall(t *testing.T) { - t.Parallel() - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { - return tableFactory(newFakeClient(t, api), nil) - }, - } { - api := scaleAPI() - _, _, err := runMK8s(t, mk(api, t), "node-pool", "delete", "workers", "--cluster", "prod") - if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" { - t.Errorf("%s: err = %v", name, err) - } - if n := len(api.requests); n != 0 { - t.Errorf("%s: %d request(s) before the check", name, n) - } - } -} - func TestNodePoolDelete_MissingTargets(t *testing.T) { t.Parallel() - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "node-pool", "delete", "--yes") + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "node-pool", "delete", "--yes") ae := cmdutil.ClassifyError(err) if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { t.Fatalf("err = %v", err) @@ -109,61 +65,10 @@ func TestNodePoolDelete_MissingTargets(t *testing.T) { } } -func TestNodePoolDelete_Interactive(t *testing.T) { - withTerminal(t) - - t.Run("the warning names the worker VMs", func(t *testing.T) { - api := scaleAPI() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), "node-pool", "delete", "workers", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "Deletes node pool workers and its worker VMs.") || !strings.Contains(errOut, "cannot be undone") { - t.Errorf("warning = %q", errOut) - } - if n := len(api.requestsMatching("DELETE")); n != 1 { - t.Errorf("DELETEs = %d", n) - } - }) - - t.Run("pickers then confirm", func(t *testing.T) { - api := scaleAPI() - p := pickerAnswers(0, 0).AddConfirm(true) - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "node-pool", "delete"); err != nil { - t.Fatal(err) - } - if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-03/prod/pools/workers" { - t.Errorf("DELETE requests = %v", dels) - } - }) - - t.Run("declined, Esc and Ctrl+C delete nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "declined": {Prompter: tuitest.New().AddConfirm(false)}, - "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, - } { - api := scaleAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - _, errOut, err := runMK8s(t, f, "node-pool", "delete", "workers", "--cluster", "prod") - if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("DELETE")) != 0 { - t.Errorf("%s: err=%v stderr=%q", name, err, errOut) - } - } - }) -} - -func TestNodePoolDelete_Errors(t *testing.T) { +func TestNodePoolDelete_UnknownPool(t *testing.T) { t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, scaleAPI()), nil), "node-pool", "delete", "ghost", "--cluster", "prod", "--yes") + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolAPI()), nil), "node-pool", "delete", "ghost", "--cluster", "prod", "--yes") if agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown pool: %v", err) - } - api := newFakeAPI() - api.disabled = true - _, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "delete", "workers", "--cluster", "prod", "-l", "FIN-03", "--yes") - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Errorf("not enabled: %v", err) + t.Errorf("err = %v", err) } } diff --git a/internal/verda-cli/cmd/mk8s/nodepool_describe.go b/internal/verda-cli/cmd/mk8s/nodepool_describe.go index 8273b8b3..468a8fda 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_describe.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_describe.go @@ -57,14 +57,10 @@ func NewCmdNodePoolDescribe(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cob } func runNodePoolDescribe(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope, args []string) error { - if err := checkPoolTarget(f, cmd, scope, args); err != nil { + if err := scope.check(f, cmd, args, argPool); err != nil { return err } - client, err := f.VerdaClient() - if err != nil { - return err - } - target, err := scope.resolve(cmd, f, ioStreams, client) + client, target, err := scope.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } diff --git a/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go b/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go index f8dbb342..4eddb06f 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go @@ -21,8 +21,6 @@ import ( "strings" "testing" - tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) @@ -71,27 +69,11 @@ func TestNodePoolDescribe_Table(t *testing.T) { t.Errorf("output misses %q:\n%s", s, out) } } - - out, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "describe", "workers", "--cluster", "prod") - if err != nil || strings.Contains(out, "Errors:") { - t.Errorf("a healthy pool shows no Errors section: err=%v\n%s", err, out) - } if got := api.paths("GET"); !slices.Contains(got, "/mk8sclusters/FIN-03/prod/pools/gpu") { t.Errorf("requests = %v", got) } } -func TestNodePoolDescribe_AgentAndAliases(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "pool", "show", "workers", "--cluster", "prod", "-l", "FIN-03") - if err != nil { - t.Fatal(err) - } - if v := decodeJSON[PoolView](t, out); v.Name != "workers" || v.Errors == nil || len(v.Errors) != 0 { - t.Errorf("view = %+v", v) - } -} - func TestNodePoolDescribe_MissingTargets(t *testing.T) { t.Parallel() _, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "node-pool", "describe") @@ -102,50 +84,7 @@ func TestNodePoolDescribe_MissingTargets(t *testing.T) { if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { t.Errorf("missing = %v", missing) } - if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "node-pool", "describe", " ", "--cluster", "prod"); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Errorf("blank pool: %v", err) - } if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolAPI()), nil), "node-pool", "describe", "ghost", "--cluster", "prod"); agentErrorCode(err) != "NOT_FOUND" { t.Errorf("unknown pool: %v", err) } } - -func TestNodePoolDescribe_Pickers(t *testing.T) { - withTerminal(t) - - t.Run("cluster picker then pool picker", func(t *testing.T) { - api := poolAPI() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0, 1)), "node-pool", "describe") - if err != nil || !strings.Contains(out, "gpu") { - t.Fatalf("out=%q err=%v", out, err) - } - }) - - t.Run("pool picker only", func(t *testing.T) { - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolAPI()), pickerAnswers(0)), "node-pool", "describe", "--cluster", "prod") - if err != nil || !strings.Contains(out, "workers") { - t.Fatalf("out=%q err=%v", out, err) - } - }) - - t.Run("cancel row and Esc end cleanly", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "cancel row": {Prompter: pickerAnswers(2)}, - "esc": {Prompter: tuitest.New(), selectErr: contextCanceled()}, - } { - f := tableFactory(newFakeClient(t, poolAPI()), nil) - f.PrompterOverride = p - out, errOut, err := runMK8s(t, f, "node-pool", "describe", "--cluster", "prod") - if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { - t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) - } - } - }) - - t.Run("no pools", func(t *testing.T) { - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), tuitest.New()), "node-pool", "describe", "--cluster", "prod") - if err != nil || !strings.Contains(out, "No node pools in cluster prod.") || !strings.Contains(errOut, "node-pool create") { - t.Errorf("out=%q err=%q error=%v", out, errOut, err) - } - }) -} diff --git a/internal/verda-cli/cmd/mk8s/nodepool_list.go b/internal/verda-cli/cmd/mk8s/nodepool_list.go index 66506b8b..884e1d64 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_list.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_list.go @@ -56,14 +56,10 @@ func NewCmdNodePoolList(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.C } func runNodePoolList(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, scope *poolScope) error { - if missing := scope.missing(f, cmd); len(missing) > 0 { - return cmdutil.NewMissingFlagsError(missing) - } - client, err := f.VerdaClient() - if err != nil { + if err := scope.check(f, cmd, nil, ""); err != nil { return err } - target, err := scope.resolve(cmd, f, ioStreams, client) + client, target, err := scope.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } diff --git a/internal/verda-cli/cmd/mk8s/nodepool_list_test.go b/internal/verda-cli/cmd/mk8s/nodepool_list_test.go index 90af35c9..84621db4 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_list_test.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_list_test.go @@ -20,23 +20,9 @@ import ( "strings" "testing" - "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - "go.yaml.in/yaml/v3" - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -// poolAPI has the clusters of seededClusters and two pools in "prod", one degraded. -func poolAPI() *fakeAPI { - api := seededClusters() - api.addPool("prod", "workers", "active", 3, 3) - api.addPool("prod", "gpu", "degraded", 2, 1) - api.pools["prod"][1].Errors = []verda.ManagedKubernetesNodePoolError{ - {Reason: "insufficient_capacity", Message: "no free 1V100.6V", Since: testCreated}, - } - return api -} - func TestNodePoolList_Table(t *testing.T) { t.Parallel() api := poolAPI() @@ -63,57 +49,22 @@ func TestNodePoolList_Table(t *testing.T) { } } -func TestNodePoolList_LocationSkipsTheClusterLookup(t *testing.T) { +func TestNodePoolList_Structured(t *testing.T) { t.Parallel() - api := poolAPI() - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "np", "ls", "--cluster", "prod", "-l", "FIN-03"); err != nil { + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, poolAPI())), "node-pool", "list", "--cluster", "prod") + if err != nil { t.Fatal(err) } - if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters/FIN-03/prod/pools"}) { - t.Errorf("requests = %v", got) + want := []PoolView{ + {Name: "workers", InstanceType: "1V100.6V", Replicas: 3, ReadyReplicas: 3, State: "active", SSHKeyIDs: []string{"key-1"}, Errors: []PoolErrorView{}, CreatedAt: testCreated}, + { + Name: "gpu", InstanceType: "1V100.6V", Replicas: 2, ReadyReplicas: 1, State: "degraded", SSHKeyIDs: []string{"key-1"}, + Errors: []PoolErrorView{{Reason: "insufficient_capacity", Message: "no free 1V100.6V", Since: testCreated}}, CreatedAt: testCreated, + }, } -} - -func TestNodePoolList_Structured(t *testing.T) { - t.Parallel() - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "json": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - } { - t.Run(name, func(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, mk(poolAPI(), t), "node-pool", "list", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - got := decodeJSON[[]PoolView](t, out) - want := []PoolView{ - {Name: "workers", InstanceType: "1V100.6V", Replicas: 3, ReadyReplicas: 3, State: "active", SSHKeyIDs: []string{"key-1"}, Errors: []PoolErrorView{}, CreatedAt: testCreated}, - { - Name: "gpu", InstanceType: "1V100.6V", Replicas: 2, ReadyReplicas: 1, State: "degraded", SSHKeyIDs: []string{"key-1"}, - Errors: []PoolErrorView{{Reason: "insufficient_capacity", Message: "no free 1V100.6V", Since: testCreated}}, CreatedAt: testCreated, - }, - } - if !reflect.DeepEqual(got, want) { - t.Errorf("got %+v\nwant %+v", got, want) - } - }) + if got := decodeJSON[[]PoolView](t, out); !reflect.DeepEqual(got, want) { + t.Errorf("got %+v\nwant %+v", got, want) } - - t.Run("yaml", func(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, yamlFactory(newFakeClient(t, poolAPI())), "node-pool", "list", "--cluster", "prod") - if err != nil { - t.Fatal(err) - } - var got []map[string]any - if err := yaml.Unmarshal([]byte(out), &got); err != nil { - t.Fatal(err) - } - if len(got) != 2 || got[1]["state"] != "degraded" || got[0]["errors"] == nil { - t.Errorf("got %v", got) - } - }) } func TestNodePoolList_Empty(t *testing.T) { @@ -129,10 +80,9 @@ func TestNodePoolList_Empty(t *testing.T) { } } -func TestNodePoolList_ClusterErrors(t *testing.T) { +func TestNodePoolList_MissingCluster(t *testing.T) { t.Parallel() - client := newFakeClient(t, seededClusters()) - _, _, err := runMK8s(t, tableFactory(client, nil), "node-pool", "list") + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, seededClusters())), "node-pool", "list") ae := cmdutil.ClassifyError(err) if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { t.Fatalf("err = %v", err) @@ -140,39 +90,4 @@ func TestNodePoolList_ClusterErrors(t *testing.T) { if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster"}) { t.Errorf("missing = %v", missing) } - if _, _, err := runMK8s(t, tableFactory(client, nil), "node-pool", "list", "--cluster", "ghost"); agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown cluster: %v", err) - } - if _, _, err := runMK8s(t, tableFactory(client, nil), "node-pool", "list", "--cluster", " "); agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Errorf("blank cluster: %v", err) - } -} - -func TestNodePoolList_Debug(t *testing.T) { - t.Parallel() - f := tableFactory(newFakeClient(t, poolAPI()), nil) - f.DebugOverride = true - _, errOut, err := runMK8s(t, f, "node-pool", "list", "--cluster", "prod") - if err != nil || !strings.Contains(errOut, "API response: 2 node pool(s):") || !strings.Contains(errOut, `"ready_replicas": 1`) { - t.Errorf("err=%v debug=%q", err, errOut) - } -} - -func TestNodePoolList_ClusterPicker(t *testing.T) { - withTerminal(t) - api := poolAPI() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), pickerAnswers(0)), "node-pool", "list") - if err != nil || !strings.Contains(out, "workers") { - t.Fatalf("out=%q err=%v", out, err) - } -} - -func TestNodePoolList_NotEnabled(t *testing.T) { - t.Parallel() - api := newFakeAPI() - api.disabled = true - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "list", "--cluster", "prod", "-l", "FIN-03") - if err == nil || agentErrorCode(err) != "AUTH_ERROR" || !strings.Contains(err.Error(), "not enabled for this project") { - t.Fatalf("err = %v", err) - } } diff --git a/internal/verda-cli/cmd/mk8s/nodepool_scale.go b/internal/verda-cli/cmd/mk8s/nodepool_scale.go index 3990965f..737a02d0 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_scale.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_scale.go @@ -101,11 +101,7 @@ func runNodePoolScale(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.I if err := opts.validate(f, cmd, args); err != nil { return err } - client, err := f.VerdaClient() - if err != nil { - return err - } - target, err := opts.resolve(cmd, f, ioStreams, client) + client, target, err := opts.connect(cmd, f, ioStreams) if err != nil || target == nil { return err } @@ -145,13 +141,9 @@ func runNodePoolScale(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.I return waitErr } -// validate checks the flags before any API call. func (o *nodePoolScaleOptions) validate(f cmdutil.Factory, cmd *cobra.Command, args []string) error { - if o.Wait.Wait && o.Wait.Timeout <= 0 { - return cmdutil.UsageErrorf(cmd, "--%s must be greater than 0", flagWaitTimeout) - } - if o.replicasSet && o.Replicas < 0 { - return cmdutil.UsageErrorf(cmd, "--%s must be 0 or more", flagReplicas) + if err := checkPoolFlags(cmd, o.Wait, o.Replicas); err != nil { + return err } if missing := o.scaleMissing(f, cmd, args); len(missing) > 0 { return cmdutil.NewMissingFlagsError(missing) @@ -159,13 +151,12 @@ func (o *nodePoolScaleOptions) validate(f cmdutil.Factory, cmd *cobra.Command, a return nil } -// poolSettled: the pool is already at the scale-done condition for want. func poolSettled(pool *verda.ManagedKubernetesNodePool, want int) bool { _, done := poolScaled(want)(pool) return done } -// applyScale confirms and sends the update. It returns nil without an error when the user declined. +// applyScale returns nil without an error when the user declined. func (o *nodePoolScaleOptions) applyScale(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, target *clusterTarget, pool *verda.ManagedKubernetesNodePool) (*verda.ManagedKubernetesNodePool, error) { if ok, err := o.confirm(cmd.Context(), f, ioStreams, target, pool); err != nil || !ok { return nil, err @@ -173,13 +164,11 @@ func (o *nodePoolScaleOptions) applyScale(cmd *cobra.Command, f cmdutil.Factory, replicas := o.Replicas req := verda.UpdateManagedKubernetesNodePoolRequest{Replicas: &replicas} cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) - ctx, cancel := context.WithTimeout(cmd.Context(), f.Options().Timeout) - defer cancel() - updated, err := cmdutil.WithSpinner(ctx, f.Status(), "Scaling node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { + updated, err := apiCall(cmd.Context(), f, "Scaling node pool...", func(ctx context.Context) (*verda.ManagedKubernetesNodePool, error) { return client.ManagedKubernetes.UpdateNodePool(ctx, target.Location, target.Name, pool.Name, req) }) if err != nil { - return nil, mapErr(err) + return nil, err } cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "API response:", newPoolView(updated)) return updated, nil @@ -192,7 +181,6 @@ func pluralize(n int, noun string) string { return fmt.Sprintf("%d %ss", n, noun) } -// reportScaleUnchanged: the pool already has the requested count, so nothing is sent. func reportScaleUnchanged(f cmdutil.Factory, ioStreams cmdutil.IOStreams, pool *verda.ManagedKubernetesNodePool) error { result := PoolScaleResult{Status: statusUnchanged, PreviousReplicas: pool.Replicas, Pool: newPoolView(pool)} if wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result); wrote { @@ -202,20 +190,15 @@ func reportScaleUnchanged(f cmdutil.Factory, ioStreams cmdutil.IOStreams, pool * return nil } -// scaleMissing lists what a scale needs that a terminal cannot ask for. func (o *nodePoolScaleOptions) scaleMissing(f cmdutil.Factory, cmd *cobra.Command, args []string) []string { - missing := o.missing(f, cmd) - if poolArgMissing(f, args) { - missing = append(missing, argPool) - } + missing := o.missing(f, cmd, args, argPool) if !o.replicasSet && !interactiveTTY(f) { missing = append(missing, "--"+flagReplicas) } return missing } -// confirm applies the confirmation rule: only a scale-down is destructive, and -// only now is the current count known. It reports whether to go on. +// confirm: only a scale-down is destructive, and only now is the current count known. func (o *nodePoolScaleOptions) confirm(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, t *clusterTarget, pool *verda.ManagedKubernetesNodePool) (bool, error) { if o.Yes { return true, nil @@ -230,7 +213,6 @@ func (o *nodePoolScaleOptions) confirm(ctx context.Context, f cmdutil.Factory, i return confirmScale(ctx, f, ioStreams, t, pool, o.Replicas, down) } -// wait polls until the new count is ready and records the outcome in result. func (o *nodePoolScaleOptions) wait(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, updated *verda.ManagedKubernetesNodePool, result *PoolScaleResult) error { last, err := waitForPool(ctx, f, ioStreams, client, t, updated.Name, poolScaled(o.Replicas), o.Wait.Timeout) if last != nil { @@ -252,7 +234,7 @@ func (o *nodePoolScaleOptions) wait(ctx context.Context, f cmdutil.Factory, ioSt return err } -// askReplicas prompts for the new count. ok is false when the user canceled. +// askReplicas: ok is false when the user canceled. func askReplicas(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, pool *verda.ManagedKubernetesNodePool) (n int, ok bool, err error) { v, err := f.Prompter().TextInput(ctx, fmt.Sprintf("New number of workers (pool %s has %d)", pool.Name, pool.Replicas), tui.WithDefault(strconv.Itoa(pool.Replicas)), tui.WithValidation(validateReplicas)) diff --git a/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go b/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go index 0d8f1cb9..0c920f8d 100644 --- a/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go +++ b/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go @@ -15,9 +15,7 @@ package mk8s import ( - "net/http" "slices" - "strconv" "strings" "sync/atomic" "testing" @@ -29,13 +27,6 @@ import ( cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -// scaleAPI has pool "workers" in cluster "prod": 3 of 3 workers, active. -func scaleAPI() *fakeAPI { - api := seededClusters() - api.addPool("prod", "workers", "active", 3, 3) - return api -} - func scaleArgs(extra ...string) []string { return append([]string{"node-pool", "scale", "workers", "--cluster", "prod"}, extra...) } @@ -51,222 +42,104 @@ func patchedReplicas(t *testing.T, api *fakeAPI) any { func TestNodePoolScale_UpNeedsNoYesOffTerminal(t *testing.T) { t.Parallel() - api := scaleAPI() - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5")...) - if err != nil { - t.Fatal(err) - } - if !strings.Contains(out, "Scaling of node pool workers from 3 to 5 workers accepted (cluster prod, FIN-03).") { - t.Errorf("stdout = %q", out) - } - if !strings.Contains(errOut, "verda mk8s node-pool describe workers --cluster prod -l FIN-03") { - t.Errorf("stderr = %q", errOut) + api := poolAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5")...) + if err != nil || !strings.Contains(out, "Scaling of node pool workers from 3 to 5 workers accepted (cluster prod, FIN-03).") { + t.Fatalf("out=%q err=%v", out, err) } if got := patchedReplicas(t, api); got != float64(5) { t.Errorf("replicas = %v", got) } - if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/pools/workers"}) { - t.Errorf("reads = %v", got) - } } -func TestNodePoolScale_DownNeedsYesOffTerminal(t *testing.T) { +func TestNodePoolScale_Structured(t *testing.T) { t.Parallel() - for name, mk := range map[string]func(*fakeAPI, *testing.T) *cmdutil.TestFactory{ - "agent": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return agentFactory(newFakeClient(t, api)) }, - "json on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { return jsonFactory(newFakeClient(t, api)) }, - "table on a pipe": func(api *fakeAPI, t *testing.T) *cmdutil.TestFactory { - return tableFactory(newFakeClient(t, api), nil) - }, - } { - for _, replicas := range []string{"1", "0"} { - api := scaleAPI() - _, _, err := runMK8s(t, mk(api, t), scaleArgs("--replicas", replicas)...) - if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "CONFIRMATION_REQUIRED" || ae.Message != confirmationRequired("scale").Message { - t.Errorf("%s to %s: err = %v", name, replicas, err) - } - if n := len(api.requestsMatching("PATCH")); n != 0 { - t.Errorf("%s to %s: %d PATCH(es) without --yes", name, replicas, n) - } - } - } - - api := scaleAPI() - if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), scaleArgs("--replicas", "0", "--yes")...); err != nil { + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), scaleArgs("--replicas", "5", "-l", "FIN-03")...) + if err != nil { t.Fatal(err) } - if got := patchedReplicas(t, api); got != float64(0) { - t.Errorf("replicas = %v, want an explicit 0", got) + if r := decodeJSON[PoolScaleResult](t, out); r.Status != "accepted" || r.PreviousReplicas != 3 || r.Pool.Name != "workers" || r.Pool.Replicas != 5 { + t.Errorf("result = %+v", r) } } -func TestNodePoolScale_SameCountSendsNothing(t *testing.T) { +func TestNodePoolScale_MissingArguments(t *testing.T) { t.Parallel() - api := scaleAPI() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "3")...) - if err != nil || out != "Node pool workers already has 3 replicas; nothing to do.\n" { - t.Fatalf("out=%q err=%v", out, err) - } - api.pools["prod"][0].Replicas, api.pools["prod"][0].ReadyReplicas = 1, 1 - out, _, err = runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "1")...) - if err != nil || out != "Node pool workers already has 1 replica; nothing to do.\n" { - t.Fatalf("out=%q err=%v", out, err) - } - api.pools["prod"][0].Replicas, api.pools["prod"][0].ReadyReplicas = 3, 3 - out, _, err = runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "3", "--wait")...) - if r := decodeJSON[PoolScaleResult](t, out); err != nil || r.Status != "unchanged" || r.PreviousReplicas != 3 || r.Pool.Replicas != 3 { - t.Errorf("result = %+v err=%v", r, err) + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "node-pool", "scale") + ae := cmdutil.ClassifyError(err) + if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { + t.Fatalf("err = %v", err) } - if n := len(api.requestsMatching("PATCH")); n != 0 { - t.Errorf("PATCH requests = %d, want none", n) + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "", "--replicas"}) { + t.Errorf("missing = %v", missing) } } -func mustAtoi(t *testing.T, s string) int { - t.Helper() - n, err := strconv.Atoi(s) - if err != nil { - t.Fatal(err) +func TestNodePoolScale_InvalidFlagsSendNothing(t *testing.T) { + t.Parallel() + for _, args := range [][]string{scaleArgs("--replicas", "-1"), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "0s")} { + api := poolAPI() + if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), args...); !isUsageError(err) || len(api.requests) != 0 { + t.Errorf("%v: err = %v, %d request(s)", args, err, len(api.requests)) + } } - return n } -func TestNodePoolScale_SameCountWaitsUntilSettled(t *testing.T) { +func TestNodePoolScale_SameCountSendsNothing(t *testing.T) { fastPoll(t) - cases := map[string]struct { - state string + for name, tc := range map[string]struct { ready int wait bool wantStatus string }{ - "not ready, no --wait": {state: "active", ready: 1, wantStatus: "unchanged"}, - "fully ready, --wait": {state: "active", ready: 2, wait: true, wantStatus: "unchanged"}, - "not ready, --wait": {state: "active", ready: 1, wait: true, wantStatus: "completed"}, - "degraded at the count, --wait": {state: "degraded", ready: 2, wait: true, wantStatus: "completed"}, - } - for name, tc := range cases { + "no --wait": {ready: 1, wantStatus: "unchanged"}, + "--wait, settled": {ready: 2, wait: true, wantStatus: "unchanged"}, + "--wait, not settled yet": {ready: 1, wait: true, wantStatus: "completed"}, + } { api := seededClusters() - api.addPool("prod", "workers", tc.state, 2, tc.ready) + api.addPool("prod", "workers", "active", 2, tc.ready) var reads atomic.Int32 api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { - if reads.Add(1) > 2 { - p.State, p.Replicas, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateActive, 2, 2 + if reads.Add(1) > 1 { + p.ReadyReplicas = 2 } } args := scaleArgs("--replicas", "2") if tc.wait { - args = append(args, "--wait", "--wait-timeout", "30s") + args = append(args, "--wait") } out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...) - if err != nil { - t.Fatalf("%s: %v", name, err) - } - if r := decodeJSON[PoolScaleResult](t, out); r.Status != tc.wantStatus || r.PreviousReplicas != 2 || r.Pool.Replicas != 2 { - t.Errorf("%s: result = %+v", name, r) - } - if n := len(api.requestsMatching("PATCH")); n != 0 { - t.Errorf("%s: %d PATCH(es), want none", name, n) - } - if waited := reads.Load() > 1; waited != (tc.wantStatus == "completed") { - t.Errorf("%s: waited = %v, pool reads = %d", name, waited, reads.Load()) - } - } -} - -func TestNodePoolScale_SameCountWaitTimeout(t *testing.T) { - fastPoll(t) - api := seededClusters() - api.addPool("prod", "workers", "active", 2, 1) - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 1 } - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "2", "--wait", "--wait-timeout", "300ms")...) - if err == nil { - t.Fatal("a timeout must fail") - } - for _, want := range []string{"timed out after 300ms", "scaling of node pool workers in cluster prod (FIN-03)", "1/2 workers ready", "The backend keeps trying to create workers."} { - if !strings.Contains(err.Error(), want) { - t.Errorf("error misses %q: %v", want, err) + if r := decodeJSON[PoolScaleResult](t, out); err != nil || r.Status != tc.wantStatus || len(api.requestsMatching("PATCH")) != 0 { + t.Errorf("%s: result = %+v, err = %v", name, r, err) } } - if strings.Contains(err.Error(), "scale back") { - t.Errorf("there is no previous count to scale back to: %v", err) - } - if r := decodeJSON[PoolScaleResult](t, out); r.Status != "accepted" || r.WaitError == "" || len(api.requestsMatching("PATCH")) != 0 { - t.Errorf("result = %+v", r) - } -} - -func TestNodePoolScale_MissingArguments(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "node-pool", "scale") - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("err = %v", err) - } - if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "", "--replicas"}) { - t.Errorf("missing = %v", missing) - } -} - -func TestNodePoolScale_Structured(t *testing.T) { - t.Parallel() - out, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), scaleArgs("--replicas", "5", "-l", "FIN-03")...) - if err != nil { - t.Fatal(err) - } - r := decodeJSON[PoolScaleResult](t, out) - if r.Status != "accepted" || r.PreviousReplicas != 3 || r.Pool.Name != "workers" || r.Pool.Replicas != 5 { - t.Errorf("result = %+v", r) + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolAPI()), nil), scaleArgs("--replicas", "3")...) + if err != nil || out != "Node pool workers already has 3 replicas; nothing to do.\n" { + t.Errorf("out=%q err=%v", out, err) } } func TestNodePoolScale_Interactive(t *testing.T) { withTerminal(t) - t.Run("scale-up shows a summary and asks", func(t *testing.T) { - api := scaleAPI() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), scaleArgs("--replicas", "5")...) - if err != nil { - t.Fatal(err) - } - if !strings.Contains(errOut, "Workers: 3 -> 5") || strings.Contains(errOut, "cannot be undone") { - t.Errorf("summary = %q", errOut) - } - if got := patchedReplicas(t, api); got != float64(5) { - t.Errorf("replicas = %v", got) - } - }) - - t.Run("scale-down, to 0 too, shows the red warning", func(t *testing.T) { - for _, replicas := range []string{"1", "0"} { - api := scaleAPI() - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(true)), scaleArgs("--replicas", replicas)...) - if err != nil { - t.Fatal(err) - } - if want := "Scales node pool workers down from 3 to " + pluralize(mustAtoi(t, replicas), "worker") + "."; !strings.Contains(errOut, want) || !strings.Contains(errOut, "VMs are deleted") { - t.Errorf("to %s: warning = %q", replicas, errOut) + t.Run("up shows a summary, down the red warning", func(t *testing.T) { + for _, tc := range []struct { + replicas, want string + sent float64 + }{{"5", "Workers: 3 -> 5", 5}, {"1", "VMs are deleted", 1}} { + api := poolAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), confirmYes()), scaleArgs("--replicas", tc.replicas)...) + if err != nil || !strings.Contains(errOut, tc.want) { + t.Errorf("to %s: err=%v stderr=%q", tc.replicas, err, errOut) } - } - }) - - t.Run("declined, Esc and Ctrl+C change nothing", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "declined": {Prompter: tuitest.New().AddConfirm(false)}, - "esc": {Prompter: tuitest.New(), confirmErr: contextCanceled()}, - "ctrl+c": {Prompter: tuitest.New(), confirmErr: errInterrupted()}, - } { - api := scaleAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - _, errOut, err := runMK8s(t, f, scaleArgs("--replicas", "1")...) - if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("PATCH")) != 0 { - t.Errorf("%s: err=%v stderr=%q", name, err, errOut) + if got := patchedReplicas(t, api); got != tc.sent { + t.Errorf("to %s: replicas = %v", tc.replicas, got) } } }) t.Run("--yes skips the confirmation", func(t *testing.T) { - api := scaleAPI() + api := poolAPI() if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New()), scaleArgs("--replicas", "1", "--yes")...); err != nil { t.Fatal(err) } @@ -276,7 +149,7 @@ func TestNodePoolScale_Interactive(t *testing.T) { }) t.Run("pickers and the replicas prompt", func(t *testing.T) { - api := scaleAPI() + api := poolAPI() p := pickerAnswers(0, 0).AddTextInput("4").AddConfirm(true) // cluster, pool, count, confirm if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), p), "node-pool", "scale"); err != nil { t.Fatal(err) @@ -285,198 +158,69 @@ func TestNodePoolScale_Interactive(t *testing.T) { t.Errorf("replicas = %v", got) } }) - - t.Run("canceling the replicas prompt changes nothing", func(t *testing.T) { - api := scaleAPI() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = &textCancelPrompter{Prompter: tuitest.New()} - _, errOut, err := runMK8s(t, f, scaleArgs()...) - if err != nil || !strings.Contains(errOut, canceledMsg) || len(api.requestsMatching("PATCH")) != 0 { - t.Errorf("err=%v stderr=%q", err, errOut) - } - }) } -// patchSeen reports whether the fake has taken an update; it runs inside a handler, which holds the lock. -func patchSeen(api *fakeAPI) bool { - for _, r := range api.requests { - if r.Method == http.MethodPatch { - return true +func TestNodePoolScale_Wait(t *testing.T) { + fastPoll(t) + for _, tc := range []struct{ replicas, want string }{ + {"5", "Node pool workers scaled from 3 to 5 workers (cluster prod, FIN-03).\n"}, + {"1", "Node pool workers scaled from 3 to 1 worker (cluster prod, FIN-03). The removed workers' VMs are still being deleted in the background.\n"}, + } { + api := poolAPI() + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = p.Replicas } + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", tc.replicas, "--yes", "--wait")...) + if err != nil || out != tc.want { + t.Errorf("to %s: out=%q err=%v", tc.replicas, out, err) } } - return false -} -func TestNodePoolScale_WaitUp(t *testing.T) { - fastPoll(t) - api := scaleAPI() - var waited atomic.Int32 + // Ready already at the target but replicas still the old count is not done: + // scale waits for poolScaled, not poolCreated. + api := poolAPI() + reads := 0 api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { - if !patchSeen(api) { - return // the read that finds the pool, before the update - } - switch waited.Add(1) { - case 1: // stale: the old count, all ready, active - p.Replicas, p.ReadyReplicas = 3, 3 - case 2: // new count, workers still joining - p.Replicas, p.ReadyReplicas = 5, 3 + reads++ // read 1 is the pool lookup before the PATCH + switch reads { + case 1: + case 2: + p.Replicas, p.ReadyReplicas = 3, 5 default: p.Replicas, p.ReadyReplicas = 5, 5 } - p.State = verda.ManagedKubernetesNodePoolStateActive - } - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "30s")...) - if err != nil || !strings.Contains(out, "Node pool workers scaled from 3 to 5 workers (cluster prod, FIN-03).") { - t.Fatalf("out=%q err=%v", out, err) - } - if strings.Contains(out, "VMs are still being deleted") { - t.Errorf("a scale-up has no removed VMs: %q", out) - } - if got := waited.Load(); got != 3 { - t.Errorf("reads while waiting = %d, want 3: the stale first read must not end the wait", got) - } -} - -func TestNodePoolScale_WaitDownSaysVMsAreStillDeleted(t *testing.T) { - fastPoll(t) - api := scaleAPI() - var reads atomic.Int32 - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { - if reads.Add(1) == 2 { // the first read after the update: workers not removed yet - p.Replicas, p.ReadyReplicas = 1, 3 - return - } - if reads.Load() > 2 { - p.Replicas, p.ReadyReplicas = 1, 1 - } } - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "1", "--yes", "--wait")...) - if err != nil || !strings.Contains(out, "scaled from 3 to 1 worker (") || !strings.Contains(out, "The removed workers' VMs are still being deleted in the background.") { - t.Fatalf("out=%q err=%v", out, err) + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "5", "--wait")...) + if r := decodeJSON[PoolScaleResult](t, out); err != nil || r.Status != "completed" || reads != 3 { + t.Errorf("status=%s reads=%d err=%v, want completed after the third read", r.Status, reads, err) } } +// After a scale-up the advice is to scale back; a same-count wait sends no +// PATCH and has no previous count to scale back to. func TestNodePoolScale_WaitTimeout(t *testing.T) { fastPoll(t) - api := scaleAPI() - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 3 } - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "300ms")...) - if err == nil { - t.Fatal("a timeout must fail") - } - for _, want := range []string{"timed out after 300ms", "node pool workers in cluster prod (FIN-03)", "3/5 workers ready", "The backend keeps trying to create workers.", "To stop, scale back: `verda mk8s node-pool scale workers --cluster prod -l FIN-03 --replicas 3 --yes`"} { - if !strings.Contains(err.Error(), want) { - t.Errorf("error misses %q: %v", want, err) - } - } - if strings.Contains(err.Error(), "node-pool delete") { - t.Errorf("a scale-up timeout must not advise deleting the pool: %v", err) - } - if r := decodeJSON[PoolScaleResult](t, out); r.Status != "accepted" || r.WaitError == "" { - t.Errorf("result = %+v", r) - } - - // For a person the advice is in the error only, not also on stderr before it. - api = scaleAPI() - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 3 } - _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "300ms")...) - if err == nil || strings.Contains(errOut, "Check progress with") { - t.Errorf("err=%v stderr=%q", err, errOut) - } -} - -func TestNodePoolScale_WaitDownTimeoutDoesNotTalkAboutCreatingWorkers(t *testing.T) { - fastPoll(t) - api := scaleAPI() - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = 3 } - _, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", "1", "--yes", "--wait", "--wait-timeout", "300ms")...) - if err == nil { - t.Fatal("a timeout must fail") - } - if !strings.Contains(err.Error(), "scale-down of node pool workers") || !strings.Contains(err.Error(), "still removing workers") { - t.Errorf("err = %v", err) - } - for _, bad := range []string{"keeps trying to create", "keep billing", "node-pool delete"} { - if strings.Contains(err.Error(), bad) { - t.Errorf("a scale-down timeout must not say %q: %v", bad, err) - } - } -} - -func TestNodePoolScale_WaitUntilActiveAndTheNewCount(t *testing.T) { - fastPoll(t) - cases := map[string]struct { - want int - reads []verda.ManagedKubernetesNodePool // the waiting reads, in order + for _, tc := range []struct { + api *fakeAPI + replicas, want string + ready, patches int }{ - "to 0, a stale read still shows the workers": {want: 0, reads: []verda.ManagedKubernetesNodePool{ - {State: "active", Replicas: 0, ReadyReplicas: 3}, - {State: "active", Replicas: 0, ReadyReplicas: 0}, - }}, - "degraded at the new count is not done": {want: 5, reads: []verda.ManagedKubernetesNodePool{ - {State: "degraded", Replicas: 5, ReadyReplicas: 5}, - {State: "active", Replicas: 5, ReadyReplicas: 5}, - }}, - "ready equals the target but replicas is still the old count": {want: 5, reads: []verda.ManagedKubernetesNodePool{ - {State: "active", Replicas: 3, ReadyReplicas: 5}, - {State: "active", Replicas: 5, ReadyReplicas: 5}, - }}, - } - for name, tc := range cases { - api := scaleAPI() - var waited atomic.Int32 - api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { - if !patchSeen(api) { - return - } - i := min(int(waited.Add(1))-1, len(tc.reads)-1) - p.State, p.Replicas, p.ReadyReplicas = tc.reads[i].State, tc.reads[i].Replicas, tc.reads[i].ReadyReplicas - } - out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", strconv.Itoa(tc.want), "--yes", "--wait")...) - if err != nil { - t.Fatalf("%s: %v", name, err) - } - if r := decodeJSON[PoolScaleResult](t, out); r.Status != "completed" || int(waited.Load()) != len(tc.reads) { - t.Errorf("%s: status=%s waiting reads=%d, want completed after %d", name, r.Status, waited.Load(), len(tc.reads)) - } - } -} - -func TestNodePoolScale_NegativeReplicasIsAUsageErrorBeforeAnyConfirmation(t *testing.T) { - t.Parallel() - for name, args := range map[string][]string{ - "with --yes": scaleArgs("--replicas", "-1", "--yes"), - "without --yes": scaleArgs("--replicas", "-1"), + {poolAPI(), "5", "To stop, scale back: `verda mk8s node-pool scale workers --cluster prod -l FIN-03 --replicas 3 --yes`", 3, 1}, + {poolAPI(), "1", "scale-down of node pool workers in cluster prod (FIN-03) (last state: active, 3/1 workers ready). The API is still removing workers.", 3, 1}, + {sameCountAPI(), "2", "scaling of node pool workers in cluster prod (FIN-03) (last state: active, 1/2 workers ready). The backend keeps trying to create workers.", 1, 0}, } { - api := scaleAPI() - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, api)), args...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { - t.Errorf("%s: err = %v, want a usage error, not CONFIRMATION_REQUIRED", name, err) + api := tc.api + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { p.ReadyReplicas = tc.ready } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), scaleArgs("--replicas", tc.replicas, "--yes", "--wait", "--wait-timeout", "300ms")...) + if err == nil || !strings.Contains(err.Error(), tc.want) || (tc.patches == 0 && strings.Contains(err.Error(), "scale back")) { + t.Errorf("to %s: err = %v", tc.replicas, err) } - if len(api.requests) != 0 { - t.Errorf("%s: %d request(s) before the check", name, len(api.requests)) + if r := decodeJSON[PoolScaleResult](t, out); r.Status != "accepted" || r.WaitError == "" || len(api.requestsMatching("PATCH")) != tc.patches { + t.Errorf("to %s: result = %+v", tc.replicas, r) } } } -func TestNodePoolScale_WaitTimeoutMustBePositive(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, scaleAPI()), nil), scaleArgs("--replicas", "5", "--wait", "--wait-timeout", "0s")...) - if agentErrorCode(err) != "VALIDATION_ERROR" || cmdutil.IsAgentError(err) { - t.Errorf("err = %v", err) - } -} - -func TestNodePoolScale_APIErrorAndValidation(t *testing.T) { - t.Parallel() - api := scaleAPI() - api.failures["PATCH /mk8sclusters/{loc}/{name}/pools/{pool}"] = apiFailure{Status: 409, Body: `{"code":"conflict","message":"busy","details":"concurrent_modification"}`} - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), scaleArgs("--replicas", "5")...) - if err == nil || !strings.Contains(err.Error(), "concurrent_modification") { - t.Errorf("err = %v", err) - } - - if _, _, err := runMK8s(t, agentFactory(newFakeClient(t, scaleAPI())), "node-pool", "scale", "ghost", "--cluster", "prod", "--replicas", "2"); agentErrorCode(err) != "NOT_FOUND" { - t.Errorf("unknown pool: %v", err) - } +func sameCountAPI() *fakeAPI { + api := seededClusters() + api.addPool("prod", "workers", "active", 2, 1) + return api } diff --git a/internal/verda-cli/cmd/mk8s/resolve.go b/internal/verda-cli/cmd/mk8s/resolve.go index 24876fa6..14800cf7 100644 --- a/internal/verda-cli/cmd/mk8s/resolve.go +++ b/internal/verda-cli/cmd/mk8s/resolve.go @@ -36,22 +36,18 @@ const ( canceledMsg = "Canceled." ) -// clusterTarget names an existing cluster: names are unique per project, but -// every API path also needs the location. +// clusterTarget: names are unique per project, but every API path also needs the location. type clusterTarget struct { Name string Location string } -// addLocationFlag registers -l/--location for commands that act on a cluster. func addLocationFlag(cmd *cobra.Command, location *string) { cmd.Flags().StringVarP(location, flagLocation, "l", "", locationUsage) } -// checkClusterArg: off a terminal the cluster is required, and a blank one is -// never sent. given says whether the user passed it; label names it in the -// error ( or --cluster). Call it before f.VerdaClient() so bad input -// fails the same with or without credentials. +// checkClusterArg runs before f.VerdaClient(), so bad input fails the same +// with or without credentials. label is or --cluster. func checkClusterArg(f cmdutil.Factory, name string, given bool, label string) error { if clusterArgMissing(f, name, given) { return cmdutil.NewMissingFlagsError([]string{label}) @@ -65,10 +61,8 @@ func clusterArgMissing(f cmdutil.Factory, name string, given bool) bool { return blank || (!given && !interactiveTTY(f)) } -// resolveCluster finds the cluster to act on: with a name and a location no -// call, with a name alone one List, with no name a picker. A nil target with a -// nil error means the user canceled or there is nothing to pick. Call -// checkClusterArg first. +// resolveCluster: name and location make no call, a name alone one List, no +// name a picker. A nil target with a nil error means canceled or nothing to pick. func resolveCluster(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, name, location string) (*clusterTarget, error) { name, location = strings.TrimSpace(name), strings.TrimSpace(location) if name != "" && location != "" { @@ -84,7 +78,6 @@ func resolveCluster(cmd *cobra.Command, f cmdutil.Factory, ioStreams cmdutil.IOS return matchCluster(cmd, clusters, name) } -// argOrEmpty is the first positional argument, or "". func argOrEmpty(args []string) string { if len(args) == 0 { return "" @@ -92,7 +85,6 @@ func argOrEmpty(args []string) string { return args[0] } -// describeCmd is the command that shows a cluster, for "check with" hints. func describeCmd(name, location string) string { return fmt.Sprintf("verda mk8s cluster describe %s -l %s", name, location) } @@ -111,15 +103,19 @@ func apiCall[T any](ctx context.Context, f cmdutil.Factory, msg string, fn func( return v, mapErr(err) } +func apiRun(ctx context.Context, f cmdutil.Factory, msg string, fn func(context.Context) error) error { + _, err := apiCall(ctx, f, msg, func(ctx context.Context) (struct{}, error) { return struct{}{}, fn(ctx) }) + return err +} + func listClusters(ctx context.Context, f cmdutil.Factory, client *verda.Client) ([]verda.ManagedKubernetesCluster, error) { return apiCall(ctx, f, "Loading clusters...", func(ctx context.Context) ([]verda.ManagedKubernetesCluster, error) { return client.ManagedKubernetes.List(ctx) }) } -// matchCluster finds name in the project's clusters. The errors are plain, not -// AgentErrors, which main prints as JSON even for a person: ClassifyError still -// gives NOT_FOUND and VALIDATION_ERROR. +// matchCluster returns plain errors, not AgentErrors (main prints those as JSON +// even for a person); ClassifyError still gives NOT_FOUND and VALIDATION_ERROR. func matchCluster(cmd *cobra.Command, clusters []verda.ManagedKubernetesCluster, name string) (*clusterTarget, error) { var locations []string for i := range clusters { @@ -137,7 +133,6 @@ func matchCluster(cmd *cobra.Command, clusters []verda.ManagedKubernetesCluster, } } -// pickCluster prompts for a cluster, limited to one location when given. func pickCluster(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, clusters []verda.ManagedKubernetesCluster, location string) (*clusterTarget, error) { var shown []verda.ManagedKubernetesCluster for i := range clusters { @@ -170,8 +165,7 @@ func pickCluster(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStr return nil, nil } -// pickerHints shows the hint bar of a picker outside a wizard. Esc ends the -// command there, so the bar says cancel, not back. +// pickerHints: outside a wizard Esc ends the command, so the bar says cancel, not back. func pickerHints() []tui.SelectOption { return []tui.SelectOption{tui.WithShowHints(true), tui.WithSelectRelabel("esc", "esc cancel")} } diff --git a/internal/verda-cli/cmd/mk8s/resolve_test.go b/internal/verda-cli/cmd/mk8s/resolve_test.go index 87e1afa2..f1847d69 100644 --- a/internal/verda-cli/cmd/mk8s/resolve_test.go +++ b/internal/verda-cli/cmd/mk8s/resolve_test.go @@ -17,7 +17,6 @@ package mk8s import ( "context" "errors" - "slices" "strings" "testing" @@ -27,44 +26,7 @@ import ( cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -func TestResolve_LocationGivenMakesNoListCall(t *testing.T) { - t.Parallel() - api := seededClusters() - if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod", "-l", "FIN-03"); err != nil { - t.Fatal(err) - } - if slices.Contains(api.paths("GET"), "/mk8sclusters") { - t.Errorf("-l must skip the List call: %v", api.paths("GET")) - } -} - -func TestResolve_NotFound(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "describe", "ghost") - if err == nil || agentErrorCode(err) != "NOT_FOUND" || !strings.Contains(err.Error(), "ghost") { - t.Fatalf("err = %v", err) - } -} - -func TestResolve_ErrorsArePlainSoMainPrintsThemAsText(t *testing.T) { - t.Parallel() - api := seededClusters() - api.addCluster("prod", "FIN-01", "running") - for name, args := range map[string][]string{ - "not found": {"cluster", "describe", "ghost"}, - "ambiguous": {"cluster", "describe", "prod"}, - } { - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), args...) - if err == nil { - t.Fatalf("%s: no error", name) - } - // main prints an AgentError as a JSON envelope even for a person. - if cmdutil.IsAgentError(err) { - t.Errorf("%s: %v is an AgentError, a person would get JSON", name, err) - } - } -} - +// With -l there is no List call; name and location are trimmed. func TestResolve_TrimsNameAndLocation(t *testing.T) { t.Parallel() api := seededClusters() @@ -76,40 +38,31 @@ func TestResolve_TrimsNameAndLocation(t *testing.T) { } } -func TestResolve_ExplicitLocationWrongIsAPI404(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), nil), "cluster", "describe", "prod", "-l", "FIN-01") - if err == nil || agentErrorCode(err) != "NOT_FOUND" { - t.Fatalf("err = %v", err) - } -} - -func TestResolve_NameInTwoLocationsAsksForLocation(t *testing.T) { +// Lookup errors are plain, not AgentErrors: main prints an AgentError as JSON even for a person. +func TestResolve_Errors(t *testing.T) { t.Parallel() api := seededClusters() - api.addCluster("prod", "FIN-01", "running") - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod") - if err == nil || agentErrorCode(err) != "VALIDATION_ERROR" { - t.Fatalf("err = %v", err) - } - for _, want := range []string{"FIN-01", "FIN-03", "-l"} { - if !strings.Contains(err.Error(), want) { - t.Errorf("error misses %q: %v", want, err) - } - } -} - -func TestResolve_MissingClusterOffTerminal(t *testing.T) { - t.Parallel() - for name, f := range map[string]*cmdutil.TestFactory{ - "table": tableFactory(newFakeClient(t, seededClusters()), nil), - "agent": agentFactory(newFakeClient(t, seededClusters())), + api.addCluster("twice", "FIN-01", "running") + api.addCluster("twice", "FIN-03", "running") + for name, tc := range map[string]struct { + args []string + code string + agentErr bool + mentions []string + }{ + "missing": {[]string{}, "MISSING_REQUIRED_FLAGS", true, nil}, + "blank": {[]string{" "}, "MISSING_REQUIRED_FLAGS", true, nil}, + "not found": {[]string{"ghost"}, "NOT_FOUND", false, []string{"ghost"}}, + "wrong -l": {[]string{"staging", "-l", "FIN-03"}, "NOT_FOUND", false, nil}, + "ambiguous": {[]string{"twice"}, "VALIDATION_ERROR", false, []string{"FIN-01", "FIN-03", "-l"}}, } { - for _, args := range [][]string{{"cluster", "describe"}, {"cluster", "describe", " "}} { - _, _, err := runMK8s(t, f, args...) - ae := cmdutil.ClassifyError(err) - if ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Errorf("%s %v: err = %v", name, args, err) + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), append([]string{"cluster", "describe"}, tc.args...)...) + if agentErrorCode(err) != tc.code || cmdutil.IsAgentError(err) != tc.agentErr { + t.Errorf("%s: err = %v", name, err) + } + for _, want := range tc.mentions { + if !strings.Contains(err.Error(), want) { + t.Errorf("%s: error misses %q: %v", name, want, err) } } } @@ -117,94 +70,31 @@ func TestResolve_MissingClusterOffTerminal(t *testing.T) { func TestResolve_Picker(t *testing.T) { withTerminal(t) - - t.Run("picks the second cluster", func(t *testing.T) { - api := seededClusters() - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddSelect(1)), "cluster", "describe") - if err != nil { - t.Fatal(err) - } - if !strings.Contains(out, "staging") || strings.Contains(out, "prod.example") { - t.Errorf("describe output = %s", out) - } - if got := api.paths("GET"); !slices.Contains(got, "/mk8sclusters/FIN-01/staging") { - t.Errorf("requests = %v", got) - } - }) - - t.Run("limited to -l", func(t *testing.T) { - var labels []string - p := &recordingPrompter{Prompter: tuitest.New().AddSelect(0), labels: &labels} - f := tableFactory(newFakeClient(t, seededClusters()), nil) - f.PrompterOverride = p - if _, _, err := runMK8s(t, f, "cluster", "describe", "-l", "FIN-01"); err != nil { - t.Fatal(err) - } - if len(labels) != 2 || !strings.HasPrefix(labels[0], "staging") || labels[1] != cancelLabel { - t.Errorf("labels = %v, want staging and Cancel only", labels) - } - }) - - t.Run("cancel row, Esc and Ctrl+C end cleanly", func(t *testing.T) { - for name, p := range map[string]*cancelPrompter{ - "cancel row": {Prompter: tuitest.New().AddSelect(2)}, - "esc": {Prompter: tuitest.New(), selectErr: context.Canceled}, - "ctrl+c": {Prompter: tuitest.New(), selectErr: tui.ErrInterrupted}, - } { - api := seededClusters() - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = p - out, errOut, err := runMK8s(t, f, "cluster", "describe") - if err != nil || out != "" || !strings.Contains(errOut, canceledMsg) { - t.Errorf("%s: out=%q err=%q error=%v", name, out, errOut, err) - } - if len(api.paths("GET")) != 1 { - t.Errorf("%s: only the List call may happen, got %v", name, api.paths("GET")) - } - } - }) -} - -func TestResolve_PickerEdgeCases(t *testing.T) { - withTerminal(t) - - t.Run("a picker error that is not a cancel is returned", func(t *testing.T) { - f := tableFactory(newFakeClient(t, seededClusters()), nil) - f.PrompterOverride = &cancelPrompter{Prompter: tuitest.New(), selectErr: errors.New("terminal broke")} - if _, _, err := runMK8s(t, f, "cluster", "describe"); err == nil || !strings.Contains(err.Error(), "terminal broke") { - t.Errorf("err = %v", err) - } - }) - - t.Run("no cluster in the chosen location", func(t *testing.T) { - out, _, err := runMK8s(t, tableFactory(newFakeClient(t, seededClusters()), tuitest.New()), "cluster", "describe", "-l", "FIN-99") - if err != nil || !strings.Contains(out, "No clusters found in FIN-99.") { - t.Errorf("out=%q err=%v", out, err) - } - }) - - t.Run("no clusters", func(t *testing.T) { - out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, newFakeAPI()), tuitest.New()), "cluster", "describe") - if err != nil || !strings.Contains(out, "No clusters found.") || !strings.Contains(errOut, "cluster create") { - t.Errorf("out=%q err=%q error=%v", out, errOut, err) + for name, tc := range map[string]struct { + api *fakeAPI + prompter *cancelPrompter + args []string + out, errOut string + err string + }{ + "picks the second": {seededClusters(), &cancelPrompter{Prompter: tuitest.New().AddSelect(1)}, nil, "staging", "", ""}, + "limited to -l": {seededClusters(), &cancelPrompter{Prompter: tuitest.New().AddSelect(0)}, []string{"-l", "FIN-01"}, "staging", "", ""}, + "cancel row": {seededClusters(), &cancelPrompter{Prompter: tuitest.New().AddSelect(2)}, nil, "", canceledMsg, ""}, + "esc": {seededClusters(), &cancelPrompter{Prompter: tuitest.New(), selectErr: context.Canceled}, nil, "", canceledMsg, ""}, + "ctrl+c": {seededClusters(), &cancelPrompter{Prompter: tuitest.New(), selectErr: tui.ErrInterrupted}, nil, "", canceledMsg, ""}, + "other picker error": {seededClusters(), &cancelPrompter{Prompter: tuitest.New(), selectErr: errors.New("terminal broke")}, nil, "", "", "terminal broke"}, + "none in the location": {seededClusters(), &cancelPrompter{Prompter: tuitest.New()}, []string{"-l", "FIN-99"}, "No clusters found in FIN-99.", "", ""}, + "no clusters": {newFakeAPI(), &cancelPrompter{Prompter: tuitest.New()}, nil, "No clusters found.", "cluster create", ""}, + } { + f := tableFactory(newFakeClient(t, tc.api), nil) + f.PrompterOverride = tc.prompter + out, errOut, err := runMK8s(t, f, append([]string{"cluster", "describe"}, tc.args...)...) + if (tc.err == "") != (err == nil) || (err != nil && !strings.Contains(err.Error(), tc.err)) || + !strings.Contains(out, tc.out) || !strings.Contains(errOut, tc.errOut) || (tc.out == "" && out != "") { + t.Errorf("%s: out=%q stderr=%q err=%v", name, out, errOut, err) } - }) - - t.Run("not offered in agent mode", func(t *testing.T) { - _, _, err := runMK8s(t, agentFactory(newFakeClient(t, seededClusters())), "cluster", "describe") - if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Errorf("err = %v", err) + if tc.out == "staging" && strings.Contains(out, "prod.example") { + t.Errorf("%s: described prod: %s", name, out) } - }) -} - -// recordingPrompter records the labels of every Select. -type recordingPrompter struct { - *tuitest.Prompter - labels *[]string -} - -func (p *recordingPrompter) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { - *p.labels = append(*p.labels, labels...) - return p.Prompter.Select(ctx, msg, labels, opts...) + } } diff --git a/internal/verda-cli/cmd/mk8s/testhelpers_test.go b/internal/verda-cli/cmd/mk8s/testhelpers_test.go deleted file mode 100644 index aca2da07..00000000 --- a/internal/verda-cli/cmd/mk8s/testhelpers_test.go +++ /dev/null @@ -1,187 +0,0 @@ -// Copyright 2026 Verda Cloud Oy -// -// 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 mk8s - -import ( - "context" - "encoding/json" - "os" - "testing" - - "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" - - "github.com/verda-cloud/verda-cli/pkg/tui" - tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" - - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" -) - -// TestMain makes every test start off a terminal, whatever runs the tests; withTerminal opts in. -func TestMain(m *testing.M) { - isInteractiveTerminal = func() bool { return false } - os.Exit(m.Run()) -} - -// addCluster seeds a cluster; a running one has an endpoint, like the API. -func (a *fakeAPI) addCluster(name, location, phase string) { - c := verda.ManagedKubernetesCluster{ - Name: name, - KubernetesVersion: "1.37.0", - Location: location, - NetworkID: "pn-1", - SubnetID: "sn-1", - PodCIDR: "10.100.0.0/16", - ServiceCIDR: "10.200.0.0/16", - Phase: phase, - CreatedAt: testCreated, - } - if phase == verda.ManagedKubernetesClusterPhaseRunning { - c.ExternalEndpoint = "https://" + name + ".example.test:6443" - } - a.clusters = append(a.clusters, c) -} - -func (a *fakeAPI) addPool(cluster, name, state string, replicas, ready int) { - a.pools[cluster] = append(a.pools[cluster], verda.ManagedKubernetesNodePool{ - Name: name, - Replicas: replicas, - ReadyReplicas: ready, - InstanceType: "1V100.6V", - SSHKeyIDs: []string{"key-1"}, - State: state, - CreatedAt: testCreated, - }) -} - -// paths returns the path of every recorded request with this method, in order. -func (a *fakeAPI) paths(method string) []string { - reqs := a.requestsMatching(method) - out := make([]string, 0, len(reqs)) - for _, r := range reqs { - out = append(out, r.Path) - } - return out -} - -// cancelPrompter answers Select or Confirm with an error (Esc, Ctrl+C) when set. -type cancelPrompter struct { - *tuitest.Prompter - selectErr error - confirmErr error -} - -func (p *cancelPrompter) Select(ctx context.Context, msg string, labels []string, opts ...tui.SelectOption) (int, error) { - if p.selectErr != nil { - return -1, p.selectErr - } - return p.Prompter.Select(ctx, msg, labels, opts...) -} - -func (p *cancelPrompter) Confirm(ctx context.Context, msg string, opts ...tui.ConfirmOption) (bool, error) { - if p.confirmErr != nil { - return false, p.confirmErr - } - return p.Prompter.Confirm(ctx, msg, opts...) -} - -// agentErrorCode is the code main reports, so an API 404 counts as NOT_FOUND. -func agentErrorCode(err error) string { - if ae := cmdutil.ClassifyError(err); ae != nil { - return ae.Code - } - return "" -} - -// isUsageError: VALIDATION_ERROR for agents, and not an AgentError, which main prints as JSON for people too. -func isUsageError(err error) bool { - return agentErrorCode(err) == "VALIDATION_ERROR" && !cmdutil.IsAgentError(err) -} - -func decodeJSON[T any](t *testing.T, s string) T { - t.Helper() - var v T - if err := json.Unmarshal([]byte(s), &v); err != nil { - t.Fatalf("decode %q: %v", s, err) - } - return v -} - -func yamlFactory(client *verda.Client) *cmdutil.TestFactory { - return &cmdutil.TestFactory{ClientOverride: client, OutputFormatOverride: "yaml"} -} - -func conditionReady() verda.ManagedKubernetesCondition { - return verda.ManagedKubernetesCondition{Type: "Ready", Status: "True", Reason: "AllGood", Message: "all components ready", LastTransitionTime: testCreated} -} - -func contextCanceled() error { return context.Canceled } - -func errInterrupted() error { return tui.ErrInterrupted } - -func (a *fakeAPI) addNetwork(id, name string) { - a.networks = append(a.networks, verda.PrivateNetwork{ID: id, Name: name, Mode: verda.NetworkModeCustom, CreatedAt: testCreated}) -} - -func (a *fakeAPI) addSubnet(networkID, id, name, location, cidr string) { - a.subnets[networkID] = append(a.subnets[networkID], verda.Subnet{ - ID: id, Name: name, CIDR: cidr, Location: verda.Location{Code: location}, CreatedAt: testCreated, - }) -} - -func confirmYes() *tuitest.Prompter { return tuitest.New().AddConfirm(true) } - -func confirmNo() *tuitest.Prompter { return tuitest.New().AddConfirm(false) } - -// textCancelPrompter answers TextInput with Esc. -type textCancelPrompter struct{ *tuitest.Prompter } - -func (p *textCancelPrompter) TextInput(context.Context, string, ...tui.TextInputOption) (string, error) { - return "", context.Canceled -} - -// pickerAnswers queues Select answers (indices) for a picker. -func pickerAnswers(indices ...int) *tuitest.Prompter { - p := tuitest.New() - for _, i := range indices { - p.AddSelect(i) - } - return p -} - -// poolWizardAPI has two clusters, instance types per location and two SSH keys. -func poolWizardAPI() *fakeAPI { - api := seededClusters() - api.availability["FIN-03"] = []string{"2A100.80S", "1V100.6V", "CPU.4V.16G"} - api.availability["FIN-01"] = []string{"1A100.22V"} - api.sshKeys = []verda.SSHKey{ - {ID: "key-1", Name: "laptop", CreatedAt: testCreated}, - {ID: "key-2", Name: "ci", CreatedAt: testCreated}, - } - return api -} - -func postBodyAny(t *testing.T, api *fakeAPI) map[string]any { - t.Helper() - reqs := api.requestsMatching("POST") - if len(reqs) != 1 { - t.Fatalf("want one POST, got %v", reqs) - } - return decodeJSON[map[string]any](t, reqs[0].Body) -} - -// addKubeconfig seeds a kubeconfig of a cluster. -func (a *fakeAPI) addKubeconfig(cluster string, k verda.ManagedKubernetesKubeconfig) { - a.kubeconfigs[cluster] = append(a.kubeconfigs[cluster], k) -} diff --git a/internal/verda-cli/cmd/mk8s/views.go b/internal/verda-cli/cmd/mk8s/views.go index e6548453..30154dfa 100644 --- a/internal/verda-cli/cmd/mk8s/views.go +++ b/internal/verda-cli/cmd/mk8s/views.go @@ -23,10 +23,7 @@ import ( // KubeconfigView is the JSON/YAML (and --debug) shape of a kubeconfig. It has // no field for the YAML, which holds a client private key: never marshal the -// SDK struct instead. -// -// Every key is always present, so scripts see one shape (a pending kubeconfig -// has not_after null, one without groups has []); only file is conditional. +// SDK struct instead. Every key but file is always present. type KubeconfigView struct { ID string `json:"id" yaml:"id"` DisplayName string `json:"display_name" yaml:"display_name"` @@ -37,7 +34,6 @@ type KubeconfigView struct { File string `json:"file,omitempty" yaml:"file,omitempty"` } -// newKubeconfigView copies the metadata only; file is the path written, if any. func newKubeconfigView(k *verda.ManagedKubernetesKubeconfig, file string) KubeconfigView { groups := k.Groups if groups == nil { @@ -139,9 +135,8 @@ func newPoolSummaryViews(pools []verda.ManagedKubernetesNodePool) []PoolSummaryV return views } -// ClusterDescribeView is a cluster with its node pools. node_pools is null -// (and node_pools_error set) when the pool call failed, [] when there are none. -// It is a pointer so YAML, which writes a nil slice as [], also says null. +// ClusterDescribeView: node_pools is null when the pool call failed, [] when +// there are none; a pointer, because YAML writes a nil slice as []. type ClusterDescribeView struct { ClusterView `yaml:",inline"` NodePools *[]PoolSummaryView `json:"node_pools" yaml:"node_pools"` @@ -166,8 +161,7 @@ func ageColumn(t, now time.Time) string { } } -// ActionResult is the structured output of a command that only accepts a -// request, such as a delete. Cluster is set for resources that belong to one. +// ActionResult is the structured output of a delete. type ActionResult struct { Resource string `json:"resource" yaml:"resource"` ID string `json:"id,omitempty" yaml:"id,omitempty"` @@ -178,12 +172,11 @@ type ActionResult struct { Status string `json:"status" yaml:"status"` } -// utcTime renders a time as "2006-01-02 15:04 UTC", the form of every date in mk8s output. +// utcTime is the form of every date in mk8s output. func utcTime(t time.Time) string { return t.UTC().Format(timeLayout) + " UTC" } -// timeColumn renders a time for a table, "-" when it is not set. func timeColumn(t time.Time) string { if t.IsZero() { return "-" diff --git a/internal/verda-cli/cmd/mk8s/wait.go b/internal/verda-cli/cmd/mk8s/wait.go index cd8ba4e8..ca11eed9 100644 --- a/internal/verda-cli/cmd/mk8s/wait.go +++ b/internal/verda-cli/cmd/mk8s/wait.go @@ -21,6 +21,7 @@ import ( "io" "net/http" "slices" + "strconv" "strings" "time" @@ -29,23 +30,18 @@ import ( cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -// maxReadFailures is how many reads in a row may fail before a wait gives up. -// One 502 from a load balancer must not fail a 15-minute wait. +// maxReadFailures: reads in a row that may fail; one 502 from a load balancer must not fail a 15-minute wait. const maxReadFailures = 3 -// pollInterval is the time between reads while --wait runs; a var so tests can shorten it. +// pollInterval is a var so tests can shorten it. var pollInterval = 5 * time.Second -var errWaitTimeout = errors.New("timed out waiting") - // pollReadError marks reads that kept failing, so the failure is not mistaken for a timeout. type pollReadError struct{ err error } func (e *pollReadError) Error() string { return e.err.Error() } -func (e *pollReadError) Unwrap() error { return e.err } -// fatalRead: a read that retrying cannot fix (bad request, no access). A 404 -// is not fatal: right after a create the read can lag behind. +// fatalRead: retrying cannot fix it. A 404 is not fatal: right after a create the read can lag behind. func fatalRead(err error) bool { apiErr, ok := errors.AsType[*verda.APIError](err) if !ok || apiErr.StatusCode < http.StatusBadRequest || apiErr.StatusCode >= http.StatusInternalServerError { @@ -58,9 +54,8 @@ func fatalRead(err error) bool { return true } -// waitFor polls get until check reports done. Each read has its own --timeout; -// the whole wait ends after timeout. It returns the last resource read and one -// of: nil (done), a *pollReadError (reads kept failing), or errWaitTimeout. +// waitFor: each read has its own --timeout; the whole wait ends after timeout. +// It returns the last read and nil (done), a *pollReadError or a timeout error. func waitFor[T any]( ctx context.Context, f cmdutil.Factory, w io.Writer, timeout time.Duration, get func(context.Context) (*T, error), check func(*T) (status string, done bool), @@ -73,11 +68,11 @@ func waitFor[T any]( defer cancel() v, err := get(readCtx) if err != nil { - // The whole wait ran out during this read: that is a timeout, not a failed read. + // The whole wait ran out during this read: a timeout, not a failed read. if ctx.Err() != nil { return status, false, nil } - // Until a first read works, a 404 is a read lagging behind the create: wait it out. + // Until a first read works, a 404 is a read lagging behind the create. if apiErr, ok := errors.AsType[*verda.APIError](err); ok && apiErr.StatusCode == http.StatusNotFound && last == nil { return status, false, nil } @@ -94,17 +89,10 @@ func waitFor[T any]( return status, done, nil } _, err := cmdutil.Poll(ctx, w, pollInterval, cmdutil.WaitOptions{Wait: true, Timeout: timeout}, pollFn) - if readErr, ok := errors.AsType[*pollReadError](err); ok { - return last, readErr - } - if err != nil { - return last, errWaitTimeout - } - return last, nil + return last, err } -// waitWriter is where the wait's spinner line goes: stderr, and only for a -// human at a terminal, so stdout and structured output stay clean. +// waitWriter: the spinner line goes to stderr, and only for a human at a terminal. func waitWriter(f cmdutil.Factory, ioStreams cmdutil.IOStreams) io.Writer { if f.OutputFormat() == outputTable && !f.AgentMode() && cmdutil.IsStderrTerminal() { return ioStreams.ErrOut @@ -112,22 +100,19 @@ func waitWriter(f cmdutil.Factory, ioStreams cmdutil.IOStreams) io.Writer { return nil } -// clusterReady: the cluster is usable once it is running and has an endpoint. func clusterReady(c *verda.ManagedKubernetesCluster) (status string, done bool) { return c.Phase, c.Phase == verda.ManagedKubernetesClusterPhaseRunning && c.ExternalEndpoint != "" } -// waitFailureInfo describes what a failed wait was waiting for. type waitFailureInfo struct { Subject string // for example "cluster prod in FIN-03" Timeout time.Duration - LastSeen string // what the last read showed - TimeoutTail string // what the user should know or do after a timeout - ReadNote string // optional: what the user should know when the reads kept failing + LastSeen string + TimeoutTail string // what to know or do after a timeout + ReadNote string // optional, after failed reads CheckCmd string } -// waitFailure turns the error of waitFor into the message the command returns. func waitFailure(err error, w *waitFailureInfo) error { if readErr, ok := errors.AsType[*pollReadError](err); ok { note := "" @@ -139,7 +124,6 @@ func waitFailure(err error, w *waitFailureInfo) error { return fmt.Errorf("timed out after %s waiting for %s (%s). %s", w.Timeout, w.Subject, w.LastSeen, w.TimeoutTail) } -// poolReasons lists the distinct error reasons of a pool, in order. func poolReasons(p *verda.ManagedKubernetesNodePool) []string { var reasons []string for i := range p.Errors { @@ -150,8 +134,7 @@ func poolReasons(p *verda.ManagedKubernetesNodePool) []string { return reasons } -// poolStatus is the one-line state shown while waiting: the state, the error -// reasons while the pool is degraded, and ready/desired workers. +// poolStatus is the line shown while waiting; reasons only while degraded. func poolStatus(p *verda.ManagedKubernetesNodePool) string { s := p.State if reasons := poolReasons(p); p.State == verda.ManagedKubernetesNodePoolStateDegraded && len(reasons) > 0 { @@ -160,16 +143,14 @@ func poolStatus(p *verda.ManagedKubernetesNodePool) string { return fmt.Sprintf("%s, %d/%d workers ready", s, p.ReadyReplicas, p.Replicas) } -// poolCreated is done when the pool is active with the requested number of -// ready workers. Comparing with the requested count, not the count read, keeps -// a stale first read from ending the wait. +// poolCreated and poolScaled compare with the requested count, not the count +// read, so a stale first read cannot end the wait. func poolCreated(want int) func(*verda.ManagedKubernetesNodePool) (string, bool) { return func(p *verda.ManagedKubernetesNodePool) (string, bool) { return poolStatus(p), p.State == verda.ManagedKubernetesNodePoolStateActive && p.ReadyReplicas == want } } -// poolScaled is done when the API reports the new count, all of it ready, and the pool active. func poolScaled(want int) func(*verda.ManagedKubernetesNodePool) (string, bool) { return func(p *verda.ManagedKubernetesNodePool) (string, bool) { done := p.State == verda.ManagedKubernetesNodePoolStateActive && p.Replicas == want && p.ReadyReplicas == want @@ -177,7 +158,7 @@ func poolScaled(want int) func(*verda.ManagedKubernetesNodePool) (string, bool) } } -// writeResult writes a structured result and returns waitErr, so a failed wait +// writeResult returns waitErr after the structured result, so a failed wait // still leaves the result on stdout. It reports false for table output. func writeResult(f cmdutil.Factory, ioStreams cmdutil.IOStreams, result any, waitErr error) (written bool, err error) { wrote, err := cmdutil.WriteStructured(ioStreams.Out, f.OutputFormat(), result) @@ -190,7 +171,6 @@ func writeResult(f cmdutil.Factory, ioStreams cmdutil.IOStreams, result any, wai return true, waitErr } -// waitForPool polls the pool until done. It returns the last pool read. func waitForPool(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, client *verda.Client, t *clusterTarget, name string, done func(*verda.ManagedKubernetesNodePool) (string, bool), timeout time.Duration) (*verda.ManagedKubernetesNodePool, error) { if f.OutputFormat() == outputTable { _, _ = fmt.Fprintf(ioStreams.ErrOut, "Node pool %s accepted; waiting for its workers (Ctrl+C stops waiting, the pool stays).\n", name) @@ -201,7 +181,6 @@ func waitForPool(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStr }, done) } -// poolWaitKind says which change a pool wait was for: its timeout text differs. type poolWaitKind int const ( @@ -211,9 +190,8 @@ const ( poolWaitSettle ) -// poolWaitFailure is the error of a pool wait that did not finish. When workers -// are being created, a timeout also says they keep billing and how to stop -// them: delete the pool after a create, scale back to previous after a scale-up. +// poolWaitFailure: while workers are being created, a timeout says they keep +// billing and how to stop them (delete after a create, scale back after a scale-up). func poolWaitFailure(err error, kind poolWaitKind, t *clusterTarget, accepted, last *verda.ManagedKubernetesNodePool, timeout time.Duration, previous int) error { seen := accepted if last != nil { @@ -221,7 +199,7 @@ func poolWaitFailure(err error, kind poolWaitKind, t *clusterTarget, accepted, l } describe := poolDescribeCmd(t, accepted.Name) subject := fmt.Sprintf("node pool %s in cluster %s (%s)", accepted.Name, t.Name, t.Location) - creating := poolBackendKeepsTrying + " To stop: `" + poolDeleteCmd(t, accepted.Name) + "`. Check with `" + describe + "`" + creating := poolBackendKeepsTrying + " To stop: `" + poolCmd("delete", t, accepted.Name) + "`. Check with `" + describe + "`" info := waitFailureInfo{Subject: subject, Timeout: timeout, LastSeen: poolSeen(seen), CheckCmd: describe} switch kind { case poolWaitScaleDown: @@ -233,7 +211,7 @@ func poolWaitFailure(err error, kind poolWaitKind, t *clusterTarget, accepted, l info.ReadNote = poolBackendKeepsTrying case poolWaitScaleUp: info.Subject = "scaling of " + subject - info.TimeoutTail = poolBackendKeepsTrying + " To stop, scale back: `" + poolScaleCmd(t, accepted.Name, previous) + " --yes`. Check with `" + describe + "`" + info.TimeoutTail = poolBackendKeepsTrying + " To stop, scale back: `" + poolCmd("scale", t, accepted.Name) + " --replicas " + strconv.Itoa(previous) + " --yes`. Check with `" + describe + "`" info.ReadNote = poolBackendKeepsTrying default: info.TimeoutTail, info.ReadNote = creating, poolBackendKeepsTrying @@ -241,7 +219,6 @@ func poolWaitFailure(err error, kind poolWaitKind, t *clusterTarget, accepted, l return waitFailure(err, &info) } -// poolSeen is what a timeout message says about the last read: state, ready/desired, error reasons. func poolSeen(p *verda.ManagedKubernetesNodePool) string { s := fmt.Sprintf("last state: %s, %d/%d workers ready", p.State, p.ReadyReplicas, p.Replicas) if reasons := poolReasons(p); len(reasons) > 0 { @@ -250,8 +227,7 @@ func poolSeen(p *verda.ManagedKubernetesNodePool) string { return s } -// kubeconfigReady: the YAML exists only once the kubeconfig is ready. Both are -// checked, so a read that says ready without the YAML does not end the wait. +// kubeconfigReady checks the YAML too: a read can say ready before it has it. func kubeconfigReady(k *verda.ManagedKubernetesKubeconfig) (status string, done bool) { return k.Status, k.Status == verda.ManagedKubernetesKubeconfigStatusReady && k.Kubeconfig != "" } diff --git a/internal/verda-cli/cmd/mk8s/wait_test.go b/internal/verda-cli/cmd/mk8s/wait_test.go index 207fb3fa..5f35bffe 100644 --- a/internal/verda-cli/cmd/mk8s/wait_test.go +++ b/internal/verda-cli/cmd/mk8s/wait_test.go @@ -17,7 +17,6 @@ package mk8s import ( "context" "errors" - "net/http" "strings" "testing" "time" @@ -27,89 +26,44 @@ import ( cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) -// scriptedGet answers each read from a list: an error, or a value. -type step struct { - err error - val int -} - -func scriptedGet(steps []step) func(context.Context) (*int, error) { - i := 0 - return func(context.Context) (*int, error) { - s := steps[min(i, len(steps)-1)] - i++ - if s.err != nil { - return nil, s.err - } - v := s.val - return &v, nil - } -} - -func apiErrStatus(code int) error { return &verda.APIError{StatusCode: code, Message: "x"} } - -func wait(t *testing.T, steps []step, timeout time.Duration) (*int, error) { - t.Helper() +// TestWaitFor feeds waitFor scripted reads: a positive number is a failed read +// with that status, a negative one a value (-3 is done). +func TestWaitFor(t *testing.T) { fastPoll(t) - return waitFor(t.Context(), &cmdutil.TestFactory{}, nil, timeout, scriptedGet(steps), - func(v *int) (string, bool) { return "reading", *v >= 3 }) -} - -func TestWaitFor_FailuresMustBeConsecutive(t *testing.T) { - // fail, ok, fail, ok, fail, ok: never maxReadFailures in a row, so the wait goes on to done. - steps := []step{{err: apiErrStatus(502)}, {val: 1}, {err: apiErrStatus(502)}, {val: 2}, {err: apiErrStatus(502)}, {val: 3}} - last, err := wait(t, steps, 5*time.Second) - if err != nil || last == nil || *last != 3 { - t.Fatalf("last=%v err=%v", last, err) - } -} - -func TestWaitFor_GivesUpAfterConsecutiveFailures(t *testing.T) { - steps := []step{{val: 1}, {err: apiErrStatus(500)}, {err: apiErrStatus(500)}, {err: apiErrStatus(500)}, {val: 3}} - last, err := wait(t, steps, 5*time.Second) - if _, ok := errors.AsType[*pollReadError](err); !ok { - t.Fatalf("err = %v, want a pollReadError", err) - } - if last == nil || *last != 1 { - t.Errorf("last = %v, want the last good read", last) - } -} - -func TestWaitFor_RetriableAndFatalStatuses(t *testing.T) { - for _, code := range []int{http.StatusRequestTimeout, http.StatusTooManyRequests, http.StatusNotFound, http.StatusBadGateway} { - // one failure then done: retriable statuses are tolerated - if _, err := wait(t, []step{{val: 1}, {err: apiErrStatus(code)}, {val: 3}}, 5*time.Second); err != nil { - t.Errorf("status %d must be retried: %v", code, err) + const ok1, done = -1, -3 + for name, tc := range map[string]struct { + reads []int + readErr bool + lastSeen int + }{ + "failures must be consecutive": {reads: []int{502, ok1, 502, ok1, 502, done}}, + "2 in a row are tolerated": {reads: []int{ok1, 502, 502, done}}, + "gives up after 3 in a row": {reads: []int{ok1, 500, 500, 500, done}, readErr: true, lastSeen: 1}, + "408 is retried": {reads: []int{ok1, 408, done}}, + "429 is retried": {reads: []int{ok1, 429, done}}, + "404 after a good read retried": {reads: []int{ok1, 404, done}}, + "400 ends at once": {reads: []int{ok1, 400, done}, readErr: true, lastSeen: 1}, + "403 ends at once": {reads: []int{ok1, 403, done}, readErr: true, lastSeen: 1}, + "404 before the first good read": {reads: []int{404, 404, 404, 404, done}}, + "404s after a good read fail": {reads: []int{ok1, 404, 404, 404, done}, readErr: true, lastSeen: 1}, + } { + i := 0 + get := func(context.Context) (*int, error) { + r := tc.reads[min(i, len(tc.reads)-1)] + i++ + if r > 0 { + return nil, &verda.APIError{StatusCode: r, Message: "x"} + } + v := -r + return &v, nil } - } - for _, code := range []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden} { - if _, err := wait(t, []step{{val: 1}, {err: apiErrStatus(code)}, {val: 3}}, 5*time.Second); err == nil { - t.Errorf("status %d must end the wait at once", code) + last, err := waitFor(t.Context(), &cmdutil.TestFactory{}, nil, 5*time.Second, get, func(v *int) (string, bool) { return "", *v == 3 }) + if _, isRead := errors.AsType[*pollReadError](err); isRead != tc.readErr || (!tc.readErr && err != nil) { + t.Errorf("%s: err = %v", name, err) + } + if tc.readErr && (last == nil || *last != tc.lastSeen) { + t.Errorf("%s: last = %v, want the last good read", name, last) } - } -} - -func TestWaitFor_A404BeforeTheFirstGoodReadIsWaitedOut(t *testing.T) { - steps := []step{{err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {val: 3}} - if _, err := wait(t, steps, 5*time.Second); err != nil { - t.Errorf("a lagging read after a create must not fail the wait: %v", err) - } - // After a good read, the same 404s are failures. - steps = []step{{val: 1}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {err: apiErrStatus(404)}, {val: 3}} - if _, err := wait(t, steps, 5*time.Second); err == nil { - t.Error("404s after a good read count as failures") - } -} - -func TestWaitFor_TimeoutWhileAReadIsInFlight(t *testing.T) { - fastPoll(t) - hang := func(ctx context.Context) (*int, error) { - <-ctx.Done() - return nil, ctx.Err() - } - _, err := waitFor(t.Context(), &cmdutil.TestFactory{}, nil, 50*time.Millisecond, hang, func(*int) (string, bool) { return "", false }) - if !errors.Is(err, errWaitTimeout) { - t.Errorf("err = %v, want errWaitTimeout, not a read failure", err) } } @@ -117,12 +71,12 @@ func TestWaitFailure_Messages(t *testing.T) { t.Parallel() info := waitFailureInfo{Subject: "thing x", Timeout: time.Minute, LastSeen: "last: y", TimeoutTail: "Tail.", ReadNote: "Note.", CheckCmd: "check it"} read := waitFailure(&pollReadError{err: errors.New("boom")}, &info).Error() - for _, want := range []string{"thing x was accepted, but --wait could not read it (boom)", "check with `check it`", "Note."} { + for _, want := range []string{"thing x was accepted, but --wait could not read it (boom)", "check with `check it`. Note."} { if !strings.Contains(read, want) { t.Errorf("read failure %q misses %q", read, want) } } - if got := waitFailure(errWaitTimeout, &info).Error(); got != "timed out after 1m0s waiting for thing x (last: y). Tail." { + if got := waitFailure(errors.New("timed out waiting"), &info).Error(); got != "timed out after 1m0s waiting for thing x (last: y). Tail." { t.Errorf("timeout = %q", got) } info.ReadNote = "" @@ -179,21 +133,3 @@ func TestPoolConditions(t *testing.T) { t.Error("degraded is not ready") } } - -func TestKubeconfigReady(t *testing.T) { - t.Parallel() - for name, tc := range map[string]struct { - status, yaml string - done bool - }{ - "pending without YAML": {verda.ManagedKubernetesKubeconfigStatusPending, "", false}, - "pending with YAML": {verda.ManagedKubernetesKubeconfigStatusPending, "y", false}, - "ready without YAML": {verda.ManagedKubernetesKubeconfigStatusReady, "", false}, - "ready with YAML": {verda.ManagedKubernetesKubeconfigStatusReady, "y", true}, - } { - status, done := kubeconfigReady(&verda.ManagedKubernetesKubeconfig{Status: tc.status, Kubeconfig: tc.yaml}) - if status != tc.status || done != tc.done { - t.Errorf("%s: got (%q, %v), want (%q, %v)", name, status, done, tc.status, tc.done) - } - } -} diff --git a/internal/verda-cli/cmd/mk8s/wizard_cache.go b/internal/verda-cli/cmd/mk8s/wizard_cache.go index 1c5ea7ae..118048ad 100644 --- a/internal/verda-cli/cmd/mk8s/wizard_cache.go +++ b/internal/verda-cli/cmd/mk8s/wizard_cache.go @@ -46,16 +46,11 @@ func newWizardCache(f cmdutil.Factory, client *verda.Client) *wizardCache { return &wizardCache{f: f, client: client, subnets: map[string][]verda.Subnet{}, types: map[string][]string{}} } -// wizardCall bounds one lookup by --timeout and shows the spinner. -func wizardCall[T any](ctx context.Context, c *wizardCache, msg string, fn func(context.Context) (T, error)) (T, error) { - return apiCall(ctx, c.f, msg, fn) -} - func (c *wizardCache) enabledLocations(ctx context.Context) ([]string, error) { if c.haveLocation { return c.locations, nil } - res, err := wizardCall(ctx, c, "Loading locations...", func(ctx context.Context) (*verda.ManagedKubernetesEnabledLocations, error) { + res, err := apiCall(ctx, c.f, "Loading locations...", func(ctx context.Context) (*verda.ManagedKubernetesEnabledLocations, error) { return c.client.ManagedKubernetes.GetEnabledLocations(ctx) }) if err != nil { @@ -69,7 +64,7 @@ func (c *wizardCache) privateNetworks(ctx context.Context) ([]verda.PrivateNetwo if c.haveNetworks { return c.networks, nil } - networks, err := wizardCall(ctx, c, "Loading private networks...", func(ctx context.Context) ([]verda.PrivateNetwork, error) { + networks, err := apiCall(ctx, c.f, "Loading private networks...", func(ctx context.Context) ([]verda.PrivateNetwork, error) { return c.client.PrivateNetworks.Get(ctx) }) if err != nil { @@ -83,7 +78,7 @@ func (c *wizardCache) networkSubnets(ctx context.Context, networkID string) ([]v if s, ok := c.subnets[networkID]; ok { return s, nil } - subnets, err := wizardCall(ctx, c, "Loading subnets...", func(ctx context.Context) ([]verda.Subnet, error) { + subnets, err := apiCall(ctx, c.f, "Loading subnets...", func(ctx context.Context) ([]verda.Subnet, error) { return c.client.PrivateNetworks.GetSubnets(ctx, networkID) }) if err != nil { @@ -93,7 +88,7 @@ func (c *wizardCache) networkSubnets(ctx context.Context, networkID string) ([]v return subnets, nil } -// networkName is the name of a network the wizard has already listed, else the ID. +// networkName: the name of a network the wizard has listed, else the ID. func (c *wizardCache) networkName(id string) string { for i := range c.networks { if c.networks[i].ID == id { @@ -103,8 +98,7 @@ func (c *wizardCache) networkName(id string) string { return id } -// stopError ends a wizard before any create call. The engine wraps loader -// errors with the step name; callers unwrap a stopError to show its text as written. +// stopError ends a wizard before any create call; runWizard shows its text as written. type stopError struct{ msg string } func (e *stopError) Error() string { return e.msg } @@ -115,9 +109,7 @@ func (c *wizardCache) clusterList(ctx context.Context) ([]verda.ManagedKubernete if c.haveClusters { return c.clusters, nil } - clusters, err := wizardCall(ctx, c, "Loading clusters...", func(ctx context.Context) ([]verda.ManagedKubernetesCluster, error) { - return c.client.ManagedKubernetes.List(ctx) - }) + clusters, err := listClusters(ctx, c.f, c.client) if err != nil { return nil, err } @@ -125,7 +117,6 @@ func (c *wizardCache) clusterList(ctx context.Context) ([]verda.ManagedKubernete return c.clusters, nil } -// clusterLocation is the location of a cluster the wizard has listed, or "". func (c *wizardCache) clusterLocation(name string) string { for i := range c.clusters { if c.clusters[i].Name == name { @@ -141,7 +132,7 @@ func (c *wizardCache) instanceTypes(ctx context.Context, location string) ([]str if t, ok := c.types[location]; ok { return t, nil } - avail, err := wizardCall(ctx, c, "Loading instance types...", func(ctx context.Context) ([]verda.LocationAvailability, error) { + avail, err := apiCall(ctx, c.f, "Loading instance types...", func(ctx context.Context) ([]verda.LocationAvailability, error) { return c.client.InstanceAvailability.GetAllAvailabilities(ctx, false, location) }) if err != nil { @@ -163,7 +154,7 @@ func (c *wizardCache) sshKeyList(ctx context.Context) ([]verda.SSHKey, error) { if c.haveKeys { return c.sshKeys, nil } - keys, err := wizardCall(ctx, c, "Loading SSH keys...", func(ctx context.Context) ([]verda.SSHKey, error) { + keys, err := apiCall(ctx, c.f, "Loading SSH keys...", func(ctx context.Context) ([]verda.SSHKey, error) { return c.client.SSHKeys.GetAllSSHKeys(ctx) }) if err != nil { diff --git a/internal/verda-cli/cmd/mk8s/wizard_cluster.go b/internal/verda-cli/cmd/mk8s/wizard_cluster.go index 2877f063..c27f3b32 100644 --- a/internal/verda-cli/cmd/mk8s/wizard_cluster.go +++ b/internal/verda-cli/cmd/mk8s/wizard_cluster.go @@ -32,6 +32,15 @@ var newWizardEngine = func(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *wiza return wizard.NewEngine(f.Prompter(), f.Status(), wizard.WithOutput(ioStreams.ErrOut)) } +// runWizard returns a stopError as written, not wrapped with the step name. +func runWizard(ctx context.Context, f cmdutil.Factory, ioStreams cmdutil.IOStreams, flow *wizard.Flow) error { + err := newWizardEngine(f, ioStreams).Run(ctx, flow) + if stop, ok := errors.AsType[*stopError](err); ok { + return stop + } + return err +} + // wizardLayout has no hint bar view: the engine renders its own below each prompt. func wizardLayout() []wizard.ViewDef { return []wizard.ViewDef{ @@ -41,7 +50,7 @@ func wizardLayout() []wizard.ViewDef { // The SDK and the API own the real rules (name format and length, CIDR size and // overlap); the wizard only rejects input that cannot be sent at all. -func validateClusterName(s string) error { +func validateName(s string) error { if s == "" { return errors.New("name must not be empty") } @@ -62,96 +71,56 @@ func validateVersion(s string) error { return nil } +func wizardText(v any) string { s, _ := v.(string); return strings.TrimSpace(s) } + +// stringStep binds a required step to *dst. set is fixed when the flow is +// built: a value from a flag skips the step. +func stringStep(name, desc string, prompt wizard.PromptType, dst *string, set bool) wizard.Step { + return wizard.Step{ + Name: name, Description: desc, Prompt: prompt, Required: true, + Setter: func(v any) { *dst = wizardText(v) }, + Resetter: func() { *dst = "" }, + IsSet: func() bool { return set }, + Value: func() any { return *dst }, + } +} + +func textStep(name, desc string, dst *string, set bool, validate func(string) error) wizard.Step { + s := stringStep(name, desc, wizard.TextInputPrompt, dst, set) + s.Validate = func(v any) error { return validate(wizardText(v)) } + return s +} + +func selectStep(name, desc string, dst *string, set bool, loader wizard.LoaderFunc, dependsOn ...string) wizard.Step { + s := stringStep(name, desc, wizard.SelectPrompt, dst, set) + s.Loader, s.DependsOn = loader, dependsOn + return s +} + +// cidrStep is optional: Enter keeps def, and Back restores it. +func cidrStep(name, desc string, dst *string, set bool, def string) wizard.Step { + s := textStep(name, desc, dst, set, func(t string) error { return validateCIDR(t, def) }) + s.Required = false + s.Default = func(map[string]any) any { return def } + s.Resetter = func() { *dst = def } + return s +} + // buildClusterCreateFlow asks for every value the flags did not give. Each step // loads its choices when it is reached, through the shared cache. func buildClusterCreateFlow(opts *clusterCreateOptions, cache *wizardCache) *wizard.Flow { - nameSet, locationSet, versionSet := opts.Name != "", opts.Location != "", opts.Version != "" - networkSet, subnetSet := opts.Network != "", opts.Subnet != "" - podSet, serviceSet := opts.podCIDRSet, opts.serviceCIDRSet - text := func(v any) string { s, _ := v.(string); return strings.TrimSpace(s) } - return &wizard.Flow{ Name: "mk8s-cluster-create", Layout: wizardLayout(), Steps: []wizard.Step{ - { - Name: flagName, - Description: "Cluster name (unique in the project)", - Prompt: wizard.TextInputPrompt, - Required: true, - Validate: func(v any) error { return validateClusterName(text(v)) }, - Setter: func(v any) { opts.Name = text(v) }, - Resetter: func() { opts.Name = "" }, - IsSet: func() bool { return nameSet }, - Value: func() any { return opts.Name }, - }, - { - Name: flagLocation, - Description: "Location", - Prompt: wizard.SelectPrompt, - Required: true, - Loader: locationLoader(cache), - Setter: func(v any) { opts.Location = text(v) }, - Resetter: func() { opts.Location = "" }, - IsSet: func() bool { return locationSet }, - Value: func() any { return opts.Location }, - }, - { - Name: flagKubernetesVersion, - Description: "Kubernetes version (for example 1.37.0)", - Prompt: wizard.TextInputPrompt, - Required: true, - Validate: func(v any) error { return validateVersion(text(v)) }, - Setter: func(v any) { opts.Version = text(v) }, - Resetter: func() { opts.Version = "" }, - IsSet: func() bool { return versionSet }, - Value: func() any { return opts.Version }, - }, - { - Name: flagNetwork, - Description: "Private network", - Prompt: wizard.SelectPrompt, - Required: true, - Loader: networkLoader(cache), - Setter: func(v any) { opts.Network = text(v) }, - Resetter: func() { opts.Network = "" }, - IsSet: func() bool { return networkSet }, - Value: func() any { return opts.Network }, - }, - { - Name: flagSubnet, - Description: "Subnet (in the network and location you chose)", - Prompt: wizard.SelectPrompt, - Required: true, - DependsOn: []string{flagNetwork, flagLocation}, - Loader: subnetLoader(opts, cache), - Setter: func(v any) { opts.Subnet = text(v) }, - Resetter: func() { opts.Subnet = "" }, - IsSet: func() bool { return subnetSet }, - Value: func() any { return opts.Subnet }, - }, - { - Name: flagPodCIDR, - Description: "Pod IP range (CIDR)", - Prompt: wizard.TextInputPrompt, - Default: func(_ map[string]any) any { return defaultPodCIDR }, - Validate: func(v any) error { return validateCIDR(text(v), defaultPodCIDR) }, - Setter: func(v any) { opts.PodCIDR = text(v) }, - Resetter: func() { opts.PodCIDR = defaultPodCIDR }, - IsSet: func() bool { return podSet }, - Value: func() any { return opts.PodCIDR }, - }, - { - Name: flagServiceCIDR, - Description: "Service IP range (CIDR)", - Prompt: wizard.TextInputPrompt, - Default: func(_ map[string]any) any { return defaultServiceCIDR }, - Validate: func(v any) error { return validateCIDR(text(v), defaultServiceCIDR) }, - Setter: func(v any) { opts.ServiceCIDR = text(v) }, - Resetter: func() { opts.ServiceCIDR = defaultServiceCIDR }, - IsSet: func() bool { return serviceSet }, - Value: func() any { return opts.ServiceCIDR }, - }, + textStep(flagName, "Cluster name (unique in the project)", &opts.Name, opts.Name != "", validateName), + selectStep(flagLocation, "Location", &opts.Location, opts.Location != "", locationLoader(cache)), + textStep(flagKubernetesVersion, "Kubernetes version (for example 1.37.0)", &opts.Version, opts.Version != "", validateVersion), + selectStep(flagNetwork, "Private network", &opts.Network, opts.Network != "", networkLoader(cache)), + selectStep(flagSubnet, "Subnet (in the network and location you chose)", &opts.Subnet, opts.Subnet != "", + subnetLoader(opts, cache), flagNetwork, flagLocation), + cidrStep(flagPodCIDR, "Pod IP range (CIDR)", &opts.PodCIDR, opts.podCIDRSet, defaultPodCIDR), + cidrStep(flagServiceCIDR, "Service IP range (CIDR)", &opts.ServiceCIDR, opts.serviceCIDRSet, defaultServiceCIDR), }, } } @@ -190,7 +159,6 @@ func networkLoader(cache *wizardCache) wizard.LoaderFunc { } } -// subnetLoader lists the subnets of the chosen network that are in the chosen location. func subnetLoader(opts *clusterCreateOptions, cache *wizardCache) wizard.LoaderFunc { return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { subnets, err := cache.networkSubnets(ctx, opts.Network) diff --git a/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go b/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go index 0d9e862d..9c2e476f 100644 --- a/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go +++ b/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go @@ -15,15 +15,10 @@ package mk8s import ( - "context" - "io" - "slices" - "strings" + "reflect" "testing" "github.com/verda-cloud/verda-cli/pkg/tui/wizard" - - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) func wizardAPI() *fakeAPI { @@ -35,25 +30,8 @@ func wizardAPI() *fakeAPI { return api } -// withWizardAnswers makes the wizard take canned answers instead of a terminal. -func withWizardAnswers(t *testing.T, results ...wizard.TestResult) { - t.Helper() - orig := newWizardEngine - newWizardEngine = func(cmdutil.Factory, cmdutil.IOStreams) *wizard.Engine { - return wizard.NewEngine(nil, nil, wizard.WithOutput(io.Discard), wizard.WithTestResults(results...)) - } - t.Cleanup(func() { newWizardEngine = orig }) -} - -func postBody(t *testing.T, api *fakeAPI) map[string]string { - t.Helper() - reqs := api.requestsMatching("POST") - if len(reqs) != 1 { - t.Fatalf("want one POST, got %v", reqs) - } - return decodeJSON[map[string]string](t, reqs[0].Body) -} - +// The wizard ends in the same request as the flags; a flag skips its step and +// needs no lookup. func TestClusterCreateWizard_EndsInTheSameRequestAsFlags(t *testing.T) { withTerminal(t) @@ -77,36 +55,25 @@ func TestClusterCreateWizard_EndsInTheSameRequestAsFlags(t *testing.T) { if _, _, err := runMK8s(t, f, "cluster", "create"); err != nil { t.Fatal(err) } - - want, got := postBody(t, flagsAPI), postBody(t, wizAPI) - for k, v := range want { - if got[k] != v { - t.Errorf("wizard body[%s] = %q, flags body has %q", k, got[k], v) - } - } - if len(got) != len(want) { + want, got := postBodyAny(t, flagsAPI), postBodyAny(t, wizAPI) + if !reflect.DeepEqual(got, want) { t.Errorf("wizard body = %v, flags body = %v", got, want) } -} -func TestClusterCreateWizard_FlagsSkipTheirSteps(t *testing.T) { - withTerminal(t) - api := wizardAPI() - // The name and --service-cidr are not given: two answers. --pod-cidr and the - // rest skip their steps. + // The name and --service-cidr are not given: two answers. + partialAPI := wizardAPI() withWizardAnswers(t, wizard.TextResult("prod"), wizard.TextResult("")) - f := tableFactory(newFakeClient(t, api), nil) + f = tableFactory(newFakeClient(t, partialAPI), nil) f.PrompterOverride = confirmYes() args := []string{"cluster", "create", "-l", "FIN-03", "--kubernetes-version", "1.37.0", "--network", "pn-123", "--subnet", "sn-456", "--pod-cidr", "10.50.0.0/16"} if _, _, err := runMK8s(t, f, args...); err != nil { t.Fatal(err) } - body := postBody(t, api) + body := postBodyAny(t, partialAPI) if body["name"] != "prod" || body["pod_cidr"] != "10.50.0.0/16" || body["service_cidr"] != defaultServiceCIDR { t.Errorf("body = %v", body) } - // Flags need no lookups. - if got := api.paths("GET"); len(got) != 0 { + if got := partialAPI.paths("GET"); len(got) != 0 { t.Errorf("unexpected reads: %v", got) } } @@ -133,15 +100,6 @@ func TestClusterCreateWizard_StopsBeforeCreateOnEmptyPickers(t *testing.T) { answers: []wizard.TestResult{wizard.TextResult("prod"), wizard.SelectResult(1), wizard.TextResult("1.37.0"), wizard.SelectResult(0)}, want: "Network beta has no subnet in FIN-03. Create one with `verda network subnet create`.", }, - "no location": { - api: func() *fakeAPI { - api := newFakeAPI() - api.locations = nil - return api - }, - answers: []wizard.TestResult{wizard.TextResult("prod")}, - want: "Managed Kubernetes is enabled for this project, but no location is configured.", - }, } { api := tc.api() withWizardAnswers(t, tc.answers...) @@ -157,66 +115,12 @@ func TestClusterCreateWizard_StopsBeforeCreateOnEmptyPickers(t *testing.T) { } } -func TestClusterCreateWizard_DeclinedSummaryCreatesNothing(t *testing.T) { - withTerminal(t) - api := wizardAPI() - withWizardAnswers(t, wizard.TextResult("prod"), wizard.TextResult(""), wizard.TextResult("")) - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = confirmNo() - args := []string{"cluster", "create", "-l", "FIN-03", "--kubernetes-version", "1.37.0", "--network", "pn-123", "--subnet", "sn-456"} - _, errOut, err := runMK8s(t, f, args...) - if err != nil || !strings.Contains(errOut, canceledMsg) { - t.Fatalf("err=%v stderr=%q", err, errOut) - } - if n := len(api.requestsMatching("POST")); n != 0 { - t.Errorf("%d POST(s)", n) - } -} - -func TestClusterCreateWizard_NotUsedOffTerminal(t *testing.T) { - t.Parallel() - // A wizard run here would hang the test: off a terminal the command fails first. - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, wizardAPI()), nil), "cluster", "create", "--name", "prod") - if agentErrorCode(err) != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("err = %v", err) - } -} - -func TestWizardCache_MemoizesLookups(t *testing.T) { - t.Parallel() - api := wizardAPI() - f := tableFactory(newFakeClient(t, api), nil) - cache := newWizardCache(f, newFakeClient(t, api)) - ctx := context.Background() - for range 3 { - if _, err := cache.enabledLocations(ctx); err != nil { - t.Fatal(err) - } - if _, err := cache.privateNetworks(ctx); err != nil { - t.Fatal(err) - } - if _, err := cache.networkSubnets(ctx, "pn-123"); err != nil { - t.Fatal(err) - } - } - want := []string{"/mk8sclusters/enabled_locations", "/private-networks", "/private-networks/pn-123/subnets"} - got := api.paths("GET") - slices.Sort(got) - slices.Sort(want) - if !slices.Equal(got, want) { - t.Errorf("requests = %v, want one each: %v", got, want) - } - if cache.networkName("pn-123") != "beta" || cache.networkName("pn-x") != "pn-x" { - t.Error("networkName falls back to the ID") - } -} - func TestValidateClusterWizardInputs(t *testing.T) { t.Parallel() // Only input that cannot be sent is rejected; the SDK and the API own the real rules. for name, ok := range map[string]bool{"prod": true, "Prod": true, "a_b": true, "": false} { - if err := validateClusterName(name); (err == nil) != ok { - t.Errorf("validateClusterName(%q) = %v, ok=%v", name, err, ok) + if err := validateName(name); (err == nil) != ok { + t.Errorf("validateName(%q) = %v, ok=%v", name, err, ok) } } for cidr, ok := range map[string]bool{"10.100.0.0/16": true, "": false, "10.0.0.1": false, "x/16": false} { @@ -224,11 +128,6 @@ func TestValidateClusterWizardInputs(t *testing.T) { t.Errorf("validateCIDR(%q) = %v, ok=%v", cidr, err, ok) } } - for example, want := range map[string]string{defaultPodCIDR: "for example 10.100.0.0/16", defaultServiceCIDR: "for example 10.200.0.0/16"} { - if err := validateCIDR("x", example); err == nil || !strings.Contains(err.Error(), want) { - t.Errorf("validateCIDR error = %v, want %q", err, want) - } - } if validateVersion(" ") == nil || validateVersion("1.37.0") != nil { t.Error("validateVersion") } diff --git a/internal/verda-cli/cmd/mk8s/wizard_nodepool.go b/internal/verda-cli/cmd/mk8s/wizard_nodepool.go index 02635c3e..2a67078e 100644 --- a/internal/verda-cli/cmd/mk8s/wizard_nodepool.go +++ b/internal/verda-cli/cmd/mk8s/wizard_nodepool.go @@ -34,22 +34,14 @@ func validateReplicas(s string) error { return nil } -func validateName(s string) error { - if s == "" { - return errors.New("name must not be empty") - } - return nil -} - // buildNodePoolCreateFlow asks for every value the flags did not give. The // cluster is already known when it came from --cluster. func buildNodePoolCreateFlow(opts *nodePoolCreateOptions, cache *wizardCache) *wizard.Flow { - clusterSet, nameSet, typeSet := opts.Cluster != "", opts.Name != "", opts.InstanceType != "" + clusterSet := opts.Cluster != "" // -l as the user gave it. The cluster step sets the location from the // cluster it picks, and going Back must undo that, not keep it. locationFlag := opts.Location keysSet, replicasSet := len(opts.SSHKeys) > 0, opts.replicasSet - text := func(v any) string { s, _ := v.(string); return strings.TrimSpace(s) } return &wizard.Flow{ Name: "mk8s-node-pool-create", @@ -62,7 +54,7 @@ func buildNodePoolCreateFlow(opts *nodePoolCreateOptions, cache *wizardCache) *w Required: true, Loader: clusterLoader(locationFlag, cache), Setter: func(v any) { - opts.Cluster = text(v) + opts.Cluster = wizardText(v) if locationFlag == "" { opts.Location = cache.clusterLocation(opts.Cluster) } @@ -74,29 +66,8 @@ func buildNodePoolCreateFlow(opts *nodePoolCreateOptions, cache *wizardCache) *w IsSet: func() bool { return clusterSet }, Value: func() any { return opts.Cluster }, }, - { - Name: flagName, - Description: "Node pool name (unique in the project, not only in the cluster)", - Prompt: wizard.TextInputPrompt, - Required: true, - Validate: func(v any) error { return validateName(text(v)) }, - Setter: func(v any) { opts.Name = text(v) }, - Resetter: func() { opts.Name = "" }, - IsSet: func() bool { return nameSet }, - Value: func() any { return opts.Name }, - }, - { - Name: flagInstanceType, - Description: "Instance type", - Prompt: wizard.SelectPrompt, - Required: true, - DependsOn: []string{flagCluster}, - Loader: instanceTypeLoader(opts, cache), - Setter: func(v any) { opts.InstanceType = text(v) }, - Resetter: func() { opts.InstanceType = "" }, - IsSet: func() bool { return typeSet }, - Value: func() any { return opts.InstanceType }, - }, + textStep(flagName, "Node pool name (unique in the project, not only in the cluster)", &opts.Name, opts.Name != "", validateName), + selectStep(flagInstanceType, "Instance type", &opts.InstanceType, opts.InstanceType != "", instanceTypeLoader(opts, cache), flagCluster), { Name: flagSSHKey, Description: "SSH keys for the workers", @@ -114,8 +85,8 @@ func buildNodePoolCreateFlow(opts *nodePoolCreateOptions, cache *wizardCache) *w Description: "Number of workers", Prompt: wizard.TextInputPrompt, Default: func(map[string]any) any { return strconv.Itoa(defaultPoolReplicas) }, - Validate: func(v any) error { return validateReplicas(text(v)) }, - Setter: func(v any) { opts.Replicas, _ = strconv.Atoi(text(v)) }, + Validate: func(v any) error { return validateReplicas(wizardText(v)) }, + Setter: func(v any) { opts.Replicas, _ = strconv.Atoi(wizardText(v)) }, Resetter: func() { opts.Replicas = defaultPoolReplicas }, IsSet: func() bool { return replicasSet }, Value: func() any { return strconv.Itoa(opts.Replicas) }, @@ -124,7 +95,6 @@ func buildNodePoolCreateFlow(opts *nodePoolCreateOptions, cache *wizardCache) *w } } -// clusterLoader lists the clusters, only those in -l when it is given. func clusterLoader(locationFlag string, cache *wizardCache) wizard.LoaderFunc { return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { clusters, err := cache.clusterList(ctx) @@ -164,8 +134,6 @@ func instanceTypeRank(t string) int { return n } -// instanceTypeLoader lists the types for the cluster's location, smallest -// first so Enter on the first row picks the cheapest, with no prices. func instanceTypeLoader(opts *nodePoolCreateOptions, cache *wizardCache) wizard.LoaderFunc { return func(ctx context.Context, _ tui.Prompter, _ tui.Status, _ *wizard.Store) ([]wizard.Choice, error) { types, err := cache.instanceTypes(ctx, opts.Location) diff --git a/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go b/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go index 7a198593..95675b7b 100644 --- a/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go +++ b/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go @@ -15,13 +15,12 @@ package mk8s import ( + "reflect" "slices" "strings" "testing" "github.com/verda-cloud/verda-cli/pkg/tui/wizard" - - cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" ) func TestNodePoolCreateWizard_EndsInTheSameRequestAsFlags(t *testing.T) { @@ -40,14 +39,11 @@ func TestNodePoolCreateWizard_EndsInTheSameRequestAsFlags(t *testing.T) { wizard.MultiSelectResult([]int{0, 1}), // key-1, key-2 wizard.TextResult("2"), // replicas ) - f := tableFactory(newFakeClient(t, wizAPI), nil) - f.PrompterOverride = confirmYes() - if _, _, err := runMK8s(t, f, "node-pool", "create"); err != nil { + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, wizAPI), confirmYes()), "node-pool", "create"); err != nil { t.Fatal(err) } - want, got := postBodyAny(t, flagsAPI), postBodyAny(t, wizAPI) - if !equalJSON(want, got) { + if want, got := postBodyAny(t, flagsAPI), postBodyAny(t, wizAPI); !reflect.DeepEqual(want, got) { t.Errorf("wizard body = %v, flags body = %v", got, want) } if posts := wizAPI.requestsMatching("POST"); posts[0].Path != "/mk8sclusters/FIN-03/prod/pools" { @@ -55,135 +51,55 @@ func TestNodePoolCreateWizard_EndsInTheSameRequestAsFlags(t *testing.T) { } } +// Only --cluster and --ssh-key are given: name, type and replicas are asked, +// and the SSH keys are not listed. func TestNodePoolCreateWizard_FlagsSkipTheirSteps(t *testing.T) { withTerminal(t) api := poolWizardAPI() - // Only --cluster and --ssh-key are given: name, type and replicas are asked. withWizardAnswers(t, wizard.TextResult("workers"), wizard.SelectResult(2), wizard.TextResult("")) - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = confirmYes() - if _, _, err := runMK8s(t, f, "node-pool", "create", "--cluster", "prod", "--ssh-key", "key-2"); err != nil { + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), confirmYes()), "node-pool", "create", "--cluster", "prod", "--ssh-key", "key-2"); err != nil { t.Fatal(err) } body := postBodyAny(t, api) - if body["instance_type"] != "2A100.80S" || body["replicas"] != float64(1) { + if keys, _ := body["ssh_key_ids"].([]any); body["instance_type"] != "2A100.80S" || body["replicas"] != float64(1) || len(keys) != 1 || keys[0] != "key-2" { t.Errorf("body = %v", body) } - if keys, _ := body["ssh_key_ids"].([]any); len(keys) != 1 || keys[0] != "key-2" { - t.Errorf("ssh_key_ids = %v", body["ssh_key_ids"]) - } - // The cluster was found once, before the wizard; no SSH key lookup for a key given by flag. - for _, p := range api.paths("GET") { - if p == "/ssh-keys" { - t.Error("SSH keys were listed although --ssh-key was given") - } - } -} - -func TestNodePoolCreateWizard_UnknownClusterFailsBeforeAnyPrompt(t *testing.T) { - withTerminal(t) - api := poolWizardAPI() - // No wizard answers queued: a prompt would hang or fail. - withWizardAnswers(t) - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = confirmYes() - _, _, err := runMK8s(t, f, "node-pool", "create", "--cluster", "ghost") - if agentErrorCode(err) != "NOT_FOUND" { - t.Fatalf("err = %v", err) - } -} - -func TestNodePoolCreateWizard_LocationLimitsTheClusters(t *testing.T) { - withTerminal(t) - api := poolWizardAPI() - // -l FIN-01 leaves one cluster, staging; its location offers 1A100.22V. - withWizardAnswers(t, wizard.SelectResult(0), wizard.TextResult("w"), wizard.SelectResult(0), wizard.MultiSelectResult([]int{0}), wizard.TextResult("")) - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = confirmYes() - if _, _, err := runMK8s(t, f, "node-pool", "create", "-l", "FIN-01"); err != nil { - t.Fatal(err) - } - if got := api.requestsMatching("POST")[0].Path; got != "/mk8sclusters/FIN-01/staging/pools" { - t.Errorf("POST path = %s", got) - } - if body := postBodyAny(t, api); body["instance_type"] != "1A100.22V" { - t.Errorf("body = %v", body) + if slices.Contains(api.paths("GET"), "/ssh-keys") { + t.Error("SSH keys were listed although --ssh-key was given") } } -func TestNodePoolCreateWizard_InstanceTypesAreSmallestFirstAndHaveNoPrice(t *testing.T) { +// The cluster step sets the location from the picked cluster; Back clears it, +// but keeps a location given with -l. +func TestNodePoolCreateWizard_BackRestoresTheLocationFilter(t *testing.T) { t.Parallel() - api := poolWizardAPI() - opts := &nodePoolCreateOptions{} - opts.Location = "FIN-03" - choices, err := instanceTypeLoader(opts, newWizardCache(tableFactory(newFakeClient(t, api), nil), newFakeClient(t, api)))(t.Context(), nil, nil, nil) - if err != nil { - t.Fatal(err) - } - labels := make([]string, 0, len(choices)) - for _, c := range choices { - labels = append(labels, c.Label) - if c.Description != "instance types in FIN-03" { - t.Errorf("description = %q", c.Description) + client := newFakeClient(t, poolWizardAPI()) + cache := newWizardCache(tableFactory(client, nil), client) + for flag, choices := range map[string]int{"": 2, "FIN-03": 1} { + opts := &nodePoolCreateOptions{} + opts.Location = flag + step := buildNodePoolCreateFlow(opts, cache).Steps[0] + if _, err := step.Loader(t.Context(), nil, nil, nil); err != nil { // the engine loads the choices first + t.Fatal(err) } - if s := strings.ToLower(c.Label + c.Description + c.Value); strings.Contains(s, "$") || strings.Contains(s, "price") || strings.Contains(s, "/h") { - t.Errorf("a price must not be shown: %+v", c) + step.Setter("prod") + if opts.Location != "FIN-03" { + t.Errorf("-l %q: location = %q after picking prod", flag, opts.Location) + } + step.Resetter() + if got, err := step.Loader(t.Context(), nil, nil, nil); opts.Cluster != "" || opts.Location != flag || err != nil || len(got) != choices { + t.Errorf("-l %q: after Back cluster=%q location=%q choices=%v err=%v", flag, opts.Cluster, opts.Location, got, err) } - } - if want := []string{"CPU.4V.16G", "1V100.6V", "2A100.80S"}; !slices.Equal(labels, want) { - t.Errorf("labels = %v, want %v (only FIN-03, smallest first)", labels, want) } } func TestNodePoolCreateWizard_StopsOnEmptyPickers(t *testing.T) { withTerminal(t) - for name, tc := range map[string]struct { - api func() *fakeAPI - answers []wizard.TestResult - want string - }{ - "no cluster": { - api: func() *fakeAPI { return newFakeAPI() }, - want: "No cluster found. Create one with `verda mk8s cluster create`.", - }, - "no instance types": { - api: func() *fakeAPI { - api := poolWizardAPI() - api.availability = map[string][]string{} - return api - }, - answers: []wizard.TestResult{wizard.SelectResult(0), wizard.TextResult("w")}, - want: "No instance types are listed for FIN-03.", - }, - "no ssh key": { - api: func() *fakeAPI { - api := poolWizardAPI() - api.sshKeys = nil - return api - }, - answers: []wizard.TestResult{wizard.SelectResult(0), wizard.TextResult("w"), wizard.SelectResult(0)}, - want: "No SSH key found. Add one with `verda ssh-key add`.", - }, - } { - api := tc.api() - withWizardAnswers(t, tc.answers...) - f := tableFactory(newFakeClient(t, api), nil) - f.PrompterOverride = confirmYes() - _, _, err := runMK8s(t, f, "node-pool", "create") - if err == nil || err.Error() != tc.want { - t.Errorf("%s: err = %v, want exactly %q", name, err, tc.want) - } - if n := len(api.requestsMatching("POST")); n != 0 { - t.Errorf("%s: %d POST(s) before the stop", name, n) - } - } -} - -func TestNodePoolCreateWizard_NotUsedOffTerminal(t *testing.T) { - t.Parallel() - _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolWizardAPI()), nil), "node-pool", "create", "--cluster", "prod") - if ae := cmdutil.ClassifyError(err); ae == nil || ae.Code != "MISSING_REQUIRED_FLAGS" { - t.Fatalf("err = %v", err) + api := newFakeAPI() + withWizardAnswers(t) + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), confirmYes()), "node-pool", "create") + if err == nil || !strings.Contains(err.Error(), "No cluster found") || len(api.requestsMatching("POST")) != 0 { + t.Errorf("err = %v, POSTs = %v", err, api.requestsMatching("POST")) } } @@ -196,70 +112,6 @@ func TestValidateReplicas(t *testing.T) { } } -func equalJSON(a, b map[string]any) bool { - if len(a) != len(b) { - return false - } - for k, v := range a { - if !deepEqualAny(v, b[k]) { - return false - } - } - return true -} - -func deepEqualAny(a, b any) bool { - as, aok := a.([]any) - bs, bok := b.([]any) - if aok && bok { - return slices.EqualFunc(as, bs, deepEqualAny) - } - return a == b -} - -func TestNodePoolCreateWizard_BackRestoresTheLocationFilter(t *testing.T) { - t.Parallel() - api := poolWizardAPI() - client := newFakeClient(t, api) - cache := newWizardCache(tableFactory(client, nil), client) - - t.Run("no -l: going Back offers every cluster again", func(t *testing.T) { - opts := &nodePoolCreateOptions{} - flow := buildNodePoolCreateFlow(opts, cache) - step := flow.Steps[0] - if _, err := step.Loader(t.Context(), nil, nil, nil); err != nil { // the engine loads the choices first - t.Fatal(err) - } - step.Setter("staging") // FIN-01 - if opts.Location != "FIN-01" { - t.Fatalf("location = %q after picking staging", opts.Location) - } - step.Resetter() - if opts.Cluster != "" || opts.Location != "" { - t.Errorf("after Back: cluster=%q location=%q, want both cleared", opts.Cluster, opts.Location) - } - choices, err := step.Loader(t.Context(), nil, nil, nil) - if err != nil || len(choices) != 2 { - t.Errorf("choices = %v err=%v, want both clusters", choices, err) - } - }) - - t.Run("with -l: Back keeps the flag", func(t *testing.T) { - opts := &nodePoolCreateOptions{} - opts.Location = "FIN-03" - flow := buildNodePoolCreateFlow(opts, cache) - step := flow.Steps[0] - step.Setter("prod") - step.Resetter() - if opts.Location != "FIN-03" { - t.Errorf("location = %q, want the -l value kept", opts.Location) - } - if choices, _ := step.Loader(t.Context(), nil, nil, nil); len(choices) != 1 || choices[0].Value != "prod" { - t.Errorf("choices = %v, want prod only", choices) - } - }) -} - func TestInstanceTypeRank(t *testing.T) { t.Parallel() for in, want := range map[string]int{ diff --git a/internal/verda-cli/cmd/util/agent_error_test.go b/internal/verda-cli/cmd/util/agent_error_test.go index e8df8a5a..aafd00bc 100644 --- a/internal/verda-cli/cmd/util/agent_error_test.go +++ b/internal/verda-cli/cmd/util/agent_error_test.go @@ -60,13 +60,6 @@ func TestWriteAgentError(t *testing.T) { } } -func TestNewMissingOneOfFlagsError_MessageNamesMissing(t *testing.T) { - got := NewMissingOneOfFlagsError([]string{"--cluster", "--common-name"}, []string{"--a", "--b"}).Message - if want := "required flags not provided: --cluster, --common-name; set at least one of --a, --b"; got != want { - t.Errorf("message = %q, want %q", got, want) - } -} - func TestNewMissingOneOfFlagsError_JSON(t *testing.T) { var buf bytes.Buffer WriteAgentError(&buf, NewMissingOneOfFlagsError(nil, []string{"--a", "--b"})) diff --git a/internal/verda-cli/cmd/util/redact_test.go b/internal/verda-cli/cmd/util/redact_test.go index 72c1172b..38b1f030 100644 --- a/internal/verda-cli/cmd/util/redact_test.go +++ b/internal/verda-cli/cmd/util/redact_test.go @@ -61,44 +61,19 @@ func TestRedactSensitiveBody_JSON(t *testing.T) { } }) - t.Run("kubeconfig YAML with escaped newlines and quotes redacted whole", func(t *testing.T) { - t.Parallel() - in := `{"id":"c-kc-1","kubeconfig":"apiVersion: v1\nusers:\n- user:\n client-key-data: \"MARKER-KEY\"\n","status":"ready"}` - got := redactSensitiveBody("application/json", in) - if strings.Contains(got, "MARKER-KEY") || strings.Contains(got, "client-key-data") { - t.Errorf("kubeconfig YAML leaked: %s", got) - } - if !strings.Contains(got, `"status":"ready"`) || !strings.Contains(got, `"id":"c-kc-1"`) { - t.Errorf("neighboring fields mangled: %s", got) - } - }) - - t.Run("unterminated value redacted to the end", func(t *testing.T) { + t.Run("unterminated or mixed-case values redacted", func(t *testing.T) { t.Parallel() for name, in := range map[string]string{ - "kubeconfig": `{"id":"c-kc-1","kubeconfig":"apiVersion: v1\nclient-key-data: MARKER-KEY`, + "unterminated": `{"id":"c-kc-1","kubeconfig":"apiVersion: v1\nclient-key-data: MARKER-KEY`, "trailing escape": `{"kubeconfig":"client-key-data: MARKER-KEY\`, - "client_secret": `{"client_secret": "MARKER-KEY`, + "Kubeconfig": `{"Kubeconfig":"MARKER-KEY","ok":true}`, } { - got := redactSensitiveBody("application/json", in) - if strings.Contains(got, "MARKER-KEY") { - t.Errorf("%s: unterminated value leaked: %s", name, got) + if got := redactSensitiveBody("application/json", in); strings.Contains(got, "MARKER-KEY") { + t.Errorf("%s: value leaked: %s", name, got) } } }) - t.Run("kubeconfig key with spaces and a pretty-printed body", func(t *testing.T) { - t.Parallel() - in := "{\n \"id\": \"c-kc-1\",\n \"kubeconfig\" : \"client-key-data: MARKER-KEY\\n\",\n \"status\": \"ready\"\n}" - got := redactSensitiveBody("application/json", in) - if strings.Contains(got, "MARKER-KEY") { - t.Errorf("spaced kubeconfig key leaked: %s", got) - } - if !strings.Contains(got, `"status": "ready"`) { - t.Errorf("neighboring field mangled: %s", got) - } - }) - t.Run("spacing variants", func(t *testing.T) { t.Parallel() in := "{ \"client_secret\":\"x\", \"refresh_token\"\t: \"y\" }" @@ -179,49 +154,32 @@ func TestRedactSensitiveBody_ContentTypeDispatch(t *testing.T) { }) } -// debugTransport must keep a kubeconfig private key out of the debug log: -// bodies of /kubeconfigs endpoints are omitted, any other path is redacted -// whatever the key's case. +// A real debugTransport round trip: a /kubeconfigs body never reaches the +// debug log, and the caller still gets it. func TestDebugTransport_KubeconfigBodies(t *testing.T) { t.Parallel() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") - _, _ = io.WriteString(w, `{"id":"x","kubeconfig":"MARKER-KEY","Kubeconfig":"MARKER-KEY"}`) + _, _ = io.WriteString(w, `{"id":"x","kubeconfig":"MARKER-KEY"}`) })) t.Cleanup(srv.Close) - - tests := []struct { - name, path, wantOut string - }{ - {"kubeconfig endpoint omits bodies", "/v1/kubeconfigs/x", ""}, - {"other endpoint redacts keys in any case", "/v1/other", `"Kubeconfig":""`}, + var out bytes.Buffer + client := &http.Client{Transport: &debugTransport{base: http.DefaultTransport, out: &out, enabled: func() bool { return true }}} + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL+"/v1/mk8sclusters/FIN-03/prod/kubeconfigs", strings.NewReader(`{"kubeconfig":"MARKER-KEY"}`)) + if err != nil { + t.Fatal(err) } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - var out bytes.Buffer - client := &http.Client{Transport: &debugTransport{base: http.DefaultTransport, out: &out, enabled: func() bool { return true }}} - req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL+tc.path, strings.NewReader(`{"kubeconfig":"MARKER-KEY"}`)) - if err != nil { - t.Fatal(err) - } - req.Header.Set("Content-Type", "application/json") - resp, err := client.Do(req) - if err != nil { - t.Fatal(err) - } - body, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() - if !bytes.Contains(body, []byte("MARKER-KEY")) { - t.Fatalf("caller must still receive the body: %s", body) - } - if strings.Contains(out.String(), "MARKER-KEY") { - t.Errorf("key leaked into debug output:\n%s", out.String()) - } - if !strings.Contains(out.String(), tc.wantOut) { - t.Errorf("debug output lacks %q:\n%s", tc.wantOut, out.String()) - } - }) + req.Header.Set("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + body, _ := io.ReadAll(resp.Body) + _ = resp.Body.Close() + if !bytes.Contains(body, []byte("MARKER-KEY")) { + t.Fatalf("caller must still receive the body: %s", body) + } + if strings.Contains(out.String(), "MARKER-KEY") || strings.Count(out.String(), "") != 2 { + t.Errorf("debug output:\n%s", out.String()) } }