X Tutup
Skip to content

Commit ddae905

Browse files
committed
Support NOTIFY_SOCKET
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
1 parent 3868dd7 commit ddae905

File tree

8 files changed

+254
-0
lines changed

8 files changed

+254
-0
lines changed

cmd/containerd/command/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ can be used and modified as necessary as a custom configuration.`
227227
}
228228
serve(ctx, l, server.ServeGRPC)
229229

230+
if err := notifyReady(ctx); err != nil {
231+
log.G(ctx).WithError(err).Warn("notify ready failed")
232+
}
233+
230234
log.G(ctx).Infof("containerd successfully booted in %fs", time.Since(start).Seconds())
231235
<-done
232236
return nil

cmd/containerd/command/main_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func handleSignals(ctx context.Context, signals chan os.Signal, serverC chan *se
5252
case unix.SIGPIPE:
5353
continue
5454
default:
55+
if err := notifyStopping(ctx); err != nil {
56+
log.G(ctx).WithError(err).Error("notify stopping failed")
57+
}
58+
5559
if server == nil {
5660
close(done)
5761
return

cmd/containerd/command/main_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func handleSignals(ctx context.Context, signals chan os.Signal, serverC chan *se
5050
server = s
5151
case s := <-signals:
5252
log.G(ctx).WithField("signal", s).Debug("received signal")
53+
54+
if err := notifyStopping(ctx); err != nil {
55+
log.G(ctx).WithError(err).Error("notify stopping failed")
56+
}
57+
5358
if server == nil {
5459
close(done)
5560
return
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// +build linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package command
20+
21+
import (
22+
"context"
23+
"os"
24+
25+
sd "github.com/coreos/go-systemd/v22/daemon"
26+
27+
"github.com/containerd/containerd/log"
28+
)
29+
30+
// notifyReady notifies systemd that the daemon is ready to serve requests
31+
func notifyReady(ctx context.Context) error {
32+
return sdNotify(ctx, sd.SdNotifyReady)
33+
}
34+
35+
// notifyStopping notifies systemd that the daemon is about to be stopped
36+
func notifyStopping(ctx context.Context) error {
37+
return sdNotify(ctx, sd.SdNotifyStopping)
38+
}
39+
40+
func sdNotify(ctx context.Context, state string) error {
41+
if os.Getenv("NOTIFY_SOCKET") != "" {
42+
notified, err := sd.SdNotify(false, state)
43+
log.G(ctx).
44+
WithError(err).
45+
WithField("notified", notified).
46+
WithField("state", state).
47+
Debug("sd notification")
48+
return err
49+
}
50+
51+
return nil
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// +build !linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package command
20+
21+
import (
22+
"context"
23+
)
24+
25+
func notifyReady(ctx context.Context) error {
26+
return nil
27+
}
28+
29+
func notifyStopping(ctx context.Context) error {
30+
return nil
31+
}

containerd.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ After=network.target local-fs.target
77
ExecStartPre=-/sbin/modprobe overlay
88
ExecStart=/usr/local/bin/containerd
99

10+
Type=notify
1011
Delegate=yes
1112
KillMode=process
1213
Restart=always

vendor/github.com/coreos/go-systemd/v22/daemon/sdnotify.go

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

vendor/github.com/coreos/go-systemd/v22/daemon/watchdog.go

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

0 commit comments

Comments
 (0)
X Tutup