What are generators in kubernetes kubectl?

5/4/2019

When I want to generate yaml by running kubectl, it denotes that I should denote --generator=something flag within the command.

For example, to get the deployment template via kubectl, I should run the below command:

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

Without mentioning --generator flag the CLI states in some kind that I should mention the generator flag with a proper value (e.g. run-pod/v1).

My question is, what is essentially a generator? What does it do? Are they some sort of object creation templates or something else?

-- Farshid
kubectl
kubernetes

1 Answer

5/4/2019

That was introduced in commit 426ef93, Jan. 2016 for Kubernetes v1.2.0-alpha.8.

The generators were described as:

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.

And:

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
-- VonC
Source: StackOverflow