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

Do you have an example of querying a custom resource? #9

Open
justinmchase opened this issue Sep 1, 2024 · 2 comments
Open

Do you have an example of querying a custom resource? #9

justinmchase opened this issue Sep 1, 2024 · 2 comments
Labels
question Further information is requested

Comments

@justinmchase
Copy link

I've created a custom resource definition and I see the api for CustomResourceDefinitions, but how do I then use this api to query for my custom resources?

@danopia
Copy link
Member

danopia commented Sep 5, 2024

Hey @justinmchase ,
The easiest way of getting an API for one of your own CRDs is by generating a client module based on your CustomResourceDefinition files and importing it into your program. This ensures that you have proper typing and validation available for working with your CRs.

Here's how I normally set this up. First, I have a directory in my project which contains my CRD files. For this example I'll create deploy/crds/crontab.yaml using the example CRD yaml from the kubernetes docs: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/

Next I create a shell script generate-apis.sh which will invoke the codegen. I'll need to rerun this script in the future if I update the CRD schema, so I like committing this script to git:

#!/bin/sh -eux

# Regenerates the Kubernetes CR client modules.
CodegenSrc="https://raw.githubusercontent.com/cloudydeno/deno-kubernetes_apis/main/generation/run-on-crds.ts"
# Path to import /x/kubernetes_apis from, align this with your own imports.
ApisImportUrl="https://deno.land/x/[email protected]/"
InputDirectory="deploy/crds"
TargetDirectory="."

deno run --allow-read=. --allow-write=lib "$CodegenSrc" "$InputDirectory" "$TargetDirectory" "$ApisImportUrl"

At this point my project looks like this:

> tree
.
├── deploy
│   └── crds
│       └── crontab.yaml
└── generate-api.sh

Then I run that script:

> chmod +x ./generate-api.sh # just once after creating the script
> ./generate-api.sh
+ CodegenSrc=https://raw.githubusercontent.com/cloudydeno/deno-kubernetes_apis/main/generation/run-on-crds.ts
+ ApisImportUrl=https://deno.land/x/[email protected]/
+ deno run --allow-read=. --allow-write=lib https://raw.githubusercontent.com/cloudydeno/deno-kubernetes_apis/main/generation/run-on-crds.ts deploy/crds . https://deno.land/x/[email protected]/
Wrote lib/stable.example.com@v1/structs.ts
Wrote lib/stable.example.com@v1/mod.ts

And now I have a client module in a new lib/ directory:

> tree
.
├── deploy
│   └── crds
│       └── crontab.yaml
├── generate-api.sh
└── lib
    └── stable.example.com@v1
        ├── mod.ts
        └── structs.ts

From here this module can be imported like so, and used just like the built-in clients:

import { autoDetectClient } from 'https://deno.land/x/[email protected]/mod.ts';
const kubernetes = await autoDetectClient();

import { StableExampleComV1Api } from "./lib/stable.example.com@v1/mod.ts"
const exampleApi = new StableExampleComV1Api(kubernetes).namespace("default");

// just something arbitrary idk
await exampleApi.createCronTab({
  metadata: {
    name: 'my-crontab',
  },
  spec: {
    cronSpec: '@daily',
    image: 'cleanup-something',
    replicas: 1,
  },
});

Hopefully this is enough information to get the ball rolling for you. I didn't really have a good prior art when I set this up because I've found the kubernetes-client/javascript codegen workflow to be extremely subpar and unmaintainable when developing NodeJS programs, while the golang workflow is to generate the CRD yaml from your golang source code, which is usually the wrong direction for my projects.

Best, Dan

@danopia danopia added the question Further information is requested label Sep 5, 2024
@justinmchase
Copy link
Author

I sort of did this manually for now but this is a great comment and I hope anyone in the future with this issue follows this advice. Thank you!

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

No branches or pull requests

2 participants