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 JSON support to kustomization files #1392

Closed
wants to merge 2 commits into from

Conversation

srfrnk
Copy link

@srfrnk srfrnk commented Jul 24, 2019

This allows you to use kustomization.json files.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 24, 2019
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jul 24, 2019
@Liujingfang1
Copy link
Contributor

I don't think this will work.
First, you need to add kustomization.json to the file name accepted by kustomize. Once this is done, kustomize run build on it.
To keep the json format of kustomization.json in kustomize edit, you need to marshal it to JSON.
There is another part related, which is more important. kustomize edit tries to keep the order and comments from the original file content. This is done with the assumption that the file is yaml. For the json file, need to do similar thing to keep the order and comments.

@srfrnk
Copy link
Author

srfrnk commented Jul 25, 2019

You're right - I forgot to squash and merge in one commit that was necessary.
Now it's complete.
I will look at the order issue as well.

@srfrnk
Copy link
Author

srfrnk commented Jul 25, 2019

Looking into the issue it appears that this is a limitation set into Go JSON/YAML parser.
Go assumes the order of JSON properties should not be kept.

Javascript for example does not behave that way:

const obj = JSON.parse('{"c":"1","b":"2","a":"3"}');
console.log(obj, JSON.stringify(obj));

Will produce:

{ c: '1', b: '2', a: '3' } '{"c":"1","b":"2","a":"3"}'

A solution for this use-case may be to use forks for marshaling/marshaling JSON such as this.
However IMHO it poses a larger threat then the issue is worth.

Since the JSON marshaling in Go sorts the properties alphabetically - it is deterministic at least.
I would think this can be an acceptable limitation for JSON kustomization files.
As a user I would be happy living with it - as opposed to not having JSON at all.
Note that after the first edit the order is maintained for every new edit.

@Liujingfang1
Copy link
Contributor

@srfrnk Let's add the support for reading from and writing to kustomization.json file for now. Add some document explaining that the order and comments of JSON kustomization file are not preserved.

@Liujingfang1 Liujingfang1 self-assigned this Jul 30, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: srfrnk
To complete the pull request process, please assign liujingfang1
You can assign the PR to them by writing /assign @liujingfang1 in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 31, 2019
@srfrnk
Copy link
Author

srfrnk commented Jul 31, 2019

@Liujingfang1 I have added the comment into the FAQ: commit
Please note another commit with other changes to docs/comments/messages that might be relevant to YAML/JSON. Please advise if these are not relevant.

Copy link
Contributor

@Liujingfang1 Liujingfang1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not adding kustomization.json to every place of kustomization.yaml is present. You can add explanation of the accepted file names only at one place, maybe in the FAQ doc where you add Order and formatting changed when using a JSON kustomization file. For other places, let's just use kustomization.yaml.

docs/FAQ.md Outdated
@@ -33,13 +33,13 @@ kustomize build --load_restrictor none $target

Example: [#1319](https://github.com/kubernetes-sigs/kustomize/issues/1319), [#1322](https://github.com/kubernetes-sigs/kustomize/issues/1322), [#1347](https://github.com/kubernetes-sigs/kustomize/issues/1347) and etc.

The fields transformed by kustomize is configured explicitly in [defaultconfig](https://github.com/kubernetes-sigs/kustomize/tree/master/pkg/transformers/config/defaultconfig). The configuration itself can be customized by including `configurations` in `kustomization.yaml`, e.g.
The fields transformed by kustomize is configured explicitly in [defaultconfig](https://github.com/kubernetes-sigs/kustomize/tree/master/pkg/transformers/config/defaultconfig). The configuration itself can be customized by including `configurations` in `kustomization.yaml`/`kustomization.json`, e.g.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add kustomization.json here since we don't have the same level support for it.

docs/fields.md Outdated
@@ -1,6 +1,6 @@
# Kustomization File Fields

An explanation of the fields in a [kustomization.yaml](glossary.md#kustomization) file.
An explanation of the fields in a [kustomization.yaml/json](glossary.md#kustomization) file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add kustomization.json here since all the following fields are in yaml format.

docs/fields.md Outdated
@@ -401,7 +401,7 @@ field.
Directory specification can be relative, absolute,
or part of a URL. URL specifications should
follow the [hashicorp URL] format. The directory
must contain a `kustomization.yaml` file.
must contain a `kustomization.yaml` / `kustomization.json` file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add kustomization.json here since we didn't do that for other accepted files like kustomization.yml, kustomization.


// Remove `kind` and `apiVersion` so as make minimal changes to original source file:
delete(k, "kind")
delete(k, "apiVersion")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the original file contains kind and apiVersion, this will also remove them.
Let's not delete those two fields.

docs/glossary.md Outdated
@@ -152,16 +152,16 @@ test or deploy) when that truth changes.
## kustomization

The term _kustomization_ refers to a
`kustomization.yaml` file, or more generally to a
`kustomization.yaml` / `kustomization.json` file, or more generally to a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add kustomization.json to this doc, since we didn't do that for kustomization.yml or kustomization.

@srfrnk
Copy link
Author

srfrnk commented Aug 2, 2019

Removed the second commit. Only the FAQ comment is added.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 31, 2019
@monopole
Copy link
Contributor

monopole commented Nov 21, 2019

it looks like this PR was never updated to handle the edit and fix command, and there are no tests showing kustomize.json usage in both commands. To support kustomize.json, we should support it fully or not at all. partial support will just be confusing.

Feel free to reopen, but the bar here has to be high. It's not just about being able to load a json file.

@monopole monopole closed this Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants