|
| 1 | +// Copyright (c) 2023-2026, Nubificus LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package containerdshim |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/containerd/containerd/runtime/v2/runc/manager" |
| 22 | + "github.com/containerd/containerd/runtime/v2/shim" |
| 23 | + "github.com/containerd/log" |
| 24 | + containerdShim "github.com/urunc-dev/urunc/pkg/containerd-shim/containerd" |
| 25 | +) |
| 26 | + |
| 27 | +const containerdGRPCAddressEnv = "GRPC_ADDRESS" |
| 28 | + |
| 29 | +func containerdGRPCAddress() string { |
| 30 | + return os.Getenv(containerdGRPCAddressEnv) |
| 31 | +} |
| 32 | + |
| 33 | +type shimManager struct { |
| 34 | + shim.Manager |
| 35 | +} |
| 36 | + |
| 37 | +func NewShimManager(runtime string) shim.Manager { |
| 38 | + return &shimManager{Manager: manager.NewShimManager(runtime)} |
| 39 | +} |
| 40 | + |
| 41 | +func (m *shimManager) Stop(ctx context.Context, id string) (shim.StopStatus, error) { |
| 42 | + bundle, err := os.Getwd() |
| 43 | + if err != nil { |
| 44 | + log.G(ctx).WithError(err).Warn("urunc(shim): getwd during delete failed") |
| 45 | + return m.Manager.Stop(ctx, id) |
| 46 | + } |
| 47 | + |
| 48 | + shouldCleanup, snapshotter, err := containerdShim.ShouldCleanupRootfsView(bundle) |
| 49 | + if err != nil { |
| 50 | + log.G(ctx).WithError(err).Warn("urunc(shim): read rootfs view cleanup state from bundle during delete failed") |
| 51 | + return m.Manager.Stop(ctx, id) |
| 52 | + } |
| 53 | + if !shouldCleanup { |
| 54 | + return m.Manager.Stop(ctx, id) |
| 55 | + } |
| 56 | + |
| 57 | + address := containerdGRPCAddress() |
| 58 | + if address == "" { |
| 59 | + log.G(ctx).Warn("urunc(shim): containerd gRPC address unset during delete; rootfs view cleanup skipped") |
| 60 | + return m.Manager.Stop(ctx, id) |
| 61 | + } |
| 62 | + |
| 63 | + session, err := containerdShim.OpenSession(ctx, address, id) |
| 64 | + if err != nil { |
| 65 | + log.G(ctx).WithError(err).Warn("urunc(shim): open containerd session for rootfs view cleanup failed") |
| 66 | + return m.Manager.Stop(ctx, id) |
| 67 | + } |
| 68 | + defer func() { |
| 69 | + if err := session.Close(); err != nil { |
| 70 | + log.G(ctx).WithError(err).Warn("urunc(shim): failed to close containerd session after rootfs view cleanup") |
| 71 | + } |
| 72 | + }() |
| 73 | + |
| 74 | + if err := containerdShim.NewRootfsViewAccessor(session).Cleanup(ctx, snapshotter); err != nil { |
| 75 | + log.G(ctx).WithError(err).Warn("urunc(shim): rootfs view cleanup during delete failed") |
| 76 | + } |
| 77 | + |
| 78 | + return m.Manager.Stop(ctx, id) |
| 79 | +} |
0 commit comments