@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"math"
22
22
"os"
23
+ "syscall"
23
24
"time"
24
25
25
26
"github.com/openshift/microshift/pkg/config"
@@ -91,6 +92,17 @@ func getSysMonTimes() (int64, int64) {
91
92
return stm .Sec , mtm .Sec
92
93
}
93
94
95
+ func sendSigterm () {
96
+ pid := os .Getpid ()
97
+ klog .Infof ("Sending SIGTERM to self (pid %d) to initiate graceful shutdown" , pid )
98
+ p , _ := os .FindProcess (pid )
99
+ err := p .Signal (syscall .SIGTERM )
100
+ if err != nil {
101
+ klog .Errorf ("failed to send SIGTERM to self. Forcing shutdown: %v" , err )
102
+ os .Exit (1 )
103
+ }
104
+ }
105
+
94
106
func (c * SysConfWatchController ) Run (ctx context.Context , ready chan <- struct {}, stopped chan <- struct {}) error {
95
107
defer close (stopped )
96
108
ticker := time .NewTicker (sysConfigCheckInterval )
@@ -110,26 +122,26 @@ func (c *SysConfWatchController) Run(ctx context.Context, ready chan<- struct{},
110
122
// Check the IP change
111
123
currentIP , err := util .GetHostIP (c .userNodeIP )
112
124
if err != nil {
113
- klog .Warningf ("cannot find an host IP: %v" , err )
114
- os . Exit ( 1 )
125
+ klog .Warningf ("Restarting MicroShift. Cannot find a host IP: %v" , err )
126
+ go sendSigterm ( )
115
127
return nil
116
128
}
117
129
if c .NodeIP != currentIP {
118
- klog .Warningf ("IP address has changed from %q to %q, restarting MicroShift " , c .NodeIP , currentIP )
119
- os . Exit ( 1 )
130
+ klog .Warningf ("Restarting MicroShift. IP address has changed from %q to %q" , c .NodeIP , currentIP )
131
+ go sendSigterm ( )
120
132
return nil
121
133
}
122
134
// Dual stack case
123
135
if c .NodeIPv6 != "" {
124
136
currentIP , err = util .GetHostIPv6 (c .userNodeIPv6 )
125
137
if err != nil {
126
- klog .Warningf ("cannot find an host IP : %v" , err )
127
- os . Exit ( 1 )
138
+ klog .Warningf ("Restarting MicroShift. Cannot find a host IPv6 : %v" , err )
139
+ go sendSigterm ( )
128
140
return nil
129
141
}
130
142
if c .NodeIPv6 != currentIP {
131
- klog .Warningf ("IP address has changed from %q to %q, restarting MicroShift " , c .NodeIPv6 , currentIP )
132
- os . Exit ( 1 )
143
+ klog .Warningf ("Restarting MicroShift. IP address has changed from %q to %q" , c .NodeIPv6 , currentIP )
144
+ go sendSigterm ( )
133
145
return nil
134
146
}
135
147
}
@@ -155,7 +167,7 @@ func (c *SysConfWatchController) Run(ctx context.Context, ready chan<- struct{},
155
167
mtimeRef = mtimeCur
156
168
} else {
157
169
klog .Warningf ("realtime clock change detected, time drifted %v seconds, restarting MicroShift" , smtDiffDrift )
158
- os . Exit ( 0 )
170
+ go sendSigterm ( )
159
171
return nil
160
172
}
161
173
}
0 commit comments