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

🐛 Fix a Machine -> KubeadmConfig mapping bug #237

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions controllers/kubeadmconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *KubeadmConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
Watches(
&source.Kind{Type: &clusterv1.Machine{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: util.MachineToInfrastructureMapFunc(bootstrapv1.GroupVersion.WithKind("KubeadmConfig")),
ToRequests: handler.ToRequestsFunc(r.MachineToBootstrapMapFunc),
},
).
Watches(
Expand Down Expand Up @@ -136,7 +136,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
log.Info("Cluster does not exist yet , waiting until it is created")
return ctrl.Result{}, nil
}

log.Error(err, "could not get cluster by machine metadata")
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -400,6 +399,22 @@ func (r *KubeadmConfigReconciler) ClusterToKubeadmConfigs(o handler.MapObject) [
return result
}

// MachineToBootstrapMapFunc is a handler.ToRequestsFunc to be used to enqeue
// request for reconciliation of KubeadmConfig.
func (r *KubeadmConfigReconciler) MachineToBootstrapMapFunc(o handler.MapObject) []ctrl.Request {
result := []ctrl.Request{}

m, ok := o.Object.(*clusterv1.Machine)
if !ok {
return nil
}
if m.Spec.Bootstrap.ConfigRef != nil && m.Spec.Bootstrap.ConfigRef.GroupVersionKind() == bootstrapv1.GroupVersion.WithKind("KubeadmConfig") {
name := client.ObjectKey{Namespace: m.Namespace, Name: m.Spec.Bootstrap.ConfigRef.Name}
result = append(result, ctrl.Request{NamespacedName: name})
}
return result
}

// reconcileDiscovery ensures that config.JoinConfiguration.Discovery is properly set for the joining node.
// The implementation func respect user provided discovery configurations, but in case some of them are missing, a valid BootstrapToken object
// is automatically injected into config.JoinConfiguration.Discovery.
Expand Down
40 changes: 40 additions & 0 deletions controllers/kubeadmconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ func setupScheme() *runtime.Scheme {
return scheme
}

// MachineToBootstrapMapFunc return kubeadm bootstrap configref name when configref exists
func TestKubeadmConfigReconciler_MachineToBootstrapMapFuncReturn(t *testing.T) {
cluster := newCluster("my-cluster")
objs := []runtime.Object{cluster}
machineObjs := []runtime.Object{}
var expectedConfigName string
for i := 0; i < 3; i++ {
m := newMachine(cluster, fmt.Sprintf("my-machine-%d", i))
configName := fmt.Sprintf("my-config-%d", i)
if i == 1 {
c := newKubeadmConfig(m, configName)
objs = append(objs, m, c)
expectedConfigName = configName
} else {
objs = append(objs, m)
}
machineObjs = append(machineObjs, m)
}
fakeClient := fake.NewFakeClientWithScheme(setupScheme(), objs...)
reconciler := &KubeadmConfigReconciler{
Log: log.Log,
Client: fakeClient,
}
for i := 0; i < 3; i++ {
o := handler.MapObject{
Object: machineObjs[i],
}
configs := reconciler.MachineToBootstrapMapFunc(o)
if i == 1 {
if configs[0].Name != expectedConfigName {
t.Fatalf("unexpected config name: %s", configs[0].Name)
}
} else {
if configs[0].Name != "" {
t.Fatalf("unexpected config name: %s", configs[0].Name)
}
}
}
}

// Reconcile returns early if the kubeadm config is ready because it should never re-generate bootstrap data.
func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *testing.T) {
config := newKubeadmConfig(nil, "cfg")
Expand Down