Skip to content

Commit

Permalink
early check if image exists at all
Browse files Browse the repository at this point in the history
  • Loading branch information
prezha committed Aug 16, 2021
1 parent ab61a07 commit 11d31dd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
10 changes: 7 additions & 3 deletions pkg/minikube/cruntime/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,14 @@ func (r *Containerd) Disable() error {
return r.Init.ForceStop("containerd")
}

// ImageExists checks if an image exists, expected input format
// 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 | grep %s", name, sha))
if _, err := r.Runner.RunCmd(c); err != nil {
c := exec.Command("/bin/bash", "-c", fmt.Sprintf("sudo ctr -n=k8s.io images check | grep %s", name))
rr, err := r.Runner.RunCmd(c)
if err != nil {
return false
}
if sha != "" && !strings.Contains(rr.Output(), sha) {
return false
}
return true
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/cruntime/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ func (r *CRIO) Disable() error {
return r.Init.ForceStop("crio")
}

// ImageExists checks if an image exists
// ImageExists checks if image exists based on image name and optionally image sha
func (r *CRIO) ImageExists(name string, sha string) bool {
// expected output looks like [NAME@sha256:SHA]
c := exec.Command("sudo", "podman", "image", "inspect", "--format", "{{.Id}}", name)
rr, err := r.Runner.RunCmd(c)
if err != nil {
return false
}
if !strings.Contains(rr.Output(), sha) {
if sha != "" && !strings.Contains(rr.Output(), sha) {
return false
}
return true
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/cruntime/cruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type Manager interface {
// Tag an image
TagImage(string, string) error

// ImageExists takes image name and image sha checks if an it exists
// ImageExists takes image name and optionally image sha to check if an image exists
ImageExists(string, string) bool
// ListImages returns a list of images managed by this container runtime
ListImages(ListImagesOptions) ([]string, error)
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/cruntime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ func (r *Docker) Disable() error {
return r.Init.Mask("docker.service")
}

// ImageExists checks if an image exists
// ImageExists checks if image exists based on image name and optionally image sha
func (r *Docker) ImageExists(name string, sha string) bool {
// expected output looks like [SHA_ALGO:SHA]
c := exec.Command("docker", "image", "inspect", "--format", "{{.Id}}", name)
rr, err := r.Runner.RunCmd(c)
if err != nil {
return false
}
if !strings.Contains(rr.Output(), sha) {
if sha != "" && !strings.Contains(rr.Output(), sha) {
return false
}
return true
Expand Down
4 changes: 4 additions & 0 deletions pkg/minikube/machine/cache_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ func transferAndSaveImage(cr command.Runner, k8s config.KubernetesConfig, dst st
return errors.Wrap(err, "runtime")
}

if !r.ImageExists(imgName, "") {
return errors.Errorf("image %s not found", imgName)
}

klog.Infof("Saving image to: %s", dst)
filename := filepath.Base(dst)

Expand Down

0 comments on commit 11d31dd

Please sign in to comment.