From 1bb5b972b815e04f8c9a1371b7784970d3f3a01f Mon Sep 17 00:00:00 2001 From: Predrag Rogic Date: Tue, 28 Nov 2023 00:50:05 +0000 Subject: [PATCH 1/3] fix containerd ImageExists to independently look for image name and image id sha --- pkg/minikube/cruntime/containerd.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/minikube/cruntime/containerd.go b/pkg/minikube/cruntime/containerd.go index aa99dc696f6e..3564c315c8c4 100644 --- a/pkg/minikube/cruntime/containerd.go +++ b/pkg/minikube/cruntime/containerd.go @@ -248,12 +248,12 @@ func (r *Containerd) Disable() error { // ImageExists checks if image exists based on image name and optionally image sha func (r *Containerd) ImageExists(name string, sha string) bool { - c := exec.Command("/bin/bash", "-c", fmt.Sprintf("sudo ctr -n=k8s.io images check | grep %s", name)) + klog.Infof("Checking existence of image with name %q and sha %q", name, sha) + c := exec.Command("sudo", "ctr", "-n=k8s.io", "images", "check") rr, err := r.Runner.RunCmd(c) - if err != nil { - return false - } - if sha != "" && !strings.Contains(rr.Output(), sha) { + if err != nil || + !strings.Contains(rr.Output(), name) || + (sha != "" && !strings.Contains(rr.Output(), sha)) { return false } return true From a5d281e28437977f4d18671976d7d21a2800e904 Mon Sep 17 00:00:00 2001 From: Predrag Rogic Date: Tue, 28 Nov 2023 01:25:46 +0000 Subject: [PATCH 2/3] simplify if statement to the context --- pkg/minikube/cruntime/containerd.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/minikube/cruntime/containerd.go b/pkg/minikube/cruntime/containerd.go index 3564c315c8c4..e62beb304b9b 100644 --- a/pkg/minikube/cruntime/containerd.go +++ b/pkg/minikube/cruntime/containerd.go @@ -250,8 +250,7 @@ func (r *Containerd) Disable() error { func (r *Containerd) ImageExists(name string, sha string) bool { klog.Infof("Checking existence of image with name %q and sha %q", name, sha) c := exec.Command("sudo", "ctr", "-n=k8s.io", "images", "check") - rr, err := r.Runner.RunCmd(c) - if err != nil || + if rr, err := r.Runner.RunCmd(c); err != nil || !strings.Contains(rr.Output(), name) || (sha != "" && !strings.Contains(rr.Output(), sha)) { return false From 199a0e3eaea8884b6f30e504f56bf5d155934cac Mon Sep 17 00:00:00 2001 From: Predrag Rogic Date: Tue, 28 Nov 2023 01:28:14 +0000 Subject: [PATCH 3/3] add comment to clarify change in checking logic --- pkg/minikube/cruntime/containerd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/minikube/cruntime/containerd.go b/pkg/minikube/cruntime/containerd.go index e62beb304b9b..fdbb28ae4336 100644 --- a/pkg/minikube/cruntime/containerd.go +++ b/pkg/minikube/cruntime/containerd.go @@ -250,6 +250,7 @@ func (r *Containerd) Disable() error { func (r *Containerd) ImageExists(name string, sha string) bool { klog.Infof("Checking existence of image with name %q and sha %q", name, sha) c := exec.Command("sudo", "ctr", "-n=k8s.io", "images", "check") + // note: image name and image id's sha can be on different lines in ctr output if rr, err := r.Runner.RunCmd(c); err != nil || !strings.Contains(rr.Output(), name) || (sha != "" && !strings.Contains(rr.Output(), sha)) {