nvkectl create 深入解析:在 NVKE 管理集群上创建租户集群、节点池与发布资源

原创2026-11-09 15:45:281,637 阅读

nvkectl create 深入解析:在 NVKE 管理集群上创建租户集群、节点池与发布资源

nvkectl create 是 NVKE(NVIDIA Kubernetes Engine)命令行工具中负责“创建资源”的入口命令,它下面挂载了三类彼此差异明显的子命令:面向 NVKE 控制器 API 的 cluster 与 nodepool,以及面向站点管理集群 CRD 的 machineimage、clusterrelease、nodepoolrelease。本文以 nvkectl create 命令参考 为骨架,逐条拆解这些子命令的参数、请求文档格式、不可变性约束与 --dry-run=client 行为,并结合仓库源码说明它们背后的实现路径与校验逻辑,帮助你在真实环境中安全、可复现地发布 NVKE 资源。

一、命令定位:一个父命令,五种创建语义

官方参考页对 nvkectl create 的描述非常简短——“Create a resource”,随后通过 SEE ALSO 列出了五个子命令。这种“父命令只做聚合、子命令各自独立”的结构在源码中一目了然:

func newCreateCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "create",
		Short: "Create a resource",
	}
	cmd.AddCommand(clusters.NewCreateCmd(loadClient))
	cmd.AddCommand(nodepools.NewCreateCmd(loadClient))
	cmd.AddCommand(operatorcmd.NewCreateCmds(loadOperator)...)
	return cmd
}

见 cli/internal/nvkectl/create.go。

子命令挂载顺序与参考页 SEE ALSO 的排序一致,其语义可以归纳为两组:

子命令 别名 作用域 目标后端 说明
create cluster clusters 租户集群 控制器 REST API 创建租户(工作负载)集群,异步供应
create nodepool nodepools 节点池 控制器 REST API 为已有租户集群增加节点池,异步供应
create machineimage machineimages 站点级 管理集群 CRD 发布不可变的节点镜像及其 NICo 引导脚本
create clusterrelease clusterreleases 站点级 管理集群 CRD 发布控制平面 release
create nodepoolrelease nodepoolreleases 站点级 管理集群 CRD 发布 worker release

关键区别在于“谁在解释你的 YAML”:cluster / nodepool 的文档直接映射为控制器 API 的 CreateClusterRequest / CreateNodePoolRequest 结构(见 clusters/create.go 与 nodepools/create.go),而三类镜像/发布资源走的是“可移植文档”模型,由 CLI 自行补齐 apiVersion 与 kind,再写入管理集群。

1.1 懒加载客户端:没有配置站点也能看帮助

父命令把 loadClient 与 loadOperator 作为工厂函数传下去,而不是传入已构造的客户端。源码中对此有明确说明:命令接受工厂函数,只有在真正执行时才解析站点配置,因此 --help 与客户端侧 --dry-run 在完全没有配置站点的机器上也能工作(见 cli/internal/operator/command/command.go)。测试 TestHelpNeedsNoSite 与 TestCreateDryRunClientPrintsAManifestAndNeedsNoSite 固化了这一行为(见 cli/internal/operator/command/command_test.go)。

二、公共参数:create 自身与继承自父命令的选项

参考文档把参数分成两层。

2.1 nvkectl create 自身的选项

  -h, --help   help for create

父命令没有业务参数,只有帮助开关。所有实际能力都由子命令提供。

2.2 从父命令继承的全局选项

      --json-logs     Emit logs as JSON
      --site string   Override the active site for this invocation
  -v, --verbose       Enable debug logging

这三个标志在根命令上以持久标志(persistent flags)注册,因而对所有 create 子命令生效,见 cli/internal/nvkectl/root.go:

  • --site:仅对本次调用覆盖当前激活站点,不修改配置文件。源码注释明确写到“overrides the active site for this invocation only, without touching the config file”,实际解析发生在 resolveSiteConfig 中(见 root.go)。在多站点环境下,这是最安全的“临时切站点”方式。
  • -v, --verbose:打开调试日志。
  • --json-logs:把日志以 JSON 输出,便于采集与结构化检索。

需要注意,--site 只在需要访问后端时才有意义;--dry-run=client 会完全跳过站点解析。

三、nvkectl create cluster 与 create nodepool:控制器 API 路径

这两个子命令共享同一套形态:必填 --filename,读取 YAML(或 stdin),反序列化为控制器生成的结构体,然后调用 REST 客户端。参考页给出的示例如下:

