Search code examples
kubernetesconfigmap

Create the YAML file for a ConfigMap without creating the corresponding Kubernetes resource


Question

Is there a way to get the YAML manifest file of a configmap, without creating the corresponding Kubernetes resource?

At the moment I'm doing this:

kubectl create configmap <name> --from-file="<name>"
kubectl get configmap <name> -o yaml > <name>.yaml
kubectl delete configmap <name>

Is there some simple way to achieve this without having to create a temporary resource in the Kubernetes cluster?

Example

Directory structure:

myheader/
  +- handler.lua
  +- schema.lua

File handler.lua:

local MyHeader = {}

MyHeader.PRIORITY = 1000
MyHeader.VERSION = "1.0.0"

function MyHeader:header_filter(conf)
  -- do custom logic here
  kong.response.set_header("myheader", conf.header_value)
end

return MyHeader

File schema.lua:

return {
  name = "myheader",
  fields = {
    {
      config = {
        type = "record",
        fields = {
          {
            header_value = {
              type = "string",
              default = "roar",
            },
          },
        },
      },
    },
  }
}

I can obtain the manifest file myheader-configmap.yaml like this:

kubectl create configmap myheader --from-file "myheader/"
kubectl get configmap myheader -o yaml > myheader-configmap.yaml
kubectl delete configmap myheader

Is there some command or anything else to obtain the same manifest file, without having to preemptively create the corresponding Kubernetes resource?


Solution

  • Yes :)

    You can use the following command:

    kubectl create configmap <name> --from-file=<file-name> --dry-run=client -o yaml
    

    If you want to save it to a file, you can just use bash redirection, e.g:

    kubectl create configmap <name> --from-file=<file-name> --dry-run=client -o yaml > filename.yaml
    

    You can specify additional argument like --namespace=default