kubectl-slice icon indicating copy to clipboard operation
kubectl-slice copied to clipboard

Add support to keep the triple dash at the beginning of splitted files

Open whitelion-github opened this issue 1 year ago • 7 comments

What did you do?

Simply run a split. kubectl-slice -f file.yaml -o work

If I can. Would it be possible for the output yamls, to leave the trailing --- for manifest separation.

---    # <--
apiVersion: apps/v1
kind: Deployment
metadata:

The goal here is to split in single manifest, to make it easier to costumize them, then remerge them after to one yaml file.

whitelion-github avatar Feb 03 '24 17:02 whitelion-github

Hey @whitelion-github!

Not sure I follow. Are you saying that the output you're getting has triple dashes at the beginning of the file?

If so, would you mind providing a reproducible example?

patrickdappollonio avatar Feb 03 '24 17:02 patrickdappollonio

After the split --- at the start of the yaml file is removed.

Before the split

---             #
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment1
---            #
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment2

After the split, we have 2 files:

deployment-deployment1.yaml
deployment-deployment2.yaml

If we look the content of the output yaml files, no --- anymore.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment1

whitelion-github avatar Feb 03 '24 17:02 whitelion-github

Ah I see. The triple dash is the separator. Technically, a file like this:

---
foo: bar

Is actually 2 yamls, not 1.

May I ask what's your use case scenario? Gluing them together after splitting them?

patrickdappollonio avatar Feb 03 '24 17:02 patrickdappollonio

Look at this yaml. This is a concrete example of a yaml i'm using : https://github.com/cert-manager/cert-manager/releases/download/v1.14.1/cert-manager.yaml

Use case : split this file into many kubernets yaml file (easier to manipulate manifests for personnalization) Modifiy some file to ajust some resources When personalized is done, I'm remerging these manifests file to a big yaml as originally. The difficulty I have is that I must keep the same sequence of manifests I had originally when I glue it back. Because the sequence is important when applying in kubernetes.

whitelion-github avatar Feb 04 '24 04:02 whitelion-github

I understand your use case scenario. My note is still though that having a file that contains this:

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment2

Is actually still 2 YAMLs. For a software that's supposed to "split" the YAML manifests, returning one that is technically two files means something is a bit wrong 😅

I'm happy to add an option/flag that, if turned on, will add the triple dashes at the beginning of the file for easier concatenation.

If you have a bunch of YAML manifests and you want to see merged though including the triple dash among them, there are a few options, like:

find . -name '*.yaml' -print0 | sort -z | xargs -0 awk 'BEGIN{print "---"}{print}' > merged.yaml

This will find any file with the .yaml extension in the current folder, then merge them adding a --- before each file (including the first). This should get you around until I add the command line flag.

patrickdappollonio avatar Feb 04 '24 04:02 patrickdappollonio

Side note, but you also mention this:

Because the sequence is important when applying in kubernetes.

Sequence is not that important. By default, when kubectl sees a number of resources, it will sort the known ones. Given a folder where you have a namespace, a deployment and a configmap, the namespace will always be applied first, even if it's the last file in the list:

aaa_deployment.yaml
aaa_configmap.yaml
zzz_namespace.yaml # this will always apply first, even if the file semantically is last

The same thing is true for Helm or this very project: https://github.com/patrickdappollonio/kubectl-slice/blob/db8ad0369f86cd822258cf9b2ecfa671fb088bd8/slice/kube.go#L22-L59

Unless you're really doing some advanced stuff, submitting the manifests out of order should not give you issues if they're all part of the same "apply batch".

patrickdappollonio avatar Feb 04 '24 04:02 patrickdappollonio

Agree for helm. But after doing some test, kubectl or oc based on kubectl does not follow a pre-defined order. kubectl proceed sequentially with a file. If you're apply multiple yaml file (kubectl apply -f , it will sort asc by filename and process in that order.

whitelion-github avatar Feb 07 '24 18:02 whitelion-github

Done! I've merged the code and released version 1.2.8. Please give it a go with the new flag --include-triple-dash.

I'll keep the issue open for a few days in case there are edge cases, then I will close it.

Thanks for the feature request!

patrickdappollonio avatar Mar 16 '24 04:03 patrickdappollonio

Closing, assuming this worked! If not, feel free to open a new issue!

patrickdappollonio avatar Mar 20 '24 21:03 patrickdappollonio

Done! I've merged the code and released version 1.2.8. Please give it a go with the new flag --include-triple-dash.

I'll keep the issue open for a few days in case there are edge cases, then I will close it.

Thanks for the feature request!

I've done my tests. Ajusted my code, to remove the part adding the "---" and rerun with success. Good job.

whitelion-github avatar Apr 03 '24 12:04 whitelion-github