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

Conversation

Tanmayshi
Copy link

🛠️ 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

  1. ✅ Incorrect parsing of octal values
    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

  1. ✅ Boolean string interpretation issues
    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

  • Applied a manifest with defaultMode: 0644 — parsed correctly as 420
  • Applied a manifest with on, yes, no — parsed correctly as booleans
  • Resource created successfully using KubernetesObjectApi

✅ Result
Manifests parsed through this library now match Kubernetes expectations

No more silent failures or misconfigurations due to YAML parsing issues
Screenshot (312)

Fixes #2539

Copy link

linux-foundation-easycla bot commented Jul 8, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Tanmayshi
Once this PR has been reviewed and has the lgtm label, please assign cjihrig for approval. For more information see the Code Review Process.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jul 8, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @Tanmayshi!

It looks like this is your first PR to kubernetes-client/javascript 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/javascript has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 8, 2025
Copy link
Contributor

@cjihrig cjihrig left a 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",
Copy link
Contributor

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?

Copy link
Author

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';
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.

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;
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.

@@ -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.

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' });
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 }); ,

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 8, 2025
@@ -16,15 +16,15 @@
"form-data": "^4.0.0",
"hpagent": "^1.2.0",
"isomorphic-ws": "^5.0.0",
"js-yaml": "^4.1.0",
Copy link
Contributor

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');
Copy link
Contributor

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);
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.

@cjihrig
Copy link
Contributor

cjihrig commented Jul 8, 2025

I started the CI. It looks like there are relevant failures.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 8, 2025
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
Copy link
Contributor

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.

Copy link
Author

@Tanmayshi Tanmayshi Jul 9, 2025

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 {
Copy link
Contributor

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)

Copy link
Author

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.

Copy link
Contributor

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[] {
Copy link
Contributor

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

Copy link
Author

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[] {
Copy link
Contributor

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)

Copy link
Member

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.

Copy link
Author

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.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

YAML 1.2 only is supported
5 participants