Package: ksux
A simple way for templating kubernetes manifests.RequirementsInstallationLocalDockerHow does it work?theop.pathExampletldrksuxreadsyaml/jsonmanifests in abase/directory and applies patches to them specified in thepatches/directory. The output are patched manfests in theoutput/directoryksux-b<path_to_base_dir>-p<path_to_patches_dir>-o<output_dir>or using docker:dockerrun--rm-v/path/to/your/configs:/configstsladecek/ksuxksux-b/configs/base-p/configs/patches-o/configs/outRequirementsThis is a python package. So the only requirements arepython3andpipInstallationLocalOptional: Create and activate a virtual env.# option 1: virualvenvvirtualvenvksuxsourceksux/bin/activate# option 2: venvpython-mvenvksuxsourceksux/bin/activate# option 3: condacondacreate-nksuxpython
condaactivateksuxInstallpipinstallksuxDockeruse thedocker imageTo run the command inside a docker container, you need to make sure that all volumes are mapped to the container. Let's say that you have a following file structure:|-/home/project||-base||-patches||-outTo generate patched manifests in the/home/project/outfolder, run following command:dockerrun--rm-v/home/project:/configstsladecek/ksuxksux-b/configs/base-p/configs/patches-o/configs/outthe important part is the-vflag, which will mount your local folder as a volume to the container.How does it work?Let's say that you have many manifests in some directory (basedirectory) that you wish to patch with patches (in thepatchesdirectory).Patches could be inyamlorjsonformat (as well as your manifests). However the patchesmustadhere to following schema:name:<patch description>target:apiVersion:<apiVersion of targeted resource>kind:<kind of targeted resource>name:<name of targeted resource>ops:-name:<operation description>path:<path to the part of the manifest to be patched>value:<value which should be replaced or added>action:<add|replace|remove>enforce_integer:<Optional - bool. whether exported value must be an integer in output json. Defaults to "false">list_key:<Optional - key by which an element in list should be targeted. Defaults to "name">each patch file can be a list of patches. You can use the classic yaml format, e.g.:-name:deployment patchestarget:apiVersion:apps/v1kind:Deploymentname:webops:-name:replace_imagepath:/spec/template/spec/containers/nginx/imagevalue:nginx:1.23action:replace-name:service patchestarget:apiVersion:v1kind:Servicename:nginx-serviceops:-name:add_https_portpath:/spec/portsvalue:name:httpsport:443protocol:TCPtargetPort:443action:add-name:rename_http_portpath:/spec/ports/http/nameaction:replacevalue:new_nameor use the---separator:---name:deployment patchestarget:apiVersion:apps/v1kind:Deploymentname:webops:-name:replace_imagepath:/spec/template/spec/containers/nginx/imagevalue:nginx:1.23action:replace---name:service patchestarget:apiVersion:v1kind:Servicename:nginx-serviceops:-name:add_https_portpath:/spec/portsvalue:name:httpsport:443protocol:TCPtargetPort:443action:add-name:rename_http_portpath:/spec/ports/http/nameaction:replacevalue:new_nameThen all you need to do, is run:ksux-b<path_to_base_dir>-p<path_to_patches_dir>-o<output_dir>note: By default, ksux will output patched manifests injsonformat. If you wish to output them inyaml, provide the-e yamlflag to the command aboveThis will save all patched manifests to the output dir. You can use the--dry-runflag to print the patched manifests to stdout:ksux-b<path_to_base_dir>-p<path_to_patches_dir>--dry-runFor a list of all options see:ksux--helptheop.pathThis is the "bread and butter" of this package. Similar to kustomize path, however youcantarget list item by names of child objects. E.g. say you have a list of ports in a service:apiVersion:v1kind:Servicemetadata:labels:app:nginx-servicename:nginx-servicespec:ports:-name:new_nameport:80protocol:TCPtargetPort:80-name:httpsport:443protocol:TCPtargetPort:443selector:app:webtype:ClusterIPTo target thehttpsservice and change its name, you can specify the path:/spec/ports/https/nameand then set the value to the new name:name:service patchestarget:apiVersion:v1kind:Servicename:nginx-serviceops:-name:rename_https_portpath:/spec/ports/https/nameaction:replacevalue:new_nameYou can extend this even further. If you provide thelist_keyprop to a patch, you can target any list item with any key you wish to use. For example, lets say you have an ingress:apiVersion:networking.k8s.io/v1kind:Ingressmetadata:name:ingressnamespace:defaultannotations:nginx.ingress.kubernetes.io/rewrite-target:/spec:ingressClassName:nginxrules:-host:"domain.com"http:paths:-path:/apipathType:Prefixbackend:service:name:backendport:number:80and you wish to use a different host in each of your environments. E.g. indevenvironment, you would like the host to bedev.domain.com, instagingenvironmentstaging.domain.com, etc.You can easily write a patch for each such environment:name:ingress dev patchestarget:apiVersion:networking.k8s.io/v1kind:Ingressname:ingressops:-name:replace hostpath:/spec/rules/domain.com/hostaction:replacevalue:"dev.domain.com"list_key:"host"Ez 🙃ExampleIn the./examplesfolder there are 3 sub-folders:
-/examples/basewith deployment, service and a configmap manifests. These are the base manifests which we wish to patch
-/examples/patchescontain the patches (notice that both base kubernetes manifests and patches can be either injsonoryml/yamlformat)
-/examples/outis the output directory where the patched resources will be outputFirst, we willdry-runthe patching:ksux-bexamples/base-pexamples/patches--dry-runYou should see the patched manifests printed out to the console. Now we can run it and save the patched manifests
to theoutputfolder:ksux-bexamples/base-pexamples/patches-oexamples/outBy default, the manifests will be saved injsonformat with.jsonextension. If you wish to  save the manifests inyamlformat, simply provide the-eflag with corresponding extension. E.g.:ksux-bexamples/base-pexamples/patches-oexamples/out-eyaml