Skip to content

Commit

Permalink
Simply tarballPath and dockerDaemon code path
Browse files Browse the repository at this point in the history
Signed-off-by: Dominic Ayre <dominicayre@microsoft.com>
  • Loading branch information
DomAyre committed Mar 27, 2024
1 parent b250d56 commit cf55c7f
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions cmd/dmverity-vhd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,45 +100,40 @@ func fetchImageLayers(ctx *cli.Context) (layers []v1.Layer, err error) {

// by default, using remote as source
var img v1.Image
if tarballPath != "" {
// create a tag and search the tarball for the image specified
var imageNameAndTag name.Tag
imageNameAndTag, err = name.NewTag(image)
if err != nil {
return nil, fmt.Errorf("failed to failed to create a tag to search tarball for %s: %w", image, err)
}
// if only an image name is provided and not a tag, the default is "latest"
img, err = tarball.ImageFromPath(tarballPath, &imageNameAndTag)
} else if dockerDaemon {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, fmt.Errorf("failed to instanciate docker client: %w", err)
}
if tarballPath != "" || dockerDaemon {
if dockerDaemon {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, fmt.Errorf("failed to instanciate docker client: %w", err)
}

imageTarReader, err := cli.ImageSave(ctx, []string{image})
if err != nil {
return nil, fmt.Errorf("failed to load image tar: %w", err)
}
defer imageTarReader.Close()
imageTarReader, err := cli.ImageSave(ctx, []string{image})
if err != nil {
return nil, fmt.Errorf("failed to load image tar: %w", err)
}
defer imageTarReader.Close()

tarFile, err := os.CreateTemp("", "image-tar")
if err != nil {
return nil, fmt.Errorf("failed to create tar file: %w", err)
}
defer tarFile.Close()
tarFile, err := os.CreateTemp("", "image-tar")
if err != nil {
return nil, fmt.Errorf("failed to create tar file: %w", err)
}
defer tarFile.Close()

if _, err := io.Copy(tarFile, imageTarReader); err != nil {
return nil, fmt.Errorf("failed to save tar: %w", err)
}
if _, err := io.Copy(tarFile, imageTarReader); err != nil {
return nil, fmt.Errorf("failed to save tar: %w", err)
}

tarballPath = tarFile.Name()
}
// create a tag and search the tarball for the image specified
var imageNameAndTag name.Tag
imageNameAndTag, err = name.NewTag(image)

img, err = tarball.ImageFromPath(tarFile.Name(), &imageNameAndTag)
if err != nil {
return nil, fmt.Errorf("failed to load image from tar: %w", err)
return nil, fmt.Errorf("failed to failed to create a tag to search tarball for %s: %w", image, err)
}
// if only an image name is provided and not a tag, the default is "latest"
img, err = tarball.ImageFromPath(tarballPath, &imageNameAndTag)
} else {
var remoteOpts []remote.Option
if ctx.IsSet(usernameFlag) && ctx.IsSet(passwordFlag) {
Expand Down

0 comments on commit cf55c7f

Please sign in to comment.