
Velero v0.5.0ark restore create命令详解恢复创建参数、默认值与底层实现原理【免费下载链接】veleroBackup and migrate Kubernetes applications and their persistent volumes项目地址: https://gitcode.com/GitHub_Trending/ve/veleroVelero 在 0.5.0 版本中 CLI 命令仍沿用ark前缀后续版本已重命名为velero。本文以 v0.5.0 官方文档 ark restore create 为主体逐条解析该命令的完整参数、默认值与格式约束并结合当前仓库的 CLI 源码说明这些参数如何被解析、如何最终构建成 Kubernetes 中的 Restore 对象帮助读者既看懂历史文档又能对照源码理解参数生效机制。命令概览与基本用法ark restore create的作用是为集群提交一次恢复restore请求。v0.5.0 文档给出的基本语法为ark restore create BACKUP [flags]其中BACKUP是位置参数指定要恢复的备份名称。该命令属于ark restore子命令族Work with restores同族还包括get、logs、delete等子命令参见 ark restore 文档。对照当前仓库源码可以看到恢复子命令族在 restore 子命令注册处 挂载了create、get、logs、describe、delete五个子命令与 v0.5.0 时期文档结构一致说明该命令族的核心组织方式延续了多年。完整参数列表v0.5.0 官方文档原文以下是 v0.5.0 文档中列出的全部命令选项--exclude-namespaces stringArray namespaces to exclude from the restore --exclude-resources stringArray resources to exclude from the restore, formatted as resource.group, such as storageclasses.storage.k8s.io -h, --help help for create --include-cluster-resources optionalBool[true] include cluster-scoped resources in the restore --include-namespaces stringArray namespaces to include in the restore (use * for all namespaces) (default *) --include-resources stringArray resources to include in the restore, formatted as resource.group, such as storageclasses.storage.k8s.io (use * for all resources) --label-columns stringArray a comma-separated list of labels to be displayed as columns --labels mapStringString labels to apply to the restore --namespace-mappings mapStringString namespace mappings from name in the backup to desired restored name in the form src1:dst1,src2:dst2,... -o, --output string Output display format. For create commands, display the object but do not send it to the server. Valid formats are table, json, and yaml. --restore-volumes optionalBool[true] whether to restore volumes from snapshots -l, --selector labelSelector only restore resources matching this label selector (default none) --show-labels show labels in the last column从源码结构看v0.5.0 的这些参数与当前版本的 CreateOptions 结构体 一脉相承IncludeNamespaces、ExcludeNamespaces、IncludeResources、ExcludeResources、NamespaceMappings、Selector、RestoreVolumes、IncludeClusterResources等字段在今天的 BindFlags 中仍然逐一注册只是新增了许多 v0.5.0 时代尚未存在的参数如--from-backup、--from-schedule、--existing-resource-policy等。关键参数逐项解析命名空间过滤--include-namespaces/--exclude-namespaces两者均为stringArray类型可重复传入或用逗号分隔。--include-namespaces默认值为*表示恢复所有命名空间。这一点在源码中可以直接确认NewCreateOptions 里初始化IncludeNamespaces: flag.NewStringArray(*)即不显式指定时默认全量恢复。若同时使用 include 与 exclude语义为在 include 的范围内剔除 exclude 的命名空间。资源类型过滤--include-resources/--exclude-resources资源必须写成resource.group格式例如storageclasses.storage.k8s.io*表示所有资源。注意带 group 的完整写法是硬性约定不带 group 的简写如仅写pods在 Velero 的资源过滤中不会被识别为pods这一点在文档示例中反复强调了resource.group的格式要求。这两个值最终映射到 Restore 对象的includedResources/excludedResources字段对应当前 RestoreSpec 中的IncludedResources与ExcludedResources注释说明If empty, all resources in the backup are included。当前版本的命令示例见 create 命令 Example演示了一个典型场景velero restore create --from-backup backup-2 --include-resources persistentvolumeclaims,persistentvolumes仅恢复备份中的 PVC 和 PV 资源。三态布尔--restore-volumes与--include-cluster-resourcesoptionalBool这两个参数是 v0.5.0 文档中值得特别理解的类型optionalBool[true]即可选布尔。它不是普通的 true/false而是三态不传该参数—— 值为nil表示未指定由服务端按默认逻辑处理include-cluster-resources按 API 文档默认true传--restore-volumes不带值—— 通过NoOptDefVal机制被解释为true显式传值——--restore-volumesfalse表示明确关闭。这一机制在 optional_bool.go 中实现OptionalBool内部是*bool指针Set方法通过strconv.ParseBool解析而在 BindFlags 中可以看到f.NoOptDefVal cmd.TRUE的用法配合注释说明允许用户直接写--restore-volumes作为--restore-volumestrue的简写。具体语义上--restore-volumes文档默认提示true控制是否从快照恢复卷。若恢复时不希望重建 PV 数据例如只恢复元数据需显式传--restore-volumesfalse。该值最终写入 RestoreSpec 的RestorePVs字段*bool见 restore_types.gonil 与 false 是两种不同状态。--include-cluster-resources控制是否纳入集群级资源如 StorageClass、ClusterRole 等不属于任何命名空间的资源API 层面默认true见 IncludeClusterResources 字段注释If null, defaults to true。命名空间重映射--namespace-mappings格式为src1:dst1,src2:dst2,...即备份中的命名空间名:恢复后的目标命名空间名用逗号分隔多条映射。未在映射中出现的源命名空间会恢复到同名命名空间。其解析器是 flag.Map默认以,为条目分隔符、为键值分隔符而NamespaceMappings在初始化时特意改用了.WithEntryDelimiter(,).WithKeyValueDelimiter(:)也就是以:分隔键值——这正是src:dst语法的来源。值得注意的是Set方法使用了csv.Reader见 map.go#L75-L93因此值中若需包含分隔符可以用引号包裹。该参数对应 RestoreSpec 的namespaceMapping字段map[string]string见 restore_types.go是跨命名空间迁移DR/迁移场景下把dev恢复成prod的核心手段。标签与选择器--labels/-l --selector--labels为mapStringString类型k1v1,k2v2格式默认分隔符即,与标签会直接应用到 Restore 对象本身的metadata.labels上——这一点在 Run 方法构建 Restore 时可以直接看到Labels: o.Labels.Data()写入ObjectMeta。-l / --selector为标准 Kubernetes 标签选择器语法keyvalue, key in {a,b}过滤的是备份内部要恢复哪些资源对象而不是给 Restore 对象打标。源码中它绑定到Selector flag.LabelSelector见 BindFlags#L151并映射到 RestoreSpec 的LabelSelector字段。从 Validate 方法 可以看到--selector与当前版本的--or-selector互斥不能同时指定——体现了选择器类参数的排他性校验模式。展示类参数-o、--show-labels、--label-columns-o, --output取值table、json、yaml。v0.5.0 文档特别注明对 create 类命令指定-o时只显示将要创建的对象而不真正提交到服务器这为干跑校验 Restore 配置提供了手段。该行为由 output 包 的PrintWithFormat实现见 Run 方法if printed, err : output.PrintWithFormat(c, restore); printed || err ! nil { return err }printed 为真时提前返回不执行Create。--show-labels/--label-columns影响ark get等列表输出的标签列展示对 create 命令主要用于返回对象的展示。继承自父命令的全局选项v0.5.0 文档还列出了继承自根命令的选项--alsologtostderr log to standard error as well as files --kubeconfig string Path to the kubeconfig file to use to talk to the Kubernetes apiserver. If unset, try the environment variable KUBECONFIG, as well as in-cluster configuration --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) --log_dir string If non-empty, write log files in this directory --logtostderr log to standard error instead of files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) -v, --v Level log level for V logs --vmodule moduleSpec comma-separated list of patternN settings for file-filtered logging其中与日常使用最相关的是--kubeconfig未设置时依次尝试环境变量KUBECONFIG与集群内配置in-cluster这意味着 CLI 既能在本地配合 kubeconfig 使用也能在 Pod 内以 ServiceAccount 方式运行-v日志级别配合--logtostderr可用于排查 CLI 与 API Server 的通信问题。从参数到 Restore 对象创建流程的源码视角虽然 v0.5.0 时代的实现细节未随当前仓库保留但从源码结构看restore create的整体执行模型完整/部分失败校验除外与今天的 create.go 高度一致可以据此理解 v0.5.0 命令的工作方式Complete 阶段处理位置参数。v0.5.0 中BACKUP为必填位置参数直接作为恢复源名称当前版本中位置参数改为可选的恢复名称缺省名生成规则见 CompletesourceName-时间戳。Validate 阶段校验参数合法性并确认备份存在。当前实现对--from-backup会执行一次Get确认备份对象存在见 Validatev0.5.0 时期对位置参数BACKUP做的是同类存在性校验。Run 阶段把各 flag 组装成Restore对象v0.5.0 的--include-namespaces→includedNamespaces、--restore-volumes→restorePVs等字段映射关系在 Run 方法 中可以逐行对照然后调用client.Create提交到 API Server输出Restore request %q submitted successfully.恢复的实际执行由服务端 Restore 控制器异步完成客户端可随时用ark get restores查看进度。提交后的恢复对象会经历New → InProgress → Completed / PartiallyFailed / Failed / FailedValidation等阶段阶段枚举见 RestorePhase 定义。当前版本的-w/--wait选项会通过 watch Restore 资源实时等待终态见 Run 方法等待循环并提示可用velero restore describe name与velero restore logs name查看详情——v0.5.0 用户在没有 wait 选项的时代则通过反复执行ark get restores达到同样效果。版本差异提示与延伸阅读需要强调的适用前提本文参数表严格对应v0.5.0 版本ark前缀、BACKUP位置参数必填。当前仓库源码代表的是演进后的velero restore create主要差异包括命令更名为velero restore create恢复源通过--from-backup/--from-schedule指定位置参数变为可选的恢复名称新增--allow-partially-failed、--preserve-nodeports、--existing-resource-policy、--existing-volume-data-policy、--resource-modifier-configmap、--resource-policies-configmap、--item-operation-timeout、--write-sparse-files、--parallel-files-download、--delete-extra-files等参数完整清单见 BindFlags--selector之外新增了--or-selector多选择器 OR 语义两者互斥。若你使用的是 v0.5.0 二进制请以本文为准若升级到新版建议以新版velero restore create --help输出为准并参考仓库中 restore create 的测试用例 了解各参数的行为边界。SEE ALSOv0.5.0 文档原列相关命令ark restore — Work with restoresark restore get、ark restore logs、ark restore deleteark backup create — 创建恢复所依赖的备份【免费下载链接】veleroBackup and migrate Kubernetes applications and their persistent volumes项目地址: https://gitcode.com/GitHub_Trending/ve/velero创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考