nvkectl create cluster -f examples/capd-cluster.yaml
cat examples/capd-cluster.yaml | nvkectl create cluster -f -

nodepool 同理:

nvkectl create nodepool -f pool.yaml
cat pool.yaml | nvkectl create nodepool -f -

3.1 参数与行为

标志 简写 是否必填 取值 说明
--filename -f 是 文件路径或 - - 表示从标准输入读取
--help -h 否 — 帮助

源码中对 --filename 调用 MarkFlagRequired,缺失时 Cobra 会直接报 required flag(s) "filename" not set(见 clusters/create.go)。

3.2 请求文档必须匹配控制器 Schema

create cluster 的 Long 文本明确要求:spec 字段必须与控制器 CreateClusterRequest schema 完全一致,并提示可以访问管理面的 /api/docs 查看定义。这一点与三类站点级资源形成对照——后者由 CLI 做结构校验,前者把字段校验交给控制器/服务端。

创建成功后,输出不是完整对象,而是两条人类可读信息:

Cluster "my-cluster" created (id: ...)
Provisioning is async — run 'nvkectl get clusters' to check status.

见 clusters/create.go。nodepool 的输出类似,但提示的查询命令是 nvkectl get nodepools --cluster <name>(见 nodepools/create.go)。这直接说明了一个重要事实:供应是异步的,create 返回成功只代表请求被接受。

3.3 示例文档的关键字段

仓库中的 cli/examples/capd-cluster.yaml 展示了本地 CAPD(Docker 支持的 CAPI)集群的最小请求:

name: my-cluster
controlPlaneKubernetesVersion: 1.36.3
cniPlugin: ovn-kubernetes
etcdEncrypted: false

controlPlaneConfig:
  desiredCount: 1
  systemReservedMemory: "1Gi"
  infra:
    preKubeadmCommands:
      - "mkdir -p /run/cluster-api"
      - "echo success > /run/cluster-api/bootstrap-success.complete"
    machineTemplate:
      apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
      kind: DockerMachineTemplate
      spec:
        template:
          spec:
            customImage: cwe-capd-kindest-node:local
            extraMounts:
              - containerPath: /var/run/docker.sock
                hostPath: /var/run/docker.sock

nodePools:
  - name: worker
    nodeNamePrefix: w
    desiredNodeCount: 1
    controlPlane: false

真实 NICo 站点的示例见 cli/examples/nico-cluster.yaml,其中要求替换所有 UUID 与 orgID,且 release 必须指向站点上已发布的 release、configProfile 必须指向管理面已存在的配置档案。注意 controlPlaneConfig.nico.instanceTypePreference 是有序列表:索引 0 为首选机型,控制平面节点拿不到容量时会沿列表下移并停留在成功供应的机型上。

四、nvkectl create machineimage:发布不可变节点镜像

从这一节开始,命令进入“可移植文档”模型。参考页对该命令的描述是“Create a machine image: an immutable node image and its NICo boot script”,别名 machineimages。

4.1 参数集合(三类发布资源共用)

      --dry-run string    Use "client" to print the complete manifest without contacting the management cluster (default "none")
  -f, --filename string   Path to the YAML or JSON request, or '-' to read from stdin
  -h, --help              help for machineimage
  -o, --output string     Output format: json or yaml (default: a one-line message, or yaml with --dry-run)

这些标志在同一个构造函数里注册,因此 machineimage、clusterrelease、nodepoolrelease 三者完全一致,见 cli/internal/operator/command/create.go。

  • --filename/-f:必填;- 从 stdin 读取。
  • --dry-run:只接受 none(默认)或 client。其他值(如 server)会返回 invalid --dry-run value ... 错误,参数校验发生在构造请求体之前(见 create.go,测试见 TestCreateInvalidFlags)。
  • --output/-o:只接受 json 或 yaml;xml 之类会报 unknown output format "xml"。解析逻辑位于 cli/internal/util/printer.go。

4.2 请求文档只有一个 metadata 和一个 spec

参考页用一段代码块规定了文档形状:

metadata:
  name: <name>
spec:
  ...

并说明 nvkectl 会自行提供 apiVersion 与 kind,因此不要在文档里写它们;未知字段会被拒绝。这一约束在 cli/internal/operator/decode.go 的 rejectUnknownTopLevel 中实现:只要顶层出现 metadata、spec 之外的键(排序后取第一个报错),就抛出形如 unknown field "apiVersion": a request document has only metadata and spec; nvkectl supplies apiVersion, kind and the rest 的错误。

