Skip to content

Commit 9ff4d96

Browse files
ramanan-raviHarshvardhan Karn
andauthored
add imageexits method to check before extracting (#44)
Co-authored-by: Harshvardhan Karn <[email protected]>
1 parent 3b8477a commit 9ff4d96

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

containerd/containerd.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ func (c Containerd) GetSocket() string {
5050
return c.socketPath
5151
}
5252

53+
// ImageExists checks if the image exists
54+
func (c Containerd) ImageExists(imageName string) bool {
55+
_, err := exec.Command("nerdctl", "inspect", imageName, "--address", c.socketPath).Output()
56+
if err != nil {
57+
return false
58+
}
59+
return true
60+
}
61+
5362
// ExtractImage will create the tarball from the containerd image, extracts into dir
5463
// and then skopeo is used to migrate oci layers using the dir to docker v1 layer spec format tar
5564
// and again extracts back to dir

crio/crio.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ func (c CRIO) GetSocket() string {
2020
return c.socketPath
2121
}
2222

23+
// ImageExists checks if the image exists
24+
func (c CRIO) ImageExists(imageName string) bool {
25+
_, err := exec.Command("podman", "inspect", imageName).Output()
26+
if err != nil {
27+
return false
28+
}
29+
return true
30+
}
31+
2332
func (c CRIO) ExtractImage(imageID, imageName, path string) error {
2433
cmd := exec.Command("podman", "save", "--events-backend", "file",
2534
"--format", "docker-dir", "--output", path, imageName)

docker/docker.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ func (d Docker) GetSocket() string {
2323
return d.socketPath
2424
}
2525

26+
// ImageExists checks if the image exists
27+
func (d Docker) ImageExists(imageName string) bool {
28+
_, err := exec.Command("docker", "inspect", imageName).Output()
29+
if err != nil {
30+
return false
31+
}
32+
return true
33+
}
34+
2635
// ExtractImage creates the tarball out of image and extracts it
2736
func (d Docker) ExtractImage(imageID, imageName, path string) error {
2837
var stderr bytes.Buffer

integ/imageexists.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"github.com/deepfence/vessel"
5+
"github.com/sirupsen/logrus"
6+
)
7+
8+
func main() {
9+
// check if image exists
10+
imageName := "nginx:latest"
11+
runtime, err := vessel.NewRuntime()
12+
if err != nil {
13+
logrus.Error(err)
14+
return
15+
}
16+
17+
if runtime.ImageExists(imageName) {
18+
logrus.Infof("Image %s exists", imageName)
19+
} else {
20+
logrus.Infof("Image %s does not exist", imageName)
21+
}
22+
}

podman/podman.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ func (d Podman) GetSocket() string {
2323
return d.socketPath
2424
}
2525

26+
// ImageExists checks if the image exists
27+
func (d Podman) ImageExists(imageName string) bool {
28+
_, err := exec.Command("podman", "--remote", "--url", d.socketPath, "inspect", imageName).Output()
29+
if err != nil {
30+
return false
31+
}
32+
return true
33+
}
34+
2635
// ExtractImage creates the tarball out of image and extracts it
2736
func (d Podman) ExtractImage(imageID, imageName, path string) error {
2837
var stderr bytes.Buffer

runtime.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ type Runtime interface {
88
GetSocket() string
99
ExtractFileSystem(imageTarPath string, outputTarPath string, imageName string) error
1010
ExtractFileSystemContainer(containerId string, namespace string, outputTarPath string) error
11+
ImageExists(imageName string) bool
1112
}

0 commit comments

Comments
 (0)