kubernetes kubectl 中的生成器是什么?

What are generators in kubernetes kubectl?

当我想通过运行ning kubectl生成yaml时,表示我应该在命令中表示--generator=something标志。

例如,要通过 kubectl 获取部署模板,我应该 运行 以下命令:

kubectl run --generator=deployment/v1beta1 nginx --image=nginx --dry-run -o yaml

在不提及 --generator 标志的情况下,CLI 以某种方式声明我应该使用适当的值(例如 run-pod/v1)提及生成器标志。

我的问题是,发电机本质上是什么?它有什么作用?它们是某种对象创建模板还是其他什么?

2016 年 1 月 commit 426ef93 针对 Kubernetes v1.2.0-alpha.8 引入。

The generators 被描述为:

Generators are kubectl commands that generate resources based on a set of inputs (other resources, flags, or a combination of both).

The point of generators is:

  • to enable users using kubectl in a scripted fashion to pin to a particular behavior which may change in the future.
    Explicit use of a generator will always guarantee that the expected behavior stays the same.
  • to enable potential expansion of the generated resources for scenarios other than just creation, similar to how -f is supported for most general-purpose commands.

并且:

Generator commands should obey to the following conventions:

  • A --generator flag should be defined. Users then can choose between different generators, if the command supports them (for example, kubectl run currently supports generators for pods, jobs, replication controllers, and deployments), or between different versions of a generator so that users depending on a specific behavior may pin to that version (for example, kubectl expose currently supports two different versions of a service generator).
  • Generation should be decoupled from creation.
    A generator should implement the kubectl.StructuredGenerator interface and have no dependencies on cobra or the Factory