metadata 的严格解码规则(见 decode.go):

  • metadata 必填;
  • metadata.name 必填;
  • name 必须通过 validation.IsDNS1123Subdomain 校验,Not_A_Name 之类会失败;
  • metadata 中只允许 name 与 annotations;namespace、resourceVersion 等 Kubernetes 传输字段会报 invalid metadata,测试中对此有专门用例。

spec 的严格解码规则(见 decode.go):先把 raw 解码进该 kind 的类型化 Spec 以拒绝未知字段和类型错误,再把原始通用文档转成 JSON 后回填为 map[string]any 发送出去。注释解释了为什么不直接发送类型化值——那会把操作者省略的每个字段补成零值,从而“凭空多出字段”。

还有一个容易被忽略的细节:字段名区分大小写。decodeStrict 使用 sigs.k8s.io/json 的 UnmarshalStrict,注释明确指出与 encoding/json 不同,键名大小写不符会被视为未知字段(见 decode.go)。因此 Architecture: amd64 或 machineImageRef.Name: ... 都会失败。

4.3 一个完整的 MachineImage 示例

仓库中的 cli/examples/machineimage.yaml 是可直接使用的模板:

metadata:
  name: example-ubuntu-24.04-amd64
spec:
  operatingSystem:
    family: ubuntu
    version: "24.04"
  architecture: amd64
  buildDate: "2026-03-23"
  nico:
    ipxeScript: |
      #!ipxe
      # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
      # SPDX-License-Identifier: Apache-2.0

      set boot_assets https://boot.example.com/ubuntu-24.04-amd64

      kernel ${boot_assets}/vmlinuz console=tty0 console=ttyS0,115200 ip=dhcp url=https://releases.example.com/ubuntu-24.04-live-server-amd64.iso autoinstall ds=nocloud-net;s=${cloudinit-url} initrd=initrd
      initrd ${boot_assets}/initrd
      boot

字段含义可对照管理引擎的 API 类型 MachineImageSpec(见 management-engine/apis/v1/machineimage_types.go):

字段 必填 校验规则(来自 kubebuilder 标记)
spec.operatingSystem.family 是 1–63 字符,模式 ^<a href="https://link.gitcode.com/i/0d0cb00f16885221daf71d1bc57fdf34" target="_blank">a-z0-9</a>?$,如 ubuntu
spec.operatingSystem.version 是 1–32 字符,模式 ^[0-9]+(\.[0-9]+)*$,如 24.04
spec.architecture 是 1–32 字符,小写模式,如 amd64、arm64
spec.variant 否 ≤63 字符,允许点号,如 fde(全盘加密变体)
spec.buildDate 是 Format=date,即 YYYY-MM-DD,建议加引号
spec.nico.ipxeScript 是 1–16384 字符;必须以独立的 #!ipxe 行开头;#!ipxe 之后必须还有命令;Go 模板分隔符必须配对

ipxeScript 是一段按机器渲染的 Go 模板,渲染结果会成为 NICo 机器资源的 iPXE 脚本,因此示例里出现了 ${boot_assets}(iPXE 变量)与 {{ .DiskSelectionStrategy }}(Go 模板变量)两种不同语法的并存——前者由 iPXE 解释,后者由 NVKE 渲染。NICoBoot 的定义与校验标记见 machineimage_types.go。

4.4 不可变性与命名策略

参考页强调:已发布内容是只读的。想改变镜像供应的内容,必须换一个新名字重新创建;用已存在的名字创建会失败。这种失败在客户端对应 operator.ErrAlreadyExists(见 cli/internal/operator/operator.go),并有测试 TestCreateExistingNameFails 覆盖。

因此“改镜像”的实际操作是:复制文档 → 改 metadata.name → 重新发布 → 让新的 release 指向新镜像名。这解释了为什么仓库文档反复强调“一次发布一个名字”,而不是就地编辑。

五、nvkectl create clusterrelease 与 create nodepoolrelease:发布控制平面与 worker 版本

这两个命令的文档结构、参数与错误语义与 machineimage 完全一致,差别集中在 spec 内容与“必须先有镜像”的前置检查。

5.1 ClusterRelease 示例与注解约定

cli/examples/clusterrelease.yaml 的骨架是:

metadata:
  name: example-ubuntu-24.04-amd64
  annotations:
    internal.nvke.nvidia.com/machine-init-kubernetes-version: "1.36.3"
    internal.nvke.nvidia.com/machine-init-kubernetes-version-injected: "false"
