diff --git a/controllers/kubeadmconfig_controller.go b/controllers/kubeadmconfig_controller.go index 3b44b2b..39d12c7 100644 --- a/controllers/kubeadmconfig_controller.go +++ b/controllers/kubeadmconfig_controller.go @@ -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( @@ -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 } @@ -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. diff --git a/controllers/kubeadmconfig_controller_test.go b/controllers/kubeadmconfig_controller_test.go index 9dfc8e4..1f4a8b5 100644 --- a/controllers/kubeadmconfig_controller_test.go +++ b/controllers/kubeadmconfig_controller_test.go @@ -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")