Skip to content

fix: support YAML 1.1 parsing in Kubernetes manifests by replacing js-yaml with yaml #2548

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@
"form-data": "^4.0.0",
"hpagent": "^1.2.0",
"isomorphic-ws": "^5.0.0",
"js-yaml": "^4.1.0",
"jsonpath-plus": "^10.3.0",
"node-fetch": "^2.6.9",
"openid-client": "^6.1.3",
"rfc4648": "^1.3.0",
"socks-proxy-agent": "^8.0.4",
"stream-buffers": "^3.0.2",
"tar-fs": "^3.0.9",
"ws": "^8.18.2"
"ws": "^8.18.2",
"yaml": "^2.8.0"
},
"devDependencies": {
"@eslint/js": "^9.18.0",
Expand Down
21 changes: 10 additions & 11 deletions src/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import yaml from 'js-yaml';
import YAML from 'yaml';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you leave this name as it was please.

import { getSerializationType } from './util.js';
import { KubernetesObject } from './types.js';
import { ObjectSerializer } from './serializer.js';
Expand All @@ -9,13 +9,12 @@ import { ObjectSerializer } from './serializer.js';
* @param opts - Optional YAML load options.
* @returns The deserialized Kubernetes object.
*/
export function loadYaml<T>(data: string, opts?: yaml.LoadOptions): T {
const yml = yaml.load(data, opts) as any as KubernetesObject;
export function loadYaml<T>(data: string): T {
const yml = YAML.parse(data, { version: '1.1' }) as any as KubernetesObject;
if (!yml) {
throw new Error('Failed to load YAML');
}
const type = getSerializationType(yml.apiVersion, yml.kind);

return ObjectSerializer.deserialize(yml, type) as T;
}

Expand All @@ -25,12 +24,12 @@ export function loadYaml<T>(data: string, opts?: yaml.LoadOptions): T {
* @param opts - Optional YAML load options.
* @returns An array of deserialized Kubernetes objects.
*/
export function loadAllYaml(data: string, opts?: yaml.LoadOptions): any[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think users may still want to pass options to the parser

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we may need to define our own types for the options passed to loadYaml(), loadAllYaml(), and dumpYaml(). These are part of the public API, and changing the underlying YAML library like this changes the actual options that are supported.

const ymls = yaml.loadAll(data, undefined, opts);
return ymls.map((yml) => {
const obj = yml as KubernetesObject;
export function loadAllYaml(data: string): any[] {
const ymls = YAML.parseAllDocuments(data, { version: '1.1' });
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this value 1.1 for version option should be default, but the user may be able to overwrite it

Copy link
Author

Choose a reason for hiding this comment

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

Yes sir @feloy, you're absolutely right

I've updated the function so that version: '1.1' is now used as the default, but users can still override it by passing their own options.

yaml.parseAllDocuments(data, { version: '1.1', ...opts }); ,

return ymls.map((doc) => {
const obj = doc.toJS() as KubernetesObject;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you keep the name as obj to keep the diff smaller.

const type = getSerializationType(obj.apiVersion, obj.kind);
return ObjectSerializer.deserialize(yml, type);
return ObjectSerializer.deserialize(obj, type);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't rename things and make unrelated changes, the diff stays smaller and easier to review. It also makes git blame easier to use too.

});
}

Expand All @@ -40,9 +39,9 @@ export function loadAllYaml(data: string, opts?: yaml.LoadOptions): any[] {
* @param opts - Optional YAML dump options.
* @returns The YAML string representation of the serialized Kubernetes object.
*/
export function dumpYaml(object: any, opts?: yaml.DumpOptions): string {
export function dumpYaml(object: any): string {
const kubeObject = object as KubernetesObject;
const type = getSerializationType(kubeObject.apiVersion, kubeObject.kind);
const serialized = ObjectSerializer.serialize(kubeObject, type);
return yaml.dump(serialized, opts);
return YAML.stringify(serialized);
}
35 changes: 35 additions & 0 deletions src/yaml_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,39 @@ spec:
// not using strict equality as types are not matching
deepEqual(actual, expected);
});

it('should parse octal-like strings as numbers (YAML 1.1 style)', () => {
const yaml = `
defaultMode: 0644
fileMode: 0755
`;
const result = loadYaml<{
defaultMode: number;
fileMode: number;
}>(yaml);

// 0644 (octal) = 420 decimal, 0755 = 493
strictEqual(result.defaultMode, 420);
strictEqual(result.fileMode, 493);
});

it('should parse boolean-like strings as booleans (YAML 1.1 style)', () => {
const yaml = `
enableFeature: yes
debugMode: ON
maintenance: no
safeMode: off
`;
const result = loadYaml<{
enableFeature: boolean;
debugMode: boolean;
maintenance: boolean;
safeMode: boolean;
}>(yaml);

strictEqual(result.enableFeature, true);
strictEqual(result.debugMode, true);
strictEqual(result.maintenance, false);
strictEqual(result.safeMode, false);
});
});