Skip to content

Commit 6161888

Browse files
committed
⚠️ Add support to auto-update external references
Signed-off-by: Vince Prignano <[email protected]>
1 parent 5aea6d2 commit 6161888

File tree

15 files changed

+314
-66
lines changed

15 files changed

+314
-66
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/

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/external/testing.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
"k8s.io/utils/pointer"
23+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2324
)
2425

2526
var (
@@ -30,6 +31,9 @@ var (
3031
},
3132
ObjectMeta: metav1.ObjectMeta{
3233
Name: "genericmachines.bootstrap.cluster.x-k8s.io",
34+
Labels: map[string]string{
35+
clusterv1.GroupVersion.String(): "v1alpha3",
36+
},
3337
},
3438
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
3539
Group: "bootstrap.cluster.x-k8s.io",
@@ -69,6 +73,9 @@ var (
6973
},
7074
ObjectMeta: metav1.ObjectMeta{
7175
Name: "genericmachinetemplates.bootstrap.cluster.x-k8s.io",
76+
Labels: map[string]string{
77+
clusterv1.GroupVersion.String(): "v1alpha3",
78+
},
7279
},
7380
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
7481
Group: "bootstrap.cluster.x-k8s.io",
@@ -108,6 +115,9 @@ var (
108115
},
109116
ObjectMeta: metav1.ObjectMeta{
110117
Name: "genericmachines.infrastructure.cluster.x-k8s.io",
118+
Labels: map[string]string{
119+
clusterv1.GroupVersion.String(): "v1alpha3",
120+
},
111121
},
112122
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
113123
Group: "infrastructure.cluster.x-k8s.io",
@@ -147,6 +157,9 @@ var (
147157
},
148158
ObjectMeta: metav1.ObjectMeta{
149159
Name: "genericmachinetemplates.infrastructure.cluster.x-k8s.io",
160+
Labels: map[string]string{
161+
clusterv1.GroupVersion.String(): "v1alpha3",
162+
},
150163
},
151164
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
152165
Group: "infrastructure.cluster.x-k8s.io",

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

0 commit comments

Comments
 (0)