Skip to content

Commit

Permalink
🐛 Fix container image resolve in v9 (#1705)
Browse files Browse the repository at this point in the history
Now, the os providers tries to find a requested container image locally
within the dcker daemon. When this doesn't work, it tries to find the
image in a remote registry.

Fixes #1699

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Sep 18, 2023
1 parent 58627c1 commit b03fcb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions providers/os/connection/docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ func NewDockerContainerImageConnection(id uint32, conf *inventory.Config, asset
return nil, err
}
}
// Determine whether the image is locally present or not.
resolver := docker_discovery.Resolver{}
resolvedAssets, err := resolver.Resolve(context.Background(), asset, conf, nil)
if err != nil {
return nil, err
}

if len(resolvedAssets) > 1 {
return nil, errors.New("provided image name resolved to more than one container image")
}

// The requested image isn't locally available, but we can pull it from a remote registry.
if len(resolvedAssets) > 0 && resolvedAssets[0].Connections[0].Type == "container-registry" {
return NewContainerRegistryImage(id, conf, resolvedAssets[0])
}

// could be an image id/name, container id/name or a short reference to an image in docker engine
ded, err := docker_discovery.NewDockerEngineDiscovery()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions providers/os/resources/discovery/docker_engine/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *Resolver) Resolve(ctx context.Context, root *inventory.Asset, conf *inv
// check if we have a tar as input
// detect if the tar is a container image format -> container image
// or a container snapshot format -> container snapshot
if conf.Backend == "tar" {
if conf.Type == "tar" {

if conf.Options == nil || conf.Options["file"] == "" {
return nil, errors.New("could not find the tar file")
Expand Down Expand Up @@ -79,7 +79,7 @@ func (r *Resolver) Resolve(ctx context.Context, root *inventory.Asset, conf *inv
ded, dockerEngErr := NewDockerEngineDiscovery()
// we do not fail here, since we pull the image from upstream if its is an image without the need for docker

if conf.Backend == "docker-container" {
if conf.Type == "docker-container" {
if dockerEngErr != nil {
return nil, errors.Wrap(dockerEngErr, "cannot connect to docker engine to fetch the container")
}
Expand All @@ -91,7 +91,7 @@ func (r *Resolver) Resolve(ctx context.Context, root *inventory.Asset, conf *inv
return []*inventory.Asset{resolvedAsset}, nil
}

if conf.Backend == "docker-image" {
if conf.Type == "docker-image" {
// NOTE, we ignore dockerEngErr here since we fallback to pulling the images directly
// resolvedAssets, err := r.images(ctx, root, conf, ded, credsResolver, sfn)
resolvedAssets, err := r.images(ctx, root, conf, ded, credsResolver)
Expand Down

0 comments on commit b03fcb7

Please sign in to comment.