Skip to content

Commit 73117aa

Browse files
authored
Merge pull request #164 from flatcar-linux/invidian/agent-improvements
pkg/agent: improve reacting to shutdown signal when waiting for ok to reboot
2 parents cd6c664 + 5196ca1 commit 73117aa

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

pkg/agent/agent.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,30 @@ func (k *klocksmith) process(ctx context.Context) error {
209209
go k.watchUpdateStatus(ctx, k.updateStatusCallback)
210210

211211
// Block until constants.AnnotationOkToReboot is set.
212-
okToReboot := false
213-
for !okToReboot {
212+
for okToReboot := false; !okToReboot; {
213+
klog.Infof("Waiting for ok-to-reboot from controller...")
214+
215+
errCh := make(chan error)
216+
217+
go func() {
218+
errCh <- k.waitForOkToReboot(ctx)
219+
}()
220+
214221
select {
215222
case <-ctx.Done():
216223
klog.Infof("Got stop signal while waiting for ok-to-reboot from controller")
217224

218225
return nil
219-
default:
220-
klog.Infof("Waiting for ok-to-reboot from controller...")
226+
case err := <-errCh:
227+
if err != nil {
228+
klog.Warningf("Error waiting for an ok-to-reboot: %v", err)
221229

222-
err := k.waitForOkToReboot(ctx)
223-
if err == nil {
224-
// Time to reboot.
225-
okToReboot = true
230+
// Break select statement to restart watching for ok to reboot.
231+
break
226232
}
227233

228-
klog.Warningf("Error waiting for an ok-to-reboot: %v", err)
234+
// Time to reboot.
235+
okToReboot = true
229236
}
230237
}
231238

pkg/agent/agent_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,6 @@ func Test_Running_agent(t *testing.T) {
15491549
t.Run("agent_receives_termination_signal_while_waiting_for_all_pods_to_be_terminated", func(t *testing.T) {
15501550
t.Parallel()
15511551

1552-
rebootTriggerred := make(chan bool, 1)
1553-
15541552
podsToCreate := []*corev1.Pod{
15551553
{
15561554
ObjectMeta: metav1.ObjectMeta{
@@ -1568,6 +1566,19 @@ func Test_Running_agent(t *testing.T) {
15681566
fakeClient := fake.NewSimpleClientset(podsToCreate[0], testNode())
15691567
addEvictionSupport(t, fakeClient)
15701568

1569+
podDeletionAttemptCh := make(chan struct{}, 1)
1570+
1571+
actionF := func(action k8stesting.Action) (bool, runtime.Object, error) {
1572+
podDeletionAttemptCh <- struct{}{}
1573+
1574+
// Never actually remove any pod.
1575+
return true, nil, nil
1576+
}
1577+
1578+
fakeClient.PrependReactor("create", "pods/eviction", actionF)
1579+
1580+
rebootTriggerred := make(chan bool, 1)
1581+
15711582
testConfig, node, _ := validTestConfig(t, testNode())
15721583
testConfig.Clientset = fakeClient
15731584
testConfig.PodDeletionGracePeriod = 30 * time.Second
@@ -1589,6 +1600,9 @@ func Test_Running_agent(t *testing.T) {
15891600

15901601
okToReboot(ctx, t, testConfig.Clientset.CoreV1().Nodes(), node.Name)
15911602

1603+
// Wait until we try to delete a pod.
1604+
<-podDeletionAttemptCh
1605+
15921606
cancel()
15931607

15941608
select {
@@ -2017,6 +2031,10 @@ func addEvictionSupport(t *testing.T, clientset *fake.Clientset) {
20172031
GroupVersion: "policy/v1",
20182032
}
20192033
clientset.Resources = append(clientset.Resources, coreResources, policyResources)
2034+
2035+
clientset.PrependReactor("create", "pods", func(action k8stesting.Action) (bool, runtime.Object, error) {
2036+
return action.GetSubresource() == "eviction", nil, nil
2037+
})
20202038
}
20212039

20222040
func testPodControllerReference() []metav1.OwnerReference {

0 commit comments

Comments
 (0)