jrhouston / tfk8s

A tool for converting Kubernetes YAML manifests to Terraform HCL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add option to sort file by resource name

AndreasBergmeier6176 opened this issue · comments

I'm open to adding a flag for this @AndreasBergmeier6176 but you could use a tool like yq to pre-process the YAML into the sort order you want, like this:

yq ea '[.] | sort_by(.metadata.name) | .[] | splitDoc' manifest.yaml | tfk8s

I'm open to adding a flag for this @AndreasBergmeier6176 but you could use a tool like yq to pre-process the YAML into the sort order you want, like this:

yq ea '[.] | sort_by(.metadata.name) | .[] | splitDoc' manifest.yaml | tfk8s

Sorry, maybe I wasn't clear enough. While I think your approach works, we semantically usually sort by Terraform Name not Kubernetes Name. So to do something along these lines we would need a tool like "hclq" - but I haven't used it yet.

Ah gotcha. You want to use the generated terraform resource name as the sort key. The name that gets generated for the terraform resource name is just a concatenation of kind metadata.namespace and metadata.name so you could still preprocess the YAML with yq to enforce that ordering, like this:

yq ea '[.] | sort_by(.kind + (.metadata.namespace // "") + .metadata.name) | .[] | splitDoc' manifest.yaml | tfk8s

I will concede that is clunky because you need to know that implementation detail of the tfk8s command. Implementing the sort in tfk8s will be kinda tricky because right now just processes the YAML linearly and spits out HCL as it goes – but it's doable, we'd just have to sort the list of YAML docs first before we process it into HCL.