Skip to content

Commit d23f445

Browse files
authored
Create and use a lease in the Compare method to prevent GC (#24)
containerd is not forwarding the lease used by nerdctl. We must set our own, time-expiring lease until it is fixed upstream. Otherwise our objects get garbage collected before we can make references to them.
1 parent 68162ec commit d23f445

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

pkg/snapshot/overlay.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949

5050
"github.com/containerd/containerd/v2/client"
5151
"github.com/containerd/containerd/v2/core/content"
52+
"github.com/containerd/containerd/v2/core/leases"
5253
"github.com/containerd/containerd/v2/core/snapshots"
5354
"github.com/containerd/containerd/v2/core/snapshots/storage"
5455
"github.com/containerd/containerd/v2/pkg/namespaces"
@@ -1372,6 +1373,27 @@ func (o *snapshotter) Compare(ctx context.Context, lower, upper []mount.Mount, o
13721373
return ocispec.Descriptor{}, errdefs.ErrNotImplemented
13731374
}
13741375

1376+
// NOTE: containerd does not forward the namespace used by nerdctl over GRPC, so we must
1377+
// set it here (hardcoded).
1378+
ctx = namespaces.WithNamespace(ctx, "k8s.io")
1379+
1380+
containerdClient, err := client.New("/run/containerd/containerd.sock")
1381+
if err != nil {
1382+
logrus.Errorln("failed to create client to containerd:", err)
1383+
return ocispec.Descriptor{}, err
1384+
}
1385+
defer containerdClient.Close()
1386+
1387+
// NOTE: containerd does not forward the lease used by nerdctl over GRPC, so we must
1388+
// make our own lease here.
1389+
ls := containerdClient.LeasesService()
1390+
lease, err := ls.Create(ctx, leases.WithRandomID(), leases.WithExpiration(1*time.Hour))
1391+
if err != nil {
1392+
logrus.Errorln("failed to acquire lease from containerd:", err)
1393+
return ocispec.Descriptor{}, err
1394+
}
1395+
ctx = leases.WithLease(ctx, lease.ID)
1396+
13751397
// Create a temp directory to store the results of overlaybd-commit.
13761398
tempDir, err := os.MkdirTemp("", fmt.Sprintf("%s-*", upperInfo.Id))
13771399
if err != nil {
@@ -1394,18 +1416,8 @@ func (o *snapshotter) Compare(ctx context.Context, lower, upper []mount.Mount, o
13941416

13951417
// Write the commit file into containerd's content store.
13961418

1397-
containerdClient, err := client.New("/run/containerd/containerd.sock")
1398-
if err != nil {
1399-
logrus.Errorln("failed to create client to containerd:", err)
1400-
return ocispec.Descriptor{}, err
1401-
}
1402-
defer containerdClient.Close()
1403-
14041419
cs := containerdClient.ContentStore()
14051420

1406-
// HACK: Hardcode the namespace, for some reason it is not propagated into our DiffService.
1407-
ctx = namespaces.WithNamespace(ctx, "k8s.io")
1408-
14091421
// Grab a new content store writer using a unique ref to ensure it is new.
14101422
writer, err := cs.Writer(ctx, content.WithRef(config.Reference), content.WithDescriptor(ocispec.Descriptor{MediaType: config.MediaType}))
14111423
if err != nil {

0 commit comments

Comments
 (0)