Skip to content

Commit 3c4fd99

Browse files
authored
Merge pull request #32 from spectrocloud/PCP-5293
PCP-5293 Upgrading controller-runtime to v0.20.4
2 parents 9376a93 + c4c5f3d commit 3c4fd99

File tree

8 files changed

+344
-734
lines changed

8 files changed

+344
-734
lines changed

controllers/configs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (r *MicroK8sControlPlaneReconciler) kubeconfigForCluster(ctx context.Contex
109109
Namespace: cluster.Namespace,
110110
Name: cluster.Name + "-kubeconfig",
111111
Labels: map[string]string{
112-
clusterv1.ClusterLabelName: cluster.Name,
112+
clusterv1.ClusterNameLabel: cluster.Name,
113113
},
114114
},
115115
Data: map[string][]byte{

controllers/microk8scontrolplane_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3737
"sigs.k8s.io/controller-runtime/pkg/handler"
3838
"sigs.k8s.io/controller-runtime/pkg/log"
39-
"sigs.k8s.io/controller-runtime/pkg/source"
4039
)
4140

4241
const requeueDuration = 30 * time.Second
@@ -203,14 +202,14 @@ func (r *MicroK8sControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, opti
203202
For(&clusterv1beta1.MicroK8sControlPlane{}).
204203
Owns(&clusterv1.Machine{}).
205204
Watches(
206-
&source.Kind{Type: &clusterv1.Cluster{}},
205+
&clusterv1.Cluster{},
207206
handler.EnqueueRequestsFromMapFunc(r.ClusterToMicroK8sControlPlane),
208207
).
209208
WithOptions(options).
210209
Complete(r)
211210
}
212211

