Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Avoid a network call for digest based images #396

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions internal/source/image_registry_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ func (i *ImageRegistry) Unpack(ctx context.Context, catalog *catalogdv1alpha1.Cl
remoteOpts = append(remoteOpts, remote.WithTransport(tlsTransport))
}

// always fetch the hash
imgDesc, err := remote.Head(imgRef, remoteOpts...)
digestHex, err := resolveDigest(imgRef, remoteOpts...)
if err != nil {
return nil, fmt.Errorf("error fetching image descriptor: %w", err)
}
l.V(1).Info("resolved image descriptor", "digest", imgDesc.Digest.String())
l.V(1).Info("resolved image descriptor", "digest", digestHex)

unpackPath := filepath.Join(i.BaseCachePath, catalog.Name, imgDesc.Digest.Hex)
resolvedRef := fmt.Sprintf("%s@sha256:%s", imgRef.Context().Name(), imgDesc.Digest.Hex)
unpackPath := filepath.Join(i.BaseCachePath, catalog.Name, digestHex)
resolvedRef := fmt.Sprintf("%s@sha256:%s", imgRef.Context().Name(), digestHex)
if stat, err := os.Stat(unpackPath); err == nil && stat.IsDir() {
l.V(1).Info("found image in filesystem cache", "digest", imgDesc.Digest.Hex)
l.V(1).Info("found image in filesystem cache", "digest", digestHex)
// TODO: https://github.com/operator-framework/catalogd/issues/389
return unpackedResult(os.DirFS(unpackPath), catalog, resolvedRef, metav1.Time{Time: time.Now()}), nil
}
Expand Down Expand Up @@ -164,6 +163,23 @@ func unpackedResult(fsys fs.FS, catalog *catalogdv1alpha1.ClusterCatalog, ref st
}
}

// resolveDigest returns hex value of the digest for image reference
func resolveDigest(imgRef name.Reference, remoteOpts ...remote.Option) (string, error) {
digest, isDigest := imgRef.(name.Digest)
if isDigest {
digestHex := strings.TrimPrefix(digest.DigestStr(), "sha256:")
// If the reference is already a digest - return without making a network call
return digestHex, nil
}

imgDesc, err := remote.Head(imgRef, remoteOpts...)
if err != nil {
return "", err
}

return imgDesc.Digest.Hex, nil
}

// unpackImage unpacks a catalog image reference to the provided unpackPath,
// returning an error if any errors are encountered along the way.
func unpackImage(ctx context.Context, imgRef name.Reference, unpackPath string, remoteOpts ...remote.Option) error {
Expand Down