Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to sort file by resource name #51

Open
AndreasBergmeier6176 opened this issue Oct 20, 2022 · 3 comments
Open

Add option to sort file by resource name #51

AndreasBergmeier6176 opened this issue Oct 20, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@AndreasBergmeier6176
Copy link

No description provided.

@jrhouston
Copy link
Owner

jrhouston commented Oct 20, 2022

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

@jrhouston jrhouston added the enhancement New feature or request label Oct 20, 2022
@AndreasBergmeier6176
Copy link
Author

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.

@jrhouston
Copy link
Owner

jrhouston commented Oct 21, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants