Skip to content

Commit d4144cb

Browse files
committed
Change remote.NewClusterClient to return a controller-runtime client.Client
1 parent 4a097d8 commit d4144cb

File tree

6 files changed

+86
-84
lines changed

6 files changed

+86
-84
lines changed

controllers/machine_controller.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"k8s.io/apimachinery/pkg/runtime"
2223
"sync"
2324
"time"
2425

@@ -31,6 +32,7 @@ import (
3132
kerrors "k8s.io/apimachinery/pkg/util/errors"
3233
"k8s.io/apimachinery/pkg/util/wait"
3334
"k8s.io/client-go/kubernetes"
35+
"k8s.io/client-go/kubernetes/scheme"
3436
"k8s.io/client-go/rest"
3537
"k8s.io/client-go/tools/record"
3638
"k8s.io/klog"
@@ -67,6 +69,7 @@ type MachineReconciler struct {
6769
controller controller.Controller
6870
recorder record.EventRecorder
6971
externalWatchers sync.Map
72+
scheme *runtime.Scheme
7073
}
7174

7275
func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
@@ -75,9 +78,16 @@ func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager, options controlle
7578
WithOptions(options).
7679
Build(r)
7780

81+
if err!=nil{
82+
return err
83+
}
84+
7885
r.controller = c
7986
r.recorder = mgr.GetEventRecorderFor("machine-controller")
8087
r.config = mgr.GetConfig()
88+
89+
r.scheme = runtime.NewScheme()
90+
err = scheme.AddToScheme(r.scheme)
8191
return err
8292
}
8393

