Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Versioning CRDs in Kubernetes

Introduction

Custom Resource Definitions (CRDs) are a powerful feature in Kubernetes that allow users to extend the API by defining their own resource types. As with any software, CRDs may require versioning as they evolve. Versioning ensures that changes to the CRD schema are handled smoothly without breaking existing deployments.

Key Concepts

What are CRDs?

CRDs are extensions of the Kubernetes API that allow users to define their own resource types.

Versioning

Versioning is the process of managing changes to the CRD schema over time, typically categorized into major, minor, and patch versions.

Importance of Versioning

Versioning helps maintain compatibility, manage migrations, and provide a clear structure for users of the CRD.

Versioning Process

Step 1: Define the Initial CRD

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myresources.example.com
spec:
  group: example.com
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              field1:
                type: string

Step 2: Create a New Version

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myresources.example.com
spec:
  group: example.com
  versions:
  - name: v1
    served: false
    storage: false
  - name: v2
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              field1:
                type: string
              field2:
                type: integer

Step 3: Migration

Ensure existing resources are updated to the new version. This may involve data transformation or validation. Use a controller to handle migration logic.

Best Practices

  • Always increment the version when making breaking changes.
  • Maintain backward compatibility when possible.
  • Document all changes associated with each version.
  • Consider using a structured naming convention for versions (e.g., v1, v2).

FAQ

Why is versioning necessary for CRDs?

Versioning is necessary to manage changes in the CRD schema, ensuring existing deployments continue to function while allowing for enhancements.

How do I know when to create a new version?

Create a new version when you introduce breaking changes, such as removing fields or changing data types.

Can I delete an old version of a CRD?

While you can delete old versions, it is advisable to keep them for a transition period to allow users to migrate their resources.

Versioning Flowchart


            graph TD;
                A[Start] --> B{Has the schema changed?};
                B -- Yes --> C{Is it a breaking change?};
                C -- Yes --> D[Create a new version];
                C -- No --> E[Update existing version];
                B -- No --> F[Continue as is];
                D --> G[Document changes];
                E --> G;
                G --> H[Deploy updated CRD];
                H --> I[End];