-
Notifications
You must be signed in to change notification settings - Fork 552
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?
fix: support YAML 1.1 parsing in Kubernetes manifests by replacing js-yaml with yaml #2548
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Tanmayshi 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 |
Welcome @Tanmayshi! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please sign the CLA.
@@ -62,15 +62,15 @@ | |||
"form-data": "^4.0.0", | |||
"hpagent": "^1.2.0", | |||
"isomorphic-ws": "^5.0.0", | |||
"js-yaml": "^4.1.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@types/js-yaml
should also be removed. Is there a @types/yaml
that should be added in its place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will remove @types/js-yaml since it's no longer used.
Regarding @types/yaml:
I won't be adding it because the yaml package already includes its own TypeScript definitions, so there's no need for a separate type package.
If you still think adding it is necessary for a specific reason, please let me know — happy to update accordingly.
src/yaml.ts
Outdated
@@ -1,4 +1,4 @@ | |||
import yaml from 'js-yaml'; | |||
import YAML from 'yaml'; |
There was a problem hiding this comment.
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.
src/yaml.ts
Outdated
export function loadAllYaml(data: string): any[] { | ||
const ymls = YAML.parseAllDocuments(data, { version: '1.1' }); | ||
return ymls.map((doc) => { | ||
const obj = doc.toJS() as KubernetesObject; |
There was a problem hiding this comment.
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.
@@ -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[] { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
src/yaml.ts
Outdated
return ymls.map((yml) => { | ||
const obj = yml as KubernetesObject; | ||
export function loadAllYaml(data: string): any[] { | ||
const ymls = YAML.parseAllDocuments(data, { version: '1.1' }); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 }); ,
@@ -16,15 +16,15 @@ | |||
"form-data": "^4.0.0", | |||
"hpagent": "^1.2.0", | |||
"isomorphic-ws": "^5.0.0", | |||
"js-yaml": "^4.1.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think @types/js-yaml
has been removed in this file.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please undo this change.
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 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.
I started the CI. It looks like there are relevant failures. |
src/yaml_test.ts
Outdated
strictEqual(objects[2].apiVersion, 'apiextensions.k8s.io/v1'); | ||
strictEqual(objects[2].kind, 'CustomResourceDefinition'); | ||
strictEqual(objects[2].metadata!.name, 'my-crd.example.com'); | ||
// Assert specific types for each object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that an existing test broke implies to me that this may be a subtly breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests themselves were not failing — the issue was purely related to TypeScript type-checking. TypeScript was warning that some properties might be undefined or missing. I've addressed those issues, and CI is now passing
If you'd prefer a more strictly type-safe solution, please let me knowd.
src/yaml.ts
Outdated
* @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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yaml.parse
accepts options
of type ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions
. Let's the user have access to all the options, not only ParseOptions
ones (especially the version
one, which is in the DocumentOptions
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions
src/yaml.ts
Outdated
const ymls = yaml.loadAll(data, undefined, opts); | ||
return ymls.map((yml) => { | ||
const obj = yml as KubernetesObject; | ||
export function loadAllYaml(data: string, opts?: yaml.ParseOptions): KubernetesObject[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type of opts would be ParseOptions & DocumentOptions & SchemaOptions
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i do it
src/yaml.ts
Outdated
const ymls = yaml.loadAll(data, undefined, opts); | ||
return ymls.map((yml) => { | ||
const obj = yml as KubernetesObject; | ||
export function loadAllYaml(data: string, opts?: yaml.ParseOptions): KubernetesObject[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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 comment
The 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.
🛠️ What’s Changed
Removed: js-yaml dependency
Added: yaml library
Updated manifest parsing logic to:
parseDocument(manifest, { version: '1.1' });
Ensures values are interpreted using YAML 1.1 rules.
🐛 Problems Solved
defaultMode: 0644 was being parsed as 644 in YAML 1.2 (incorrect)
In YAML 1.1, it's correctly interpreted as octal 0644 → decimal 420
This fixes configMap and secret mount permission issues in Kubernetes
YAML 1.2 doesn't treat yes, no, on, off as booleans
In YAML 1.1, these are properly recognized as true/false
Prevents unexpected behavior in manifest logic that depends on boolean flags
✅ Tested
defaultMode: 0644
— parsed correctly as 420on
,yes
,no
— parsed correctly as booleans✅ Result
Manifests parsed through this library now match Kubernetes expectations
No more silent failures or misconfigurations due to YAML parsing issues

Fixes #2539