From dfd4e42e57b3ff3080469cb929b8f37ffb04dede Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Tue, 3 Aug 2021 13:07:02 -0700 Subject: [PATCH] Wrap context with lease before importing images Signed-off-by: Brad Davidson --- pkg/agent/containerd/containerd.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/agent/containerd/containerd.go b/pkg/agent/containerd/containerd.go index 446643c082..6c9e9e7ef1 100644 --- a/pkg/agent/containerd/containerd.go +++ b/pkg/agent/containerd/containerd.go @@ -20,6 +20,7 @@ import ( "github.com/containerd/containerd/leases" "github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/reference/docker" + "github.com/containerd/cri/pkg/constants" "github.com/klauspost/compress/zstd" "github.com/natefinch/lumberjack" "github.com/pierrec/lz4" @@ -155,9 +156,9 @@ func preloadImages(ctx context.Context, cfg *config.Node) error { } defer criConn.Close() - // Ensure that nothing else can modify the image store while we're importing, - // and that our images are imported into the k8s.io namespace - ctx = namespaces.WithNamespace(ctx, "k8s.io") + // Ensure that our images are imported into the correct namespace + ctx = namespaces.WithNamespace(ctx, constants.K8sContainerdNamespace) + // At startup all leases from k3s are cleared ls := client.LeasesService() existingLeases, err := ls.List(ctx) @@ -173,11 +174,14 @@ func preloadImages(ctx context.Context, cfg *config.Node) error { } // Any images found on import are given a lease that never expires - _, err = ls.Create(ctx, leases.WithID(version.Program)) + lease, err := ls.Create(ctx, leases.WithID(version.Program)) if err != nil { return err } + // Ensure that our images are locked by the lease + ctx = leases.WithLease(ctx, lease.ID) + for _, fileInfo := range fileInfos { if fileInfo.IsDir() { continue