Skip to content

Commit 0519491

Browse files
Set default platform
* Set default platform for scratch images * Set default platform constraint for FileOps if none is set via ConstraintOpt. * Explicitly set platform ConstraintOpt in dockerfile frontend based on dispatch state platform. Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent fbbaa39 commit 0519491

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

client/llb/fileop.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
_ "crypto/sha256" // for opencontainers/go-digest
66
"os"
77
"path/filepath"
8-
"runtime"
98
"strconv"
109
"strings"
1110
"time"
@@ -772,12 +771,7 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
772771
}
773772
}
774773

775-
// Assume that we're building an image for the same OS we're running on.
776-
platformOS := runtime.GOOS
777-
if f.constraints.Platform != nil {
778-
platformOS = f.constraints.Platform.OS
779-
}
780-
action, err := st.action.toProtoAction(ctx, parent, st.base, platformOS)
774+
action, err := st.action.toProtoAction(ctx, parent, st.base, f.constraints.Platform.OS)
781775
if err != nil {
782776
return "", nil, nil, nil, err
783777
}

client/llb/state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ func (s State) File(a *FileAction, opts ...ConstraintsOpt) State {
299299
o.SetConstraintsOption(&c)
300300
}
301301

302+
// No platform was set by constraint options. Set the current OS and arch.
303+
if c.Platform == nil {
304+
platform := platforms.DefaultSpec()
305+
c.Platform = &platform
306+
}
302307
return s.WithOutput(NewFileOp(s, a, c).Output())
303308
}
304309

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111
"path"
1212
"path/filepath"
13-
"runtime"
1413
"sort"
1514
"strconv"
1615
"strings"
@@ -361,6 +360,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
361360
if d.stage.BaseName == emptyImageName {
362361
d.state = llb.Scratch()
363362
d.image = emptyImage(platformOpt.targetPlatform)
363+
d.platform = &platformOpt.targetPlatform
364364
continue
365365
}
366366
func(i int, d *dispatchState) {
@@ -479,11 +479,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
479479

480480
// make sure that PATH is always set
481481
if _, ok := shell.BuildEnvs(d.image.Config.Env)["PATH"]; !ok {
482-
var pathOS string
483-
if d.platform != nil {
484-
pathOS = d.platform.OS
485-
}
486-
d.image.Config.Env = append(d.image.Config.Env, "PATH="+system.DefaultPathEnv(pathOS))
482+
d.image.Config.Env = append(d.image.Config.Env, "PATH="+system.DefaultPathEnv(d.platform.OS))
487483
}
488484

489485
// initialize base metadata from image conf
@@ -892,6 +888,7 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
892888
st := llb.Scratch().Dir(sourcePath).File(
893889
llb.Mkfile(f, 0755, []byte(data)),
894890
dockerui.WithInternalName("preparing inline document"),
891+
llb.Platform(*d.platform),
895892
)
896893

897894
mount := llb.AddMount(destPath, st, llb.SourcePath(sourcePath), llb.Readonly)
@@ -995,11 +992,7 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
995992
}
996993

997994
func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bool, opt *dispatchOpt) error {
998-
platformOS := runtime.GOOS
999-
if d.platform != nil {
1000-
platformOS = d.platform.OS
1001-
}
1002-
wd, err := system.NormalizeWorkdir(d.image.Config.WorkingDir, c.Path, platformOS)
995+
wd, err := system.NormalizeWorkdir(d.image.Config.WorkingDir, c.Path, d.platform.OS)
1003996
if err != nil {
1004997
return errors.Wrap(err, "normalizing workdir")
1005998
}
@@ -1024,6 +1017,7 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo
10241017
d.state = d.state.File(llb.Mkdir(wd, 0755, mkdirOpt...),
10251018
llb.WithCustomName(prefixCommand(d, uppercaseCmd(processCmdEnv(opt.shlex, c.String(), env)), d.prefixPlatform, &platform, env)),
10261019
location(opt.sourceMap, c.Location()),
1020+
llb.Platform(*d.platform),
10271021
)
10281022
withLayer = true
10291023
}
@@ -1033,11 +1027,7 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo
10331027
}
10341028

