Skip to content

CustomResourceDefinition Serialization | x-kubernetes-int-or-string Dropped #2483

Closed

Description

@sebastianserrano

Describe the bug

When trying to create a CRD with x-kubernetes-int-or-string, its being dropped causing the server to respond with The CustomResourceDefinition "middlewares.traefik.io" is invalid: spec.validation.openAPIV3Schema.properties[spec].properties[circuitBreaker].properties[checkPeriod].type: Required value: must not be empty for specified object fields.

Original CRD

original.yaml

Image

Broken CRD

broken.yaml

This is what is actually being passed to the server. How do I know? I print it out before sending the request in the underlying node_module, node_modules/@kubernetes/client-node/dist/object.js

Image

Image

Client Version
1.0.0 to 1.3.0

Server Version
1.30.0 to 1.33.0

To Reproduce
Steps to reproduce the behavior:

First option
  1. Use example code
Second option
  1. kubectl apply -f [USE HERE BROKEN CRD YAML]

Image

^^ These are the errors you get while using the first option

Expected behavior

The create call should not strip out x-kubernetes-int-or-string. In essence, it should replicate the behaviour of kubectl apply -f [USE HERE ORIGINAL CRD YAML] which successfully creates the resource

Example Code

import yaml from 'yaml';
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const client = k8s.KubernetesObjectApi.makeApiClient(kc);
const spec = yaml.parse(USE HERE ORIGINAL CRD YAML).toJS()

await client.create(spec);

Environment (please complete the following information):

  • OS: Linux
  • Node.js version: v20.14.0

Additional context
Add any other context about the problem here.

Activity

  1. brendandburns commented on Jun 9, 2025

    @brendandburns
    Contributor

    So the field is definitely there:

    https://github.com/kubernetes-client/javascript/blob/main/src/gen/models/V1JSONSchemaProps.ts#L83

    Have you tried outputting the spec in your sample code? You are using the yaml library which is not part of this library. My guess is that it is handling the x-... field name in some other way than the code expects. The code expects underscores instead of dashes, since dash syntax isn't legal in Javascript/Typescript.

  2. sebastianserrano commented on Jun 10, 2025

    @sebastianserrano
    Author

    Thanks for you reply @brendandburns. Here is what I've gathered:

    1. Have you tried outputting the spec in your sample code? You are using the yaml library which is not part of this library.
      • Yes, if I print out what yaml produces, I can still see the x-kubernetes-int-or-string: true

    Image

    1. I have another CRD which is being applied correctly, without having its x-kubernetes-int-or-string: true stripped out.

    good.yaml

    Image
    1. These are the differences between both
      • middlewares.traefik.io (broken)
        • x-kubernetes-int-or-string: true at path: .spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.circuitBreaker.properties.checkPeriod.x-kubernetes-int-or-string
      • ingressrouteudps.traefik.io
        • x-kubernetes-int-or-string: trueat path: .spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.routes.items.properties.services.items.properties.port.x-kubernetes-int-or-string
    Image

    Given these details, it seems like the behaviour should be predictable across the board? Either the serialization keeps all of the instances of x-kubernetes-int-or-string found or not.

    Thoughts?

  3. sebastianserrano commented on Jun 10, 2025

    @sebastianserrano
    Author

    So the field is definitely there:

    https://github.com/kubernetes-client/javascript/blob/main/src/gen/models/V1JSONSchemaProps.ts#L83

    Have you tried outputting the spec in your sample code? You are using the yaml library which is not part of this library. My guess is that it is handling the x-... field name in some other way than the code expects. The code expects underscores instead of dashes, since dash syntax isn't legal in Javascript/Typescript.

    Also, using

     import * as k8s from '@kubernetes/client-node';
     k8s.loadYaml<KubernetesObject>(ORIGINAL CRD HERE)

    has the same effect as using the yaml package. It simply keeps x-kubernetes-int-or-string as is

  4. sebastianserrano commented on Jun 11, 2025

    @sebastianserrano
    Author

    I can see the reason why this wasn't popping up before is because everything was getting serialized as a KubernetesObject.

    Starting in 1.0.0 that seemed to change

  5. brendandburns commented on Jun 19, 2025

    @brendandburns
    Contributor

    1.0.0 introduced a new code generator, so changes/bugs are expected. We'd be happy to look at PRs to fix/improve things.

  6. brendandburns commented on Jun 19, 2025

    @brendandburns
    Contributor

    Also, 1.0.0 forked off of the 0.x branch and stayed forked for a while under development, so it is also possible that previous fixes/updates in the 0.x branch weren't properly picked up for the 1.0 series.

  7. gastoner commented on Jun 19, 2025

    @gastoner

    From my observations, the issue has started appearing from 1.1.2 -> 1.2.0 in #2400

  8. feloy commented on Jun 20, 2025

    @feloy
    Contributor

    In the serializer, there is a confusion between the attributeType.name and attributeType.baseName:

    https://github.com/kubernetes-client/javascript/blob/1.3.0/src/gen/models/ObjectSerializer.ts#L2256

                    instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type, attributeType.format);
    

    For x-kubernetes-int-or-string attribute (and similar x* attributes), name is x_kubernetes_int_or_string and baseName is x-kubernetes-int-or-string, and data contains the field x-kubernetes-int-or-string, not the other one.

    There are a few other attributes in V1JSONSchemaProps with name different from baseName, and I'm not sure which one we should get in this case:

      { name: 'ref', baseName: '$ref', type: 'string', format: '' },
      { name: 'schema', baseName: '$schema', type: 'string', format: '' },
      { name: '_default', baseName: 'default', type: 'any', format: '' },
      { name: '_enum', baseName: 'enum', type: 'Array<any>', format: '' },
    
  9. feloy commented on Jun 23, 2025

    @feloy
    Contributor

    @schrodit I can see you have worked on serializers recently, do you have an idea on where this confusion between baseName and name may come from?

  10. feloy commented on Jun 23, 2025

    @feloy
    Contributor

    After some research, I think I understand more clearly the situation:

    The ObjectSerializer.deserialize (from serializer.ts) seems to be the one method to use to get a correct object to pass to the client.create() method, with he help of KubernetesObjectApi.getSerializationType (from object.ts) to get the type of the object to deserialize.

    to sum up, we would need to add such a function to the library:

    function parseYamlManifest(manifest: string): KubernetesObject {
        const yml = yaml.load(manifest) as KubernetesObject;
        const type = getSerializationType(yml.apiVersion, yml.kind);
    
        return ObjectSerializer.deserialize(yml, type) as KubernetesObject;
    }
    

    @brendandburns WDYT?

  11. brendandburns commented on Jun 24, 2025

    @brendandburns
    Contributor

    @feloy that looks fine to me, I'm open to replacing yaml.load(...) with that method if that seems better, assuming it is backward compatible (which I think it would be)

  12. feloy commented on Jun 24, 2025

    @feloy
    Contributor

    Great, I'll work on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions