Skip to content

Commit f8fd8cc

Browse files
committed
Adjust controller setup, mapping funcs and fakeclient
Signed-off-by: Stefan Büringer [email protected]
1 parent 846b9e4 commit f8fd8cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+204
-218
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"sigs.k8s.io/controller-runtime/pkg/client"
3939
"sigs.k8s.io/controller-runtime/pkg/controller"
4040
"sigs.k8s.io/controller-runtime/pkg/handler"
41-
"sigs.k8s.io/controller-runtime/pkg/source"
4241

4342
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4443
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
@@ -117,19 +116,19 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
117116
For(&bootstrapv1.KubeadmConfig{}).
118117
WithOptions(options).
119118
Watches(
120-
&source.Kind{Type: &clusterv1.Machine{}},
119+
&clusterv1.Machine{},
121120
handler.EnqueueRequestsFromMapFunc(r.MachineToBootstrapMapFunc),
122121
).WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue))
123122

124123
if feature.Gates.Enabled(feature.MachinePool) {
125124
b = b.Watches(
126-
&source.Kind{Type: &expv1.MachinePool{}},
125+
&expv1.MachinePool{},
127126
handler.EnqueueRequestsFromMapFunc(r.MachinePoolToBootstrapMapFunc),
128127
)
129128
}
130129

