Skip to content

Commit 2928804

Browse files
Merge pull request kubernetes#1292 from soltysh/generators
Add generators section in kubectl conventions
2 parents 99e12c6 + deae72a commit 2928804

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

docs/user-guide/kubectl-conventions.md

+42-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ assignees:
88
* TOC
99
{:toc}
1010

11-
## Using `kubectl` in Reusable Scripts
11+
## Using `kubectl` in Reusable Scripts
1212

1313
If you need stable output in a script, you should:
1414

15-
* Request one of the machine-oriented output forms, such as `-o name`, `-o json`, `-o yaml`, `-o go-template`, or `-o jsonpath`
15+
* Request one of the machine-oriented output forms, such as `-o name`, `-o json`, `-o yaml`, `-o go-template`, or `-o jsonpath`
1616
* Specify `--output-version`, since those output forms (other than `-o name`) output the resource using a particular API version
1717
* Specify `--generator` to pin to a specific behavior forever, if using generator-based commands (such as `kubectl run` or `kubectl expose`)
1818
* Don't rely on context, preferences, or other implicit state
@@ -27,8 +27,46 @@ In order for `kubectl run` to satisfy infrastructure as code:
2727
* If the image is lightly parameterized, capture the parameters in a checked-in script, or at least use `--record`, to annotate the created objects with the command line.
2828
* If the image is heavily parameterized, definitely check in the script.
2929
* If features are needed that are not expressible via `kubectl run` flags, switch to configuration files checked into source control.
30-
* Pin to a specific generator version, such as `kubectl run --generator=deployment/v1beta1`
30+
* Pin to a specific [generator](#generators) version, such as `kubectl run --generator=deployment/v1beta1`
31+
32+
#### Generators
33+
34+
`kubectl run` allows you to generate the following resources (using `--generator` flag):
35+
36+
* Pod - use `run-pod/v1`.
37+
* Replication controller - use `run/v1`.
38+
* Deployment - use `deployment/v1beta1`.
39+
* Job (using `extension/v1beta1` endpoint) - use `job/v1beta1`.
40+
* Job - use `job/v1`.
41+
* ScheduledJob - use `scheduledjob/v2alpha1`.
42+
43+
Additionally, if you didn't specify a generator flag, other flags will suggest using
44+
a specific generator. Below table shows which flags force using specific generators,
45+
depending on your cluster version:
46+
47+
| Generated Resource | Cluster v1.4 | Cluster v1.3 | Cluster v1.2 | Cluster v1.1 and eariler |
48+
|:----------------------:|-----------------------|-----------------------|--------------------------------------------|--------------------------------------------|
49+
| Pod | `--restart=Never` | `--restart=Never` | `--generator=run-pod/v1` | `--restart=OnFailure` OR `--restart=Never` |
50+
| Replication Controller | `--generator=run/v1` | `--generator=run/v1` | `--generator=run/v1` | `--restart=Always` |
51+
| Deployment | `--restart=Always` | `--restart=Always` | `--restart=Always` | N/A |
52+
| Job | `--restart=OnFailure` | `--restart=OnFailure` | `--restart=OnFailure` OR `--restart=Never` | N/A |
53+
| Scheduled Job | `--schedule=<cron>` | N/A | N/A | N/A |
54+
55+
Note that these flags will use a default generator only when you have not specified
56+
any flag. This also means that combining `--generator` with other flags won't
57+
change the generator you specified. For example, in a 1.4 cluster, if you specify
58+
`--restart=Always`, a Deployment will be created; if you specify `--restart=Always`
59+
and `--generator=run/v1`, a Replication Controller will be created instead.
60+
This becomes handy if you want to pin to a specific behavior with the generator,
61+
even when the defaulted generator is changed in the future.
62+
63+
Finally, the order in which flags set the generator is: schedule flag has the highest
64+
priority, then restart policy and finally the generator itself.
65+
66+
If in doubt about the final resource being created, you can always use `--dry-run`
67+
flag, which will provide the object to be submitted to the cluster.
68+
3169

3270
### `kubectl apply`
3371

34-
* To use `kubectl apply` to update resources, always create resources initially with `kubectl apply` or with `--save-config`. See [managing resources with kubectl apply](/docs/user-guide/managing-deployments/#kubectl-apply) for the reason behind it.
72+
* To use `kubectl apply` to update resources, always create resources initially with `kubectl apply` or with `--save-config`. See [managing resources with kubectl apply](/docs/user-guide/managing-deployments/#kubectl-apply) for the reason behind it.

0 commit comments

Comments
 (0)