Split up Kustomize Output with yq
Kustomize is an incredibly powerful tool for integrating existing Kubernetes manifests in your own cluster. You can write a configuration file in YAML that references the manifest you want to install. That manifest can include local files as well as remote repositories.
This configuration will pull in the local
deployment.yaml as well as the
CustomResourceDefinitions that are hosted in the Gateway API repository.
When you run
kustomize build the combined YAML to configure your cluster
will be sent to
stdout. This can be piped into a
.yaml file if you
would like to examine or save the configuration.
However, Kustomize will only output the config as a single YAML file with
multiple “documents” concatenated together. If you want to split this output
across multiple files, the
yq command line
utility provides a handy way of
doing just that.
kustomize build | yq --split-exp '.metadata.name + ".yaml"' --no-doc
Here, we pipe the output of
kustomize build to
yq. Passing the
yq to split the input into separate documents. It takes an argument that
yq expression language (which is the same as
jq) to name the files based on the value in the
metadata.name field. The
--no-doc flag simply omits the
to separate YAML documents from the output.