Skip to content

Commit 905fd3d

Browse files
Add support for image platform (#2)
1 parent 5bce32b commit 905fd3d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

extractor/docker/docker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ func init() {
5454
func NewDockerExtractor(ctx context.Context, imageName string, option types.DockerOption) (Extractor, func(), error) {
5555
ref := image.Reference{Name: imageName, IsFile: false}
5656
transports := []string{"docker-daemon:", "docker://"}
57+
if option.Platform != "" {
58+
// When platform is specified, prioritize registry lookup so the manifest list
59+
// can be filtered by OS/arch and fail fast if unsupported, instead of silently
60+
// using a pre-existing local image from the daemon.
61+
transports = []string{"docker://"}
62+
}
5763
return newDockerExtractor(ctx, ref, transports, option)
5864
}
5965

extractor/image/image.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package image
33
import (
44
"context"
55
"io"
6+
"strings"
67

78
"github.com/containers/image/v5/image"
89
"github.com/containers/image/v5/pkg/blobinfocache"
@@ -53,6 +54,11 @@ func NewImage(ctx context.Context, image Reference, transports []string, option
5354
var domain string
5455
var auth *imageTypes.DockerAuthConfig
5556

57+
osChoice, archChoice, variantChoice, err := parsePlatform(option.Platform)
58+
if err != nil {
59+
return RealImage{}, err
60+
}
61+
5662
originalName := image.Name
5763
if !image.IsFile {
5864
named, err := reference.ParseNormalizedNamed(image.Name)
@@ -70,8 +76,9 @@ func NewImage(ctx context.Context, image Reference, transports []string, option
7076
}
7177

7278
sys := &imageTypes.SystemContext{
73-
// TODO: make OSChoice configurable
74-
OSChoice: "linux",
79+
OSChoice: osChoice,
80+
ArchitectureChoice: archChoice,
81+
VariantChoice: variantChoice,
7582
DockerAuthConfig: auth,
7683
DockerDisableV1Ping: option.SkipPing,
7784
DockerInsecureSkipTLSVerify: imageTypes.NewOptionalBool(option.InsecureSkipTLSVerify),
@@ -94,6 +101,31 @@ func NewImage(ctx context.Context, image Reference, transports []string, option
94101
}, nil
95102
}
96103

104+
func parsePlatform(p string) (osChoice, archChoice, variantChoice string, err error) {
105+
osChoice = "linux"
106+
if p == "" {
107+
return osChoice, "", "", nil
108+
}
109+
parts := strings.Split(p, "/")
110+
switch len(parts) {
111+
case 1:
112+
archChoice = parts[0]
113+
case 2:
114+
osChoice = parts[0]
115+
archChoice = parts[1]
116+
case 3:
117+
osChoice = parts[0]
118+
archChoice = parts[1]
119+
variantChoice = parts[2]
120+
default:
121+
return "", "", "", xerrors.Errorf("invalid platform %q", p)
122+
}
123+
if osChoice == "" || archChoice == "" {
124+
return "", "", "", xerrors.Errorf("invalid platform %q", p)
125+
}
126+
return osChoice, archChoice, variantChoice, nil
127+
}
128+
97129
func newSource(ctx context.Context, imageName string, transports []string, sys *imageTypes.SystemContext) (
98130
ImageSource, ImageCloser, error) {
99131
err := xerrors.New("no valid transport")

types/docker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ type DockerOption struct {
77
UserName string
88
Password string
99

10+
// Platform (e.g. linux/amd64, linux/arm64/v8)
11+
Platform string
12+
1013
// ECR
1114
AwsAccessKey string
1215
AwsSecretKey string

0 commit comments

Comments
 (0)