mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
The repository has a hand-maintained quick reference and workflow guides, but
no complete reference. An operator cannot browse the whole public CLI without
a built binary, and nothing ties the prose to the commands, so the two drift
apart silently.
Add cli/docs/reference: an index and one page per public command, generated
from the cobra tree nvkectl itself runs. Each page carries the command's
purpose, invocation and positional arguments, aliases, examples, local and
inherited options with types and defaults, the required and mutually exclusive
option groups, and a deprecation notice when it has one.
cobra's doc package writes the body of each page. It does not emit aliases,
deprecation notices, flag-group constraints or an index, and it never removes
the page of a deleted command, so internal/clidocs adds those around it. All
of it is read from the command, so there is no second catalogue to maintain.
Deterministic, so repeated generation from unchanged source produces identical
files:
- cobra's generated-on timestamp is switched off on every command.
- The NVKECTL_ and NKECTL_ variables are cleared before the tree is built.
Two of `bootstrap site`'s flag defaults -- --diagnostics-dir and --timeout
-- are read from the environment by the command constructor, through
internal/envalias, which still honours the old prefix. Without this the
pages would record whatever the generating shell happened to have set.
- HOME and the XDG config directory are pinned, and the build metadata is a
fixed placeholder.
Generation runs no command and contacts nothing: it builds the tree and reads
its metadata, so it needs no credential, kubeconfig, configured site or
cluster. newRootCmd is exported for it, which is what the move to an
importable package was for.
`make -C cli docs` regenerates and `make -C cli docs-check` verifies, failing
on output that is stale, missing, obsolete, or newly generated and never
committed. The check joins the generated-artefact gate, which already covers
the provisioned-provider manifest and the generated API code, and joins
`make gate` and `make preflight` locally.
Two commands lazily added by cobra needed initialising before the traversal,
or the root page would link to pages that were never written: `help` and
`completion`. cobra's doc package also calls InitDefaultHelpCmd on every
command it renders, not just the root, which invented a `help` subcommand
under each group command -- `nvkectl bootstrap help` is not a command -- so
each one is given a hidden help command first to decide what cobra adds.
Hidden commands and help topics are excluded, which is why `render` and
`inspect` do not appear; a deprecated command keeps its page for as long as it
still runs.
Constraints enforced inside RunE are invisible to cobra. Those on `config
set-site` and the two patch commands are now stated in the flag usage strings
and Long text, so --help and the reference agree. No behaviour changes: the
checks still run where they did, with the messages they had.
cobra/doc brings go-md2man and blackfriday into cli/go.mod. Neither is
reachable from ./cmd/nvkectl, so the shipped binary is unaffected and
THIRD_PARTY_NOTICES.md is unchanged.
Signed-off-by: Jonathan Meier <jomeier@nvidia.com>
149 lines
4.9 KiB
Go
149 lines
4.9 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package nodepools
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/dsx-ai-factory/nvke/cli/internal/util"
|
|
)
|
|
|
|
// NewPatchCmd returns the "nvkectl patch nodepool <name>" subcommand.
|
|
// It sends the patch document to the controller, leaving every field the document does
|
|
// not carry unchanged.
|
|
func NewPatchCmd(clientFn ControllerClientFn) *cobra.Command {
|
|
var (
|
|
clusterName string
|
|
patch string
|
|
patchFile string
|
|
)
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "nodepool <name> --cluster <cluster>",
|
|
Aliases: []string{"nodepools"},
|
|
Short: "Patch a node pool",
|
|
Long: `Patch a node pool on the management plane.
|
|
|
|
Node pool names are unique within a cluster, so --cluster is required to
|
|
identify which pool to patch.
|
|
|
|
PATCH semantics apply: only the fields in the patch document are changed;
|
|
everything else on the node pool is left as it is. Pass the patch inline with
|
|
--patch, or from a file (or stdin via -) with --patch-file. Exactly one of
|
|
the two is required.
|
|
|
|
nvkectl can set ` + strings.Join(settableFields(), ", ") + ` so far. The API
|
|
accepts more, but anything else is rejected before the request is sent.
|
|
|
|
Example — scale a pool:
|
|
nvkectl patch nodepool gpu-workers --cluster my-cluster --patch 'desiredNodeCount: 8'
|
|
|
|
Example — patch file:
|
|
nvkectl patch nodepool gpu-workers --cluster my-cluster --patch-file patch.yaml
|
|
|
|
The patch document's fields must match the controller UpdateNodePoolRequest schema
|
|
exactly. See /api/docs on your management plane.`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if patch == "" && patchFile == "" {
|
|
return fmt.Errorf("nothing to patch: pass --patch or --patch-file")
|
|
}
|
|
if patch != "" && patchFile != "" {
|
|
return fmt.Errorf("pass only one of --patch or --patch-file")
|
|
}
|
|
req, err := loadPatchNodePoolRequest(cmd, patch, patchFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c, err := clientFn()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pool, err := c.FindNodePoolByName(cmd.Context(), clusterName, args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
updated, err := c.PatchNodePool(cmd.Context(), pool.ID, req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(cmd.OutOrStdout(), "Node pool %q patched (id: %s)\n", updated.Name, updated.ID)
|
|
fmt.Fprintf(cmd.OutOrStdout(),
|
|
"Update is async — run 'nvkectl get nodepools --cluster %s' to check status.\n", clusterName)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&clusterName, "cluster", "c", "", "Name of the cluster that owns the node pool")
|
|
cmd.Flags().StringVarP(&patch, "patch", "p", "", "Inline patch document. Accepts YAML or JSON")
|
|
cmd.Flags().StringVar(&patchFile, "patch-file", "", "Path to a YAML or JSON patch file, or '-' to read from stdin")
|
|
_ = cmd.MarkFlagRequired("cluster")
|
|
return cmd
|
|
}
|
|
|
|
// supportedFields is the subset of UpdateNodePoolRequest this command can
|
|
// change. UpdateNodePoolRequest carries more, and the controller accepts them, but only
|
|
// what is declared here is supported through nvkectl today.
|
|
//
|
|
// The patch document is validated against this rather than against the full
|
|
// request type, which is what rejects an unsupported field. Supporting another
|
|
// field is declaring it here and listing it in settableFields.
|
|
type supportedFields struct {
|
|
DesiredNodeCount *int `json:"desiredNodeCount,omitempty"`
|
|
}
|
|
|
|
// settableFields lists the fields a patch document may carry, for the
|
|
// command's help and for the error an unsupported field produces.
|
|
func settableFields() []string {
|
|
return []string{"desiredNodeCount"}
|
|
}
|
|
|
|
// loadPatchNodePoolRequest reads the patch from the inline document or the
|
|
// named file ("-" reads stdin) and returns it as the JSON body to send.
|
|
func loadPatchNodePoolRequest(cmd *cobra.Command, patch, patchFile string) ([]byte, error) {
|
|
raw := []byte(patch)
|
|
if patch == "" {
|
|
data, err := util.ReadFileOrStdin(cmd.InOrStdin(), patchFile, "patch")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
raw = data
|
|
}
|
|
|
|
patchMap, err := util.YAMLToGenericMap(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(patchMap) == 0 {
|
|
return nil, fmt.Errorf("nothing to patch: the patch document is empty")
|
|
}
|
|
// The controller declares no field of the request nullable, so a null would come back
|
|
// as a validator message naming a JSON pointer. Refuse it here instead.
|
|
for field, value := range patchMap {
|
|
if value == nil {
|
|
return nil, fmt.Errorf(
|
|
"%s: null is not a value; omit the field to leave it unchanged", field,
|
|
)
|
|
}
|
|
}
|
|
data, err := json.Marshal(patchMap)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encoding patch: %w", err)
|
|
}
|
|
|
|
var validate supportedFields
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
dec.DisallowUnknownFields()
|
|
if err := dec.Decode(&validate); err != nil {
|
|
return nil, fmt.Errorf("validating patch: %w; nvkectl can set %s",
|
|
err, strings.Join(settableFields(), ", "))
|
|
}
|
|
return data, nil
|
|
}
|