How to create boiler plate for any k8s object

12/19/2018

I want to create boiler plate for any k8s object.

For instance, the deployment object boilerplate can be generated by using kubectl:

kubectl run --dry-run -o yaml ...

This will generate the yaml configuration file of the deployment object. I can redirect this to a file and modify the fields I need.

But how about objects other than deployment? What about CronJob? Are there any ways to generate boilerplate config file for CronJob object (or any other k8s object at that matter)?

-- Tran Triet
kubectl
kubernetes

3 Answers

12/19/2018

You can create other objects than deployment using kubectl run, but you have to specify the generator or restart flag. You can find more informations about it here - https://kubernetes.io/docs/reference/kubectl/conventions/#generators.

Regarding objects other than those available via kubectl run you can use kubectl create to see kind of objects you can create.

-- wgorczyca
Source: StackOverflow

12/19/2018

While kubectl create object-type -o yaml will give you the very basics, it doesn't normally cover much of the spec.

Instead, I prefer to fetch existing objects and modify:

kubectl get configmap configMapName -o yaml > configmap.yaml

Strip away everything you don't need, including generated fields; and you're good to go. This step probably requires a solid understanding of what to expect in each YAML.

EDIT://

I just realised there's --export when using this approach that strips generated fields for you :)

-- Rawkode
Source: StackOverflow

12/19/2018

There is no defacto standard of "base" templates to use. I am actually working on a site to curb this problem (https://k8specs.com).

What I would recommend doing is slowing accumulating your kubernetes objects like I have (see: https://github.com/mateothegreat?tab=repositories&q=k8-byexamples -- look in my manifests directories throughout the repo's) where I can use envsubst to then update the values I want to "template" using simple environment variables.

The kubernetes.io documentation has a plethora of documentation with line-by-line examples of each object definition that there is, I would recommend starting there.

If you're looking for anything specific just mention what you need and we will help you drafting your spec's!

-- yomateo
Source: StackOverflow