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

Avoid emitting YAML back-references in kpt fn output #183

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ts/kpt-functions/src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const YAML_STYLE: DumpOptions = {
skipInvalid: true,
// unset lineWidth from default of 80 to avoid reformatting
lineWidth: -1,
// avoid refs because many YAML parsers in the k8s ecosystem don't support them
noRefs: true,
};

/**
Expand Down
68 changes: 67 additions & 1 deletion ts/kpt-functions/src/io_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

import { FileFormat, parse, stringify } from './io';
import { Configs, KubernetesObject } from './types';
import { Configs, JsonArray, KubernetesObject } from './types';
import { kubernetesObjectResult } from './result';

describe('read', () => {
describe('in YAML format', () => {
Expand Down Expand Up @@ -686,3 +687,68 @@ results:
});
});
});

describe('roundtrip', () => {
describe('using YAML', () => {
it('should not insert YAML references', () => {
interface Baz {
baz: number;
}

interface Foo extends KubernetesObject {
spec: {
array: Baz[];
};
}

const input =
'items: [{apiVersion: v1, kind: Foo, metadata: {name: bar}, spec: {array: [{baz: 1}]}}]';
const configs = parse(input, FileFormat.YAML);

const foo = configs.getAll()[0] as Foo;
configs.addResults(
kubernetesObjectResult('something is wrong', foo, {
path: 'spec.array',
// Note: we re-use objects from the input to trigger YAML refs to normally be created
currentValue: (foo.spec.array as unknown) as JsonArray,
suggestedValue: (foo.spec.array.concat([
{ baz: 3 },
]) as unknown) as JsonArray,
})
);

const stringified = stringify(configs, FileFormat.YAML);

// We want to verify that there are no back-references like &ref and *ref in the output
expect(stringified).toEqual(`apiVersion: v1
kind: ResourceList
metadata:
name: output
items:
- apiVersion: v1
kind: Foo
metadata:
name: bar
spec:
array:
- baz: 1
results:
- message: something is wrong
severity: error
resourceRef:
apiVersion: v1
kind: Foo
namespace: ''
name: bar
file: {}
field:
path: spec.array
currentValue:
- baz: 1
suggestedValue:
- baz: 1
- baz: 3
`);
});
});
});
6 changes: 3 additions & 3 deletions ts/kpt-functions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,13 @@ export interface Result {
*/
export type Severity = 'error' | 'warn' | 'info';

interface JsonArray extends Array<Json> {}
export interface JsonArray extends Array<Json> {}
Copy link
Contributor

Choose a reason for hiding this comment

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

All exported types need to be documented with a comment.


interface JsonMap {
export interface JsonMap {
[field: string]: Json;
}

type Json = null | boolean | number | string | JsonArray | JsonMap;
export type Json = null | boolean | number | string | JsonArray | JsonMap;

/**
* Metadata about a specific field in a Kubernetes object.
Expand Down