Skip to content

Commit ec0fb27

Browse files
Merge pull request #5284 from pacevedom/USHIFT-5088
USHIFT-5088: Send sigterm to self on sysconfwatch events
2 parents bee1f31 + f624334 commit ec0fb27

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

pkg/sysconfwatch/sysconfwatch_linux.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"math"
2222
"os"
23+
"syscall"
2324
"time"
2425

2526
"github.com/openshift/microshift/pkg/config"
@@ -91,6 +92,17 @@ func getSysMonTimes() (int64, int64) {
9192
return stm.Sec, mtm.Sec
9293
}
9394

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+
94106
func (c *SysConfWatchController) Run(ctx context.Context, ready chan<- struct{}, stopped chan<- struct{}) error {
95107
defer close(stopped)
96108
ticker := time.NewTicker(sysConfigCheckInterval)
@@ -110,26 +122,26 @@ func (c *SysConfWatchController) Run(ctx context.Context, ready chan<- struct{},
110122
// Check the IP change
111123
currentIP, err := util.GetHostIP(c.userNodeIP)
112124
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()
115127
return nil
116128
}
117129
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()
120132
return nil
121133
}
122134
// Dual stack case
123135
if c.NodeIPv6 != "" {
124136
currentIP, err = util.GetHostIPv6(c.userNodeIPv6)
125137
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()
128140
return nil
129141
}
130142
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()
133145
return nil
134146
}
135147
}
@@ -155,7 +167,7 @@ func (c *SysConfWatchController) Run(ctx context.Context, ready chan<- struct{},
155167
mtimeRef = mtimeCur
156168
} else {
157169
klog.Warningf("realtime clock change detected, time drifted %v seconds, restarting MicroShift", smtDiffDrift)
158-
os.Exit(0)
170+
go sendSigterm()
159171
return nil
160172
}
161173
}

0 commit comments

Comments
 (0)