10351029
func dispatchCopy(d *dispatchState, cfg copyConfig) error {
1036-
platformOS := runtime.GOOS
1037-
if d.platform != nil {
1038-
platformOS = d.platform.OS
1039-
}
1040-
pp, err := pathRelativeToWorkingDir(d.state, cfg.params.DestPath, platformOS)
1030+
pp, err := pathRelativeToWorkingDir(d.state, cfg.params.DestPath, *d.platform)
10411031
if err != nil {
10421032
return err
10431033
}
@@ -1146,7 +1136,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
11461136
a = a.Copy(st, f, dest, opts...)
11471137
}
11481138
} else {
1149-
src, err = system.CheckSystemDriveAndRemoveDriveLetter(src, platformOS)
1139+
src, err = system.CheckSystemDriveAndRemoveDriveLetter(src, d.platform.OS)
11501140
if err != nil {
11511141
return errors.Wrap(err, "removing drive letter")
11521142
}
@@ -1173,21 +1163,22 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
11731163
commitMessage.WriteString(" <<" + src.Path)
11741164

11751165
data := src.Data
1176-
f, err := system.CheckSystemDriveAndRemoveDriveLetter(src.Path, platformOS)
1166+
f, err := system.CheckSystemDriveAndRemoveDriveLetter(src.Path, d.platform.OS)
11771167
if err != nil {
11781168
return errors.Wrap(err, "removing drive letter")
11791169
}
11801170
st := llb.Scratch().File(
11811171
llb.Mkfile(f, 0664, []byte(data)),
11821172
dockerui.WithInternalName("preparing inline document"),
1173+
llb.Platform(*d.platform),
11831174
)
11841175

11851176
opts := append([]llb.CopyOption{&llb.CopyInfo{
11861177
Mode: mode,
11871178
CreateDestPath: true,
11881179
}}, copyOpt...)
11891180

1190-
dest, err = system.CheckSystemDriveAndRemoveDriveLetter(dest, platformOS)
1181+
dest, err = system.CheckSystemDriveAndRemoveDriveLetter(dest, d.platform.OS)
11911182
if err != nil {
11921183
return errors.Wrap(err, "removing drive letter")
11931184
}
@@ -1226,7 +1217,9 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
12261217
d.cmdIndex-- // prefixCommand increases it
12271218
pgName := prefixCommand(d, name, d.prefixPlatform, &platform, env)
12281219

1229-
var copyOpts []llb.ConstraintsOpt
1220+
copyOpts := []llb.ConstraintsOpt{
1221+
llb.Platform(*d.platform),
1222+
}
12301223
copy(copyOpts, fileOpt)
12311224
copyOpts = append(copyOpts, llb.ProgressGroup(pgID, pgName, true))
12321225

@@ -1418,8 +1411,8 @@ func dispatchArg(d *dispatchState, c *instructions.ArgCommand, metaArgs []instru
14181411
return commitToHistory(&d.image, "ARG "+strings.Join(commitStrs, " "), false, nil, d.epoch)
14191412
}
14201413

1421-
func pathRelativeToWorkingDir(s llb.State, p, platform string) (string, error) {
1422-
dir, err := s.GetDir(context.TODO())
1414+
func pathRelativeToWorkingDir(s llb.State, p string, platform ocispecs.Platform) (string, error) {
1415+
dir, err := s.GetDir(context.TODO(), llb.Platform(platform))
14231416
if err != nil {
14241417
return "", err
14251418
}
@@ -1428,15 +1421,15 @@ func pathRelativeToWorkingDir(s llb.State, p, platform string) (string, error) {
14281421
return dir, nil
14291422
}
14301423

1431-
p, err = system.CheckSystemDriveAndRemoveDriveLetter(p, platform)
1424+
p, err = system.CheckSystemDriveAndRemoveDriveLetter(p, platform.OS)
14321425
if err != nil {
14331426
return "", errors.Wrap(err, "remving drive letter")
14341427
}
14351428

1436-
if system.IsAbs(p, platform) {
1429+
if system.IsAbs(p, platform.OS) {
14371430
return p, nil
14381431
}
1439-
return filepath.Join(dir, p), nil
1432+
return system.NormalizePath(dir, p, platform.OS, false)
14401433
}
14411434

14421435
func addEnv(env []string, k, v string) []string {

0 commit comments

Comments
 (0)