-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,63 @@ | ||
package build | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
) | ||
|
||
func GetBaseImage(ctx context.Context, baseName string, cfg RunConfig) (v1.Image, error) { | ||
if baseName == "scratch" { | ||
func GetBaseImage(ctx RunCtx, baseRef string, platform Platform, keychain authn.Keychain) (v1.Image, error) { | ||
if baseRef == "scratch" { | ||
return empty.Image, nil | ||
} | ||
|
||
baseRef, baseIndex, err := fetchImageIndex(ctx, baseName, cfg) | ||
ref, index, err := fetchImageIndex(ctx, baseRef, keychain) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to retrieve base image index: %w", err) | ||
} | ||
baseDigest, err := baseIndex.Digest() | ||
baseDigest, err := index.Digest() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to retrieve base image digest: %w", err) | ||
} | ||
log.Println("Using base image:", baseRef.Name()+"@"+baseDigest.String()) | ||
log.Println("Using base image:", ref.Name()+"@"+baseDigest.String()) | ||
|
||
return getImageForPlatform(baseIndex, cfg.PlatformArch, cfg.PlatformOs) | ||
return getImageForPlatform(index, platform) | ||
} | ||
|
||
func fetchImageIndex(ctx context.Context, src string, cfg RunConfig) (name.Reference, v1.ImageIndex, error) { | ||
func fetchImageIndex(ctx RunCtx, src string, keychain authn.Keychain) (name.Reference, v1.ImageIndex, error) { | ||
ref, err := name.ParseReference(src) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
base, err := remote.Index(ref, remote.WithContext(ctx), remote.WithAuthFromKeychain(cfg.RemoteKeychain)) | ||
base, err := remote.Index(ref, remote.WithContext(ctx.Ctx), remote.WithAuthFromKeychain(keychain)) | ||
return ref, base, err | ||
} | ||
|
||
func getDigestForPlatform(index v1.ImageIndex, arch string, os string) (v1.Hash, error) { | ||
func getImageForPlatform(index v1.ImageIndex, platform Platform) (v1.Image, error) { | ||
digest, err := getDigestForPlatform(index, platform) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return index.Image(digest) | ||
} | ||
|
||
func getDigestForPlatform(index v1.ImageIndex, platform Platform) (v1.Hash, error) { | ||
manifest, err := index.IndexManifest() | ||
if err != nil { | ||
return v1.Hash{}, err | ||
} | ||
|
||
// Find the manifest for the platform | ||
for _, m := range manifest.Manifests { | ||
if m.Platform.Architecture == arch && m.Platform.OS == os { | ||
if m.Platform.Architecture == platform.Arch && m.Platform.OS == platform.OS { | ||
return m.Digest, nil | ||
} | ||
} | ||
|
||
return v1.Hash{}, fmt.Errorf("no manifest found for platform %s/%s", arch, os) | ||
} | ||
|
||
func getImageForPlatform(index v1.ImageIndex, arch string, os string) (v1.Image, error) { | ||
digest, err := getDigestForPlatform(index, arch, os) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return index.Image(digest) | ||
return v1.Hash{}, fmt.Errorf("no manifest found for platform %s", platform) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters