This project demonstrates how to write API conversions for Kubernetes CRDs by taking a use case of Covid19 Vaccine registration.
All kubernetes projects contains several APIs that evolves over time and moves to more stable version. This evolution leads to multiple release and keeps updating API server to support latest version. There are clients who also upgrade to latest version with subsequent releases. But what about those clients who still use older versions? So to serve them the requested data in older version, k8s API server needs to support older versions also, for that we write API conversion functions that fetches data from API server in latest version and convert it to requested version and serves the client.
- Install Go
- kind cluster
- kubebuilder
>> mkdir kube-vaccine
>> cd kube-vaccine
>> go mod init
>> kubebuilder init —domain gov.io
Now let's create APIs for kind Registration using
>> Kubebuilder create api —group cowin —kind Registration —version v1
Create resource(y/n) - y
Create controller(y/n) - y
Similarly we create other API versions also, so finally output will be
// v1 API
type RegistrationSpec struct {
Name string `json:"name,omitempty"`
VerifiedID string `json:"verified_id,omitempty"`
RegistrationDate string `json:"registration_date,omitempty"`
}
// v2 API
type RegistrationSpec struct {
Name string `json:"name,omitempty"`
VerifiedID string `json:"verified_id,omitempty"`
RegistrationDate string `json:"registration_date,omitempty"`
VaccineName string `json:"vaccine_name,omitempty"`
}
// v3 API
type RegistrationSpec struct {
Name string `json:"name,omitempty"`
VerifiedID string `json:"verified_id,omitempty"`
VaccineDetails []*VaccineDetail `json:"vaccine_details,omitempty"`
}
type VaccineDetail struct {
RegistrationDate string `json:"registration_date,omitempty"`
VaccineName string `json:"vaccine_name,omitempty"`
}
Add tag // +kubebuilder:storageversion
on v3 API Registration
kubebuilder create webhook --group cowin --kind Registration --version v3 --conversion
- Uncomment
patches/webhook_in_registrations.yaml
andpatches/cainjection_in_registrations.yaml
under patchesStrategy inconfig/crd/kustomization.yaml
- Uncomment
../webhook
and../certmanager
and all the vars related withCERTMANAGER
inconfig/default/kustomization.yaml
. - Delete
maifest.yaml
file fromconfig/webhook
and also commentmanifest.yaml
fromconfig/webhook/kustomization.yaml
- In Makefile file add
CRD_OPTIONS ?= "crd:trivialVersions=false"
- Update
conversionReviewVersions: ["v1","v2","v3"]
in config/crd/patches/webhook_in_registrations.yaml
make conversion-gen
make manifests
make install
bin/conversion-gen --input-dirs=./api/v1 --input-dirs=./api/v2 --build-tag=ignore_autogenerated_conversions --output-file-base=zz_generated.conversion --go-header-file=./hack/boilerplate.go.txt
make docker-build docker-push IMG=docker.io/<docker-name>/<img-name>:<img-tag>
kind load docker-image docker.io/<docker-name>/<img-name>:<img-tag> --name kind
make deploy IMG=docker.io/<docker-name>/<img-name>:<img-tag>
- Apply
v3
sample, usingkubectl apply -f config/samples/cowin_v3_registration.yaml
- Fetch v3 object in v1 API and store it in another file
v3Tov1.yaml
usingkubectl get registration.v1.cowin.gov.io/registration-sample-v3 -o json > v3Tov1.yaml
- Update the
v3Tov1.yaml
and changespec.name
, then save it and applykubectl apply -f v3Tov1.yaml
- Now fetch the same object in v3 API and observe there is no loss in data,
kubectl get registration.v3.cowin.gov.io/registration-sample-v3 -o json