1. Answers
  2. Defining Custom Resource Definitions To Extend Kubernetes Via ProviderConfig

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

  1. Ensure you have the Pulumi CLI installed. If not, you can download it from the Pulumi website.
  2. Create a new Pulumi project using TypeScript:
    pulumi new typescript
    
  3. Follow the prompts to set up your project.

Step 2: Define the Custom Resource Definition (CRD)

  1. Create a new file called crd.ts in your project directory.
  2. 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

  1. In your index.ts file, import and deploy the CRD:
    import * as pulumi from "@pulumi/pulumi";
    import crd from "./crd";
    
    export const myCrd = crd;
    
  2. Run pulumi up to deploy the CRD to your Kubernetes cluster.

Step 4: Create Custom Resources

  1. 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;
    
  2. In your index.ts file, import and deploy the custom resource:
    import customResource from "./customResource";
    
    export const myCustomResource = customResource;
    
  3. 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 up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up