131130
b = b.Watches(
132-
&source.Kind{Type: &clusterv1.Cluster{}},
131+
&clusterv1.Cluster{},
133132
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmConfigs),
134133
builder.WithPredicates(
135134
predicates.All(ctrl.LoggerFrom(ctx),
@@ -807,7 +806,7 @@ func (r *KubeadmConfigReconciler) resolveSecretPasswordContent(ctx context.Conte
807806

808807
// ClusterToKubeadmConfigs is a handler.ToRequestsFunc to be used to enqueue
809808
// requests for reconciliation of KubeadmConfigs.
810-
func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(o client.Object) []ctrl.Request {
809+
func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(ctx context.Context, o client.Object) []ctrl.Request {
811810
result := []ctrl.Request{}
812811

813812
c, ok := o.(*clusterv1.Cluster)
@@ -823,7 +822,7 @@ func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(o client.Object) []ctr
823822
}
824823

825824
machineList := &clusterv1.MachineList{}
826-
if err := r.Client.List(context.TODO(), machineList, selectors...); err != nil {
825+
if err := r.Client.List(ctx, machineList, selectors...); err != nil {
827826
return nil
828827
}
829828

@@ -837,7 +836,7 @@ func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(o client.Object) []ctr
837836

838837
if feature.Gates.Enabled(feature.MachinePool) {
839838
machinePoolList := &expv1.MachinePoolList{}
840-
if err := r.Client.List(context.TODO(), machinePoolList, selectors...); err != nil {
839+
if err := r.Client.List(ctx, machinePoolList, selectors...); err != nil {
841840
return nil
842841
}
843842

@@ -855,7 +854,7 @@ func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(o client.Object) []ctr
855854

856855
// MachineToBootstrapMapFunc is a handler.ToRequestsFunc to be used to enqueue
857856
// request for reconciliation of KubeadmConfig.
858-
func (r *KubeadmConfigReconciler) MachineToBootstrapMapFunc(o client.Object) []ctrl.Request {
857+
func (r *KubeadmConfigReconciler) MachineToBootstrapMapFunc(_ context.Context, o client.Object) []ctrl.Request {
859858
m, ok := o.(*clusterv1.Machine)
860859
if !ok {
861860
panic(fmt.Sprintf("Expected a Machine but got a %T", o))
@@ -871,7 +870,7 @@ func (r *KubeadmConfigReconciler) MachineToBootstrapMapFunc(o client.Object) []c
871870

872871
// MachinePoolToBootstrapMapFunc is a handler.ToRequestsFunc to be used to enqueue
873872
// request for reconciliation of KubeadmConfig.
874-
func (r *KubeadmConfigReconciler) MachinePoolToBootstrapMapFunc(o client.Object) []ctrl.Request {
873+
func (r *KubeadmConfigReconciler) MachinePoolToBootstrapMapFunc(_ context.Context, o client.Object) []ctrl.Request {
875874
m, ok := o.(*expv1.MachinePool)
876875
if !ok {
877876
panic(fmt.Sprintf("Expected a MachinePool but got a %T", o))

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestKubeadmConfigReconciler_MachineToBootstrapMapFuncReturn(t *testing.T) {
8080
}
8181
for i := 0; i < 3; i++ {
8282
o := machineObjs[i]
83-
configs := reconciler.MachineToBootstrapMapFunc(o)
83+
configs := reconciler.MachineToBootstrapMapFunc(ctx, o)
8484
if i == 1 {
8585
g.Expect(configs[0].Name).To(Equal(expectedConfigName))
8686
} else {
@@ -158,7 +158,7 @@ func TestKubeadmConfigReconciler_TestSecretOwnerReferenceReconciliation(t *testi
158158
secret,
159159
cluster,
160160
}
161-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
161+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
162162

163163
k := &KubeadmConfigReconciler{
164164
Client: myclient,
@@ -272,7 +272,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasDataSecretName
272272
machine,
273273
config,
274274
}
275-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
275+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
276276

277277
k := &KubeadmConfigReconciler{
278278
Client: myclient,
@@ -315,7 +315,7 @@ func TestKubeadmConfigReconciler_ReturnEarlyIfClusterInfraNotReady(t *testing.T)
315315
machine,
316316
config,
317317
}
318-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
318+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
319319

320320
k := &KubeadmConfigReconciler{
321321
Client: myclient,
@@ -450,7 +450,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
450450
t.Run(tc.name, func(t *testing.T) {
451451
g := NewWithT(t)
452452

453-
myclient := fake.NewClientBuilder().WithObjects(tc.objects...).Build()
453+
myclient := fake.NewClientBuilder().WithObjects(tc.objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
454454

455455
k := &KubeadmConfigReconciler{
456456
Client: myclient,
@@ -492,7 +492,7 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
492492
}
493493
objects = append(objects, createSecrets(t, cluster, controlPlaneInitConfig)...)
494494

495-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
495+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
496496

497497
k := &KubeadmConfigReconciler{
498498
Client: myclient,
@@ -553,7 +553,7 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
553553
controlPlaneJoinConfig,
554554
}
555555
objects = append(objects, createSecrets(t, cluster, controlPlaneInitConfig)...)
556-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
556+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
557557

558558
k := &KubeadmConfigReconciler{
559559
Client: myclient,
@@ -597,7 +597,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
597597
}
598598
objects = append(objects, createSecrets(t, cluster, controlPlaneInitConfig)...)
599599

600-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
600+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
601601

602602
k := &KubeadmConfigReconciler{
603603
Client: myclient,
@@ -675,7 +675,7 @@ func TestReconcileIfJoinCertificatesAvailableConditioninNodesAndControlPlaneIsRe
675675
config,
676676
}
677677
objects = append(objects, createSecrets(t, cluster, config)...)
678-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
678+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
679679
k := &KubeadmConfigReconciler{
680680
Client: myclient,
681681
KubeadmInitLock: &myInitLocker{},
@@ -752,7 +752,7 @@ func TestReconcileIfJoinNodePoolsAndControlPlaneIsReady(t *testing.T) {
752752
config,
753753
}
754754
objects = append(objects, createSecrets(t, cluster, config)...)
755-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
755+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
756756
k := &KubeadmConfigReconciler{
757757
Client: myclient,
758758
KubeadmInitLock: &myInitLocker{},
@@ -851,7 +851,7 @@ func TestBootstrapDataFormat(t *testing.T) {
851851
}
852852
objects = append(objects, createSecrets(t, cluster, config)...)
853853

854-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
854+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
855855

856856
k := &KubeadmConfigReconciler{
857857
Client: myclient,
@@ -932,7 +932,7 @@ func TestKubeadmConfigSecretCreatedStatusNotPatched(t *testing.T) {
932932
}
933933

934934
objects = append(objects, createSecrets(t, cluster, initConfig)...)
935-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
935+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
936936
k := &KubeadmConfigReconciler{
937937
Client: myclient,
938938
KubeadmInitLock: &myInitLocker{},
@@ -1009,7 +1009,7 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
10091009
}
10101010

10111011
objects = append(objects, createSecrets(t, cluster, initConfig)...)
1012-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
1012+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}, &clusterv1.Machine{}).Build()
10131013
k := &KubeadmConfigReconciler{
10141014
Client: myclient,
10151015
KubeadmInitLock: &myInitLocker{},
@@ -1210,7 +1210,7 @@ func TestBootstrapTokenRotationMachinePool(t *testing.T) {
12101210
}
12111211

12121212
objects = append(objects, createSecrets(t, cluster, initConfig)...)
1213-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
1213+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}, &expv1.MachinePool{}).Build()
12141214
k := &KubeadmConfigReconciler{
12151215
Client: myclient,
12161216
KubeadmInitLock: &myInitLocker{},
@@ -1766,7 +1766,7 @@ func TestKubeadmConfigReconciler_ClusterToKubeadmConfigs(t *testing.T) {
17661766
reconciler := &KubeadmConfigReconciler{
17671767
Client: fakeClient,
17681768
}
1769-
configs := reconciler.ClusterToKubeadmConfigs(cluster)
1769+
configs := reconciler.ClusterToKubeadmConfigs(ctx, cluster)
17701770
names := make([]string, 6)
17711771
for i := range configs {
17721772
names[i] = configs[i].Name
@@ -1835,7 +1835,7 @@ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitiali
18351835
controlPlaneInitMachineSecond,
18361836
controlPlaneInitConfigSecond,
18371837
}
1838-
myclient := fake.NewClientBuilder().WithObjects(objects...).Build()
1838+
myclient := fake.NewClientBuilder().WithObjects(objects...).WithStatusSubresource(&bootstrapv1.KubeadmConfig{}).Build()
18391839
k := &KubeadmConfigReconciler{
18401840
Client: myclient,
18411841
KubeadmInitLock: &myInitLocker{},

bootstrap/kubeadm/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"sigs.k8s.io/controller-runtime/pkg/cache"
4444
"sigs.k8s.io/controller-runtime/pkg/client"
4545
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
46-
"sigs.k8s.io/controller-runtime/pkg/config"
4746
"sigs.k8s.io/controller-runtime/pkg/controller"
4847
"sigs.k8s.io/controller-runtime/pkg/webhook"
4948

@@ -276,5 +275,5 @@ func setupWebhooks(mgr ctrl.Manager) {
276275
}
277276

278277
func concurrency(c int) controller.Options {
279-
return controller.Options{Controller: config.Controller{MaxConcurrentReconciles: c}}
278+
return controller.Options{MaxConcurrentReconciles: c}
280279
}

controlplane/kubeadm/internal/controllers/controller.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"sigs.k8s.io/controller-runtime/pkg/controller"
3737
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3838
"sigs.k8s.io/controller-runtime/pkg/handler"
39-
"sigs.k8s.io/controller-runtime/pkg/source"
4039

4140
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4241
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
@@ -100,7 +99,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
10099
WithOptions(options).
101100
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
102101
Watches(
103-
&source.Kind{Type: &clusterv1.Cluster{}},
102+
&clusterv1.Cluster{},
104103
handler.EnqueueRequestsFromMapFunc(r.ClusterToKubeadmControlPlane),
105104
builder.WithPredicates(
106105
predicates.All(ctrl.LoggerFrom(ctx),
@@ -524,7 +523,7 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, clu
524523

525524
// ClusterToKubeadmControlPlane is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
526525
// for KubeadmControlPlane based on updates to a Cluster.
527-
func (r *KubeadmControlPlaneReconciler) ClusterToKubeadmControlPlane(o client.Object) []ctrl.Request {
526+
func (r *KubeadmControlPlaneReconciler) ClusterToKubeadmControlPlane(_ context.Context, o client.Object) []ctrl.Request {
528527
c, ok := o.(*clusterv1.Cluster)
529528
if !ok {
530529
panic(fmt.Sprintf("Expected a Cluster but got a %T", o))

controlplane/kubeadm/internal/controllers/controller_test.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestClusterToKubeadmControlPlane(t *testing.T) {
9191
recorder: record.NewFakeRecorder(32),
9292
}
9393

94-
got := r.ClusterToKubeadmControlPlane(cluster)
94+
got := r.ClusterToKubeadmControlPlane(ctx, cluster)
9595
g.Expect(got).To(Equal(expectedResult))
9696
}
9797

@@ -106,7 +106,7 @@ func TestClusterToKubeadmControlPlaneNoControlPlane(t *testing.T) {
106106
recorder: record.NewFakeRecorder(32),
107107
}
108108

109-
got := r.ClusterToKubeadmControlPlane(cluster)
109+
got := r.ClusterToKubeadmControlPlane(ctx, cluster)
110110
g.Expect(got).To(BeNil())
111111
}
112112

@@ -129,7 +129,7 @@ func TestClusterToKubeadmControlPlaneOtherControlPlane(t *testing.T) {
129129
recorder: record.NewFakeRecorder(32),
130130
}
131131

132-
got := r.ClusterToKubeadmControlPlane(cluster)
132+
got := r.ClusterToKubeadmControlPlane(ctx, cluster)
133133
g.Expect(got).To(BeNil())
134134
}
135135

@@ -246,7 +246,8 @@ func TestReconcileNoClusterOwnerRef(t *testing.T) {
246246
},
247247
}
248248
kcp.Default()
249-
g.Expect(kcp.ValidateCreate()).To(Succeed())
249+
_, err := kcp.ValidateCreate()
250+
g.Expect(err).NotTo(HaveOccurred())
250251

251252
fakeClient := newFakeClient(kcp.DeepCopy())
252253
r := &KubeadmControlPlaneReconciler{
@@ -322,15 +323,16 @@ func TestReconcileNoCluster(t *testing.T) {
322323
},
323324
}
324325
kcp.Default()
325-
g.Expect(kcp.ValidateCreate()).To(Succeed())
326+
_, err := kcp.ValidateCreate()
327+
g.Expect(err).NotTo(HaveOccurred())
326328

327329
fakeClient := newFakeClient(kcp.DeepCopy())
328330
r := &KubeadmControlPlaneReconciler{
329331
Client: fakeClient,
330332
recorder: record.NewFakeRecorder(32),
331333
}
332334

333-
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: util.ObjectKey(kcp)})
335+
_, err = r.Reconcile(ctx, ctrl.Request{NamespacedName: util.ObjectKey(kcp)})
334336
g.Expect(err).To(HaveOccurred())
335337

336338
machineList := &clusterv1.MachineList{}
@@ -371,14 +373,15 @@ func TestReconcilePaused(t *testing.T) {
371373
},
372374
}
373375
kcp.Default()
374-
g.Expect(kcp.ValidateCreate()).To(Succeed())
376+
_, err := kcp.ValidateCreate()
377+
g.Expect(err).NotTo(HaveOccurred())
375378
fakeClient := newFakeClient(kcp.DeepCopy(), cluster.DeepCopy())
376379
r := &KubeadmControlPlaneReconciler{
377380
Client: fakeClient,
378381
recorder: record.NewFakeRecorder(32),
379382
}
380383

381-
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: util.ObjectKey(kcp)})
384+
_, err = r.Reconcile(ctx, ctrl.Request{NamespacedName: util.ObjectKey(kcp)})
382385
g.Expect(err).NotTo(HaveOccurred())
383386

384387
machineList := &clusterv1.MachineList{}
@@ -424,7 +427,8 @@ func TestReconcileClusterNoEndpoints(t *testing.T) {
424427
},
425428
}
426429
kcp.Default()
427-
g.Expect(kcp.ValidateCreate()).To(Succeed())
430+
_, err := kcp.ValidateCreate()
431+
g.Expect(err).NotTo(HaveOccurred())
428432

429433
fakeClient := newFakeClient(kcp.DeepCopy(), cluster.DeepCopy())
430434
r := &KubeadmControlPlaneReconciler{
@@ -2165,7 +2169,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) {
21652169
func newFakeClient(initObjs ...client.Object) client.Client {
21662170
return &fakeClient{
21672171
startTime: time.Now(),
2168-
Client: fake.NewClientBuilder().WithObjects(initObjs...).Build(),
2172+
Client: fake.NewClientBuilder().WithObjects(initObjs...).WithStatusSubresource(&controlplanev1.KubeadmControlPlane{}).Build(),
21692173
}
21702174
}
21712175

0 commit comments

Comments
 (0)