V1Lease fails to serialize Date
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
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 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!
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/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas 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
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/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas 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
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/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas 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: 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/staleis applied- After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied- After 30d of inactivity since
lifecycle/rottenwas applied, the issue is closedYou 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.