Skip to content

Commit c96c5c6

Browse files
committed
Change remote.NewClusterClient to return a controller-runtime client.Client
1 parent f869765 commit c96c5c6

12 files changed

+129
-122
lines changed

bootstrap/kubeadm/controllers/kubeadmconfig_controller.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
}

bootstrap/kubeadm/controllers/kubeadmconfig_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
533533
}
534534
}
535535

536-
func testRemoteClient(c client.Client) func(client.Client, *clusterv1.Cluster) (client.Client, error) {
537-
return func(client.Client, *clusterv1.Cluster) (client.Client, error) {
536+
func testRemoteClient(c client.Client) func(client.Client, *clusterv1.Cluster, *runtime.Scheme) (client.Client, error) {
537+
return func(client.Client, *clusterv1.Cluster, *runtime.Scheme) (client.Client, error) {
538538
return c, nil
539539
}
540540
}

controllers/cluster_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,14 @@ func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager, options controlle
7676
WithOptions(options).
7777
Build(r)
7878

79+
if err != nil {
80+
return errors.Wrap(err, "failed setting up with a controller manager")
81+
}
82+
7983
r.controller = c
8084
r.recorder = mgr.GetEventRecorderFor("cluster-controller")
81-
return err
85+
86+
return nil
8287
}
8388

8489
func (r *ClusterReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, reterr error) {

controllers/machine_controller.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
apierrors "k8s.io/apimachinery/pkg/api/errors"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
31+
"k8s.io/apimachinery/pkg/runtime"
3132
kerrors "k8s.io/apimachinery/pkg/util/errors"
3233
"k8s.io/apimachinery/pkg/util/wait"
3334
"k8s.io/client-go/kubernetes"
@@ -67,6 +68,7 @@ type MachineReconciler struct {
6768
controller controller.Controller
6869
recorder record.EventRecorder
6970
externalWatchers sync.Map
71+
scheme *runtime.Scheme
7072
}
7173

7274
func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
@@ -75,10 +77,16 @@ func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager, options controlle
7577
WithOptions(options).
7678
Build(r)
7779

80+
if err != nil {
81+
return errors.Wrap(err, "failed setting up with a controller manager")
82+
}
83+
7884
r.controller = c
7985
r.recorder = mgr.GetEventRecorderFor("machine-controller")
8086
r.config = mgr.GetConfig()
81-
return err
87+
88+
r.scheme = mgr.GetScheme()
89+
return nil
8290
}
8391

8492
func (r *MachineReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, reterr error) {
@@ -273,12 +281,12 @@ func (r *MachineReconciler) drainNode(cluster *clusterv1.Cluster, nodeName strin
273281
}
274282
} else {
275283
// Otherwise, proceed to get the remote cluster client and get the Node.
276-
remoteClient, err := remote.NewClusterClient(r.Client, cluster)
284+
restConfig, err := remote.RESTConfig(r.Client, cluster)
277285
if err != nil {
278286
logger.Error(err, "Error creating a remote client while deleting Machine, won't retry")
279287
return nil
280288
}
281-
kubeClient, err = kubernetes.NewForConfig(remoteClient.RESTConfig())
289+
kubeClient, err = kubernetes.NewForConfig(restConfig)
282290
if err != nil {
283291
logger.Error(err, "Error creating a remote client while deleting Machine, won't retry")
284292
return nil
@@ -334,33 +342,25 @@ func (r *MachineReconciler) drainNode(cluster *clusterv1.Cluster, nodeName strin
334342
}
335343

336344
func (r *MachineReconciler) deleteNode(ctx context.Context, cluster *clusterv1.Cluster, name string) error {
337-
logger := r.Log.WithValues("machine", name)
338-
339-
if cluster == nil {
340-
// Try to retrieve the Node from the local cluster, if no Cluster reference is found.
341-
var node corev1.Node
342-
if err := r.Client.Get(ctx, client.ObjectKey{Name: name}, &node); err != nil {
343-
return err
344-
}
345-
return r.Client.Delete(ctx, &node)
346-
}
347-
348-
logger = logger.WithValues("cluster", cluster.Name, "namespace", cluster.Namespace)
345+
logger := r.Log.WithValues("machine", name, "cluster", cluster.Name, "namespace", cluster.Namespace)
349346

350-
// Otherwise, proceed to get the remote cluster client and get the Node.
351-
remoteClient, err := remote.NewClusterClient(r.Client, cluster)
347+
// Create a remote client to delete the node
348+
c, err := remote.NewClusterClient(r.Client, cluster, r.scheme)
352349
if err != nil {
353350
logger.Error(err, "Error creating a remote client for cluster while deleting Machine, won't retry")
354351
return nil
355352
}
356353

357-
corev1Remote, err := remoteClient.CoreV1()
358-
if err != nil {
359-
logger.Error(err, "Error creating a remote client for cluster while deleting Machine, won't retry")
360-
return nil
354+
node := &corev1.Node{
355+
ObjectMeta: metav1.ObjectMeta{
356+
Name: name,
357+
},
361358
}
362359

363-
return corev1Remote.Nodes().Delete(name, &metav1.DeleteOptions{})
360+
if err := c.Delete(ctx, node); err != nil {
361+
return errors.Wrapf(err, "error deleting node %s", name)
362+
}
363+
return nil
364364
}
365365

366366
// reconcileDeleteExternal tries to delete external references, returning true if it cannot find any.

controllers/machine_controller_noderef.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import (
2323
"github.com/pkg/errors"
2424
apicorev1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26-
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
2726
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2827
"sigs.k8s.io/cluster-api/controllers/noderefutil"
2928
"sigs.k8s.io/cluster-api/controllers/remote"
3029
capierrors "sigs.k8s.io/cluster-api/errors"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
3131
)
3232

3333
var (
@@ -65,18 +65,13 @@ func (r *MachineReconciler) reconcileNodeRef(_ context.Context, cluster *cluster
6565
return err
6666
}
6767

68-
clusterClient, err := remote.NewClusterClient(r.Client, cluster)
69-
if err != nil {
70-
return err
71-
}
72-
73-
corev1Client, err := clusterClient.CoreV1()
68+
clusterClient, err := remote.NewClusterClient(r.Client, cluster, r.scheme)
7469
if err != nil {
7570
return err
7671
}
7772

7873
// Get the Node reference.
79-
nodeRef, err := r.getNodeReference(corev1Client, providerID)
74+
nodeRef, err := r.getNodeReference(clusterClient, providerID)
8075
if err != nil {
8176
if err == ErrNodeNotFound {
8277
return errors.Wrapf(&capierrors.RequeueAfterError{RequeueAfter: 10 * time.Second},
@@ -94,13 +89,15 @@ func (r *MachineReconciler) reconcileNodeRef(_ context.Context, cluster *cluster
9489
return nil
9590
}
9691

97-
func (r *MachineReconciler) getNodeReference(client corev1.NodesGetter, providerID *noderefutil.ProviderID) (*apicorev1.ObjectReference, error) {
92+
func (r *MachineReconciler) getNodeReference(client client.Client, providerID *noderefutil.ProviderID) (*apicorev1.ObjectReference, error) {
9893
logger := r.Log.WithValues("providerID", providerID)
9994

10095
listOpt := metav1.ListOptions{}
10196

10297
for {
103-
nodeList, err := client.Nodes().List(listOpt)
98+
nodeList := apicorev1.NodeList{}
99+
// TODO Add a context to this method
100+
err := client.List(context.TODO(), &nodeList)
104101
if err != nil {
105102
return nil, err
106103
}

controllers/machine_controller_noderef_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
corev1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/runtime"
27-
fakeclient "k8s.io/client-go/kubernetes/fake"
2827
"k8s.io/client-go/kubernetes/scheme"
2928
"k8s.io/client-go/tools/record"
3029
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
@@ -70,7 +69,7 @@ func TestGetNodeReference(t *testing.T) {
7069
},
7170
}
7271

73-
coreV1Client := fakeclient.NewSimpleClientset(nodeList...).CoreV1()
72+
client := fake.NewFakeClientWithScheme(scheme.Scheme, nodeList...)
7473

7574
testCases := []struct {
7675
name string
@@ -108,7 +107,7 @@ func TestGetNodeReference(t *testing.T) {
108107
t.Fatalf("Expected no error parsing provider id %q, got %v", test.providerID, err)
109108
}
110109

111-
reference, err := r.getNodeReference(coreV1Client, providerID)
110+
reference, err := r.getNodeReference(client, providerID)
112111
if err != nil {
113112
if (test.err != nil && !strings.Contains(err.Error(), test.err.Error())) || test.err == nil {
114113
t.Fatalf("Expected error %v, got %v", test.err, err)

controllers/machinedeployment_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ func (r *MachineDeploymentReconciler) SetupWithManager(mgr ctrl.Manager, options
6767
WithOptions(options).
6868
Complete(r)
6969

70+
if err != nil {
71+
return errors.Wrap(err, "failed setting up with a controller manager")
72+
}
73+
7074
r.recorder = mgr.GetEventRecorderFor("machinedeployment-controller")
71-
return err
75+
76+
return nil
7277
}
7378

7479
func (r *MachineDeploymentReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

controllers/machineset_controller.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3232
"k8s.io/apimachinery/pkg/labels"
33+
"k8s.io/apimachinery/pkg/runtime"
3334
"k8s.io/client-go/tools/record"
3435
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
3536
"sigs.k8s.io/cluster-api/controllers/external"
@@ -65,6 +66,7 @@ type MachineSetReconciler struct {
6566
Log logr.Logger
6667

6768
recorder record.EventRecorder
69+
scheme *runtime.Scheme
6870
}
6971

7072
func (r *MachineSetReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
@@ -78,8 +80,14 @@ func (r *MachineSetReconciler) SetupWithManager(mgr ctrl.Manager, options contro
7880
WithOptions(options).
7981
Complete(r)
8082

83+
if err != nil {
84+
return errors.Wrap(err, "failed setting up with a controller manager")
85+
}
86+
8187
r.recorder = mgr.GetEventRecorderFor("machineset-controller")
82-
return err
88+
89+
r.scheme = mgr.GetScheme()
90+
return nil
8391
}
8492

8593
func (r *MachineSetReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

controllers/machineset_status.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/go-logr/logr"
24+
"github.com/pkg/errors"
2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/labels"
@@ -138,23 +139,14 @@ func updateMachineSetStatus(c client.Client, ms *clusterv1.MachineSet, newStatus
138139
}
139140

140141
func (r *MachineSetReconciler) getMachineNode(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (*corev1.Node, error) {
141-
if cluster == nil {
142-
// Try to retrieve the Node from the local cluster, if no Cluster reference is found.
143-
node := &corev1.Node{}
144-
err := r.Client.Get(context.Background(), client.ObjectKey{Name: machine.Status.NodeRef.Name}, node)
145-
return node, err
146-
}
147-
148-
// Otherwise, proceed to get the remote cluster client and get the Node.
149-
remoteClient, err := remote.NewClusterClient(r.Client, cluster)
142+
c, err := remote.NewClusterClient(r.Client, cluster, r.scheme)
150143
if err != nil {
151144
return nil, err
152145
}
153-
154-
corev1Remote, err := remoteClient.CoreV1()
146+
node := &corev1.Node{}
147+
err = c.Get(context.TODO(), client.ObjectKey{Name: machine.Status.NodeRef.Name}, node)
155148
if err != nil {
156-
return nil, err
149+
return nil, errors.Wrapf(err, "error retrieving node %s for machine %s/%s", machine.Status.NodeRef.Name, machine.Namespace, machine.Name)
157150
}
158-
159-
return corev1Remote.Nodes().Get(machine.Status.NodeRef.Name, metav1.GetOptions{})
151+
return node, nil
160152
}

0 commit comments

Comments
 (0)