Skip to content

Commit

Permalink
feat: sort keys of ApiObject manifests (#67)
Browse files Browse the repository at this point in the history
Fixes #17 

Using [`json-stable-stringify`](https://github.com/substack/json-stable-stringify) to sort output alphabetically and recursively. 

Signed-off-by: campionfellin <campionfellin@gmail.com>
  • Loading branch information
campionfellin committed Mar 12, 2020
1 parent 7b1d613 commit 1fe89bd
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 248 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
],
"nohoist": [
"cdk8s/yaml",
"cdk8s/yaml/**"
"cdk8s/yaml/**",
"cdk8s/json-stable-stringify",
"cdk8s/json-stable-stringify/**"
]
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/cdk8s/lib/api-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Construct } from '@aws-cdk/core';
import { Chart } from './chart';
import { removeEmpty } from './_util';
import { resolve } from './_tokens';
import * as stringify from 'json-stable-stringify';

/**
* Metadata associated with this object.
Expand Down Expand Up @@ -121,7 +122,7 @@ export class ApiObject extends Construct {

// convert to "pure data" so, for example, when we convert to yaml these
// references are not converted to anchors.
return JSON.parse(JSON.stringify(removeEmpty(resolve(this, data))));
return JSON.parse(stringify(removeEmpty(resolve(this, data))));
}
}

7 changes: 5 additions & 2 deletions packages/cdk8s/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@aws-cdk/core": "^1.27.0",
"@aws-cdk/cx-api": "^1.27.0",
"@types/jest": "^25.1.2",
"@types/json-stable-stringify": "^1.0.32",
"@types/node": "13.7.1",
"@types/yaml": "^1.2.0",
"@typescript-eslint/eslint-plugin": "^2.20.0",
Expand All @@ -84,10 +85,12 @@
]
},
"bundledDependencies": [
"yaml"
"yaml",
"json-stable-stringify"
],
"dependencies": {
"yaml": "^1.7.2"
"yaml": "^1.7.2",
"json-stable-stringify": "^1.0.1"
},
"peerDependencies": {
"@aws-cdk/core": "^1.27.0",
Expand Down
19 changes: 19 additions & 0 deletions packages/cdk8s/test/__snapshots__/api-object.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ Array [
]
`;

exports[`printed yaml is alphabetical 1`] = `
Array [
Object {
"apiVersion": "v1",
"kind": "MyResource",
"metadata": Object {
"name": "test-my-resource-2998ce93",
},
"spec": Object {
"firstProperty": "hello",
"secondProperty": Object {
"beforeThirdProperty": "world",
"innerThirdProperty": "!",
},
},
},
]
`;

exports[`synthesized resource name is based on path 1`] = `
Array [
Object {
Expand Down
21 changes: 21 additions & 0 deletions packages/cdk8s/test/api-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ test('minimal configuration', () => {
expect(Testing.synth(stack)).toMatchSnapshot();
});

test('printed yaml is alphabetical', () => {
const app = Testing.app();
const stack = new Chart(app, 'test');

// Object keys in random order
new ApiObject(stack, 'my-resource', {
kind: 'MyResource',
apiVersion: 'v1',
spec: {
secondProperty: {
innerThirdProperty: '!',
beforeThirdProperty: 'world'
},
firstProperty: 'hello'
}
});

// Should match alphabetically-ordered snapshot
expect(Testing.synth(stack)).toMatchSnapshot();
});

test('synthesized resource name is based on path', () => {
// GIVEN
const app = Testing.app();
Expand Down
12 changes: 6 additions & 6 deletions test/test-python-app/expected/test.k8s.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: test-pod-84164e5f
spec:
containers:
- name: hello-kubernetes
image: paulbouwer/hello-kubernetes:1.7
- image: paulbouwer/hello-kubernetes:1.7
name: hello-kubernetes
ports:
- containerPort: 8080
kind: Pod
apiVersion: v1
metadata:
name: test-pod-84164e5f
12 changes: 6 additions & 6 deletions test/test-typescript-app/expected/test.k8s.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: test-pod-84164e5f
spec:
containers:
- name: hello-kubernetes
image: paulbouwer/hello-kubernetes:1.7
- image: paulbouwer/hello-kubernetes:1.7
name: hello-kubernetes
ports:
- containerPort: 8080
kind: Pod
apiVersion: v1
metadata:
name: test-pod-84164e5f
Loading

0 comments on commit 1fe89bd

Please sign in to comment.