How to create new custom resource definitions

Note

This guide is intended for developers who want to extend Chantico itself. Users who only install or configure Chantico can skip it.

This guide describes how to add a new Kubernetes custom resource definition (CRD) to Chantico using Kubebuilder.

Relevant resources (this documentation may be outdated, so check the links for the latest information):

Prerequisites

  1. Install kubebuilder:

    curl -L -o kubebuilder \
        "https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)"
    chmod +x kubebuilder && sudo mv kubebuilder /usr/local/bin/
  2. Make sure your local environment is set up: How to set up the local development environment

Create the API and controller scaffolding

  1. Generate the resource scaffolding:

    kubebuilder create api --group chantico --version v1alpha1 --kind <RESOURCE_TYPE>
  2. Remove the generated integration tests (these are not used in this repo):

    rm internal/controller/suite_test.go internal/controller/<resource_type>_controller_test.go

Define the schema

  1. Update the Go types in api/v1alpha1/<resource>_types.go:

    • Add the Spec fields for the desired state.
    • Add the Status fields for observed state.
    • Add validation markers if needed.
  2. Regenerate code and manifests:

    make build

Implement controller behavior

  1. Update the controller in internal/controller/<resource>_controller.go:

    • Implement reconcile logic.
    • Add required RBAC markers for the CRD.
  2. Add or adjust tests in internal/<resource>/ as needed.

Apply to a cluster (optional)

If you want to test against a running dev cluster:

make install

This installs the CRDs into the current kube-context.