Skip to content

Commit e1319b5

Browse files
committed
🏃 Add support to auto-update external references
Signed-off-by: Vince Prignano <[email protected]>
1 parent 5ac3f42 commit e1319b5

File tree

9 files changed

+118
-5
lines changed

9 files changed

+118
-5
lines changed

bootstrap/kubeadm/config/crd/kustomization.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
commonLabels:
2+
cluster.x-k8s.io/v1alpha2: v1alpha2
3+
cluster.x-k8s.io/v1alpha3: v1alpha3
4+
15
# This kustomization.yaml is not intended to be run by itself,
26
# since it depends on service name and namespace that are out of this kustomize package.
37
# It should be run by config/default

controllers/cluster_controller_phases.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"sigs.k8s.io/cluster-api/controllers/external"
3030
capierrors "sigs.k8s.io/cluster-api/errors"
3131
"sigs.k8s.io/cluster-api/util"
32+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
3233
"sigs.k8s.io/cluster-api/util/kubeconfig"
3334
"sigs.k8s.io/cluster-api/util/patch"
3435
"sigs.k8s.io/cluster-api/util/secret"
@@ -62,6 +63,10 @@ func (r *ClusterReconciler) reconcilePhase(_ context.Context, cluster *clusterv1
6263
func (r *ClusterReconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.Cluster, ref *corev1.ObjectReference) (external.ReconcileOutput, error) {
6364
logger := r.Log.WithValues("cluster", cluster.Name, "namespace", cluster.Namespace)
6465

66+
if err := utilconversion.ConvertReferenceAPIContract(ctx, r.Client, ref); err != nil {
67+
return external.ReconcileOutput{}, err
68+
}
69+
6570
obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace)
6671
if err != nil {
6772
if apierrors.IsNotFound(errors.Cause(err)) {

controllers/machine_controller_phases.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
2828
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2929
"k8s.io/utils/pointer"
30+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
3031
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3132
"sigs.k8s.io/controller-runtime/pkg/handler"
3233

@@ -77,6 +78,10 @@ func (r *MachineReconciler) reconcilePhase(_ context.Context, m *clusterv1.Machi
7778
func (r *MachineReconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.Cluster, m *clusterv1.Machine, ref *corev1.ObjectReference) (external.ReconcileOutput, error) {
7879
logger := r.Log.WithValues("machine", m.Name, "namespace", m.Namespace)
7980

81+
if err := utilconversion.ConvertReferenceAPIContract(ctx, r.Client, ref); err != nil {
82+
return external.ReconcileOutput{}, err
83+
}
84+
8085
obj, err := external.Get(ctx, r.Client, ref, m.Namespace)
8186
if err != nil {
8287
if apierrors.IsNotFound(errors.Cause(err)) {
@@ -146,6 +151,7 @@ func (r *MachineReconciler) reconcileBootstrap(ctx context.Context, cluster *clu
146151
// Call generic external reconciler if we have an external reference.
147152
var bootstrapConfig *unstructured.Unstructured
148153
if m.Spec.Bootstrap.ConfigRef != nil {
154+
149155
bootstrapReconcileResult, err := r.reconcileExternal(ctx, cluster, m, m.Spec.Bootstrap.ConfigRef)
150156
if err != nil {
151157
return err

controllers/machinedeployment_controller.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223

2324
"github.com/go-logr/logr"
2425
"github.com/pkg/errors"
@@ -29,7 +30,9 @@ import (
2930
kerrors "k8s.io/apimachinery/pkg/util/errors"
3031
"k8s.io/client-go/tools/record"
3132
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
33+
"sigs.k8s.io/cluster-api/controllers/external"
3234
"sigs.k8s.io/cluster-api/util"
35+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
3336
"sigs.k8s.io/cluster-api/util/patch"
3437
ctrl "sigs.k8s.io/controller-runtime"
3538
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -131,7 +134,7 @@ func (r *MachineDeploymentReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result
131134
return result, nil
132135
}
133136

134-
func (r *MachineDeploymentReconciler) reconcile(_ context.Context, cluster *clusterv1.Cluster, d *clusterv1.MachineDeployment) (ctrl.Result, error) {
137+
func (r *MachineDeploymentReconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster, d *clusterv1.MachineDeployment) (ctrl.Result, error) {
135138
logger := r.Log.WithValues("machinedeployment", d.Name, "namespace", d.Namespace)
136139
logger.V(4).Info("Reconcile MachineDeployment")
137140

@@ -166,6 +169,17 @@ func (r *MachineDeploymentReconciler) reconcile(_ context.Context, cluster *clus
166169
return ctrl.Result{}, nil
167170
}
168171

172+
// Make sure to reconcile the external infrastructure reference.
173+
if err := r.reconcileExternalReference(ctx, cluster, &d.Spec.Template.Spec.InfrastructureRef); err != nil {
174+
return ctrl.Result{}, err
175+
}
176+
// Make sure to reconcile the external bootstrap reference, if any.
177+
if d.Spec.Template.Spec.Bootstrap.ConfigRef != nil {
178+
if err := r.reconcileExternalReference(ctx, cluster, d.Spec.Template.Spec.Bootstrap.ConfigRef); err != nil {
179+
return ctrl.Result{}, err
180+
}
181+
}
182+
169183
msList, err := r.getMachineSetsForDeployment(d)
170184
if err != nil {
171185
return ctrl.Result{}, err
@@ -182,6 +196,18 @@ func (r *MachineDeploymentReconciler) reconcile(_ context.Context, cluster *clus
182196
return ctrl.Result{}, errors.Errorf("unexpected deployment strategy type: %s", d.Spec.Strategy.Type)
183197
}
184198

199+
func (r *MachineDeploymentReconciler) reconcileExternalReference(ctx context.Context, cluster *clusterv1.Cluster, ref *corev1.ObjectReference) error {
200+
if !strings.HasSuffix(ref.Kind, external.TemplateSuffix) {
201+
return nil
202+
}
203+
204+
if err := utilconversion.ConvertReferenceAPIContract(ctx, r.Client, ref); err != nil {
205+
return err
206+
}
207+
208+
return nil
209+
}
210+
185211
// getMachineSetsForDeployment returns a list of MachineSets associated with a MachineDeployment.
186212
func (r *MachineDeploymentReconciler) getMachineSetsForDeployment(d *clusterv1.MachineDeployment) ([]*clusterv1.MachineSet, error) {
187213
logger := r.Log.WithValues("machinedeployemnt", d.Name, "namespace", d.Namespace)

controllers/machineset_controller.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"sigs.k8s.io/cluster-api/controllers/noderefutil"
3939
"sigs.k8s.io/cluster-api/controllers/remote"
4040
"sigs.k8s.io/cluster-api/util"
41+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
4142
"sigs.k8s.io/cluster-api/util/patch"
4243
ctrl "sigs.k8s.io/controller-runtime"
4344
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -163,12 +164,12 @@ func (r *MachineSetReconciler) reconcile(ctx context.Context, cluster *clusterv1
163164
}
164165

165166
// Make sure to reconcile the external infrastructure reference.
166-
if err := r.reconcileExternalReference(ctx, cluster, machineSet.Spec.Template.Spec.InfrastructureRef); err != nil {
167+
if err := r.reconcileExternalReference(ctx, cluster, &machineSet.Spec.Template.Spec.InfrastructureRef); err != nil {
167168
return ctrl.Result{}, err
168169
}
169170
// Make sure to reconcile the external bootstrap reference, if any.
170171
if machineSet.Spec.Template.Spec.Bootstrap.ConfigRef != nil {
171-
if err := r.reconcileExternalReference(ctx, cluster, *machineSet.Spec.Template.Spec.Bootstrap.ConfigRef); err != nil {
172+
if err := r.reconcileExternalReference(ctx, cluster, machineSet.Spec.Template.Spec.Bootstrap.ConfigRef); err != nil {
172173
return ctrl.Result{}, err
173174
}
174175
}
@@ -268,12 +269,16 @@ func (r *MachineSetReconciler) reconcile(ctx context.Context, cluster *clusterv1
268269
return ctrl.Result{}, nil
269270
}
270271

271-
func (r *MachineSetReconciler) reconcileExternalReference(ctx context.Context, cluster *clusterv1.Cluster, ref corev1.ObjectReference) error {
272+
func (r *MachineSetReconciler) reconcileExternalReference(ctx context.Context, cluster *clusterv1.Cluster, ref *corev1.ObjectReference) error {
272273
if !strings.HasSuffix(ref.Kind, external.TemplateSuffix) {
273274
return nil
274275
}
275276

276-
obj, err := external.Get(ctx, r.Client, &ref, cluster.Namespace)
277+
if err := utilconversion.ConvertReferenceAPIContract(ctx, r.Client, ref); err != nil {
278+
return err
279+
}
280+
281+
obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace)
277282
if err != nil {
278283
return err
279284
}

controlplane/kubeadm/config/crd/kustomization.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
commonLabels:
2+
cluster.x-k8s.io/v1alpha3: v1alpha3
3+
14
# This kustomization.yaml is not intended to be run by itself,
25
# since it depends on service name and namespace that are out of this kustomize package.
36
# It should be run by config/default

docs/book/src/developer/providers/v1alpha2-to-v1alpha3.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,22 @@ unique keys to `failureDomainSpec`s as well as respecting a set `Machine.Spec.Fa
154154
instances.
155155

156156
Please see the cluster and machine infrastructure provider specifications for more detail.
157+
158+
# Apply the contract version label `cluster.x-k8s.io/<version>: version1,version2,version3` to your CRDs
159+
160+
- Providers MUST set `cluster.x-k8s.io/<version>` labels on all Custom Resource Definitions related to Cluster API starting with v1alpha3.
161+
- The label is a map from an API Version of Cluster API (contract) to your Custom Resource Definition versions.
162+
- The value is a comma-delimited list of versions.
163+
- Each value MUST point to an available version in your CRD Spec.
164+
- The label allows Cluster API controllers to perform automatic conversions for object references, the controllers will
165+
pick the last available version in the list if multiple versions are found.
166+
- To apply the label to CRDs it's possible to use `commonLabels` in your `kustomize.yaml` file, usually in `config/crd`.
167+
168+
In this example we show how to map a particular Cluster API contract version to your own CRD using Kustomize's `commonLabels` feature:
169+
170+
```yaml
171+
commonLabels:
172+
cluster.x-k8s.io/v1alpha2: v1alpha1
173+
cluster.x-k8s.io/v1alpha3: v1alpha2
174+
cluster.x-k8s.io/v1beta1: v1alphaX,v1beta1
175+
```

test/infrastructure/docker/config/crd/kustomization.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
commonLabels:
2+
cluster.x-k8s.io/v1alpha3: v1alpha3
3+
14
# This kustomization.yaml is not intended to be run by itself,
25
# since it depends on service name and namespace that are out of this kustomize package.
36
# It should be run by config/default

util/conversion/conversion.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ limitations under the License.
1717
package conversion
1818

1919
import (
20+
"context"
2021
"math/rand"
22+
"strings"
2123
"testing"
2224

2325
fuzz "github.com/google/gofuzz"
2426
"github.com/onsi/gomega"
27+
"github.com/pkg/errors"
28+
corev1 "k8s.io/api/core/v1"
29+
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2530
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
2631
apiequality "k8s.io/apimachinery/pkg/api/equality"
2732
metafuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer"
@@ -30,13 +35,50 @@ import (
3035
"k8s.io/apimachinery/pkg/runtime/serializer"
3136
"k8s.io/apimachinery/pkg/util/json"
3237
"k8s.io/utils/diff"
38+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
39+
"sigs.k8s.io/controller-runtime/pkg/client"
3340
"sigs.k8s.io/controller-runtime/pkg/conversion"
3441
)
3542

3643
const (
3744
DataAnnotation = "cluster.x-k8s.io/conversion-data"
3845
)
3946

47+
var (
48+
contract = clusterv1.GroupVersion.String()
49+
)
50+
51+
// ConvertReferenceAPIContract takes a client and object reference, queries the API Server for
52+
// the Custom Resource Definition and looks which one is the stored version available.
53+
//
54+
// The object passed as input is modified in place if an updated compatible version is found.
55+
func ConvertReferenceAPIContract(ctx context.Context, c client.Client, ref *corev1.ObjectReference) error {
56+
// TODO(vincepri): Use CRDv1 once we have support for it.
57+
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
58+
key := client.ObjectKey{Name: ref.Kind}
59+
if err := c.Get(ctx, key, crd); err != nil {
60+
return errors.Wrapf(err, "cannot find CustomResourceDefinition for %v", ref.GroupVersionKind())
61+
}
62+
63+
// If there is no label, return early without changing the reference.
64+
supportedVersions, ok := crd.Labels[contract]
65+
if !ok {
66+
return nil
67+
}
68+
69+
// If the label is present, but there list is empty, return an error.
70+
versions := strings.Split(supportedVersions, ",")
71+
if len(versions) == 0 {
72+
return errors.Errorf("cannot find any version matching contract %q for CRD %v", contract, crd.Name)
73+
}
74+
75+
// Modify the GroupVersionKind with the new version.
76+
gvk := ref.GroupVersionKind()
77+
gvk.Version = versions[len(versions)-1]
78+
ref.SetGroupVersionKind(gvk)
79+
return nil
80+
}
81+
4082
// MarshalData stores the source object as json data in the destination object annotations map.
4183
func MarshalData(src metav1.Object, dst metav1.Object) error {
4284
data, err := json.Marshal(src)

0 commit comments

Comments
 (0)