Skip to content

Commit a4c1c94

Browse files
committed
dockerfile: maintain osversion/variant from the base image
Restores the behavior from v0.12. Signed-off-by: Tonis Tiigi <[email protected]>
1 parent c4f673e commit a4c1c94

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,10 +801,15 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
801801
target.state = target.state.SetMarshalDefaults(defaults...)
802802

803803
if !platformOpt.implicitTarget {
804+
sameOsArch := platformOpt.targetPlatform.OS == target.image.OS && platformOpt.targetPlatform.Architecture == target.image.Architecture
804805
target.image.OS = platformOpt.targetPlatform.OS
805806
target.image.Architecture = platformOpt.targetPlatform.Architecture
806-
target.image.Variant = platformOpt.targetPlatform.Variant
807-
target.image.OSVersion = platformOpt.targetPlatform.OSVersion
807+
if platformOpt.targetPlatform.Variant != "" || !sameOsArch {
808+
target.image.Variant = platformOpt.targetPlatform.Variant
809+
}
810+
if platformOpt.targetPlatform.OSVersion != "" || !sameOsArch {
811+
target.image.OSVersion = platformOpt.targetPlatform.OSVersion
812+
}
808813
if platformOpt.targetPlatform.OSFeatures != nil {
809814
target.image.OSFeatures = append([]string{}, platformOpt.targetPlatform.OSFeatures...)
810815
}

frontend/dockerfile/dockerfile_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ var allTests = integration.TestFuncs(
217217
testPowershellInDefaultPathOnWindows,
218218
testOCILayoutMultiname,
219219
testPlatformWithOSVersion,
220+
testMaintainBaseOSVersion,
220221
)
221222

222223
// Tests that depend on the `security.*` entitlements
@@ -9634,6 +9635,119 @@ EOF
96349635
require.Equal(t, p2.OSVersion+"\n", string(dt))
96359636
}
96369637

9638+
func testMaintainBaseOSVersion(t *testing.T, sb integration.Sandbox) {
9639+
integration.SkipOnPlatform(t, "windows")
9640+
workers.CheckFeatureCompat(t, sb, workers.FeatureDirectPush)
9641+
9642+
ctx := sb.Context()
9643+
9644+
c, err := client.New(ctx, sb.Address())
9645+
require.NoError(t, err)
9646+
defer c.Close()
9647+
9648+
f := getFrontend(t, sb)
9649+
9650+
p1 := ocispecs.Platform{
9651+
OS: "windows",
9652+
OSVersion: "10.20.30",
9653+
Architecture: "amd64",
9654+
}
9655+
p1Str := platforms.FormatAll(p1)
9656+
9657+
registry, err := sb.NewRegistry()
9658+
if errors.Is(err, integration.ErrRequirements) {
9659+
t.Skip(err.Error())
9660+
}
9661+
require.NoError(t, err)
9662+
target := registry + "/buildkit/testplatformwithosversion-1:latest"
9663+
9664+
dockerfile := []byte(`
9665+
FROM scratch
9666+
ARG TARGETPLATFORM
9667+
COPY <<EOF /platform
9668+
${TARGETPLATFORM}
9669+
EOF
9670+
`)
9671+
9672+
dir := integration.Tmpdir(
9673+
t,
9674+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
9675+
)
9676+
9677+
_, err = f.Solve(sb.Context(), c, client.SolveOpt{
9678+
FrontendAttrs: map[string]string{
9679+
"platform": p1Str,
9680+
},
9681+
Exports: []client.ExportEntry{
9682+
{
9683+
Type: client.ExporterImage,
9684+
Attrs: map[string]string{
9685+
"name": target,
9686+
"push": "true",
9687+
},
9688+
},
9689+
},
9690+
9691+
LocalMounts: map[string]fsutil.FS{
9692+
dockerui.DefaultLocalNameDockerfile: dir,
9693+
dockerui.DefaultLocalNameContext: dir,
9694+
},
9695+
}, nil)
9696+
9697+
require.NoError(t, err)
9698+
9699+
desc, provider, err := contentutil.ProviderFromRef(target)
9700+
require.NoError(t, err)
9701+
9702+
info, err := testutil.ReadImages(ctx, provider, desc)
9703+
require.NoError(t, err)
9704+
require.Len(t, info.Images, 1)
9705+
require.Equal(t, info.Images[0].Img.Platform.OSVersion, p1.OSVersion)
9706+
9707+
dockerfile = []byte(fmt.Sprintf(`
9708+
FROM %s
9709+
COPY <<EOF /other
9710+
hello
9711+
EOF
9712+
`, target))
9713+
9714+
dir = integration.Tmpdir(
9715+
t,
9716+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
9717+
)
9718+
9719+
target2 := registry + "/buildkit/testplatformwithosversion-2:latest"
9720+
9721+
_, err = f.Solve(sb.Context(), c, client.SolveOpt{
9722+
FrontendAttrs: map[string]string{
9723+
"platform": p1.OS + "/" + p1.Architecture,
9724+
},
9725+
Exports: []client.ExportEntry{
9726+
{
9727+
Type: client.ExporterImage,
9728+
Attrs: map[string]string{
9729+
"name": target2,
9730+
"push": "true",
9731+
},
9732+
},
9733+
},
9734+
9735+
LocalMounts: map[string]fsutil.FS{
9736+
dockerui.DefaultLocalNameDockerfile: dir,
9737+
dockerui.DefaultLocalNameContext: dir,
9738+
},
9739+
}, nil)
9740+
require.NoError(t, err)
9741+
9742+
desc, provider, err = contentutil.ProviderFromRef(target2)
9743+
require.NoError(t, err)
9744+
9745+
info, err = testutil.ReadImages(ctx, provider, desc)
9746+
require.NoError(t, err)
9747+
require.Len(t, info.Images, 1)
9748+
require.Equal(t, info.Images[0].Img.Platform.OSVersion, p1.OSVersion)
9749+
}
9750+
96379751
func runShell(dir string, cmds ...string) error {
96389752
for _, args := range cmds {
96399753
var cmd *exec.Cmd

0 commit comments

Comments
 (0)