Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Use rest config from ClusterCacheTracker consistently #8894

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
4 changes: 3 additions & 1 deletion controlplane/kubeadm/internal/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -102,10 +103,11 @@ func (m *Management) GetMachinePoolsForCluster(ctx context.Context, cluster *clu
func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.ObjectKey) (WorkloadCluster, error) {
// TODO(chuckha): Inject this dependency.
// TODO(chuckha): memoize this function. The workload client only exists as long as a reconciliation loop.
restConfig, err := remote.RESTConfig(ctx, KubeadmControlPlaneControllerName, m.Client, clusterKey)
restConfig, err := m.Tracker.GetRESTConfig(ctx, clusterKey)
if err != nil {
return nil, err
}
restConfig = rest.CopyConfig(restConfig)
restConfig.Timeout = 30 * time.Second

if m.Tracker == nil {
Expand Down
19 changes: 11 additions & 8 deletions controlplane/kubeadm/internal/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ func TestGetWorkloadCluster(t *testing.T) {
delete(emptyKeyEtcdSecret.Data, secret.TLSKeyDataName)
badCrtEtcdSecret := etcdSecret.DeepCopy()
badCrtEtcdSecret.Data[secret.TLSCrtDataName] = []byte("bad cert")
tracker, err := remote.NewClusterCacheTracker(
env.Manager,
remote.ClusterCacheTrackerOptions{
Log: &log.Log,
},
)
g.Expect(err).ToNot(HaveOccurred())

// Create kubeconfig secret
// Store the envtest config as the contents of the kubeconfig secret.
Expand Down Expand Up @@ -188,10 +181,20 @@ func TestGetWorkloadCluster(t *testing.T) {
for _, o := range tt.objs {
g.Expect(env.Client.Create(ctx, o)).To(Succeed())
defer func(do client.Object) {
g.Expect(env.Cleanup(ctx, do)).To(Succeed())
g.Expect(env.CleanupAndWait(ctx, do)).To(Succeed())
}(o)
}

// We have to create a new ClusterCacheTracker for every test case otherwise
// it could still have a rest config from a previous run cached.
tracker, err := remote.NewClusterCacheTracker(
env.Manager,
remote.ClusterCacheTrackerOptions{
Log: &log.Log,
},
)
g.Expect(err).ToNot(HaveOccurred())

// Note: The API reader is intentionally used instead of the regular (cached) client
// to avoid test failures when the local cache isn't able to catch up in time.
m := Management{
Expand Down
7 changes: 1 addition & 6 deletions internal/controllers/machine/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ import (
"sigs.k8s.io/cluster-api/util/predicates"
)

const (
// controllerName defines the controller used when creating clients.
controllerName = "machine-controller"
)

var (
errNilNodeRef = errors.New("noderef is nil")
errLastControlPlaneNode = errors.New("last control plane member")
Expand Down Expand Up @@ -579,7 +574,7 @@ func (r *Reconciler) isDeleteNodeAllowed(ctx context.Context, cluster *clusterv1
func (r *Reconciler) drainNode(ctx context.Context, cluster *clusterv1.Cluster, nodeName string) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx, "Node", klog.KRef("", nodeName))

restConfig, err := remote.RESTConfig(ctx, controllerName, r.Client, util.ObjectKey(cluster))
restConfig, err := r.Tracker.GetRESTConfig(ctx, util.ObjectKey(cluster))
if err != nil {
log.Error(err, "Error creating a remote client while deleting Machine, won't retry")
return ctrl.Result{}, nil
Expand Down
Loading