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

Listing/Watching kubernetes Custom Resources #341

Closed
SachinVarghese opened this issue Sep 13, 2019 · 8 comments
Closed

Listing/Watching kubernetes Custom Resources #341

SachinVarghese opened this issue Sep 13, 2019 · 8 comments

Comments

@SachinVarghese
Copy link

SachinVarghese commented Sep 13, 2019

Is it currently possible to list or watch k8s CRDs in javascript and create client code for the same?

@brendandburns
Copy link
Contributor

Please see this example here:

https://github.com/kubernetes-client/javascript/blob/master/examples/cache-example.js#L13

And the informer example here:

https://github.com/kubernetes-client/javascript/blob/master/examples/typescript/informer/informer.ts

Closing this issue as the question is answered. Please re-open if you need more info.

@SachinVarghese
Copy link
Author

SachinVarghese commented Sep 14, 2019

@brendandburns Thanks for the links but I was actually looking for an example with a custom resource definition listing and not simply listing/watching for pods which could be very similar. Please let me know how can such a use case be implemented in javascript similar to maybe https://github.com/kubernetes/code-generator

@zoidyzoidzoid
Copy link

So I just stumbled upon this issue looking for a similar answer to @SachinVarghese

So this client is generated with this script which uses the kubernetes-client/gen repo to generate a typescript client.

I found a issue that hasn't been updated in 6 months about making that 'kubernetes-client/gen' repo work for CRDs too: kubernetes-client/gen#121

@zoidyzoidzoid
Copy link

Can we re-open it but consider it blocked by kubernetes-client/gen#121?

zoidyzoidzoid added a commit to zoidyzoidzoid/shipper that referenced this issue Dec 4, 2019
Some of the modern Kubernetes clients use clients generated from a
swagger.json

kubernetes-client/javascript uses kubernetes-client/gen which uses
OpenApiTools/openapi-generator and kubernetes/kubernetes/api/openapi-spec/swagger.json

There are open/closed/rotten/ignored issues all over the place about
making these easier to use with CRDs.

Wrongly closed issue on kubernetes-client/javascript: kubernetes-client/javascript#341
Open issue on kubernetes-client/gen: kubernetes-client/gen#121

It seems like kubernetes/kube-openapi is one of the repos working on
solving the main part of this problem, generating the Go code to make
it easy to generate a swagger.json

I found the following Argo code via the following issue:

kubernetes/kube-openapi#13

https://github.com/argoproj/argo/blob/master/hack/gen-openapi-spec/main.go
https://github.com/argoproj/argo/blob/master/pkg/apis/workflow/v1alpha1/openapi_generated.go#L14:6
https://github.com/kubernetes/kube-openapi/blob/master/cmd/openapi-gen/openapi-gen.go

So I'm working on adding similar stuff to Shipper.

gen-openapi-spec uses the generated openapi_generated.go module that is
generated via the openapi-gen command.

The following commit adds the code to generate openapi_generated.go

Signed-off-by: William Stewart <[email protected]>
@brandonc
Copy link
Contributor

brandonc commented Dec 5, 2019

@zoidbergwill I was able to generate a cluster-specific client with CRD apis/models with a couple of simple steps using the existing code:

  1. Download the cluster's /openapi/v2 spec and replace src/gen/swagger.json.unprocessed with it
  2. Append export OPENAPI_SKIP_FETCH_SPEC=1 to the file settings
  3. npm run generate

@mcandre
Copy link

mcandre commented Aug 27, 2020

Does this handle updates and deletes, or just creates?

@pfisterer
Copy link

@brendandburns @SachinVarghese Can you point me to a working example that uses Informer to watch custom resources?

I'm getting this error:

[DEBUG] Reconciler - init: Watching group 'mk8.farberg.de', version 'v1', plural 'microkeights'
./node_modules/@kubernetes/client-node/dist/cache.js:77
            const list = result.body;
                                ^

TypeError: Cannot read property 'body' of undefined
    at ListWatch.<anonymous> (./node_modules/@kubernetes/client-node/dist/cache.js:77:33)
    at Generator.next (<anonymous>)
    at fulfilled (./node_modules/tslib/tslib.js:112:62)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)

when using this code:

async init() {
  //Create K8s Client
  this.logger.debug(`init: Creating kubeconfig`)
  this.kubeConfig = new k8s.KubeConfig();
  this.kubeConfig.loadFromDefault();
  this.k8sApi = this.kubeConfig.makeApiClient(k8s.CoreV1Api);
  this.customObjectsApi = this.kubeConfig.makeApiClient(k8s.CustomObjectsApi)
  
  // Register custom resource definition
  // registerCustomResourceDefinition @ https://github.com/dot-i/k8s-operator-node/blob/master/src/operator.ts
  this.logger.debug(`init: Registering CRD from file ${this.options.crdFile}`)
  const { group, versions, plural } = await this.registerCustomResourceDefinition(this.options.crdFile);
  this.crdGroup = group;
  this.crdVersions = versions;
  this.crdPlural = plural;
  
  console.log("XXX", await this.customObjectsApi.listClusterCustomObject(this.crdGroup, versions[0].name, this.crdPlural))
  
  this.logger.debug(`init: Watching group '${this.crdGroup}', version '${this.crdVersions[0].name}', plural '${this.crdPlural}'`)
  
  const informer = k8s.makeInformer(this.kubeConfig, `/apis/${this.crdGroup}/${versions[0].name}/${this.crdPlural}`, () => {
     console.log("Called")
  })
}

@benbonnet
Copy link

benbonnet commented Jun 1, 2022

quite the same as @pfisterer, simply listing pods :

const informer = k8s.makeInformer(
      kc,
      `/api/v1/namespaces/${namespace}/pods`,
     () => {
       ...
      }
    );

seems to break at @kubernetes/client-node/dist/cache.js:99, result.body being null/undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants