@@ -26,16 +26,16 @@ import (
2626 "github.com/pkg/errors"
2727 apierrors "k8s.io/apimachinery/pkg/api/errors"
2828 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29- "k8s.io/client-go/tools/clientcmd "
29+ "k8s.io/apimachinery/pkg/runtime "
3030 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
3131 bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2"
3232 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/cloudinit"
3333 internalcluster "sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/cluster"
3434 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/locking"
3535 kubeadmv1beta1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
36+ "sigs.k8s.io/cluster-api/controllers/remote"
3637 capierrors "sigs.k8s.io/cluster-api/errors"
3738 "sigs.k8s.io/cluster-api/util"
38- "sigs.k8s.io/cluster-api/util/kubeconfig"
3939 "sigs.k8s.io/cluster-api/util/patch"
4040 "sigs.k8s.io/cluster-api/util/secret"
4141 ctrl "sigs.k8s.io/controller-runtime"
@@ -60,9 +60,10 @@ type KubeadmConfigReconciler struct {
6060 Client client.Client
6161 KubeadmInitLock InitLocker
6262 Log logr.Logger
63+ scheme * runtime.Scheme
6364
6465 // for testing
65- remoteClient func (client.Client , * clusterv1.Cluster ) (client.Client , error )
66+ remoteClient func (client.Client , * clusterv1.Cluster , * runtime. Scheme ) (client.Client , error )
6667}
6768
6869// SetupWithManager sets up the reconciler with the Manager.
@@ -71,10 +72,12 @@ func (r *KubeadmConfigReconciler) SetupWithManager(mgr ctrl.Manager, option cont
7172 r .KubeadmInitLock = locking .NewControlPlaneInitMutex (ctrl .Log .WithName ("init-locker" ), mgr .GetClient ())
7273 }
7374 if r .remoteClient == nil {
74- r .remoteClient = defaultRemoteClient
75+ r .remoteClient = remote . NewClusterClient
7576 }
7677
77- return ctrl .NewControllerManagedBy (mgr ).
78+ r .scheme = mgr .GetScheme ()
79+
80+ err := ctrl .NewControllerManagedBy (mgr ).
7881 For (& bootstrapv1.KubeadmConfig {}).
7982 Watches (
8083 & source.Kind {Type : & clusterv1.Machine {}},
@@ -90,6 +93,12 @@ func (r *KubeadmConfigReconciler) SetupWithManager(mgr ctrl.Manager, option cont
9093 ).
9194 WithOptions (option ).
9295 Complete (r )
96+
97+ if err != nil {
98+ return errors .Wrap (err , "failed setting up with a controller manager" )
99+ }
100+
101+ return nil
93102}
94103
95104// Reconcile handles KubeadmConfig events.
@@ -158,8 +167,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
158167 case config .Status .Ready && (config .Spec .JoinConfiguration != nil && config .Spec .JoinConfiguration .Discovery .BootstrapToken != nil ):
159168 token := config .Spec .JoinConfiguration .Discovery .BootstrapToken .Token
160169
161- // TODO replace with remote.NewClusterClient once it returns a client.Client
162- remoteClient , err := r .remoteClient (r .Client , cluster )
170+ remoteClient , err := r .remoteClient (r .Client , cluster , r .scheme )
163171 if err != nil {
164172 log .Error (err , "error creating remote cluster client" )
165173 return ctrl.Result {}, err
@@ -495,8 +503,7 @@ func (r *KubeadmConfigReconciler) reconcileDiscovery(cluster *clusterv1.Cluster,
495503
496504 // if BootstrapToken already contains a token, respect it; otherwise create a new bootstrap token for the node to join
497505 if config .Spec .JoinConfiguration .Discovery .BootstrapToken .Token == "" {
498- // TODO replace with remote.NewClusterClient once it returns a client.Client
499- remoteClient , err := r .remoteClient (r .Client , cluster )
506+ remoteClient , err := r .remoteClient (r .Client , cluster , r .scheme )
500507 if err != nil {
501508 return err
502509 }
@@ -564,23 +571,3 @@ func (r *KubeadmConfigReconciler) reconcileTopLevelObjectSettings(cluster *clust
564571 log .Info ("Altering ClusterConfiguration" , "KubernetesVersion" , config .Spec .ClusterConfiguration .KubernetesVersion )
565572 }
566573}
567-
568- // TODO remove this when remote.NewClusterClient returns a client.Client
569- func defaultRemoteClient (c client.Client , cluster * clusterv1.Cluster ) (client.Client , error ) {
570- kubeConfig , err := kubeconfig .FromSecret (c , cluster )
571- if err != nil {
572- return nil , errors .Wrapf (err , "failed to retrieve kubeconfig secret for Cluster %s/%s" , cluster .Namespace , cluster .Name )
573- }
574-
575- restConfig , err := clientcmd .RESTConfigFromKubeConfig (kubeConfig )
576- if err != nil {
577- return nil , errors .Wrapf (err , "failed to create client configuration for Cluster %s/%s" , cluster .Namespace , cluster .Name )
578- }
579-
580- ret , err := client .New (restConfig , client.Options {})
581- if err != nil {
582- return nil , errors .Wrapf (err , "failed to create client for Cluster %s/%s" , cluster .Namespace , cluster .Name )
583- }
584-
585- return ret , nil
586- }
0 commit comments