Skip to content

Commit 3cdfccb

Browse files
committed
move systemd timer cleanup to oci hooks
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 53bf005 commit 3cdfccb

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

pkg/cmd/container/kill.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/containerd/nerdctl/v2/pkg/api/types"
3636
"github.com/containerd/nerdctl/v2/pkg/clientutil"
3737
"github.com/containerd/nerdctl/v2/pkg/containerutil"
38-
"github.com/containerd/nerdctl/v2/pkg/healthcheck"
3938
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
4039
"github.com/containerd/nerdctl/v2/pkg/labels"
4140
"github.com/containerd/nerdctl/v2/pkg/netutil"
@@ -113,9 +112,9 @@ func killContainer(ctx context.Context, container containerd.Container, signal s
113112
}
114113

115114
// Clean up healthcheck systemd units
116-
if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, container); err != nil {
117-
log.G(ctx).Warnf("failed to clean up healthcheck units for container %s: %s", container.ID(), err)
118-
}
115+
// if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, container); err != nil {
116+
// log.G(ctx).Warnf("failed to clean up healthcheck units for container %s: %s", container.ID(), err)
117+
// }
119118

120119
// signal will be sent once resume is finished
121120
if paused {

pkg/cmd/container/remove.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24-
"github.com/containerd/nerdctl/v2/pkg/healthcheck"
2524
"os"
2625
"syscall"
2726

@@ -181,9 +180,9 @@ func RemoveContainer(ctx context.Context, c containerd.Container, globalOptions
181180
retErr = nil
182181

183182
// Clean up healthcheck systemd units
184-
if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, c); err != nil {
185-
log.G(ctx).WithError(err).Warnf("failed to clean up healthcheck units for container %q", id)
186-
}
183+
// if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, c); err != nil {
184+
// log.G(ctx).WithError(err).Warnf("failed to clean up healthcheck units for container %q", id)
185+
// }
187186

188187
// Now, delete the actual container
189188
var delOpts []containerd.DeleteOpts

pkg/containerutil/containerutil.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
362362
}()
363363

364364
// Clean up healthcheck units if configured.
365-
if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, container); err != nil {
366-
return fmt.Errorf("failed to clean up healthcheck units for container %s", container.ID())
367-
}
365+
// if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, container); err != nil {
366+
// return fmt.Errorf("failed to clean up healthcheck units for container %s", container.ID())
367+
// }
368368

369369
if timeout == nil {
370370
t, ok := l[labels.StopTimeout]
@@ -505,9 +505,9 @@ func Pause(ctx context.Context, client *containerd.Client, id string) error {
505505
}
506506

507507
// Clean up healthcheck units if configured.
508-
if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, container); err != nil {
509-
return fmt.Errorf("failed to clean up healthcheck units for container %s", container.ID())
510-
}
508+
// if err := healthcheck.RemoveTransientHealthCheckFiles(ctx, container); err != nil {
509+
// return fmt.Errorf("failed to clean up healthcheck units for container %s", container.ID())
510+
// }
511511

512512
switch status.Status {
513513
case containerd.Paused:

pkg/healthcheck/healthcheck_manager_linux.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,25 @@ func RemoveTransientHealthCheckFiles(ctx context.Context, container containerd.C
118118
return nil
119119
}
120120

121-
fmt.Printf("⏱️ Removing healthcheck timer unit: %s\n", container.ID()) // TODO remove this later
121+
return RemoveTransientHealthCheckFilesByID(ctx, container.ID())
122+
}
123+
124+
// RemoveTransientHealthCheckFilesByID stops and cleans up the transient timer and service using just the container ID.
125+
func RemoveTransientHealthCheckFilesByID(ctx context.Context, containerID string) error {
126+
// Don't proceed if systemd is unavailable or disabled
127+
if !defaults.IsSystemdAvailable() || os.Getenv("DISABLE_HC_SYSTEMD") == "true" {
128+
return nil
129+
}
130+
131+
fmt.Printf("⏱️ Removing healthcheck timer unit: %s\n", containerID) // TODO remove this later
122132

123133
conn, err := dbus.NewSystemConnectionContext(context.Background())
124134
if err != nil {
125135
return fmt.Errorf("systemd DBUS connect error: %w", err)
126136
}
127137
defer conn.Close()
128138

129-
unitName := hcUnitName(container.ID(), true)
139+
unitName := hcUnitName(containerID, true)
130140
timer := unitName + ".timer"
131141
service := unitName + ".service"
132142

pkg/ocihook/ocihook.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939

4040
"github.com/containerd/nerdctl/v2/pkg/bypass4netnsutil"
4141
"github.com/containerd/nerdctl/v2/pkg/dnsutil/hostsstore"
42+
"github.com/containerd/nerdctl/v2/pkg/healthcheck"
4243
"github.com/containerd/nerdctl/v2/pkg/internal/filesystem"
4344
"github.com/containerd/nerdctl/v2/pkg/labels"
4445
"github.com/containerd/nerdctl/v2/pkg/namestore"
@@ -560,8 +561,11 @@ func onCreateRuntime(opts *handlerOpts) error {
560561
}
561562

562563
func onPostStop(opts *handlerOpts) error {
564+
log.L.Debugf("onPostStop hook triggered for container %s (namespace: %s)", opts.state.ID, opts.state.Annotations[labels.Namespace])
565+
563566
lf, err := state.New(opts.state.Annotations[labels.StateDir])
564567
if err != nil {
568+
log.L.WithError(err).Errorf("failed to create state store for container %s", opts.state.ID)
565569
return err
566570
}
567571

@@ -585,6 +589,13 @@ func onPostStop(opts *handlerOpts) error {
585589

586590
ctx := context.Background()
587591
ns := opts.state.Annotations[labels.Namespace]
592+
593+
if err := healthcheck.RemoveTransientHealthCheckFilesByID(ctx, opts.state.ID); err != nil {
594+
log.L.WithError(err).Warnf("failed to clean up healthcheck units for container %s", opts.state.ID)
595+
} else {
596+
log.L.Infof("successfully cleaned up healthcheck files for container %s", opts.state.ID)
597+
}
598+
588599
if opts.cni != nil {
589600
var err error
590601
b4nnEnabled, b4nnBindEnabled, err := bypass4netnsutil.IsBypass4netnsEnabled(opts.state.Annotations)

0 commit comments

Comments
 (0)