From 8ab2db1237b6f327050f386a1e0710748e1271f5 Mon Sep 17 00:00:00 2001 From: Michael McCune Date: Mon, 4 May 2020 13:50:53 -0400 Subject: [PATCH] Add mutex to DeleteNodes in cluster-autoscaler CAPI provider This change adds a mutex to the MachineController structure which is used to gate access to the DeleteNodes function. This is one in a series of PRs to mitigate kubernetes#3104 --- .../cloudprovider/clusterapi/clusterapi_controller.go | 2 ++ .../cloudprovider/clusterapi/clusterapi_nodegroup.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller.go index 507682e210d2..0acbc6729c52 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "strings" + "sync" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -65,6 +66,7 @@ type machineController struct { machineSetResource *schema.GroupVersionResource machineResource *schema.GroupVersionResource machineDeploymentResource *schema.GroupVersionResource + accessLock sync.Mutex } type machineSetFilterFunc func(machineSet *MachineSet) error diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup.go index e7ac6aaa9469..a106c8a195b7 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup.go @@ -82,6 +82,9 @@ func (ng *nodegroup) IncreaseSize(delta int) error { // group. This function should wait until node group size is updated. // Implementation required. func (ng *nodegroup) DeleteNodes(nodes []*corev1.Node) error { + ng.machineController.accessLock.Lock() + defer ng.machineController.accessLock.Unlock() + // Step 1: Verify all nodes belong to this node group. for _, node := range nodes { actualNodeGroup, err := ng.machineController.nodeGroupForNode(node)