-
Notifications
You must be signed in to change notification settings - Fork 553
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,47 @@ | ||
import yaml from 'js-yaml'; | ||
import yaml from 'yaml'; | ||
import { getSerializationType } from './util.js'; | ||
import { KubernetesObject } from './types.js'; | ||
import { ObjectSerializer } from './serializer.js'; | ||
|
||
/** | ||
* Load a Kubernetes object from YAML. | ||
* Load a single Kubernetes object from YAML. | ||
* @param data - The YAML string to load. | ||
* @param opts - Optional YAML load options. | ||
* @param opts - Optional YAML parse 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, opts?: yaml.ParseOptions): T { | ||
const yml = yaml.parse(data, { version: '1.1', ...opts }) as any as KubernetesObject; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Updated the type to ParseOptions & DocumentOptions so version: '1.1' and similar options work correctly now. Let me know if more should be added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot be sure some users won't have an interest on using options from the other ones, I would set all of them: |
||
if (!yml) { | ||
throw new Error('Failed to load YAML'); | ||
throw new Error('Failed to load yaml'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please undo this change. |
||
const type = getSerializationType(yml.apiVersion, yml.kind); | ||
|
||
return ObjectSerializer.deserialize(yml, type) as T; | ||
} | ||
|
||
/** | ||
* Load all Kubernetes objects from YAML. | ||
* Load all Kubernetes objects from a multi-document YAML string. | ||
* @param data - The YAML string to load. | ||
* @param opts - Optional YAML load options. | ||
* @param opts - Optional YAML parse options. | ||
* @returns An array of deserialized Kubernetes objects. | ||
*/ | ||
export function loadAllYaml(data: string, opts?: yaml.LoadOptions): any[] { | ||
const ymls = yaml.loadAll(data, undefined, opts); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think users may still want to pass options to the parser There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return ymls.map((yml) => { | ||
const obj = yml as KubernetesObject; | ||
export function loadAllYaml(data: string, opts?: yaml.ParseOptions): KubernetesObject[] { | ||
const ymls = yaml.parseAllDocuments(data, { version: '1.1', ...opts }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type of opts would be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i do it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we want to change the type of the return value, as it will be a breaking change for user (and is what makes the tests fail) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not. We should try to keep backwards compatibility as much as possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We initially used KubernetesObject[] for better type safety and IntelliSense — especially when accessing standard fields like apiVersion, kind, or metadata. But you're right — changing it from any[] would be a breaking change for users. So we’ve reverted back to any[] to preserve backward compatibility. |
||
return ymls.map((doc) => { | ||
const obj = doc.toJSON() as KubernetesObject; | ||
const type = getSerializationType(obj.apiVersion, obj.kind); | ||
return ObjectSerializer.deserialize(yml, type); | ||
return ObjectSerializer.deserialize(obj, type); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
/** | ||
* Dump a Kubernetes object to YAML. | ||
* Dump a Kubernetes object to a YAML string. | ||
* @param object - The Kubernetes object to dump. | ||
* @param opts - Optional YAML dump options. | ||
* @returns The YAML string representation of the serialized Kubernetes object. | ||
* @param opts - Optional YAML stringify options. | ||
* @returns The YAML string representation of the serialized object. | ||
*/ | ||
export function dumpYaml(object: any, opts?: yaml.DumpOptions): string { | ||
export function dumpYaml(object: any, opts?: yaml.ToStringOptions): 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, opts); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.