Skip to content

Commit

Permalink
Add function to inspect and pull the image
Browse files Browse the repository at this point in the history
To use BIB we will need to also inpect images and pull them
if required. So, let's make a function to take care of this task
instead of copy-paste the same code.

I think we should go back to have a podman pkg, it will make it
easier in the future to switch between remote and local podman.

Signed-off-by: German Maglione <[email protected]>
  • Loading branch information
germag committed Jul 9, 2024
1 parent 5001fd9 commit 664e527
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
30 changes: 7 additions & 23 deletions pkg/bootc/bootc_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/containers/podman-bootc/pkg/utils"

"github.com/containers/podman/v5/pkg/bindings/containers"
"github.com/containers/podman/v5/pkg/bindings/images"
"github.com/containers/podman/v5/pkg/domain/entities/types"
"github.com/containers/podman/v5/pkg/specgen"
"github.com/docker/go-units"
Expand Down Expand Up @@ -277,32 +276,17 @@ func (p *BootcDisk) bootcInstallImageToDisk(quiet bool, diskConfig DiskImageConf
}

// pullImage fetches the container image if not present
func (p *BootcDisk) pullImage() (err error) {
pullPolicy := "missing"
ids, err := images.Pull(p.Ctx, p.ImageNameOrId, &images.PullOptions{Policy: &pullPolicy})
func (p *BootcDisk) pullImage() error {
imageData, err := utils.PullAndInspect(p.Ctx, p.ImageNameOrId)
if err != nil {
return fmt.Errorf("failed to pull image: %w", err)
}

if len(ids) == 0 {
return fmt.Errorf("no ids returned from image pull")
}

if len(ids) > 1 {
return fmt.Errorf("multiple ids returned from image pull")
}

image, err := images.GetImage(p.Ctx, p.ImageNameOrId, &images.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get image: %w", err)
return err
}
p.imageData = image

imageId := ids[0]
p.ImageId = imageId
p.RepoTag = image.RepoTags[0]
p.imageData = imageData
p.ImageId = imageData.ID
p.RepoTag = imageData.RepoTags[0]

return
return nil
}

// runInstallContainer runs the bootc installer in a container to create a disk image
Expand Down
18 changes: 18 additions & 0 deletions pkg/utils/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/containers/podman/v5/pkg/bindings/images"
"github.com/containers/podman/v5/pkg/domain/entities/types"
"os"
"os/exec"
"strings"
Expand All @@ -28,6 +30,22 @@ type machineInfo struct {
rootful bool
}

// PullAndInspect inpects the image, pulling in if the image if required
func PullAndInspect(ctx context.Context, imageNameOrId string) (*types.ImageInspectReport, error) {
pullPolicy := "missing"
_, err := images.Pull(ctx, imageNameOrId, &images.PullOptions{Policy: &pullPolicy})
if err != nil {
return nil, fmt.Errorf("failed to pull image: %w", err)
}

imageInfo, err := images.GetImage(ctx, imageNameOrId, &images.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to inspect image: %w", err)
}

return imageInfo, nil
}

func GetMachineContext() (*MachineContext, error) {
//podman machine connection
machineInfo, err := getMachineInfo()
Expand Down

0 comments on commit 664e527

Please sign in to comment.