Files
Davanum SrinivasandClaude Fable 5.1 668bc38a9b feat(cli)!: rename nkectl to nvkectl, keeping the old names for one release (#1456)
The binary, its directory, its config directory and its environment
variables take the new name. For one release the previous names still
work: every NVKECTL_* setting is also read under its NKECTL_* name with a
deprecation warning (the NKECTL_CWE_* names of the generation before are
no longer read), a config file left under nkectl/ is read and copied to
nvkectl/ once, and releases publish both nvkectl-<os>-<arch> and
nkectl-<os>-<arch>, to GitHub and to S3 under the existing prefix.

The e2e helper package, the e2e test, the smoke and fetch scripts and
the bootstrap e2e workflow follow the binary; the fetch script accepts a
release that carries only the old asset name. The CLI's own documents
and the two installation documents that print the download URLs change
with it.

Kept on purpose: the NVCR repository name until an nvkectl repository
exists in the NGC org, the S3 prefix the publishing role's policy names,
and the prose in the other documents.

BREAKING CHANGE: the CLI is installed and invoked as nvkectl. The
previous binary name, environment variable names and config directory
are honoured for one release, as described above.

Closes #1455

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 17:04:03 -04:00

153 lines
4.7 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package command
import (
"context"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/dsx-ai-factory/nvke/cli/internal/operator"
"github.com/dsx-ai-factory/nvke/cli/internal/util"
)
const (
dryRunNone = "none"
dryRunClient = "client"
)
// NewCreateCmds returns the "nvkectl create machineimage|clusterrelease|
// nodepoolrelease" subcommands.
func NewCreateCmds(clientFn ClientFn) []*cobra.Command {
cmds := make([]*cobra.Command, 0, len(kindInfos))
for _, k := range kindInfos {
cmds = append(cmds, newCreateCmd(k, clientFn))
}
return cmds
}
func newCreateCmd(k kindInfo, clientFn ClientFn) *cobra.Command {
var (
filename string
outputFormat string
dryRun string
)
cmd := &cobra.Command{
Use: k.use,
Aliases: []string{k.plural()},
Short: "Create " + k.what,
Long: fmt.Sprintf(`Create a %s on the active site's management cluster.
The request is a single YAML or JSON document with a metadata and a spec, and
nothing else. nvkectl supplies the apiVersion and kind, so leave them out:
metadata:
name: <name>
spec:
...
Pass a file path or '-' to read from stdin. Unknown fields are rejected.
Published content is immutable. To change what a %s provisions, create it
again under a new name; creating an existing name fails. A release must name a
machine image that already exists, so create the machine image first.
With --dry-run=client the document is checked and printed as a complete
manifest, and no management cluster is contacted, so no site is needed. The
check covers structure only: required fields and value rules are enforced by the
management cluster when the resource is created, and the machine image a
release names is not looked up.
Example:
nvkectl create %s -f %s.yaml
Example - print the complete manifest without creating anything:
nvkectl create %s -f %s.yaml --dry-run=client -o yaml`,
k.noun, k.noun, k.use, k.use, k.use, k.use),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
format, err := util.ParseFormat(outputFormat)
if err != nil {
return err
}
if dryRun != dryRunNone && dryRun != dryRunClient {
return fmt.Errorf("invalid --dry-run value %q: must be %q or %q", dryRun, dryRunNone, dryRunClient)
}
data, err := readDocument(cmd, filename)
if err != nil {
return err
}
res, err := operator.Decode(k.kind, data)
if err != nil {
return err
}
if dryRun == dryRunClient {
if format == util.FormatTable {
format = util.FormatYAML
}
return util.Print(cmd.OutOrStdout(), format, operator.Manifest(k.kind, res))
}
c, err := clientFn()
if err != nil {
return err
}
if err := checkMachineImageExists(cmd.Context(), c, k, res); err != nil {
return err
}
created, err := c.Create(cmd.Context(), k.kind, res)
if err != nil {
return err
}
if format != util.FormatTable {
return util.Print(cmd.OutOrStdout(), format, created)
}
fmt.Fprintf(cmd.OutOrStdout(), "%s %q created\n", capitalize(k.noun), created.Metadata.Name)
return nil
},
}
cmd.Flags().StringVarP(&filename, "filename", "f", "", "Path to the YAML or JSON request, or '-' to read from stdin")
_ = cmd.MarkFlagRequired("filename")
cmd.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format: json or yaml (default: a one-line message, or yaml with --dry-run)")
cmd.Flags().StringVar(&dryRun, "dry-run", dryRunNone, "Use \"client\" to print the complete manifest without contacting the management cluster")
return cmd
}
// checkMachineImageExists fails when a release names a machine image that does
// not exist, so a release is never published pointing at nothing. Machine
// images name nothing and are not checked.
//
// The check and the create are two requests, so an image deleted in between is
// not caught. It reads the machine image, which needs get on machineimages.
func checkMachineImageExists(ctx context.Context, c operator.Client, k kindInfo, res operator.Resource) error {
if k.kind == operator.KindMachineImage {
return nil
}
ref, _ := res.Spec["machineImageRef"].(map[string]any)
name, _ := ref["name"].(string)
if name == "" {
return nil
}
if _, err := c.Get(ctx, operator.KindMachineImage, name); err != nil {
if isNotFound(err) {
return fmt.Errorf("machine image %q not found: create it first with 'nvkectl create machineimage'", name)
}
return fmt.Errorf("checking machine image %q: %w", name, err)
}
return nil
}
// capitalize upper-cases the first letter of s.
func capitalize(s string) string {
if s == "" {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
}