@@ -277,12 +287,8 @@ func (r *MachineReconciler) drainNode(cluster *clusterv1.Cluster, nodeName strin
277287
}
278288
} else {
279289
// Otherwise, proceed to get the remote cluster client and get the Node.
280-
remoteClient, err := remote.NewClusterClient(r.Client, cluster)
281-
if err != nil {
282-
logger.Error(err, "Error creating a remote client while deleting Machine, won't retry")
283-
return nil
284-
}
285-
kubeClient, err = kubernetes.NewForConfig(remoteClient.RESTConfig())
290+
restConfig , err := remote.RESTConfig(r.Client, cluster)
291+
kubeClient, err = kubernetes.NewForConfig(restConfig)
286292
if err != nil {
287293
logger.Error(err, "Error creating a remote client while deleting Machine, won't retry")
288294
return nil
@@ -339,32 +345,26 @@ func (r *MachineReconciler) drainNode(cluster *clusterv1.Cluster, nodeName strin
339345

340346
func (r *MachineReconciler) deleteNode(ctx context.Context, cluster *clusterv1.Cluster, name string) error {
341347
logger := r.Log.WithValues("machine", name)
342-
348+
var c client.Client
349+
var err error
343350
if cluster == nil {
344351
// Try to retrieve the Node from the local cluster, if no Cluster reference is found.
345-
var node corev1.Node
346-
if err := r.Client.Get(ctx, client.ObjectKey{Name: name}, &node); err != nil {
347-
return err
352+
c = r.Client
353+
}else{
354+
logger = logger.WithValues("cluster", cluster.Name, "namespace", cluster.Namespace)
355+
// Otherwise, proceed to get the remote cluster client to get the Node.
356+
c, err = remote.NewClusterClient(r.Client, cluster, r.scheme)
357+
if err != nil {
358+
logger.Error(err, "Error creating a remote client for cluster while deleting Machine, won't retry")
359+
return nil
348360
}
349-
return r.Client.Delete(ctx, &node)
350-
}
351-
352-
logger = logger.WithValues("cluster", cluster.Name, "namespace", cluster.Namespace)
353-
354-
// Otherwise, proceed to get the remote cluster client and get the Node.
355-
remoteClient, err := remote.NewClusterClient(r.Client, cluster)
356-
if err != nil {
357-
logger.Error(err, "Error creating a remote client for cluster while deleting Machine, won't retry")
358-
return nil
359361
}
360362

361-
corev1Remote, err := remoteClient.CoreV1()
362-
if err != nil {
363-
logger.Error(err, "Error creating a remote client for cluster while deleting Machine, won't retry")
364-
return nil
363+
var node corev1.Node
364+
if err := c.Get(ctx, client.ObjectKey{Name: name}, &node); err != nil {
365+
return err
365366
}
366-
367-
return corev1Remote.Nodes().Delete(name, &metav1.DeleteOptions{})
367+
return c.Delete(ctx, &node)
368368
}
369369

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

controllers/machine_controller_noderef.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ package controllers
1818

1919
import (
2020
"context"
21+
"sigs.k8s.io/controller-runtime/pkg/client"
2122
"time"
2223

2324
"github.com/pkg/errors"
2425
apicorev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26-
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
2727
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2828
"sigs.k8s.io/cluster-api/controllers/noderefutil"
2929
"sigs.k8s.io/cluster-api/controllers/remote"
@@ -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,14 @@ 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+
err := client.List(context.Background(), &nodeList)
104100
if err != nil {
105101
return nil, err
106102
}
@@ -129,4 +125,4 @@ func (r *MachineReconciler) getNodeReference(client corev1.NodesGetter, provider
129125
}
130126

131127
return nil, ErrNodeNotFound
132-
}
128+
}

controllers/machineset_controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"k8s.io/apimachinery/pkg/runtime"
23+
"k8s.io/client-go/kubernetes/scheme"
2224
"strings"
2325
"sync"
2426
"time"
@@ -65,6 +67,7 @@ type MachineSetReconciler struct {
6567
Log logr.Logger
6668

6769
recorder record.EventRecorder
70+
scheme *runtime.Scheme
6871
}
6972

7073
func (r *MachineSetReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
@@ -78,7 +81,14 @@ func (r *MachineSetReconciler) SetupWithManager(mgr ctrl.Manager, options contro
7881
WithOptions(options).
7982
Complete(r)
8083

84+
if err!=nil{
85+
return err
86+
}
87+
8188
r.recorder = mgr.GetEventRecorderFor("machineset-controller")
89+
90+
r.scheme = runtime.NewScheme()
91+
err = scheme.AddToScheme(r.scheme)
8292
return err
8393
}
8494

controllers/machineset_status.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,19 @@ func updateMachineSetStatus(c client.Client, ms *clusterv1.MachineSet, newStatus
138138
}
139139

140140
func (r *MachineSetReconciler) getMachineNode(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (*corev1.Node, error) {
141+
var c client.Client
142+
var err error
141143
if cluster == nil {
142144
// 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)
150-
if err != nil {
151-
return nil, err
152-
}
153-
154-
corev1Remote, err := remoteClient.CoreV1()
155-
if err != nil {
156-
return nil, err
145+
c = r.Client
146+
} else {
147+
// Otherwise, proceed to get the remote cluster client and get the Node.
148+
c, err = remote.NewClusterClient(r.Client, cluster, r.scheme)
149+
if err != nil {
150+
return nil, err
151+
}
157152
}
158-
159-
return corev1Remote.Nodes().Get(machine.Status.NodeRef.Name, metav1.GetOptions{})
153+
node := &corev1.Node{}
154+
err = c.Get(context.Background(), client.ObjectKey{Name: machine.Status.NodeRef.Name}, node)
155+
return node, err
160156
}

controllers/remote/cluster.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,53 +18,41 @@ package remote
1818

1919
import (
2020
"github.com/pkg/errors"
21-
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
21+
"k8s.io/apimachinery/pkg/runtime"
2222
restclient "k8s.io/client-go/rest"
2323
"k8s.io/client-go/tools/clientcmd"
2424
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2525
kcfg "sigs.k8s.io/cluster-api/util/kubeconfig"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727
)
2828

29-
// ClusterClient is an interface encapsulating methods
30-
// to access a remote cluster.
31-
type ClusterClient interface {
32-
RESTConfig() *restclient.Config
33-
CoreV1() (corev1.CoreV1Interface, error)
34-
}
35-
36-
// clusterClient is a helper struct to connect to remote workload clusters.
37-
type clusterClient struct {
38-
restConfig *restclient.Config
39-
cluster *clusterv1.Cluster
40-
}
29+
// NewClusterClient returns a Client for interacting with a remote Cluster
30+
func NewClusterClient(c client.Client, cluster *clusterv1.Cluster, scheme *runtime.Scheme) (client.Client, error){
4131

42-
// NewClusterClient creates a new ClusterClient.
43-
func NewClusterClient(c client.Client, cluster *clusterv1.Cluster) (ClusterClient, error) {
44-
kubeconfig, err := kcfg.FromSecret(c, cluster)
32+
restConfig, err := RESTConfig(c, cluster)
4533
if err != nil {
46-
return nil, errors.Wrapf(err, "failed to retrieve kubeconfig secret for Cluster %q in namespace %q",
47-
cluster.Name, cluster.Namespace)
34+
return nil, err
4835
}
4936

50-
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig)
37+
ret, err := client.New(restConfig, client.Options{Scheme:scheme})
5138
if err != nil {
52-
return nil, errors.Wrapf(err, "failed to create client configuration for Cluster %q in namespace %q",
53-
cluster.Name, cluster.Namespace)
39+
return nil, errors.Wrapf(err, "failed to create client for Cluster %s/%s", cluster.Namespace, cluster.Name)
5440
}
5541

56-
return &clusterClient{
57-
restConfig: restConfig,
58-
cluster: cluster,
59-
}, nil
42+
return ret, nil
6043
}
6144

6245
// RESTConfig returns a configuration instance to be used with a Kubernetes client.
63-
func (c *clusterClient) RESTConfig() *restclient.Config {
64-
return c.restConfig
65-
}
46+
func RESTConfig(c client.Client, cluster *clusterv1.Cluster) (*restclient.Config, error) {
47+
kubeConfig, err := kcfg.FromSecret(c, cluster)
48+
if err != nil {
49+
return nil, errors.Wrapf(err, "failed to retrieve kubeconfig secret for Cluster %s/%s", cluster.Namespace, cluster.Name)
50+
}
51+
52+
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfig)
53+
if err != nil {
54+
return nil, errors.Wrapf(err, "failed to create REST configuration for Cluster %s/%s", cluster.Namespace, cluster.Name)
55+
}
6656

67-
// CoreV1 returns a new Kubernetes CoreV1 client.
68-
func (c *clusterClient) CoreV1() (corev1.CoreV1Interface, error) {
69-
return corev1.NewForConfig(c.RESTConfig())
57+
return restConfig, nil
7058
}

docs/book/src/providers/v1alpha2-to-v1alpha3.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,15 @@
3636
## Cluster and Machine controllers now set `cluster.x-k8s.io/cluster-name` to external objects.
3737

3838
- In addition to the OwnerReference back to the Cluster, a label is now added as well to any external objects, for example objects such as KubeadmConfig (bootstrap provider), AWSCluster (infrastructure provider), AWSMachine (infrastructure provider), etc.
39+
40+
## Changes to `sigs.k8s.io/cluster-api/controllers/remote`
41+
42+
- The `ClusterClient` interface has been removed.
43+
- `remote.NewClusterClient` now returns a `sigs.k8s.io/controller-runtime/pkg/client` Client. The signature changed from
44+
45+
`func NewClusterClient(c client.Client, cluster *clusterv1.Cluster) (ClusterClient, error)`
46+
47+
to
48+
49+
`func NewClusterClient(c client.Client, cluster *clusterv1.Cluster, scheme runtime.Scheme) (client.Client, error)`
50+

0 commit comments

Comments
 (0)