213-
func (r *MicroK8sControlPlaneReconciler) ClusterToMicroK8sControlPlane(o client.Object) []ctrl.Request {
212+
func (r *MicroK8sControlPlaneReconciler) ClusterToMicroK8sControlPlane(ctx context.Context, o client.Object) []ctrl.Request {
214213
c, ok := o.(*clusterv1.Cluster)
215214
if !ok {
216215
fmt.Printf("expected a Cluster but got a %T\n", o)
@@ -227,8 +226,8 @@ func (r *MicroK8sControlPlaneReconciler) ClusterToMicroK8sControlPlane(o client.
227226

228227
func (r *MicroK8sControlPlaneReconciler) getControlPlaneMachinesForCluster(ctx context.Context, cluster client.ObjectKey, cpName string) ([]clusterv1.Machine, error) {
229228
selector := map[string]string{
230-
clusterv1.ClusterLabelName: cluster.Name,
231-
clusterv1.MachineControlPlaneLabelName: "",
229+
clusterv1.ClusterNameLabel: cluster.Name,
230+
clusterv1.MachineControlPlaneNameLabel: "",
232231
}
233232

234233
machineList := clusterv1.MachineList{}

controllers/reconcile.go

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ import (
99
"strings"
1010
"time"
1111

12-
clusterv1beta1 "github.com/canonical/cluster-api-control-plane-provider-microk8s/api/v1beta1"
13-
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/clusteragent"
14-
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/images"
15-
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/token"
1612
"github.com/go-logr/logr"
17-
"golang.org/x/mod/semver"
18-
1913
"github.com/pkg/errors"
14+
"golang.org/x/mod/semver"
2015
corev1 "k8s.io/api/core/v1"
2116
apierrors "k8s.io/apimachinery/pkg/api/errors"
2217
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2319
kerrors "k8s.io/apimachinery/pkg/util/errors"
2420
"k8s.io/apimachinery/pkg/util/sets"
2521
"k8s.io/apiserver/pkg/storage/names"
@@ -33,6 +29,11 @@ import (
3329
"sigs.k8s.io/controller-runtime/pkg/client"
3430
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3531
"sigs.k8s.io/controller-runtime/pkg/log"
32+
33+
clusterv1beta1 "github.com/canonical/cluster-api-control-plane-provider-microk8s/api/v1beta1"
34+
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/clusteragent"
35+
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/images"
36+
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/token"
3637
)
3738

3839
const (
@@ -331,7 +332,8 @@ func (r *MicroK8sControlPlaneReconciler) reconcileMachines(ctx context.Context,
331332
}
332333

333334
func (r *MicroK8sControlPlaneReconciler) reconcileExternalReference(ctx context.Context, ref corev1.ObjectReference, cluster *clusterv1.Cluster) error {
334-
obj, err := external.Get(ctx, r.Client, &ref, cluster.Namespace)
335+
ref.Namespace = cluster.Namespace
336+
obj, err := external.Get(ctx, r.Client, &ref)
335337
if err != nil {
336338
return err
337339
}
@@ -362,17 +364,7 @@ func (r *MicroK8sControlPlaneReconciler) bootControlPlane(ctx context.Context, c
362364
}
363365

364366
// Clone the infrastructure template
365-
infraRef, err := external.CloneTemplate(ctx, &external.CloneTemplateInput{
366-
Client: r.Client,
367-
TemplateRef: &mcp.Spec.InfrastructureTemplate,
368-
Namespace: mcp.Namespace,
369-
OwnerRef: infraCloneOwner,
370-
ClusterName: cluster.Name,
371-
Labels: map[string]string{
372-
clusterv1.ClusterLabelName: cluster.Name,
373-
clusterv1.MachineControlPlaneLabelName: "",
374-
},
375-
})
367+
infraRef, err := r.cloneInfrastructureTemplate(ctx, cluster, mcp, infraCloneOwner)
376368
if err != nil {
377369
conditions.MarkFalse(mcp, clusterv1beta1.MachinesCreatedCondition,
378370
clusterv1beta1.InfrastructureTemplateCloningFailedReason,
@@ -398,8 +390,8 @@ func (r *MicroK8sControlPlaneReconciler) bootControlPlane(ctx context.Context, c
398390
Name: names.SimpleNameGenerator.GenerateName(mcp.Name + "-"),
399391
Namespace: mcp.Namespace,
400392
Labels: map[string]string{
401-
clusterv1.ClusterLabelName: cluster.Name,
402-
clusterv1.MachineControlPlaneLabelName: "",
393+
clusterv1.ClusterNameLabel: cluster.Name,
394+
clusterv1.MachineControlPlaneNameLabel: "",
403395
},
404396
OwnerReferences: []metav1.OwnerReference{
405397
*metav1.NewControllerRef(mcp, clusterv1beta1.GroupVersion.WithKind("MicroK8sControlPlane")),
@@ -823,3 +815,57 @@ func isMachineUpgraded(m clusterv1.Machine, newVersion string) bool {
823815
newVersion = semver.MajorMinor(newVersion) // just being extra careful
824816
return semver.Compare(machineVersion, newVersion) == 0
825817
}
818+
819+
func (r *MicroK8sControlPlaneReconciler) cloneInfrastructureTemplate(ctx context.Context, cluster *clusterv1.Cluster, mcp *clusterv1beta1.MicroK8sControlPlane, owner *metav1.OwnerReference) (*corev1.ObjectReference, error) {
820+
templateRef := &mcp.Spec.InfrastructureTemplate
821+
822+
template := &unstructured.Unstructured{}
823+
template.SetAPIVersion(templateRef.APIVersion)
824+
template.SetKind(templateRef.Kind)
825+
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: mcp.Namespace, Name: templateRef.Name}, template); err != nil {
826+
return nil, errors.Wrapf(err, "failed to get infrastructure template %s", templateRef.Name)
827+
}
828+
829+
// Get the unstructured template from the template's spec
830+
templateSpec, found, err := unstructured.NestedMap(template.Object, "spec", "template")
831+
if err != nil {
832+
return nil, errors.Wrap(err, "failed to get unstructured template spec")
833+
}
834+
if !found {
835+
return nil, errors.New("spec.template not found in infrastructure template")
836+
}
837+
838+
clone := &unstructured.Unstructured{Object: templateSpec}
839+
840+
// Set the APIVersion and Kind for the clone.
841+
clone.SetAPIVersion(templateRef.APIVersion)
842+
clone.SetKind(strings.TrimSuffix(templateRef.Kind, "Template"))
843+
844+
clone.SetName(names.SimpleNameGenerator.GenerateName(mcp.Name + "-"))
845+
clone.SetNamespace(mcp.Namespace)
846+
847+
// Set owner reference.
848+
clone.SetOwnerReferences([]metav1.OwnerReference{*owner})
849+
850+
// Add cluster labels.
851+
labels := clone.GetLabels()
852+
if labels == nil {
853+
labels = make(map[string]string)
854+
}
855+
labels[clusterv1.ClusterNameLabel] = cluster.Name
856+
labels[clusterv1.MachineControlPlaneNameLabel] = ""
857+
clone.SetLabels(labels)
858+
859+
if err := r.Client.Create(ctx, clone); err != nil {
860+
return nil, errors.Wrap(err, "failed to create infrastructure clone")
861+
}
862+
863+
infraRef := &corev1.ObjectReference{
864+
APIVersion: clone.GetAPIVersion(),
865+
Kind: clone.GetKind(),
866+
Name: clone.GetName(),
867+
Namespace: clone.GetNamespace(),
868+
}
869+
870+
return infraRef, nil
871+
}

controllers/status.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
func (r *MicroK8sControlPlaneReconciler) updateStatus(ctx context.Context, mcp *clusterv1beta1.MicroK8sControlPlane, cluster *clusterv1.Cluster) error {
2121
clusterSelector := &metav1.LabelSelector{
2222
MatchLabels: map[string]string{
23-
clusterv1.ClusterLabelName: cluster.Name,
24-
clusterv1.MachineControlPlaneLabelName: "",
23+
clusterv1.ClusterNameLabel: cluster.Name,
24+
clusterv1.MachineControlPlaneNameLabel: "",
2525
},
2626
}
2727

@@ -115,7 +115,7 @@ func (r *MicroK8sControlPlaneReconciler) updateProviderID(ctx context.Context, c
115115
}
116116

117117
selector := map[string]string{
118-
clusterv1.ClusterLabelName: cluster.Name,
118+
clusterv1.ClusterNameLabel: cluster.Name,
119119
}
120120

121121
machineList := clusterv1.MachineList{}

controllers/suite_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/client-go/kubernetes/scheme"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727
"sigs.k8s.io/controller-runtime/pkg/envtest"
28-
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
2928
logf "sigs.k8s.io/controller-runtime/pkg/log"
3029
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3130

@@ -41,9 +40,7 @@ var testEnv *envtest.Environment
4140
func TestAPIs(t *testing.T) {
4241
RegisterFailHandler(Fail)
4342

44-
RunSpecsWithDefaultAndCustomReporters(t,
45-
"Controller Suite",
46-
[]Reporter{printer.NewlineReporter{}})
43+
RunSpecs(t, "Controller Suite")
4744
}
4845

4946
var _ = BeforeSuite(func() {

go.mod

Lines changed: 52 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,81 @@
11
module github.com/canonical/cluster-api-control-plane-provider-microk8s
22

3-
go 1.22
3+
go 1.23.0
44

5-
toolchain go1.22.4
5+
toolchain go1.24.6
66

77
require (
88
github.com/canonical/cluster-api-bootstrap-provider-microk8s v0.6.9
99
github.com/onsi/ginkgo v1.16.5
10-
github.com/onsi/gomega v1.22.1
11-
k8s.io/api v0.25.3
12-
k8s.io/apimachinery v0.25.3
13-
k8s.io/apiserver v0.25.3
14-
k8s.io/client-go v0.25.3
15-
sigs.k8s.io/controller-runtime v0.13.0
10+
github.com/onsi/gomega v1.36.3
11+
k8s.io/api v0.32.3
12+
k8s.io/apimachinery v0.32.3
13+
k8s.io/apiserver v0.32.3
14+
k8s.io/client-go v0.32.3
15+
sigs.k8s.io/controller-runtime v0.20.4
1616
)
1717

1818
require (
19-
cloud.google.com/go/compute v1.10.0 // indirect
20-
github.com/blang/semver v3.5.1+incompatible // indirect
21-
github.com/emicklei/go-restful/v3 v3.10.0 // indirect
22-
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
23-
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
24-
github.com/go-openapi/jsonpointer v0.19.5 // indirect
25-
github.com/go-openapi/jsonreference v0.20.0 // indirect
26-
github.com/go-openapi/swag v0.22.3 // indirect
27-
github.com/gobuffalo/flect v0.3.0 // indirect
28-
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
29-
github.com/google/gnostic v0.6.9 // indirect
19+
github.com/blang/semver/v4 v4.0.0 // indirect
20+
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
21+
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
22+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
23+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
24+
github.com/go-openapi/jsonreference v0.20.2 // indirect
25+
github.com/go-openapi/swag v0.23.0 // indirect
26+
github.com/gobuffalo/flect v1.0.3 // indirect
27+
github.com/google/btree v1.1.3 // indirect
28+
github.com/google/gnostic-models v0.6.8 // indirect
3029
github.com/josharian/intern v1.0.0 // indirect
3130
github.com/mailru/easyjson v0.7.7 // indirect
3231
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
32+
github.com/x448/float16 v0.8.4 // indirect
33+
golang.org/x/sync v0.12.0 // indirect
34+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
3335
)
3436

3537
require (
36-
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
37-
github.com/Azure/go-autorest/autorest v0.11.28 // indirect
38-
github.com/Azure/go-autorest/autorest/adal v0.9.21 // indirect
39-
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
40-
github.com/Azure/go-autorest/logger v0.2.1 // indirect
41-
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
4238
github.com/beorn7/perks v1.0.1 // indirect
43-
github.com/cespare/xxhash/v2 v2.1.2 // indirect
44-
github.com/davecgh/go-spew v1.1.1 // indirect
45-
github.com/fsnotify/fsnotify v1.6.0 // indirect
46-
github.com/go-logr/logr v1.2.3
47-
github.com/go-logr/zapr v1.2.3 // indirect
39+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
40+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
41+
github.com/fsnotify/fsnotify v1.8.0 // indirect
42+
github.com/go-logr/logr v1.4.2
43+
github.com/go-logr/zapr v1.3.0 // indirect
4844
github.com/gogo/protobuf v1.3.2 // indirect
49-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
50-
github.com/golang/protobuf v1.5.2 // indirect
51-
github.com/google/go-cmp v0.5.9 // indirect
45+
github.com/golang/protobuf v1.5.4 // indirect
46+
github.com/google/go-cmp v0.7.0 // indirect
5247
github.com/google/gofuzz v1.2.0 // indirect
53-
github.com/google/uuid v1.3.0 // indirect
54-
github.com/imdario/mergo v0.3.13 // indirect
48+
github.com/google/uuid v1.6.0 // indirect
5549
github.com/json-iterator/go v1.1.12 // indirect
56-
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
5750
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5851
github.com/modern-go/reflect2 v1.0.2 // indirect
5952
github.com/nxadm/tail v1.4.8 // indirect
6053
github.com/pkg/errors v0.9.1
61-
github.com/prometheus/client_golang v1.13.0 // indirect
62-
github.com/prometheus/client_model v0.3.0 // indirect
63-
github.com/prometheus/common v0.37.0 // indirect
64-
github.com/prometheus/procfs v0.8.0 // indirect
65-
github.com/spf13/pflag v1.0.5 // indirect
66-
go.uber.org/atomic v1.10.0 // indirect
67-
go.uber.org/multierr v1.8.0 // indirect
68-
go.uber.org/zap v1.23.0 // indirect
69-
golang.org/x/crypto v0.31.0 // indirect
70-
golang.org/x/mod v0.17.0
71-
golang.org/x/net v0.33.0 // indirect
72-
golang.org/x/oauth2 v0.1.0 // indirect
73-
golang.org/x/sys v0.28.0 // indirect
74-
golang.org/x/term v0.27.0 // indirect
75-
golang.org/x/text v0.21.0 // indirect
76-
golang.org/x/time v0.1.0 // indirect
77-
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
78-
google.golang.org/appengine v1.6.7 // indirect
79-
google.golang.org/protobuf v1.28.1 // indirect
54+
github.com/prometheus/client_golang v1.19.1 // indirect
55+
github.com/prometheus/client_model v0.6.1 // indirect
56+
github.com/prometheus/common v0.55.0 // indirect
57+
github.com/prometheus/procfs v0.15.1 // indirect
58+
github.com/spf13/pflag v1.0.6 // indirect
59+
go.uber.org/multierr v1.11.0 // indirect
60+
go.uber.org/zap v1.27.0 // indirect
61+
golang.org/x/mod v0.21.0
62+
golang.org/x/net v0.38.0 // indirect
63+
golang.org/x/oauth2 v0.28.0 // indirect
64+
golang.org/x/sys v0.31.0 // indirect
65+
golang.org/x/term v0.30.0 // indirect
66+
golang.org/x/text v0.23.0 // indirect
67+
golang.org/x/time v0.8.0 // indirect
68+
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
69+
google.golang.org/protobuf v1.36.5 // indirect
8070
gopkg.in/inf.v0 v0.9.1 // indirect
8171
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
82-
gopkg.in/yaml.v2 v2.4.0 // indirect
8372
gopkg.in/yaml.v3 v3.0.1 // indirect
84-
k8s.io/apiextensions-apiserver v0.25.3 // indirect
85-
k8s.io/component-base v0.25.3 // indirect
86-
k8s.io/klog/v2 v2.80.1 // indirect
87-
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
73+
k8s.io/apiextensions-apiserver v0.32.3 // indirect
74+
k8s.io/klog/v2 v2.130.1 // indirect
75+
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
8876
k8s.io/utils v0.0.0-20241210054802-24370beab758
89-
sigs.k8s.io/cluster-api v1.2.4
90-
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
91-
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
92-
sigs.k8s.io/yaml v1.3.0 // indirect
77+
sigs.k8s.io/cluster-api v1.10.4
78+
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
79+
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect
80+
sigs.k8s.io/yaml v1.4.0 // indirect
9381
)

0 commit comments

Comments
 (0)