spec:
  machineImageRef:
    name: example-ubuntu-24.04-amd64
  machineInit:
    userDataTemplate: |
      #cloud-config
      ...

文件顶部注释说明:machineImageRef 与 machineInit 一经发布即不可变,需要改动就换新名字;唯一可以原地变化的是 lifecycle,通过 nvkectl deprecate clusterrelease <name> 修改。

两个注解说明了 machineInit.userDataTemplate 支持的 Kubernetes 版本,且它们只描述模板本身:

  • "" 与 "true":模板通过 {{ .KubernetesVersion }} 从控制器获取版本,因此支持任意版本;
  • "1.36.3" 与 "false":模板只针对该版本编写。

userDataTemplate 使用 Go 模板渲染,示例中直接展示了可用的模板变量:.Hostname、.Username、.HashedPassword、.Files(含 Path/Owner/Permissions/Encoding/Content)、.CNIImagePackage、.CNIChartsPackage,以及 quote 函数。这些变量与控制器侧渲染上下文对应,模板最终写入机器的 CAPI bootstrap Secret。

结构与校验对应 ClusterReleaseSpec(见 management-engine/apis/v1/clusterrelease_types.go)与 MachineInit(见 management-engine/apis/v1/common_types.go):

  • machineImageRef:不可变(XValidation: self == oldSelf);
  • machineInit:不可变;
  • lifecycle:可选,默认 {};
  • machineInit.userDataTemplate:1–65536 字符,不能全为空白,必须以 cloud-init 头开始(#cloud-config\n、#!、#include、#cloud-boothook、#part-handler、#cloud-config-archive 或 Content-Type:),Go 模板分隔符必须配对。

ReleaseLifecycle 提供三个字段(见 common_types.go):deprecated(默认 false)、supportedUntil、hidden。其中 deprecated 的语义是“已依赖它的资源可以继续使用,但新建集群或节点池引用它会被拒绝”——这正是“只允许原地改 lifecycle”的原因。

5.2 NodePoolRelease 的额外字段

NodePoolReleaseSpec 在 release 基础上多了两个字段(见 management-engine/apis/v1/nodepoolrelease_types.go):

字段 必填 约束
profile 否 资源名;不可变;标识面向租户的 profile 族
tag 否 1–128 字符,模式 ^[a-zA-Z0-9_][a-zA-Z0-9_.-]{0,127}$;不可变;标识 profile 内的修订

示例文档 cli/examples/nodepoolrelease.yaml 与 clusterrelease 示例的 spec 几乎相同,这符合设计:一个镜像可以支撑多个 release,而初始化差异体现在 release 上,而不是镜像上。

5.3 发布顺序:先镜像,再 release

这是 create 唯一带业务前置检查的地方。源码注释写得很直白:release 必须指向一个已经存在的 machine image,所以必须先创建镜像;检查与创建是两个请求,因此中途被删除的镜像不会被发现,且该检查需要调用方对 machineimages 具备 get 权限(见 cli/internal/operator/command/create.go)。

实现上,checkMachineImageExists 从 Spec["machineImageRef"]["name"] 取名字;若为空则跳过;否则调用 c.Get(KindMachineImage, name),遇到 not found 时给出可操作提示:

machine image "img-1" not found: create it first with 'nvkectl create machineimage'

测试 TestCreateReleaseNeedsItsMachineImage 断言此时只发生一次 get MachineImage/img-1、不发生任何 create;TestCreateReleaseReportsAFailedImageLookup 则验证权限错误(ErrForbidden)会以 checking machine image "img-1" 包裹后返回。MachineImage 自身不做引用检查(TestCreateMachineImageChecksNoReference)。

六、--dry-run=client:不接触管理集群的完整清单渲染

参考页用专门段落解释了该模式:

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.

用一句话概括:它只做结构校验并补齐 apiVersion/kind,不做服务端准入校验,也不解析镜像引用。

6.1 行为细节

代码路径(见 create.go):

  1. 解析 -o;
  2. 读取文档(文件或 stdin);
  3. operator.Decode 严格校验结构;
  4. 若 dryRun == "client",当输出格式为默认 table 时自动切换为 YAML,然后打印 operator.Manifest(kind, res) 并返回——此时不会调用 clientFn()。

Manifest 只补三项容器字段,不添加任何 GitOps 注解(见 decode.go):apiVersion 取 internal.nvke.nvidia.com/v1,kind 取资源类型,metadata 仅保留 name 与非空 annotations。测试 TestCreateDryRunClientPrintsAManifestAndNeedsNoSite 明确断言:客户端工厂一旦被调用就判失败,输出里含 apiVersion: internal.nvke.nvidia.com/v1、kind: MachineImage,多行脚本保持 ipxeScript: | 可读形式,且不出现 annotations/argocd。

6.2 一个高价值的实际用法:生成 bootstrap 用的完整清单

sitespec 文档 指出:infra.nico.releases 下的每个文件必须是带 apiVersion 与 kind 的完整清单,而 releases/ 中给 nvkectl create 用的请求文档恰恰没有这两项。于是推荐用 dry-run 生成:

mkdir bootstrap-releases
nvkectl create machineimage -f releases/machineimage.yaml --dry-run=client -o yaml > bootstrap-releases/machineimage.yaml
nvkectl create clusterrelease -f releases/clusterrelease.yaml --dry-run=client -o yaml > bootstrap-releases/clusterrelease.yaml

这份转换完全在本地完成,不需要任何可达站点,因此可以放进 CI 或离线发布流水线。注意两个目录必须分开保存:releases/ 存请求文档,bootstrap-releases/ 存完整清单。

6.3 dry-run 也会失败

结构错误在 dry-run 阶段同样会被拒绝(TestCreateDryRunStillRejectsABadDocument):未知字段 arch 会直接报错。这是设计意图——把能提前发现的错误尽量提前。

七、输出格式:-o json|yaml 与默认表格

三类发布资源使用统一的打印工具(见 cli/internal/util/printer.go):

  • 不传 -o:table(默认)→ 打印一行人类可读消息,例如 Machine image "img-1" created;
  • -o json:以两空格缩进输出 JSON;
  • -o yaml:先 Marshal 为 JSON 再转 YAML,这样字段名会使用 json: 结构标签,而不是退化成小写的 Go 字段名。

值得注意的是,非 table 格式下输出的是可移植形态(只有 metadata 与 spec),而不是 dry-run 时的完整清单。测试 TestCreateOutputJSONIsThePortableShape 断言 JSON 顶层的键恰好是 metadata 与 spec。因此:

场景 建议命令 得到的内容
只想确认创建成功 nvkectl create machineimage -f x.yaml 一行提示
想拿到服务端存储后的对象 ... -o json / -o yaml metadata + spec
想得到可 kubectl apply 的清单 ... --dry-run=client -o yaml apiVersion + kind + metadata + spec

八、错误与边界:从测试用例读命令契约

create 的边界行为被测试固定得非常细,理解这些等价于掌握了它的“隐性文档”:

情况 结果
顶层出现 apiVersion 或 kind unknown field "apiVersion" / unknown field "kind"
metadata 中写 namespace 或 resourceVersion invalid metadata
缺少 metadata / 缺少 metadata.name metadata is required / metadata.name is required
metadata.name 非法 is not a valid name
缺少 spec spec is required
spec 有未知字段(含大小写错误、嵌套未知字段) unknown field ...
spec 字段类型错误(如 architecture: [amd64]) invalid spec
文档是列表 one mapping with a metadata and a spec, not a list; create one resource per run
文档含多个 YAML 文档 multiple YAML documents
空文档 empty document
文件不存在 opening "...": ...
名字已存在 operator.ErrAlreadyExists
--dry-run 非 client/none invalid --dry-run value
-o 非 json/yaml unknown output format
缺少 -f required flag(s) "filename" not set

这些用例集中在 cli/internal/operator/decode_test.go 与 cli/internal/operator/command/command_test.go。其中一个值得注意的设计细节:metadata.name 之外的传输字段被拒绝,是因为可移植模型刻意不暴露 Kubernetes 存储细节——operator.Metadata 的注释写明它只包含操作者选择的内容,namespace、uid、resourceVersion 不属于其中(见 cli/internal/operator/operator.go)。

另外,TestDecodeKeepsAnUnquotedBuildDateAsWritten 与 TestDecodeKeepsAQuotedBuildDateAsWritten 说明 buildDate 无论加不加引号都会按原样传递,这避免了 YAML 把日期解析为时间类型后再被强行转换的问题;在日常实践中仍建议加引号,以消除歧义。

九、端到端工作流:发布一套镜像与 release,再创建集群

把上述内容串起来,一个完整、可复现的流程如下(示例文件均来自 cli/examples):

# 1. 准备三份文档,三者的 metadata.name 必须一致,两个 release 的 machineImageRef.name 指向该名字
cp cli/examples/machineimage.yaml     ./machineimage.yaml
cp cli/examples/clusterrelease.yaml  ./clusterrelease.yaml
cp cli/examples/nodepoolrelease.yaml ./nodepoolrelease.yaml

# 2. 本地先校验结构并查看最终清单(不需要站点,不需要网络)
nvkectl create machineimage     -f machineimage.yaml     --dry-run=client -o yaml
nvkectl create clusterrelease   -f clusterrelease.yaml   --dry-run=client -o yaml
nvkectl create nodepoolrelease  -f nodepoolrelease.yaml  --dry-run=client -o yaml

# 3. 按顺序发布:镜像在前,release 在后
nvkectl create machineimage     -f machineimage.yaml
nvkectl create clusterrelease   -f clusterrelease.yaml
nvkectl create nodepoolrelease  -f nodepoolrelease.yaml

# 4. 确认发布结果
nvkectl get machineimages

# 5. 创建租户集群与节点池(异步供应)
nvkectl create cluster -f nico-cluster.yaml
nvkectl get clusters
nvkectl create nodepool -f pool.yaml
nvkectl get nodepools --cluster <cluster-name>

需要注意的推论与限制:

  • 第 3 步顺序不可颠倒,否则会得到 machine image ... not found;
  • 若管理集群尚未安装三类 release CRD,客户端会返回 operator.ErrCRDsNotInstalled(“the release CRDs are not installed on the management cluster”,见 operator.go),此时应先完成管理面安装;
  • 想改已发布内容只能换名重发;想下线一个 release,应使用 nvkectl deprecate 而不是重新创建;
  • 想临时操作其他站点,用 --site <name>,不要改全局配置。

十、实现视角:create 背后的四层结构

把源码按调用链梳理,nvkectl create 可以清楚分成四层:

  1. 命令层:cli/internal/nvkectl/create.go 组装子命令;cli/internal/operator/command/create.go 定义 flags 与执行流程;ing 控制器类子命令在 clusters/create.go 与 nodepools/create.go。
  2. 模型层:cli/internal/operator/decode.go 负责严格解码与清单投影,operator.Kind、Resource、Metadata 在 operator.go 中定义。
  3. 客户端层:operator.Client 是与后端无关的接口(创建、读取、列表、删除、置为废弃);Dynamic 是其基于管理集群 dynamic client 的实现,三类资源均为 cluster-scoped,请求带 fieldValidation=Strict(见 cli/internal/operator/dynamic.go),单次请求上限 30 秒。
  4. 工具层:cli/internal/util/input.go 统一处理“文件或 stdin”,cli/internal/util/printer.go 统一处理输出格式与校验。

这四层分离解释了为什么文档中会出现看似矛盾的两句话:CLI 既“严格拒绝未知字段”(解码层的 UnmarshalStrict),又“把必填字段与取值规则留给管理集群”(服务端准入层)。前者保证请求形状正确、不会因为拼写错误静默丢字段;后者保证资源语义符合 CRD 与 RBAC 要求。

十一、参考文档的维护方式:为什么不要直接改页面

参考页顶部有一段醒目声明:该文件是生成产物,内容来自 cli/internal/nvkectl 的命令树;要改文案就改命令自身的 Short、Long、Example、Args、Aliases、废弃说明或 flag 用法字符串,然后运行 make -C cli docs;直接改页面会在下次生成时丢失,且 make -C cli docs-check 会失败直到提交的页面与命令树重新一致。

这解释了一个设计选择:命令树放在可导入的包 cli/internal/nvkectl 而不是 cmd/nvkectl 中,正是为了让文档生成器能导入并遍历同一棵树(见 root.go)。构造命令树时不读配置、不连任何后端,因此生成文档不需要 kubeconfig 或可达集群。若你在扩展 create 子命令后发现参考页与行为不符,正确的修复点是命令的元数据,而不是 Markdown。

小结

nvkectl create 表面只是“创建资源”的一个聚合入口,实际承载了两套完全不同的契约:cluster/nodepool 把 YAML 直接交给控制器 API,成功即返回异步供应提示;machineimage/clusterrelease/nodepoolrelease 则通过“只有 metadata 与 spec 的可移植文档”发布不可变内容,CLI 负责严格结构校验、补齐 apiVersion/kind,并在发布前确认 release 引用的镜像确实存在。掌握 --dry-run=client 的“结构校验 + 完整清单输出”这两重能力,就能在无站点、无网络的条件下先把发布物准备好,再按“镜像 → release → 集群/节点池”的顺序落地,把一次性的手工操作变成可审计、可复现的发布流程。

登录后查看全文
nvke