Skip to content

Commit a3c9afc

Browse files
committed
Doc for updating v1a1 provider to v1a2
This patch includes developer documentation to assist with updating a v1alpha1 provider to v1alpha2.
1 parent b90e680 commit a3c9afc

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Updating a v1alpha1 provider to a v1alpha2 infrastructure provider
2+
3+
This document outlines how to update a cluster API (CAPI) v1alpha1 provider to a v1alpha2 infrastructure provider.
4+
5+
* [Updating a v1alpha1 provider to a v1alpha2 infrastructure provider](#updating-a-v1alpha1-provider-to-a-v1alpha2-infrastructure-provider)
6+
* [General information](#general-information)
7+
* [The new API group](#the-new-api-group)
8+
* [The infrastructure API group](#the-infrastructure-api-group)
9+
* [Kubebuilder](#kubebuilder)
10+
* [Sample code and other examples](#sample-code-and-other-examples)
11+
* [Create a branch for new v1alpha1 work](#create-a-branch-for-new-v1alpha1-work)
12+
* [Update the API group in the `PROJECT` file](#update-the-api-group-in-the-project-file)
13+
* [Create the provider's v1alpha2 resources](#create-the-providers-v1alpha2-resources)
14+
* [The cluster and machine resources](#the-cluster-and-machine-resources)
15+
* [The spec and status types](#the-spec-and-status-types)
16+
* [Provider cluster status fields](#provider-cluster-status-fields)
17+
* [Provider cluster status `ready`](#provider-cluster-status-ready)
18+
* [Provider cluster status `apiEndpoints`](#provider-cluster-status-apiendpoints)
19+
* [Create the infrastructure controllers](#create-the-infrastructure-controllers)
20+
* [The provider cluster controller](#the-provider-cluster-controller)
21+
* [The provider machine controller](#the-provider-machine-controller)
22+
23+
## General information
24+
25+
This section contains several general notes about the update process.
26+
27+
### The new API group
28+
29+
Cluster API v1alpha2 custom resource definitions (CRDs) belong to the API group `cluster.x-k8s.io`.
30+
31+
### The infrastructure API group
32+
33+
An infrastructure provider's CRDs should belong to the API group `infrastructure.cluster.x-k8s.io`.
34+
35+
### Kubebuilder
36+
37+
While [kubebuilder](https://github.com/kubernetes-sigs/kubebuilder) v2 is available, the recommended approach for updating a CAPI provider to v1alpha2 is to stick with kubebuilder v1 during the update process and then reevaluate kubebuilder v2 after a successful migration to CAPI v1alpha2.
38+
39+
### Sample code and other examples
40+
41+
This document uses the CAPI provider for AWS ([CAPA](https://github.com/kubernetes-sigs/cluster-api-provider-aws)) for sample code and other examples.
42+
43+
## Create a branch for new v1alpha1 work
44+
45+
This document assumes the work required to update a provider to v1alpha2 will occur on the project's `master` branch. Therefore, the recommendation is to create a branch `release-MAJOR.MINOR` in the repository from the latest v1alpha1-based release. For example, if the latest release of a provider based on CAPI v1alpha1 was `v0.4.1` then the branch `release-0.4` should be created. Now the project's `master` branch is free to be a target for the work required to update the provider to v1alpha2, and fixes or backported features for the v1alpha1 version of the provider may target the `release-0.4` branch.
46+
47+
## Update the API group in the `PROJECT` file
48+
49+
Please update the `PROJECT` file at the root of the provider's repository to reflect the API group `cluster.x-k8s.io`:
50+
51+
```properties
52+
version: "1"
53+
domain: cluster.x-k8s.io
54+
repo: sigs.k8s.io/cluster-api-provider-aws
55+
```
56+
57+
## Create the provider's v1alpha2 resources
58+
59+
The new v1alpha2 types are located in `pkg/apis/infrastructure/v1alpha2`.
60+
61+
### The cluster and machine resources
62+
63+
Providers no longer store configuration and status data for clusters and machines in the CAPI `Cluster` and `Machine` resources. Instead, this information is stored in two, new, provider-specific CRDs:
64+
65+
* `pkg/apis/infrastructure/v1alpha2.`_Provider_`Cluster`
66+
* `pkg/apis/infrastructure/v1alpha2.`_Provider_`Machine`
67+
68+
For example, the AWS provider defines:
69+
70+
* [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/infrastructure/v1alpha2.AWSCluster`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/6de25b31def9b4203a3c0a92b868a1819ea6e3e7/pkg/apis/infrastructure/v1alpha2/awscluster_types.go#L138-L146)
71+
* [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/infrastructure/v1alpha2.AWSMachine`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/6de25b31def9b4203a3c0a92b868a1819ea6e3e7/pkg/apis/infrastructure/v1alpha2/awsmachine_types.go#L144-L152)
72+
73+
### The spec and status types
74+
75+
The `Spec` and `Status` types used to store configuration and status information are effectively the same in v1alpha2 as they were in v1alpha1:
76+
77+
| v1alpha1 | v1alpha2 |
78+
|---|---|
79+
| [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1.AWSClusterProviderSpec`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/e6a57dc61826b8c7806eba22a513c9722c420754/pkg/apis/awsprovider/v1alpha1/awsclusterproviderconfig_types.go#L30-L65) | [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/infrastructure/v1alpha2.AWSClusterSpec`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/6de25b31def9b4203a3c0a92b868a1819ea6e3e7/pkg/apis/infrastructure/v1alpha2/awscluster_types.go#L33-L43) |
80+
| [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1.AWSClusterProviderStatus`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/e6a57dc61826b8c7806eba22a513c9722c420754/pkg/apis/awsprovider/v1alpha1/awsclusterproviderstatus_types.go#L26-L35) | [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/infrastructure/v1alpha2.AWSClusterStatus`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/6de25b31def9b4203a3c0a92b868a1819ea6e3e7/pkg/apis/infrastructure/v1alpha2/awscluster_types.go#L116-L124) |
81+
| [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1.AWSMachineProviderSpec`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/e6a57dc61826b8c7806eba22a513c9722c420754/pkg/apis/awsprovider/v1alpha1/awsmachineproviderconfig_types.go#L28-L97) | [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/infrastructure/v1alpha2.AWSMachineSpec`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/6de25b31def9b4203a3c0a92b868a1819ea6e3e7/pkg/apis/infrastructure/v1alpha2/awsmachine_types.go#L31-L87) |
82+
| [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1.AWSMachineProviderStatus`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/e6a57dc61826b8c7806eba22a513c9722c420754/pkg/apis/awsprovider/v1alpha1/awsmachineproviderstatus_types.go#L26-L44) | [`sigs.k8s.io/cluster-api-provider-aws/pkg/apis/infrastructure/v1alpha2.AWSMachineStatus`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/6de25b31def9b4203a3c0a92b868a1819ea6e3e7/pkg/apis/infrastructure/v1alpha2/awsmachine_types.go#L89-L139) |
83+
84+
#### Provider cluster status fields
85+
86+
A CAPI v1alpha2 provider cluster status resource has two special fields, `ready` and `apiEndpoints`. For example, take the `AWSClusterStatus`:
87+
88+
```golang
89+
// AWSClusterStatus defines the observed state of AWSCluster
90+
type AWSClusterStatus struct {
91+
Ready bool `json:"ready"`
92+
// APIEndpoints represents the endpoints to communicate with the control plane.
93+
// +optional
94+
APIEndpoints []APIEndpoint `json:"apiEndpoints,omitempty"`
95+
}
96+
```
97+
98+
##### Provider cluster status `ready`
99+
100+
A Provider`Cluster`'s `status` object must define a boolean field named `ready` and set the value to `true` only when the infrastructure required to provision a cluster is ready and available.
101+
102+
##### Provider cluster status `apiEndpoints`
103+
104+
A Provider`Cluster`'s `status` object may optionally define a field named `apiEndpoints` that is a list of the following objects:
105+
106+
```golang
107+
// APIEndpoint represents a reachable Kubernetes API endpoint.
108+
type APIEndpoint struct {
109+
// The hostname on which the API server is serving.
110+
Host string `json:"host"`
111+
112+
// The port on which the API server is serving.
113+
Port int `json:"port"`
114+
}
115+
```
116+
117+
If present, this field is automatically inspected in order to obtain an endpoint at which the Kubernetes cluster may be accessed.
118+
119+
## Create the infrastructure controllers
120+
121+
The actuator model from v1alpha1 has been replaced by the infrastructure controllers in v1alpha2:
122+
123+
| v1alpha1 | v1alpha2 |
124+
|---|---|
125+
| [`sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators/cluster.Actuator`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/e6a57dc61826b8c7806eba22a513c9722c420754/pkg/cloud/aws/actuators/cluster/actuator.go#L50-L57) | [`sigs.k8s.io/cluster-api-provider-aws/pkg/controller/awscluster.ReconcileAWSCluster`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/0a05127734a4fb955742b27c6e326a65821851ce/pkg/controller/awscluster/awscluster_controller.go#L98-L103) |
126+
| [`sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators/machine.Actuator`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/e6a57dc61826b8c7806eba22a513c9722c420754/pkg/cloud/aws/actuators/machine/actuator.go#L57-L65) | [`sigs.k8s.io/cluster-api-provider-aws/pkg/controller/awsmachine.ReconcileAWSMachine`](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/0a05127734a4fb955742b27c6e326a65821851ce/pkg/controller/awsmachine/awsmachine_controller.go#L104-L109) |
127+
128+
### The provider cluster controller
129+
130+
Instead of processing the CAPI `Cluster` resources like the old actuator model, the new provider cluster controller operates on the new provider `Cluster` CRD. However, the overall workflow should feel the same as the old cluster actuator. For example, take the AWS cluster controller's [reconcile function](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/0a05127734a4fb955742b27c6e326a65821851ce/pkg/controller/awscluster/awscluster_controller.go#L105-L162), it:
131+
132+
1. Fetches the [AWS cluster resource](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/0a05127734a4fb955742b27c6e326a65821851ce/pkg/controller/awscluster/awscluster_controller.go#L113-L121):
133+
134+
```golang
135+
awsCluster := &infrastructurev1alpha2.AWSCluster{}
136+
err := r.Get(ctx, request.NamespacedName, awsCluster)
137+
if err != nil {
138+
if apierrors.IsNotFound(err) {
139+
return reconcile.Result{}, nil
140+
}
141+
return reconcile.Result{}, err
142+
}
143+
```
144+
145+
2. [Fetches the CAPI cluster resource](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/0a05127734a4fb955742b27c6e326a65821851ce/pkg/controller/awscluster/awscluster_controller.go#L125-L133) that has a one-to-one relationship with the AWS cluster resource:
146+
147+
```golang
148+
cluster, err := util.GetOwnerCluster(ctx, r.Client, awsCluster.ObjectMeta)
149+
if err != nil {
150+
return reconcile.Result{}, err
151+
}
152+
if cluster == nil {
153+
logger.Info("Waiting for Cluster Controller to set OwnerRef on AWSCluster")
154+
return reconcile.Result{RequeueAfter: 10 * time.Second}, nil
155+
}
156+
```
157+
158+
If the AWS cluster resource does not have a corresponding CAPI cluster resource then the reconcile operation is requeued until such time the relationship is established.
159+
160+
3. Uses a `defer` statement to [ensure the AWS and CAPI cluster resources are always patched](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/0a05127734a4fb955742b27c6e326a65821851ce/pkg/controller/awscluster/awscluster_controller.go#L148-L153) back to the API server:
161+
162+
```golang
163+
defer func() {
164+
if err := clusterScope.Close(); err != nil && reterr == nil {
165+
reterr = err
166+
}
167+
}()
168+
```
169+
170+
4. Handles both [deleted and non-deleted](https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/0a05127734a4fb955742b27c6e326a65821851ce/pkg/controller/awscluster/awscluster_controller.go#L155-L161) clusters resources:
171+
172+
```golang
173+
// Handle deleted clusters
174+
if !awsCluster.DeletionTimestamp.IsZero() {
175+
return reconcileDelete(clusterScope)
176+
}
177+
178+
// Handle non-deleted clusters
179+
return reconcileNormal(clusterScope)
180+
```
181+
182+
### The provider machine controller
183+
184+
The new provider machine controller is a slightly larger departure from the v1alpha1 machine actuator. This is because the machine actuator was based around a _Create_, _Read_, _Update_, _Delete_ (CRUD) model. Providers implementing the v1alpha1 machine actuator would implement each of those four functions. However, this was just an abstract way to represent a Kubernetes controller's reconcile loop.
185+
186+
The new, v1alpha2, provider machine controller merely takes the same CRUD model from the v1alpha1 machine actuator and applies it to a Kubernetes reconcile activity. The CAPI provider for vSphere (CAPV) actually includes a diagram that illustrates the v1alpha1 machine actuator CRUD operations as a reconcile loop.
187+
188+
![CAPV machine reconcile](https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-vsphere/28e01e4d1037c63970181a81378f38b294972c14/docs/design/machine-controller-reconcile.svg?sanitize=true)

0 commit comments

Comments
 (0)