Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit b6b0f8f

Browse files
committed
🐛 Fix a Machine -> KubeadmConfig mapping bug
Co-authored-by: [email protected]
1 parent 46acfbd commit b6b0f8f

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

controllers/kubeadmconfig_controller.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
7373
Watches(
7474
&source.Kind{Type: &clusterv1.Machine{}},
7575
&handler.EnqueueRequestsFromMapFunc{
76-
ToRequests: util.MachineToInfrastructureMapFunc(bootstrapv1.GroupVersion.WithKind("KubeadmConfig")),
76+
ToRequests: handler.ToRequestsFunc(r.MachineToBootstrapMapFunc),
7777
},
7878
).
7979
Watches(
@@ -136,7 +136,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
136136
log.Info("Cluster does not exist yet , waiting until it is created")
137137
return ctrl.Result{}, nil
138138
}
139-
140139
log.Error(err, "could not get cluster by machine metadata")
141140
return ctrl.Result{}, err
142141
}
@@ -400,6 +399,22 @@ func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(o handler.MapObject) [
400399
return result
401400
}
402401

402+
// MachineToBootsrapMapFunc is a handler.ToRequestsFunc to be used to enqeue
403+
// request for reconciliation of KubeadmConfig.
404+
func (r *KubeadmConfigReconciler) MachineToBootstrapMapFunc(o handler.MapObject) []ctrl.Request {
405+
result := []ctrl.Request{}
406+
407+
m, ok := o.Object.(*clusterv1.Machine)
408+
if !ok {
409+
return nil
410+
}
411+
if m.Spec.Bootstrap.ConfigRef != nil && m.Spec.Bootstrap.ConfigRef.GroupVersionKind() == bootstrapv1.GroupVersion.WithKind("KubeadmConfig") {
412+
name := client.ObjectKey{Namespace: m.Namespace, Name: m.Spec.Bootstrap.ConfigRef.Name}
413+
result = append(result, ctrl.Request{NamespacedName: name})
414+
}
415+
return result
416+
}
417+
403418
// reconcileDiscovery ensures that config.JoinConfiguration.Discovery is properly set for the joining node.
404419
// The implementation func respect user provided discovery configurations, but in case some of them are missing, a valid BootstrapToken object
405420
// is automatically injected into config.JoinConfiguration.Discovery.

controllers/kubeadmconfig_controller_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,46 @@ func setupScheme() *runtime.Scheme {
5656
return scheme
5757
}
5858

59+
// MachineToBootstrapMapFunc return kubeadm bootstrap configref name when configref exists
60+
func TestKubeadmConfigReconciler_MachineToBootstrapMapFuncReturn(t *testing.T) {
61+
cluster := newCluster("my-cluster")
62+
objs := []runtime.Object{cluster}
63+
machineObjs := []runtime.Object{}
64+
var expectedConfigName string
65+
for i := 0; i < 3; i++ {
66+
m := newMachine(cluster, fmt.Sprintf("my-machine-%d", i))
67+
configName := fmt.Sprintf("my-config-%d", i)
68+
if i == 1 {
69+
c := newKubeadmConfig(m, configName)
70+
objs = append(objs, m, c)
71+
expectedConfigName = configName
72+
} else {
73+
objs = append(objs, m)
74+
}
75+
machineObjs = append(machineObjs, m)
76+
}
77+
fakeClient := fake.NewFakeClientWithScheme(setupScheme(), objs...)
78+
reconciler := &KubeadmConfigReconciler{
79+
Log: log.Log,
80+
Client: fakeClient,
81+
}
82+
for i := 0; i < 3; i++ {
83+
o := handler.MapObject{
84+
Object: machineObjs[i],
85+
}
86+
configs := reconciler.MachineToBootstrapMapFunc(o)
87+
if i == 1 {
88+
if configs[0].Name != expectedConfigName {
89+
t.Fatalf("unexpected config name: %s", configs[0].Name)
90+
}
91+
} else {
92+
if configs[0].Name != "" {
93+
t.Fatalf("unexpected config name: %s", configs[0].Name)
94+
}
95+
}
96+
}
97+
}
98+
5999
// Reconcile returns early if the kubeadm config is ready because it should never re-generate bootstrap data.
60100
func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *testing.T) {
61101
config := newKubeadmConfig(nil, "cfg")

0 commit comments

Comments
 (0)