Skip to content

Commit 0d58bd9

Browse files
Merge pull request #174 from ingvagabund/node-draining
Move node draining from actuator into machine controller
2 parents 680d8e0 + 289f0e5 commit 0d58bd9

File tree

4 files changed

+75
-60
lines changed

4 files changed

+75
-60
lines changed

pkg/actuators/machine/actuator.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ import (
2121
"fmt"
2222
"time"
2323

24-
"github.com/go-log/log/info"
2524
"github.com/golang/glog"
2625

2726
corev1 "k8s.io/api/core/v1"
2827
"k8s.io/apimachinery/pkg/api/equality"
2928
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
errorutil "k8s.io/apimachinery/pkg/util/errors"
31-
"k8s.io/client-go/kubernetes"
3230
"k8s.io/client-go/rest"
3331
"k8s.io/client-go/tools/record"
3432

@@ -41,8 +39,6 @@ import (
4139

4240
awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client"
4341
"sigs.k8s.io/controller-runtime/pkg/client"
44-
45-
kubedrain "github.com/openshift/kubernetes-drain"
4642
)
4743

4844
const (
@@ -55,8 +51,6 @@ const (
5551

5652
// MachineCreationFailed indicates that machine creation failed
5753
MachineCreationFailed = "MachineCreationFailed"
58-
// ExcludeNodeDrainingAnnotation annotation explicitly skips node draining if set
59-
ExcludeNodeDrainingAnnotation = "machine.openshift.io/exclude-node-draining"
6054
)
6155

6256
// Actuator is the AWS-specific actuator for the Cluster API machine controller
@@ -255,50 +249,6 @@ func (gl *glogLogger) Logf(format string, v ...interface{}) {
255249

256250
// DeleteMachine deletes an AWS instance
257251
func (a *Actuator) DeleteMachine(cluster *machinev1.Cluster, machine *machinev1.Machine) error {
258-
// Drain node before deleting
259-
// If a machine is not linked to a node, just delete the machine. Since a node
260-
// can be unlinked from a machine when the node goes NotReady and is removed
261-
// by cloud controller manager. In that case some machines would never get
262-
// deleted without a manual intervention.
263-
if _, exists := machine.ObjectMeta.Annotations[ExcludeNodeDrainingAnnotation]; !exists && machine.Status.NodeRef != nil {
264-
glog.Infof("Draining node before delete")
265-
if a.config == nil {
266-
err := fmt.Errorf("missing client config, unable to build kube client")
267-
glog.Error(err)
268-
return err
269-
}
270-
kubeClient, err := kubernetes.NewForConfig(a.config)
271-
if err != nil {
272-
return fmt.Errorf("unable to build kube client: %v", err)
273-
}
274-
node, err := kubeClient.CoreV1().Nodes().Get(machine.Status.NodeRef.Name, metav1.GetOptions{})
275-
if err != nil {
276-
return fmt.Errorf("unable to get node %q: %v", machine.Status.NodeRef.Name, err)
277-
}
278-
279-
if err := kubedrain.Drain(
280-
kubeClient,
281-
[]*corev1.Node{node},
282-
&kubedrain.DrainOptions{
283-
Force: true,
284-
IgnoreDaemonsets: true,
285-
DeleteLocalData: true,
286-
GracePeriodSeconds: -1,
287-
Logger: info.New(glog.V(0)),
288-
// If a pod is not evicted in 20 second, retry the eviction next time the
289-
// machine gets reconciled again (to allow other machines to be reconciled)
290-
Timeout: 20 * time.Second,
291-
},
292-
); err != nil {
293-
// Machine still tries to terminate after drain failure
294-
glog.Warningf("drain failed for machine %q: %v", machine.Name, err)
295-
return &clustererror.RequeueAfterError{RequeueAfter: requeueAfterSeconds * time.Second}
296-
}
297-
298-
glog.Infof("drain successful for machine %q", machine.Name)
299-
a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, "Deleted", "Node %q drained", node.Name)
300-
}
301-
302252
machineProviderConfig, err := providerConfigFromMachine(a.client, machine, a.codec)
303253
if err != nil {
304254
return a.handleMachineError(machine, apierrors.InvalidMachineConfiguration("error decoding MachineProviderConfig: %v", err), deleteEventAction)

pkg/actuators/machine/stubs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414

1515
machinev1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
16+
machinecontroller "github.com/openshift/cluster-api/pkg/controller/machine"
1617
providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1beta1"
1718
"sigs.k8s.io/cluster-api-provider-aws/test/utils"
1819
)
@@ -119,7 +120,7 @@ func stubMachine() (*machinev1.Machine, error) {
119120
},
120121
Annotations: map[string]string{
121122
// skip node draining since it's not mocked
122-
ExcludeNodeDrainingAnnotation: "",
123+
machinecontroller.ExcludeNodeDrainingAnnotation: "",
123124
},
124125
},
125126

test/machines/machines_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"sigs.k8s.io/cluster-api-provider-aws/test/utils"
1919

2020
MachineV1beta1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
21+
machinecontroller "github.com/openshift/cluster-api/pkg/controller/machine"
2122
apiv1 "k8s.io/api/core/v1"
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324

@@ -136,7 +137,7 @@ var _ = framework.SigKubeDescribe("Machines", func() {
136137
if testMachine.Annotations == nil {
137138
testMachine.Annotations = map[string]string{}
138139
}
139-
testMachine.Annotations[machineutils.ExcludeNodeDrainingAnnotation] = ""
140+
testMachine.Annotations[machinecontroller.ExcludeNodeDrainingAnnotation] = ""
140141
f.CreateMachineAndWait(testMachine, acw)
141142
machinesToDelete.AddMachine(testMachine, f, acw)
142143

@@ -214,7 +215,7 @@ var _ = framework.SigKubeDescribe("Machines", func() {
214215
if testMachine.Annotations == nil {
215216
testMachine.Annotations = map[string]string{}
216217
}
217-
testMachine.Annotations[machineutils.ExcludeNodeDrainingAnnotation] = ""
218+
testMachine.Annotations[machinecontroller.ExcludeNodeDrainingAnnotation] = ""
218219
f.CreateMachineAndWait(testMachine, acw)
219220
machinesToDelete.AddMachine(testMachine, f, acw)
220221

@@ -278,7 +279,7 @@ var _ = framework.SigKubeDescribe("Machines", func() {
278279
if masterMachine.Annotations == nil {
279280
masterMachine.Annotations = map[string]string{}
280281
}
281-
masterMachine.Annotations[machineutils.ExcludeNodeDrainingAnnotation] = ""
282+
masterMachine.Annotations[machinecontroller.ExcludeNodeDrainingAnnotation] = ""
282283
f.CreateMachineAndWait(masterMachine, acw)
283284
machinesToDelete.AddMachine(masterMachine, f, acw)
284285

@@ -326,7 +327,7 @@ var _ = framework.SigKubeDescribe("Machines", func() {
326327
if workerMachineSet.Annotations == nil {
327328
workerMachineSet.Annotations = map[string]string{}
328329
}
329-
workerMachineSet.Annotations[machineutils.ExcludeNodeDrainingAnnotation] = ""
330+
workerMachineSet.Annotations[machinecontroller.ExcludeNodeDrainingAnnotation] = ""
330331
fmt.Printf("workerMachineSet: %#v\n", workerMachineSet)
331332
clusterFramework.CreateMachineSetAndWait(workerMachineSet, acw)
332333
machinesToDelete.AddMachineSet(workerMachineSet, clusterFramework, acw)

vendor/github.com/openshift/cluster-api/pkg/controller/machine/controller.go

Lines changed: 68 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)