Sub topic 14
Versioning and conversion
This chapter assumes you can write a CRD with a structural schema and that you know what the served and storage fields in a version entry mean. It covers how to change a custom API without breaking existing clients.
Why versions exist
A CRD can list several versions of the same type, such as v1alpha1 and v1. Clients choose a version in apiVersion. The API server stores each object in etcd in one version only, the storage version. It converts to and from that version when a client asks for a different one.
Each version entry has two flags:
served: truemeans clients can read and write that version.storage: truemarks the version written to etcd. Exactly one version must have it.
Two versions with no conversion
If the schemas differ only in details such as a new optional field, the default strategy None works. The API server changes only the apiVersion field and leaves all other fields as they are.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: webapps.example.com
spec:
group: example.com
scope: Namespaced
names:
plural: webapps
singular: webapp
kind: WebApp
conversion:
strategy: None
versions:
- name: v1alpha1
served: true
storage: false
deprecated: true
deprecationWarning: "example.com/v1alpha1 WebApp is deprecated; use v1"
schema:
openAPIV3Schema:
type: object
x-kubernetes-preserve-unknown-fields: true
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
image:
type: string
replicas:
type: integerThe deprecated flag makes kubectl print your warning when someone uses the old version.
When you need a conversion webhook
Use strategy: Webhook when a field is renamed, restructured or computed. The API server then sends a ConversionReview request to your service. Your service returns the converted objects.
conversion:
strategy: Webhook
webhook:
conversionReviewVersions: ["v1"]
clientConfig:
service:
name: webapp-webhook
namespace: webapp-system
path: /convert
port: 443
caBundle: <base64-encoded CA certificate>Conversion must be lossless in both directions. A common design is hub and spoke. You pick one version as the hub, and every other version converts only to and from the hub. This takes N conversions for N versions, where converting between every pair would take far more. controller-runtime follows this model with its Hub and Convertible interfaces.
Common errors
- Conversion webhook down: every read or write of the type can fail, including your controller's.
- Lossy conversion: a field present in
v1but missing inv1alpha1is dropped on a round trip. Preserve such data in an annotation. - Changing the storage version: old objects stay in the old version until they are rewritten.
status.storedVersionsstill lists the old version, so you cannot remove it from the CRD until you rewrite every object and updatestoredVersions.
Exercise
You add v1 as the storage version and keep v1alpha1 served. Why can you not yet delete v1alpha1 from the CRD?
Show answer
Existing objects may still be stored in etcd as v1alpha1, and status.storedVersions still lists it. First rewrite every object, for example by reading and updating each one, so it is stored as v1. Then update storedVersions and remove the old version.