diff --git a/CLAUDE.md b/CLAUDE.md index 564d3e5..721e81a 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 e62b7b8..241ddd4 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 180431f..545ecb1 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 7228b09..173c784 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 0000000..ae0727b --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/CLAUDE.md @@ -0,0 +1,66 @@ +# 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`/`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; `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. + +## 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` 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"). +- **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`, `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`. +- **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`. + +## Tests + +- `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`; `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`, `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 0000000..11acbee --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/README.md @@ -0,0 +1,66 @@ +# 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 (`pool`, `np`: the worker VMs) and their kubeconfigs (client certificates for `kubectl`), plus `locations`. Commands, flags and defaults: `verda mk8s --help`. + +## 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 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` + +| 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 | + +## Waiting + +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. +- 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 (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. 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_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.go b/internal/verda-cli/cmd/mk8s/cluster.go new file mode 100644 index 0000000..3f66071 --- /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 0000000..720c9ae --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_create.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" + "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 Changed. + 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) + } +} + +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 := runWizard(cmd.Context(), f, ioStreams, flow); err != nil { + 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) + cluster, err := apiCall(cmd.Context(), f, "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+"?") +} + +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 0000000..6741970 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_create_test.go @@ -0,0 +1,170 @@ +// 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/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...) } + +// The IDs go through unchecked: no network, subnet or cluster read happens. +func TestClusterCreate_FlagsSendExactlyTheGivenIDs(t *testing.T) { + t.Parallel() + 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) + } + } +} + +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() + _, _, err := runMK8s(t, agentFactory(newFakeClient(t, newFakeAPI())), "cluster", "create", "--name", "prod", "-l", "FIN-03") + 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_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.onClusterGet = func(c *verda.ManagedKubernetesCluster) { + c.Phase = verda.ManagedKubernetesClusterPhaseRunning + c.ExternalEndpoint = "https://prod.example.test:6443" + } + 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("%s: POSTs = %d", name, n) + } + 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 + } + 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 || errOut != "" { + t.Errorf("no wait_error and a quiet stderr off a terminal: %v %q", got, errOut) + } + } +} + +func TestClusterCreate_WaitTimeout(t *testing.T) { + fastPoll(t) + 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") + } + // 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) + } + } + 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) + } +} + +func TestClusterCreate_InteractiveSummaryAndConfirm(t *testing.T) { + withTerminal(t) + for _, confirm := range []bool{true, false} { + api := newFakeAPI() + _, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tuitest.New().AddConfirm(confirm)), 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 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 new file mode 100644 index 0000000..3c9a8d2 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_delete.go @@ -0,0 +1,104 @@ +// 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}) + if err := apiRun(cmd.Context(), f, "Deleting cluster...", func(ctx context.Context) error { + return client.ManagedKubernetes.Delete(ctx, target.Location, target.Name) + }); err != nil { + return 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 0000000..b7fad0f --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_delete_test.go @@ -0,0 +1,105 @@ +// 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_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) + 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), tc.prompter), append([]string{"cluster", "delete", "prod"}, tc.args...)...) + if err != nil { + 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) != tc.warning { + t.Errorf("%s: warning and %q: %s", name, want, errOut) + } + } + if n := len(api.requestsMatching("DELETE")); n != 1 { + t.Errorf("%s: DELETEs = %d", name, n) + } + } +} 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 0000000..f6cb3a1 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_describe.go @@ -0,0 +1,147 @@ +// 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" +) + +// 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)} + + pools, poolErr := listPools(cmd.Context(), f, client, target) + if poolErr != nil { + 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) + 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 0000000..8c9f760 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_describe_test.go @@ -0,0 +1,130 @@ +// 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 ( + "regexp" + "slices" + "strings" + "testing" +) + +func describedAPI() *fakeAPI { + api := seededClusters() + api.clusters[0].Conditions = append(api.clusters[0].Conditions, conditionReady()) + api.addPool("prod", "workers", "active", 3, 2) + 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() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "cluster", "describe", "prod") + if 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) + } + 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]) + } + } + 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", + } { + 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, "AllGood") && strings.Index(line, "AllGood") > strings.Index(line, "all components ready") { + t.Errorf("condition columns are REASON then MESSAGE: %q", line) + } + } +} + +func TestClusterDescribe_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, describedAPI())), "cluster", "describe", "prod") + if err != nil { + t.Fatal(err) + } + 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) + } + 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"]) + } +} + +// 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() + failing := func() *fakeAPI { + api := describedAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/pools"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"pools unavailable"}`} + return api + } + 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 new file mode 100644 index 0000000..7b956b9 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_list.go @@ -0,0 +1,116 @@ +// 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: 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 { + 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 0000000..c33d15b --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_list_test.go @@ -0,0 +1,112 @@ +// 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" +) + +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_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, seededClusters())), "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 []string{ + "name", "location", "kubernetes_version", "phase", "external_endpoint", "network", "subnet", + "pod_cidr", "service_cidr", "dns_suffix", "conditions", "created_at", + } { + 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"]) + } +} + +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 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 0000000..958e929 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_upgrade.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" + + "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) + 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 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 +} + +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: 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 0000000..442e0d1 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/cluster_upgrade_test.go @@ -0,0 +1,110 @@ +// 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_Structured(t *testing.T) { + t.Parallel() + 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) + } + 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_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) + } +} + +// Without --kubernetes-version a terminal asks for it, then shows the warning and confirms. +func TestClusterUpgrade_Interactive(t *testing.T) { + withTerminal(t) + 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) { + 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) + } +} diff --git a/internal/verda-cli/cmd/mk8s/confirm.go b/internal/verda-cli/cmd/mk8s/confirm.go new file mode 100644 index 0000000..f8b8f3b --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/confirm.go @@ -0,0 +1,72 @@ +// 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: 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 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) + } + return nil +} + +// confirmWarning prints a red warning with detail lines on stderr, then asks. +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 0000000..b1bfc38 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/errors.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 + +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." +) + +const ( + kindCluster = "cluster" + kindNodePool = "node pool" + kindKubeconfig = "kubeconfig" +) + +// 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 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)) { + return &verda.APIError{StatusCode: http.StatusForbidden, Code: reasonFeatureNotEnabled, Message: notEnabledMessage} + } + return err +} + +// 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 + } + 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) +} + +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" +} + +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 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) { + 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 0000000..f1197f4 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/errors_test.go @@ -0,0 +1,105 @@ +// 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" + "net/http" + "net/url" + "os" + "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(t *testing.T) { + t.Parallel() + 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}, + } { + 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) + } + } +} + +// TestCreateMayHaveSucceeded guards the SDK error texts that createMayHaveSucceeded matches. +func TestCreateMayHaveSucceeded(t *testing.T) { + t.Parallel() + 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) + } + } + + // 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 { + t.Fatal(err) + } + 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 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 new file mode 100644 index 0000000..57e5a06 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/fake_test.go @@ -0,0 +1,676 @@ +// 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" + "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" +) + +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 +} + +// 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 { + 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) { + 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) { + 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) { + 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) + 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) { + cluster := r.PathValue("name") + var req verda.CreateManagedKubernetesNodePoolRequest + _ = json.NewDecoder(r.Body).Decode(&req) + 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) { + 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) { + 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) { + 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) { + 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) { + 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) { + 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) + }) +} + +// 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.go b/internal/verda-cli/cmd/mk8s/kubeconfig.go new file mode 100644 index 0000000..dee2f3f --- /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 0000000..800d8e9 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_create.go @@ -0,0 +1,322 @@ +// 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 = "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: 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: 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, nil, "") + 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 + } + // Input, then the file, then prompts and calls: no certificate is issued for a path that fails. + 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, target, err := opts.connect(cmd, f, ioStreams) + 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 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 { + return req, nil, err + } + dest, err := o.destination(cmd, o.Cluster, o.CommonName) + return req, dest, err +} + +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) + 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 { + 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 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) +} + +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: 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 + } + 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 0000000..7e433a7 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_create_test.go @@ -0,0 +1,361 @@ +// 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" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + 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" + +// 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 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, + } { + 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() + for name, tc := range map[string]struct { + flags []string + groups []any + extra map[string]any + }{ + "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) + } + } + 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 := setHome(t) + args := []string{"kubeconfig", "create", "--cluster", "prod", "--common-name", "alice", "--admin"} + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, kcAPI()), 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) + 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) + } +} + +// A path that cannot be written stops the command before a certificate is issued. +func TestKubeconfigCreate_ExistingTargetStopsBeforeAnyPOST(t *testing.T) { + t.Parallel() + dir := t.TempDir() + file := filepath.Join(dir, "kc.yaml") + if err := os.WriteFile(file, []byte("old"), 0o600); err != nil { + t.Fatal(err) + } + 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)), 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() + 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, kcAPI())), 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) + } +} + +// --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) + } + 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") + } +} + +// --file - would share stdout with the JSON: it fails before any API call. +func TestKubeconfigCreate_FileDashRejectedWithStructuredOutput(t *testing.T) { + t.Parallel() + for name, args := range map[string][]string{ + "create": kcArgs("-"), + "save": {"kubeconfig", "save", "prod-kc-1", "--cluster", "prod", "--file", "-"}, + } { + 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() + 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) + } +} + +func TestKubeconfigCreate_InvalidInput(t *testing.T) { + t.Parallel() + api := kcAPI() + file := filepath.Join(t.TempDir(), "kc.yaml") + for name, extra := range map[string][]string{ + "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) + } + } + if n := len(api.requests); n != 0 { + t.Errorf("%d request(s) for invalid input", n) + } +} + +// 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) + } + } + 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) + } + if tc.readFails { + api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 403, Body: `{"code":"forbidden","message":"no access"}`} + } + f := tableFactory(newFakeClient(t, api), nil) + if tc.agent { + f = 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" + } + 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) + } + 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 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("%s: POSTs = %d", name, n) + } + kcAssertErrNoKey(t, err) + assertNoPrivateKey(t, "stderr", errOut) + } +} + +// 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() + reads := 0 + api.onKubeconfigGet = func(k *verda.ManagedKubernetesKubeconfig) { + reads++ + switch reads { + case 1: + k.Status = verda.ManagedKubernetesKubeconfigStatusPending + case 2: + 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 || reads < 3 { + t.Errorf("file = %q, reads = %d", got, reads) + } + assertNoPrivateKey(t, "stdout", out) +} 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 0000000..aef6ecf --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_delete.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 + +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 := scope.check(f, cmd, args, argKubeconfigRef); err != nil { + return err + } + if err := checkConfirmed(f, yes, "delete"); err != nil { + return err + } + client, target, err := scope.connect(cmd, f, ioStreams) + 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}) + if err := apiRun(cmd.Context(), f, "Deleting kubeconfig...", func(ctx context.Context) error { + return client.ManagedKubernetes.DeleteKubeconfig(ctx, target.Location, target.Name, kc.ID) + }); err != nil { + return 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: 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. +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 0000000..28c29e6 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_delete_test.go @@ -0,0 +1,89 @@ +// 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" +) + +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() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "kubeconfig", "delete", "prod-kc-aaa", "--cluster", "prod", "--yes") + if err != nil { + t.Fatal(err) + } + 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 got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters"}) { + t.Errorf("reads = %v, want only the cluster lookup", got) + } +} + +func TestKubeconfigDelete_Structured(t *testing.T) { + t.Parallel() + 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) + } + want := ActionResult{Resource: "kubeconfig", ID: "prod-kc-aaa", Name: "prod-kc-aaa", Location: "FIN-03", Cluster: "prod", Action: "delete", Status: "accepted"} + if got := decodeJSON[ActionResult](t, out); got != want { + t.Errorf("got %+v, want %+v", got, want) + } +} + +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) + } + 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 !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_Errors(t *testing.T) { + t.Parallel() + _, _, 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", "ghost", "--cluster", "prod", "--yes") + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown id: %v", err) + } +} 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 0000000..5225c76 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_describe.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" + "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 := scope.check(f, cmd, args, argKubeconfigRef); err != nil { + return err + } + client, target, err := scope.connect(cmd, f, ioStreams) + 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 0000000..c4b35ac --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_describe_test.go @@ -0,0 +1,132 @@ +// 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 ( + "path/filepath" + "reflect" + "regexp" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// 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() + 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]) + } + } +} + +var kcDescribeLine = regexp.MustCompile(`^([A-Za-z][A-Za-z ]*):\s+(\S.*)$`) + +func TestKubeconfigDescribe_Table(t *testing.T) { + t.Parallel() + api := kcFixtureAPI() + 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]) + } + } + 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 !reflect.DeepEqual(got, want) { + t.Errorf("got %v\nwant %v", got, want) + } + 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()) + _, _, 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) + } +} + +// 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) + 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) + } + if got := api.paths("GET"); !slices.Equal(got, []string{"/mk8sclusters", "/mk8sclusters/FIN-03/prod/kubeconfigs"}) { + t.Errorf("requests = %v", got) + } +} + +// 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() + failingRead := func() *fakeAPI { + api := kcFixtureAPI() + api.failures["GET /mk8sclusters/{loc}/{name}/kubeconfigs/{id}"] = apiFailure{Status: 500, Body: `{"code":"internal","message":"read failed"}`} + 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) + } + } +} 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 0000000..389caf4 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_file.go @@ -0,0 +1,224 @@ +// 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 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). +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: 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: ~/.kube is created 0700 if missing; a custom --file parent must exist. + defaultDir bool +} + +// 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 { + if file != "" { + return kubeconfigTarget{path: file}, nil + } + return kubeconfigTarget{}, fmt.Errorf("find home directory for the default kubeconfig path (use --file): %w", err) + } + // '.' 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 { + return kubeconfigTarget{path: file}, nil + } + return kubeconfigTarget{path: def, defaultDir: true}, nil +} + +func sanitizeFileName(s string) string { + return nonFileNameChars.ReplaceAllString(s, "-") +} + +// 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 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)} + } + return probeWritable(dir) +} + +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 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 { + return &inputError{msg: fmt.Sprintf("cannot write to %s: %v", dir, err)} + } + name := f.Name() + _ = f.Close() + _ = os.Remove(name) + return nil +} + +// 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 + } + 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: an existing ~/.kube 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 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 { + 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 0000000..57c0207 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_file_test.go @@ -0,0 +1,158 @@ +// 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" + "runtime" + "strings" + "testing" +) + +func skipModeTestOnWindows(t *testing.T) { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("POSIX file modes are not enforced on Windows") + } +} + +// 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) + } + } + + // 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 + } +} + +// 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") + } + 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 + } + + 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) + } + if entries, _ := os.ReadDir(dir); len(entries) != 0 { + t.Errorf("Check left files behind: %v", entries) + } + + 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) + } + + 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 { //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) + } + assertMode(t, kube, 0o755) + 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 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) + } +} 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 0000000..a4323e8 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_list.go @@ -0,0 +1,173 @@ +// 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 err := scope.check(f, cmd, nil, ""); err != nil { + return err + } + client, target, err := scope.connect(cmd, f, ioStreams) + 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 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) + 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 + }) +} + +// 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]) + 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 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 0000000..17b20ef --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_list_test.go @@ -0,0 +1,131 @@ +// 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" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + 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) + } +} + +// Every key but file is always present: a pending kubeconfig has groups [] and not_after null. +func TestKubeconfigList_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, kcFixtureAPI())), "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 len(m) != 6 { + t.Errorf("item %d = %v, want the 6 keys of the view", i, m) + } + 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 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) + } + out, _, err = runMK8s(t, jsonFactory(client), "kubeconfig", "list", "--cluster", "prod") + if err != nil || strings.TrimSpace(out) != "[]" { + t.Errorf("json: empty output = %q, error=%v", out, err) + } +} + +func TestKubeconfigList_ClusterErrors(t *testing.T) { + t.Parallel() + client := newFakeClient(t, kcFixtureAPI()) + _, _, 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) + } +} 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 0000000..77a2ad8 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts.go @@ -0,0 +1,225 @@ +// 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 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: each ask reports ok=false after "Canceled." on 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 +} + +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 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){ + 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: 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 + } + 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 +} + +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 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) + 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: 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 == "" { + 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 0000000..d3f9819 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_prompts_test.go @@ -0,0 +1,161 @@ +// 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" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verda-cli/pkg/tui" + tuitest "github.com/verda-cloud/verda-cli/pkg/tui/testing" +) + +// 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 := setHome(t) + orig := currentUserName + currentUserName = func() string { return "tester" } + t.Cleanup(func() { currentUserName = orig }) + return home +} + +var kcInteractiveBase = []string{"kubeconfig", "create", "--cluster", "prod"} + +// Each flag skips its prompt; an empty display name or duration is left out of the request. +func TestKubeconfigCreateInteractive(t *testing.T) { + withTerminal(t) + 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)...) + } + api := kcAPI() + out, errOut, err := runMK8s(t, tableFactory(newFakeClient(t, api), tc.prompter(file)), args...) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + 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) + } + 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 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 := 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"), ""}, + "decline the summary": {confirmNo(), full, ""}, + "prompt failure": {tuitest.New(), kcInteractiveBase, "no text input responses queued"}, + } { + 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 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 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) + } + 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) + } + } +} 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 0000000..480cfe9 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_save.go @@ -0,0 +1,253 @@ +// 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 = "-" +) + +// shellSafe leaves out ':', the separator of a KUBECONFIG list. +var shellSafe = regexp.MustCompile(`^[A-Za-z0-9_@%+=,./-]+$`) + +func shellQuote(s string) string { + if shellSafe.MatchString(s) { + return s + } + return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" +} + +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 +} + +type kubeconfigDest struct { + stdout bool + target kubeconfigTarget + replaced bool // set by write +} + +// 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 + } + 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 +} + +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 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 { + s += " --file " + shellQuote(d.target.path) + } + if force && !d.stdout { + s += " --force" + } + return s +} + +// 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 { + 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 + } + if err := opts.check(f, cmd, args, argKubeconfigRef); err != nil { + return err + } + client, target, err := opts.connect(cmd, f, ioStreams) + 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) +} + +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 0000000..a5f6ad9 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/kubeconfig_save_test.go @@ -0,0 +1,155 @@ +// 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" + "slices" + "strings" + "testing" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + 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...) +} + +// 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, + } { + 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() + 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"}, + } { + 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 _, statErr := os.Stat(file); statErr == nil { + t.Errorf("%s: no file may be written", name) + } + } +} + +func TestKubeconfigSave_DefaultPathUsesTheCommonName(t *testing.T) { + home := setHome(t) + 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) + _, _, 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")) + if agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown id: %v", 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 0000000..78b4839 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/locations.go @@ -0,0 +1,77 @@ +// 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" + + "github.com/spf13/cobra" + + 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 + } + res, err := apiCall(cmd.Context(), f, "Loading locations...", client.ManagedKubernetes.GetEnabledLocations) + if err != nil { + return 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 0000000..83cea7e --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/locations_test.go @@ -0,0 +1,131 @@ +// 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" + + 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_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) + } +} + +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 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 _, 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 0000000..5d8a9a2 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/mk8s.go @@ -0,0 +1,114 @@ +// 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" + + statusAccepted = "accepted" + statusCompleted = "completed" + statusUnchanged = "unchanged" // nothing was sent + + timeLayout = "2006-01-02 15:04" + + // 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." + + // 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" +) + +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: 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() +} + +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 0000000..ec6ef1e --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool.go @@ -0,0 +1,162 @@ +// 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 --cluster and -l, shared by the node-pool and kubeconfig commands. +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, 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)) { + m = append(m, "--"+flagCluster) + } + if arg != "" && clusterArgMissing(f, argOrEmpty(args), len(args) > 0) { + m = append(m, arg) + } + return m +} + +// 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 +} + +// 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 + } + 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 { + 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 { + return nil, err + } + return pickPool(cmd.Context(), f, ioStreams, t, pools) +} + +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) + }) +} + +// 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 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 { + 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 0000000..7cda0ef --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_create.go @@ -0,0 +1,256 @@ +// 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" + "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) + o.SSHKeys = cleanGroups(o.SSHKeys) +} + +// 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 replicas < 0 { + return cmdutil.UsageErrorf(cmd, "--%s must be 0 or more", flagReplicas) + } + return nil +} + +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 := checkPoolFlags(cmd, opts.Wait, opts.Replicas); 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 := runWizard(cmd.Context(), f, ioStreams, flow); err != nil { + 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) + 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 { + 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) { + _, _ = 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, *req.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 0000000..4c8d393 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_create_test.go @@ -0,0 +1,158 @@ +// 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/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() + 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) + } + } +} + +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() + _, _, 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 missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "--name", "--instance-type", "--ssh-key"}) { + t.Errorf("missing = %v", missing) + } +} + +func TestNodePoolCreate_InvalidFlagsSendNothing(t *testing.T) { + t.Parallel() + for _, extra := range [][]string{{"--replicas", "-1"}, {"--wait", "--wait-timeout", "0s"}} { + api := poolWizardAPI() + 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)) + } + } +} + +// A read at the count but not yet active must not end the wait. +func TestNodePoolCreate_WaitCompletes(t *testing.T) { + fastPoll(t) + api := poolWizardAPI() + reads := 0 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + 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")...) + 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 reads != 2 { + t.Errorf("reads = %d, want 2", reads) + } +} + +func TestNodePoolCreate_WaitTimeout(t *testing.T) { + fastPoll(t) + api := poolWizardAPI() + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + p.State, p.ReadyReplicas = verda.ManagedKubernetesNodePoolStateDegraded, 1 + } + 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{"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_InteractiveSummaryAndConfirm(t *testing.T) { + withTerminal(t) + for _, tc := range []struct { + p *tuitest.Prompter + posts int + }{{confirmYes(), 1}, {confirmNo(), 0}} { + api := poolWizardAPI() + _, 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) + } + } + } +} 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 0000000..7e17e8e --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_delete.go @@ -0,0 +1,116 @@ +// 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 := scope.check(f, cmd, args, argPool); err != nil { + return err + } + if err := checkConfirmed(f, yes, "delete"); err != nil { + return err + } + client, target, err := scope.connect(cmd, f, ioStreams) + 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}) + if err := apiRun(cmd.Context(), f, "Deleting node pool...", func(ctx context.Context) error { + return client.ManagedKubernetes.DeleteNodePool(ctx, target.Location, target.Name, name) + }); err != nil { + return 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: "" 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 0000000..064041e --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_delete_test.go @@ -0,0 +1,74 @@ +// 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" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +func TestNodePoolDelete_YesMakesOneDeleteCall(t *testing.T) { + t.Parallel() + api := poolAPI() + out, _, err := runMK8s(t, tableFactory(newFakeClient(t, api), nil), "node-pool", "delete", "workers", "--cluster", "prod", "--yes") + if err != nil || !strings.Contains(out, "Deletion accepted. Worker VMs are deleted in the background.") { + t.Fatalf("out=%q err=%v", out, err) + } + if dels := api.requestsMatching("DELETE"); len(dels) != 1 || dels[0].Path != "/mk8sclusters/FIN-03/prod/pools/workers" { + t.Fatalf("DELETE requests = %v", dels) + } + // 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_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), "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_MissingTargets(t *testing.T) { + t.Parallel() + _, _, 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) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", ""}) { + t.Errorf("missing = %v", missing) + } +} + +func TestNodePoolDelete_UnknownPool(t *testing.T) { + t.Parallel() + _, _, err := runMK8s(t, tableFactory(newFakeClient(t, poolAPI()), nil), "node-pool", "delete", "ghost", "--cluster", "prod", "--yes") + if agentErrorCode(err) != "NOT_FOUND" { + 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 new file mode 100644 index 0000000..468a8fd --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_describe.go @@ -0,0 +1,109 @@ +// 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 := scope.check(f, cmd, args, argPool); err != nil { + return err + } + client, target, err := scope.connect(cmd, f, ioStreams) + 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 0000000..4eddb06 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_describe_test.go @@ -0,0 +1,90 @@ +// 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" + + 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) + } + } + if got := api.paths("GET"); !slices.Contains(got, "/mk8sclusters/FIN-03/prod/pools/gpu") { + t.Errorf("requests = %v", got) + } +} + +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, tableFactory(newFakeClient(t, poolAPI()), nil), "node-pool", "describe", "ghost", "--cluster", "prod"); agentErrorCode(err) != "NOT_FOUND" { + t.Errorf("unknown pool: %v", 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 0000000..884e1d6 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_list.go @@ -0,0 +1,89 @@ +// 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 err := scope.check(f, cmd, nil, ""); err != nil { + return err + } + client, target, err := scope.connect(cmd, f, ioStreams) + 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 0000000..84621db --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_list_test.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 ( + "reflect" + "slices" + "strings" + "testing" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +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_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, poolAPI())), "node-pool", "list", "--cluster", "prod") + if err != nil { + t.Fatal(err) + } + 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 got := decodeJSON[[]PoolView](t, out); !reflect.DeepEqual(got, want) { + t.Errorf("got %+v\nwant %+v", got, want) + } +} + +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_MissingCluster(t *testing.T) { + t.Parallel() + _, _, 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) + } + if missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster"}) { + t.Errorf("missing = %v", missing) + } +} 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 0000000..737a02d --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_scale.go @@ -0,0 +1,277 @@ +// 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, target, err := opts.connect(cmd, f, ioStreams) + 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 +} + +func (o *nodePoolScaleOptions) validate(f cmdutil.Factory, cmd *cobra.Command, args []string) error { + 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) + } + return nil +} + +func poolSettled(pool *verda.ManagedKubernetesNodePool, want int) bool { + _, done := poolScaled(want)(pool) + return done +} + +// 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 + } + replicas := o.Replicas + req := verda.UpdateManagedKubernetesNodePoolRequest{Replicas: &replicas} + cmdutil.DebugJSON(ioStreams.ErrOut, f.Debug(), "Request payload:", req) + 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, 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) +} + +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 +} + +func (o *nodePoolScaleOptions) scaleMissing(f cmdutil.Factory, cmd *cobra.Command, args []string) []string { + missing := o.missing(f, cmd, args, argPool) + if !o.replicasSet && !interactiveTTY(f) { + missing = append(missing, "--"+flagReplicas) + } + return missing +} + +// 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 + } + 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) +} + +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: 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 0000000..0c920f8 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/nodepool_scale_test.go @@ -0,0 +1,226 @@ +// 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" +) + +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 := 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) + } +} + +func TestNodePoolScale_Structured(t *testing.T) { + t.Parallel() + out, _, err := runMK8s(t, agentFactory(newFakeClient(t, poolAPI())), scaleArgs("--replicas", "5", "-l", "FIN-03")...) + if err != nil { + t.Fatal(err) + } + 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_MissingArguments(t *testing.T) { + t.Parallel() + _, _, 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 missing, _ := ae.Details["missing"].([]string); !slices.Equal(missing, []string{"--cluster", "", "--replicas"}) { + t.Errorf("missing = %v", missing) + } +} + +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)) + } + } +} + +func TestNodePoolScale_SameCountSendsNothing(t *testing.T) { + fastPoll(t) + for name, tc := range map[string]struct { + ready int + wait bool + wantStatus string + }{ + "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", "active", 2, tc.ready) + var reads atomic.Int32 + api.onPoolGet = func(p *verda.ManagedKubernetesNodePool) { + if reads.Add(1) > 1 { + p.ReadyReplicas = 2 + } + } + args := scaleArgs("--replicas", "2") + if tc.wait { + args = append(args, "--wait") + } + out, _, err := runMK8s(t, jsonFactory(newFakeClient(t, api)), args...) + 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) + } + } + 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("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) + } + 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 := poolAPI() + 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 := 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) + } + if got := patchedReplicas(t, api); got != float64(4) { + t.Errorf("replicas = %v", got) + } + }) +} + +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) + } + } + + // 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) { + 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 + } + } + 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) + for _, tc := range []struct { + api *fakeAPI + replicas, want string + ready, patches int + }{ + {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 := 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 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 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 new file mode 100644 index 0000000..14800cf --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/resolve.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 ( + "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 are unique per project, but every API path also needs the location. +type clusterTarget struct { + Name string + Location string +} + +func addLocationFlag(cmd *cobra.Command, location *string) { + cmd.Flags().StringVarP(location, flagLocation, "l", "", locationUsage) +} + +// 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}) + } + 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: 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 != "" { + 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) +} + +func argOrEmpty(args []string) string { + if len(args) == 0 { + return "" + } + return args[0] +} + +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 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 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 { + 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, ", ")) + } +} + +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: 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")} +} + +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 0000000..f1847d6 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/resolve_test.go @@ -0,0 +1,100 @@ +// 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" + "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" +) + +// With -l there is no List call; name and location are trimmed. +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) + } +} + +// 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("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"}}, + } { + _, _, 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) + } + } + } +} + +func TestResolve_Picker(t *testing.T) { + withTerminal(t) + 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) + } + if tc.out == "staging" && strings.Contains(out, "prod.example") { + t.Errorf("%s: described prod: %s", name, out) + } + } +} diff --git a/internal/verda-cli/cmd/mk8s/views.go b/internal/verda-cli/cmd/mk8s/views.go new file mode 100644 index 0000000..30154df --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/views.go @@ -0,0 +1,233 @@ +// 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 but file is always present. +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"` +} + +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: 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"` + 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 delete. +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 is the form of every date in mk8s output. +func utcTime(t time.Time) string { + return t.UTC().Format(timeLayout) + " UTC" +} + +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 0000000..ca11eed --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wait.go @@ -0,0 +1,233 @@ +// 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" + "strconv" + "strings" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// 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 a var so tests can shorten it. +var pollInterval = 5 * time.Second + +// 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() } + +// 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 { + return false + } + switch apiErr.StatusCode { + case http.StatusNotFound, http.StatusRequestTimeout, http.StatusTooManyRequests: + return false + } + return true +} + +// 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), +) (*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: 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. + 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) + return last, err +} + +// 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 + } + return nil +} + +func clusterReady(c *verda.ManagedKubernetesCluster) (status string, done bool) { + return c.Phase, c.Phase == verda.ManagedKubernetesClusterPhaseRunning && c.ExternalEndpoint != "" +} + +type waitFailureInfo struct { + Subject string // for example "cluster prod in FIN-03" + Timeout time.Duration + LastSeen string + TimeoutTail string // what to know or do after a timeout + ReadNote string // optional, after failed reads + CheckCmd string +} + +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) +} + +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 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 { + s += " (" + strings.Join(reasons, ", ") + ")" + } + return fmt.Sprintf("%s, %d/%d workers ready", s, p.ReadyReplicas, p.Replicas) +} + +// 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 + } +} + +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 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) + if !wrote { + return false, nil + } + if err != nil { + return true, err + } + return true, waitErr +} + +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) +} + +type poolWaitKind int + +const ( + poolWaitCreate poolWaitKind = iota + poolWaitScaleUp + poolWaitScaleDown + poolWaitSettle +) + +// 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 { + 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: `" + poolCmd("delete", 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: `" + poolCmd("scale", t, accepted.Name) + " --replicas " + strconv.Itoa(previous) + " --yes`. Check with `" + describe + "`" + info.ReadNote = poolBackendKeepsTrying + default: + info.TimeoutTail, info.ReadNote = creating, poolBackendKeepsTrying + } + return waitFailure(err, &info) +} + +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 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 new file mode 100644 index 0000000..5f35bff --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wait_test.go @@ -0,0 +1,135 @@ +// 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" + "strings" + "testing" + "time" + + "github.com/verda-cloud/verdacloud-sdk-go/pkg/verda" + + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" +) + +// 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) + 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 + } + 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 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(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 = "" + 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") + } +} 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 0000000..118048a --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_cache.go @@ -0,0 +1,165 @@ +// 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{}} +} + +func (c *wizardCache) enabledLocations(ctx context.Context) ([]string, error) { + if c.haveLocation { + return c.locations, nil + } + res, err := apiCall(ctx, c.f, "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 := apiCall(ctx, c.f, "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 := apiCall(ctx, c.f, "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: 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 { + return c.networks[i].Name + } + } + return id +} + +// 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 } + +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 := listClusters(ctx, c.f, c.client) + if err != nil { + return nil, err + } + c.clusters, c.haveClusters = clusters, true + return c.clusters, nil +} + +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 := 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 { + 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 := apiCall(ctx, c.f, "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 0000000..c27f3b3 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_cluster.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" + "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)) +} + +// 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{ + {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 validateName(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 +} + +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 { + return &wizard.Flow{ + Name: "mk8s-cluster-create", + Layout: wizardLayout(), + Steps: []wizard.Step{ + 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), + }, + } +} + +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 + } +} + +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 0000000..9c2e476 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_cluster_test.go @@ -0,0 +1,134 @@ +// 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" + "testing" + + "github.com/verda-cloud/verda-cli/pkg/tui/wizard" +) + +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 +} + +// 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) + + 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 := postBodyAny(t, flagsAPI), postBodyAny(t, wizAPI) + if !reflect.DeepEqual(got, want) { + t.Errorf("wizard body = %v, flags body = %v", got, want) + } + + // The name and --service-cidr are not given: two answers. + partialAPI := wizardAPI() + withWizardAnswers(t, wizard.TextResult("prod"), wizard.TextResult("")) + 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 := postBodyAny(t, partialAPI) + if body["name"] != "prod" || body["pod_cidr"] != "10.50.0.0/16" || body["service_cidr"] != defaultServiceCIDR { + t.Errorf("body = %v", body) + } + if got := partialAPI.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`.", + }, + } { + 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 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 := 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} { + if err := validateCIDR(cidr, defaultPodCIDR); (err == nil) != ok { + t.Errorf("validateCIDR(%q) = %v, ok=%v", cidr, err, ok) + } + } + 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 0000000..2a67078 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_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 ( + "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 +} + +// 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 := 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 + + 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 = wizardText(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 }, + }, + 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", + 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(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) }, + }, + }, + } +} + +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 +} + +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 0000000..95675b7 --- /dev/null +++ b/internal/verda-cli/cmd/mk8s/wizard_nodepool_test.go @@ -0,0 +1,124 @@ +// 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/verda-cli/pkg/tui/wizard" +) + +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 + ) + if _, _, err := runMK8s(t, tableFactory(newFakeClient(t, wizAPI), confirmYes()), "node-pool", "create"); err != nil { + t.Fatal(err) + } + + 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" { + t.Errorf("POST path = %s", posts[0].Path) + } +} + +// 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() + withWizardAnswers(t, wizard.TextResult("workers"), wizard.SelectResult(2), wizard.TextResult("")) + 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 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 slices.Contains(api.paths("GET"), "/ssh-keys") { + t.Error("SSH keys were listed although --ssh-key was given") + } +} + +// 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() + 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) + } + 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) + } + } +} + +func TestNodePoolCreateWizard_StopsOnEmptyPickers(t *testing.T) { + withTerminal(t) + 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")) + } +} + +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 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 1d7d1e4..dcef0bf 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 dcb49bd..aafd00b 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,8 @@ 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"]) } } diff --git a/internal/verda-cli/cmd/util/factory.go b/internal/verda-cli/cmd/util/factory.go index c484c31..ee41a61 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 2b1c471..9b0463f 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 2c6e51d..456de78 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 236a61b..38b1f03 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,19 @@ func TestRedactSensitiveBody_JSON(t *testing.T) { } }) + t.Run("unterminated or mixed-case values redacted", func(t *testing.T) { + t.Parallel() + for name, in := range map[string]string{ + "unterminated": `{"id":"c-kc-1","kubeconfig":"apiVersion: v1\nclient-key-data: MARKER-KEY`, + "trailing escape": `{"kubeconfig":"client-key-data: MARKER-KEY\`, + "Kubeconfig": `{"Kubeconfig":"MARKER-KEY","ok":true}`, + } { + if got := redactSensitiveBody("application/json", in); strings.Contains(got, "MARKER-KEY") { + t.Errorf("%s: value leaked: %s", name, got) + } + } + }) + t.Run("spacing variants", func(t *testing.T) { t.Parallel() in := "{ \"client_secret\":\"x\", \"refresh_token\"\t: \"y\" }" @@ -136,3 +153,33 @@ func TestRedactSensitiveBody_ContentTypeDispatch(t *testing.T) { } }) } + +// 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"}`) + })) + t.Cleanup(srv.Close) + 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) + } + 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()) + } +} diff --git a/internal/verda-cli/options/paths.go b/internal/verda-cli/options/paths.go index fbde291..ee3e8a2 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 }