Defining Custom Resource Definitions to Extend Kubernetes via ProviderConfig
Introduction
In this guide, we will walk through the process of defining Custom Resource Definitions (CRDs) to extend Kubernetes using Pulumi and ProviderConfig. CRDs allow you to create custom resources that can be managed like native Kubernetes resources. We will use Pulumi’s TypeScript SDK to define and deploy these resources.
Step-by-Step Explanation
Step 1: Set Up Your Pulumi Project
- Ensure you have the Pulumi CLI installed. If not, you can download it from the Pulumi website.
- Create a new Pulumi project using TypeScript:
pulumi new typescript
- Follow the prompts to set up your project.
Step 2: Define the Custom Resource Definition (CRD)
- Create a new file called
crd.ts
in your project directory. - Define the CRD using Pulumi’s Kubernetes SDK. Here is an example of a CRD definition:
import * as k8s from "@pulumi/kubernetes"; const crd = new k8s.apiextensions.v1.CustomResourceDefinition("mycrd", { metadata: { name: "myresources.mydomain.com", }, spec: { group: "mydomain.com", versions: [{ name: "v1", served: true, storage: true, schema: { openAPIV3Schema: { type: "object", properties: { spec: { type: "object", properties: { foo: { type: "string" }, bar: { type: "integer" }, }, }, }, }, }, }], scope: "Namespaced", names: { plural: "myresources", singular: "myresource", kind: "MyResource", shortNames: ["mr"], }, }, }); export default crd;
Step 3: Deploy the CRD
- In your
index.ts
file, import and deploy the CRD:import * as pulumi from "@pulumi/pulumi"; import crd from "./crd"; export const myCrd = crd;
- Run
pulumi up
to deploy the CRD to your Kubernetes cluster.
Step 4: Create Custom Resources
- Define a custom resource using the CRD in a new file called
customResource.ts
:import * as k8s from "@pulumi/kubernetes"; const customResource = new k8s.apiextensions.CustomResource("myresource-instance", { apiVersion: "mydomain.com/v1", kind: "MyResource", metadata: { name: "example", }, spec: { foo: "Hello", bar: 42, }, }); export default customResource;
- In your
index.ts
file, import and deploy the custom resource:import customResource from "./customResource"; export const myCustomResource = customResource;
- Run
pulumi up
to deploy the custom resource.
Summary
In this guide, we have demonstrated how to extend Kubernetes by defining and deploying Custom Resource Definitions (CRDs) using Pulumi and ProviderConfig. We created a CRD, deployed it, and then created a custom resource based on that CRD. This allows you to manage custom resources in your Kubernetes cluster just like native resources.
Full Code Example
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Define the Custom Resource Definition (CRD)
const crd = new k8s.apiextensions.v1.CustomResourceDefinition("mycrd", {
metadata: {
name: "myresources.mydomain.com",
},
spec: {
group: "mydomain.com",
versions: [{
name: "v1",
served: true,
storage: true,
schema: {
openAPIV3Schema: {
type: "object",
properties: {
spec: {
type: "object",
properties: {
foo: { type: "string" },
bar: { type: "integer" },
},
},
},
},
},
}],
scope: "Namespaced",
names: {
plural: "myresources",
singular: "myresource",
kind: "MyResource",
shortNames: ["mr"],
},
},
});
// Export the CRD
export const myCrd = crd;
// Define a custom resource using the CRD
const customResource = new k8s.apiextensions.CustomResource("myresource-instance", {
apiVersion: "mydomain.com/v1",
kind: "MyResource",
metadata: {
name: "example",
},
spec: {
foo: "Hello",
bar: 42,
},
});
// Export the custom resource
export const myCustomResource = customResource;
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.