Remove auto import label

Signed-off-by: Vitor Savian <vitor.savian@suse.com>
pull/10973/head
Vitor Savian 2 weeks ago
parent 5b349a23b0
commit 352a2f8eaf

@ -34,9 +34,8 @@ var (
// In addition to using the CRI pinned label, we add our own label to indicate that
// the image was pinned by the import process, so that we can clear the pin on subsequent startups.
// ref: https://github.com/containerd/containerd/blob/release/1.7/pkg/cri/labels/labels.go
k3sPinnedImageLabelKey = "io.cattle." + version.Program + ".pinned"
k3sAutoImportImageLabelKey = "io.cattle." + version.Program + ".import"
k3sPinnedImageLabelValue = "pinned"
k3sPinnedImageLabelKey = "io.cattle." + version.Program + ".pinned"
k3sPinnedImageLabelValue = "pinned"
)
// Run configures and starts containerd as a child process. Once it is up, images are preloaded
@ -251,23 +250,6 @@ func clearLeases(ctx context.Context, client *containerd.Client) error {
return nil
}
// clearLabelFromFile removes the auto import label on images with the value based on the file name
func clearLabelFromAutoImport(ctx context.Context, client *containerd.Client, filePath string) error {
var errs []error
imageService := client.ImageService()
images, err := imageService.List(ctx, fmt.Sprintf("labels.%q==%s", k3sAutoImportImageLabelKey, filepath.Base(filePath)))
if err != nil {
return err
}
for _, image := range images {
delete(image.Labels, k3sAutoImportImageLabelKey)
if _, err := imageService.Update(ctx, image, "labels"); err != nil {
errs = append(errs, errors.Wrap(err, "failed to delete auto import label from image "+image.Name))
}
}
return merr.NewErrors(errs...)
}
// clearLabels removes the pinned labels on all images in the image store that were previously pinned by k3s
func clearLabels(ctx context.Context, client *containerd.Client) error {
var errs []error
@ -277,7 +259,6 @@ func clearLabels(ctx context.Context, client *containerd.Client) error {
return err
}
for _, image := range images {
delete(image.Labels, k3sAutoImportImageLabelKey)
delete(image.Labels, k3sPinnedImageLabelKey)
delete(image.Labels, labels.PinnedImageLabelKey)
if _, err := imageService.Update(ctx, image, "labels"); err != nil {
@ -294,8 +275,7 @@ func labelImages(ctx context.Context, client *containerd.Client, images []images
imageService := client.ImageService()
for i, image := range images {
if image.Labels[k3sPinnedImageLabelKey] == k3sPinnedImageLabelValue &&
image.Labels[labels.PinnedImageLabelKey] == labels.PinnedImageLabelValue &&
image.Labels[k3sAutoImportImageLabelKey] == fileName {
image.Labels[labels.PinnedImageLabelKey] == labels.PinnedImageLabelValue {
continue
}
@ -303,7 +283,6 @@ func labelImages(ctx context.Context, client *containerd.Client, images []images
image.Labels = map[string]string{}
}
image.Labels[k3sAutoImportImageLabelKey] = fileName
image.Labels[k3sPinnedImageLabelKey] = k3sPinnedImageLabelValue
image.Labels[labels.PinnedImageLabelKey] = labels.PinnedImageLabelValue
updatedImage, err := imageService.Update(ctx, image, "labels")

@ -247,11 +247,7 @@ func (w *Watcher) processImageEvent(ctx context.Context, event fsnotify.Event, c
return nil
}
// Remove the auto import label from the image
err := clearLabelFromAutoImport(ctx, client, event.Name)
if err != nil {
logrus.Errorf("Failed to clear auto import label for %s: %v", event.Name, err)
}
// delete the file from the file map
delete(w.filesMap, event.Name)
logrus.Debugf("Removed file from the image watcher controller: %s", event.Name)
}
@ -269,12 +265,7 @@ func (w *Watcher) processImageEvent(ctx context.Context, event fsnotify.Event, c
return nil
}
// clear label because when a new file will be created because of the rename
// the create event will label the images with the name of the new file
err := clearLabelFromAutoImport(ctx, client, event.Name)
if err != nil {
logrus.Errorf("Failed to clear auto import label for %s: %v", event.Name, err)
}
// delete the file from the file map
delete(w.filesMap, event.Name)
logrus.Debugf("Removed file from the image watcher controller: %s", event.Name)
}

@ -97,20 +97,18 @@ var _ = Describe("Verify Create", Ordered, func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/redis`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.import=testautoimport.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
})
It("Change name for the file and see if the label is removed", func() {
It("Change name for the file and see if the label is still pinned", func() {
cmd := `mv /var/lib/rancher/k3s/agent/images/testautoimport.txt /var/lib/rancher/k3s/agent/images/testautoimportrename.txt`
_, err := e2e.RunCmdOnNode(cmd, serverNodeNames[0])
Expect(err).NotTo(HaveOccurred(), "failed: "+cmd)
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/redis`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.import=testautoimportrename.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
@ -123,7 +121,6 @@ var _ = Describe("Verify Create", Ordered, func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/busybox`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.import=bb.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
@ -134,7 +131,6 @@ var _ = Describe("Verify Create", Ordered, func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/busybox`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).ShouldNot(ContainSubstring("io.cattle.k3s.import=bb.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
@ -145,13 +141,12 @@ var _ = Describe("Verify Create", Ordered, func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/busybox`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.import=bb.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
})
It("Move the folder, add another image and then see if the image was added", func() {
It("Move the folder, add a image and then see if the image is going to be pinned", func() {
cmd := `mv /var/lib/rancher/k3s/agent/images /var/lib/rancher/k3s/agent/test`
_, err := e2e.RunCmdOnNode(cmd, serverNodeNames[0])
Expect(err).NotTo(HaveOccurred(), "failed: "+cmd)
@ -166,7 +161,6 @@ var _ = Describe("Verify Create", Ordered, func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/mysql`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.import=mysql.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
@ -188,7 +182,6 @@ var _ = Describe("Verify Create", Ordered, func() {
It("Verify bb.txt image and see if are pinned", func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/busybox`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.import=bb.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
@ -201,7 +194,6 @@ var _ = Describe("Verify Create", Ordered, func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/busybox`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).ShouldNot(ContainSubstring("io.cattle.k3s.import=bb.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).Should(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())
@ -223,7 +215,6 @@ var _ = Describe("Verify Create", Ordered, func() {
It("Verify if bb.txt image is unpinned", func() {
Eventually(func(g Gomega) {
cmd := `k3s ctr images list | grep library/busybox`
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).ShouldNot(ContainSubstring("io.cattle.k3s.import=bb.txt"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).ShouldNot(ContainSubstring("io.cattle.k3s.pinned=pinned"))
g.Expect(e2e.RunCmdOnNode(cmd, serverNodeNames[0])).ShouldNot(ContainSubstring("io.cri-containerd.pinned=pinned"))
}, "620s", "5s").Should(Succeed())

Loading…
Cancel
Save