Skip to content
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

Document how to do equivalent of kubectl apply -f {filename} using this client #367

Open
pbelbin opened this issue Nov 13, 2019 · 15 comments
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.

Comments

@pbelbin
Copy link

pbelbin commented Nov 13, 2019

The documentation is not clear (to me) as to how the equivalent of the kubectl apply -f {filename} might be achieved using this client.

eg: use this to create/update a Deployment.

So, how is it done?

FYI:
I have done some digging through some of the issues in connection with this, which lead me to the 'patch' commands with the 'force' set to 'true', (confirmation (or not) that this is indeed equivalent would be good), and that the 'options' needs to provide an override to the default 'content-type' value.

But, I have yet to see a clear definition of the 'body' that is supposed to be passed, as, the object model simply lists this as an 'object', which, could be anything at all, but, realistically, when I pass the same object as used to createNamespacedDeployment() with success, it fails miserably when used with patchNamespacedDeployment(), with an error message indicating that the body was unable to be processed.

By the way, if it makes a difference, I'm using TypeScript.

Thank you in advance!
:)

@brendandburns
Copy link
Contributor

brendandburns commented Nov 14, 2019

kubectl apply is actually a pretty complicated command. I don't think that this library will support it until we regenerate for 1.16 and we include server side apply

The Go code for apply is here:
https://github.com/kubernetes/kubectl/tree/master/pkg/apply

If you want to try to emulate it with this library (but honestly waiting for Server-Side apply is a better idea...)

@pbelbin
Copy link
Author

pbelbin commented Nov 15, 2019

Thank you for the information! I like the idea of using the server side apply functionality via the client.
Do we have any idea as to when this will be available?

@brendandburns
Copy link
Contributor

We need to regenerate for the Kubernetes 1.16 API an then see if it works :)

I'd guess sometime in the next month or so.

@garyo
Copy link

garyo commented Dec 16, 2019

This is exactly what I want to do with this lib -- kick off jobs. apply is the right way to do that, correct? I'd appreciate any news on this.
(As well as the authentication issue #295, loading auth data, which I guess is closed, though I'd still like some help with using Google Kubernetes Engine -- happy to open a new issue if that's preferred here.)

@JoeHegarty
Copy link

JoeHegarty commented Mar 9, 2020

Any update on when the API may be regenerated for 1.16/1.17?

@brendandburns
Copy link
Contributor

I'll try to get to it this week.

@ryanclark
Copy link

@brendanburns sorry to bug you, have you got an ETA at all? If you're swamped, I can try and do the PR myself (if you could lend a little guidance that would be awesome).

@pyrho
Copy link

pyrho commented May 30, 2020

@garyo I haven't used it myself yet, but can't you already create jobs using the following code ?

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

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

const k8sApi = kc.makeApiClient(k8s.BatchV1Api);
k8sApi.createNamespacedJob(....)

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 23, 2020
@unixfox
Copy link

unixfox commented Oct 23, 2020

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 23, 2020
@brendandburns
Copy link
Contributor

/lifecycle frozen

@k8s-ci-robot k8s-ci-robot added the lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. label Oct 23, 2020
@soerenbrockmann
Copy link

A bit late but are you looking for this example here?
https://github.com/kubernetes-client/javascript/blob/master/examples/typescript/apply/apply-example.ts

Just create a yml file with your resource and apply it.

@raakeysh
Copy link

@soerenbrockmann, how do I resolve the error on line# 16 saying 'Property 'KubernetesObjectApi' does not exist on type 'typeof ' @kubernetes/client-node

@videate-josh
Copy link

It does seem that the types are broken for this apply-example.ts

I get the following for line 19: const specs: k8s.KubernetesObject[] = yaml.loadAll(specString);

Type 'unknown[]' is not assignable to type 'KubernetesObject[]'.
  Type 'unknown' is not assignable to type 'KubernetesObject'.ts

and the following error for line 32: await client.read(spec);

Argument of type 'KubernetesObject' is not assignable to parameter of type 'KubernetesObjectHeader<KubernetesObject>'.
  Type 'KubernetesObject' is not assignable to type '{ metadata: { name: string; namespace: string; }; }'.
    Types of property 'metadata' are incompatible.
      Type 'V1ObjectMeta | undefined' is not assignable to type '{ name: string; namespace: string; }'.
        Type 'undefined' is not assignable to type '{ name: string; namespace: string; }'

@emmanuel
Copy link

emmanuel commented Aug 30, 2024

We worked around this by typing the return value of read as (for our calls where we know the object, if found, definitely does have a name & namespace):

export type KubernetesObjectWithMetadata = KubernetesObject & {
  metadata: V1ObjectMeta & {
    name: string;
    namespace: string;
  };
};

I'd describe it as narrowing the type by removing the possibility of undefined values in name or namespace.

That said, I think it's reasonable to hope/expect that the types defined in the Kubernetes client would allow this kind of usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

No branches or pull requests