javascript icon indicating copy to clipboard operation
javascript copied to clipboard

V1Lease fails to serialize Date

Open kurtb opened this issue 4 years ago • 6 comments

Describe the bug Attempting to create/update a Lease using the CoordinationV1Api fails due to the Date serializer calling toISOString(). Kubernetes expects this string to use the microsecond format (https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#leasespec-v1-coordination-k8s-io) - not the millisecond format that the Date object provides. (i.e. Date creates 2022-06-23T18:43:04.180Z but Kubernetes expects 2022-06-23T18:43:04.180000Z).

The response from the Kubernetes API is

 "message": "Lease in version \"v1\" cannot be handled as a Lease: v1.Lease.Spec: v1.LeaseSpec.HolderIdentity: AcquireTime: unmarshalerDecoder: parsing time \"2022-06-23T18:10:50.278Z\" as \"2006-01-02T15:04:05.000000Z07:00\": cannot parse \".278Z\" as \".000000\", error found in #10 byte of ...|0:50.278Z\",\"holderId|..., bigger context ...|},\"spec\":{\"acquireTime\":\"2022-06-23T18:10:50.278Z\",\"holderIdentity\":\"test\",\"leaseDurationSeconds\":60|...",

As a workaround you can create a custom Date that overrides toISOString to convert to the correct format (see example code).

** Client Version ** 0.16.3

** Server Version ** 1.22.3

Expected behavior Lease is created/updated

** Example Code**

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

export async function brokenTest() {
    const kc = new k8s.KubeConfig();
    kc.loadFromDefault();

    const leaseApi = kc.makeApiClient(k8s.CoordinationV1Api);
    await leaseApi.createNamespacedLease(
        'default', 
        {
            apiVersion: 'coordination.k8s.io/v1',
            kind: 'Lease',
            metadata: {
                name: 'test',
                namespace: 'default',
            },
            spec: {
                acquireTime: new Date(),
                holderIdentity: 'test',
                leaseDurationSeconds: 60,
                leaseTransitions: 0,
            },
        });
}

class MicroDate extends Date {
    public toISOString(): string {
        const str = super.toISOString();
        return str.slice(0, -1) + '000Z'; 
    }
}

export async function workingTest() {
    const kc = new k8s.KubeConfig();
    kc.loadFromDefault();

    const leaseApi = kc.makeApiClient(k8s.CoordinationV1Api);
    await leaseApi.createNamespacedLease(
        'default', 
        {
            apiVersion: 'coordination.k8s.io/v1',
            kind: 'Lease',
            metadata: {
                name: 'test',
                namespace: 'default',
            },
            spec: {
                acquireTime: new MicroDate(),
                holderIdentity: 'test',
                leaseDurationSeconds: 60,
                leaseTransitions: 0,
            },
        });
}

Environment (please complete the following information):

  • OS: Linux
  • NodeJS Version 16.15.0

kurtb avatar Jun 23 '22 18:06 kurtb

This is related to this in the Java client code:

https://github.com/kubernetes-client/java/blob/a0e456ec0b5a455448e50f5e907bcbeef2f7bb7e/kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java#L251

Also discussion here: https://github.com/kubernetes-client/java/pull/1477

This probably requires changes us to insert a custom type for the v1Date objects instead of using javascript Date.

brendandburns avatar Jun 23 '22 23:06 brendandburns

@brendandburns I created the PR to represent micro seconds fields baesd on @kurtb code in the comment. Please review the PR when you have a time!

uesyn avatar Jun 25 '22 02:06 uesyn

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Sep 23 '22 04:09 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Oct 23 '22 05:10 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-triage-robot avatar Nov 22 '22 05:11 k8s-triage-robot

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Nov 22 '22 05:11 k8s-ci-robot