Skip to content

Commit a3c97be

Browse files
committed
image: implement CheckOS, deprecate pkg/system IsOSSupported
Implement a function that returns an error to replace existing uses of the IsOSSupported utility, where callers had to produce the error after checking. The IsOSSupported function was used in combination with images, so implementing a utility in "image" to prevent having to import pkg/system (which contains many unrelated functions) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 150b657 commit a3c97be

16 files changed

Lines changed: 70 additions & 55 deletions

File tree

builder/dockerfile/dispatchers.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/docker/docker/errdefs"
2323
"github.com/docker/docker/image"
2424
"github.com/docker/docker/pkg/jsonmessage"
25-
"github.com/docker/docker/pkg/system"
2625
"github.com/docker/go-connections/nat"
2726
"github.com/moby/buildkit/frontend/dockerfile/instructions"
2827
"github.com/moby/buildkit/frontend/dockerfile/parser"
@@ -331,8 +330,8 @@ func dispatchWorkdir(ctx context.Context, d dispatchRequest, c *instructions.Wor
331330
// RUN echo hi # cmd /S /C echo hi (Windows)
332331
// RUN [ "echo", "hi" ] # echo hi
333332
func dispatchRun(ctx context.Context, d dispatchRequest, c *instructions.RunCommand) error {
334-
if !system.IsOSSupported(d.state.operatingSystem) {
335-
return system.ErrNotSupportedOperatingSystem
333+
if err := image.CheckOS(d.state.operatingSystem); err != nil {
334+
return err
336335
}
337336

338337
if len(c.FlagsUsed) > 0 {

builder/dockerfile/evaluator.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"github.com/docker/docker/api/types/container"
2929
"github.com/docker/docker/builder"
3030
"github.com/docker/docker/errdefs"
31+
"github.com/docker/docker/image"
3132
"github.com/docker/docker/oci"
32-
"github.com/docker/docker/pkg/system"
3333
"github.com/docker/docker/runconfig/opts"
3434
"github.com/moby/buildkit/frontend/dockerfile/instructions"
3535
"github.com/moby/buildkit/frontend/dockerfile/shell"
@@ -213,21 +213,21 @@ func (s *dispatchState) hasFromImage() bool {
213213
return s.imageID != "" || (s.baseImage != nil && s.baseImage.ImageID() == "")
214214
}
215215

216-
func (s *dispatchState) beginStage(stageName string, image builder.Image) error {
216+
func (s *dispatchState) beginStage(stageName string, img builder.Image) error {
217217
s.stageName = stageName
218-
s.imageID = image.ImageID()
219-
s.operatingSystem = image.OperatingSystem()
220-
if !system.IsOSSupported(s.operatingSystem) {
221-
return system.ErrNotSupportedOperatingSystem
218+
s.imageID = img.ImageID()
219+
s.operatingSystem = img.OperatingSystem()
220+
if err := image.CheckOS(s.operatingSystem); err != nil {
221+
return err
222222
}
223223

224-
if image.RunConfig() != nil {
224+
if img.RunConfig() != nil {
225225
// copy avoids referencing the same instance when 2 stages have the same base
226-
s.runConfig = copyRunConfig(image.RunConfig())
226+
s.runConfig = copyRunConfig(img.RunConfig())
227227
} else {
228228
s.runConfig = &container.Config{}
229229
}
230-
s.baseImage = image
230+
s.baseImage = img
231231
s.setDefaultPath()
232232
s.runConfig.OpenStdin = false
233233
s.runConfig.StdinOnce = false

daemon/containerd/image_builder.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/docker/docker/pkg/progress"
3232
"github.com/docker/docker/pkg/streamformatter"
3333
"github.com/docker/docker/pkg/stringid"
34-
"github.com/docker/docker/pkg/system"
3534
"github.com/opencontainers/go-digest"
3635
"github.com/opencontainers/image-spec/identity"
3736
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -49,8 +48,8 @@ func (i *ImageService) GetImageAndReleasableLayer(ctx context.Context, refOrID s
4948
if opts.Platform != nil {
5049
imgOS = opts.Platform.OS
5150
}
52-
if !system.IsOSSupported(imgOS) {
53-
return nil, nil, system.ErrNotSupportedOperatingSystem
51+
if err := dimage.CheckOS(imgOS); err != nil {
52+
return nil, nil, err
5453
}
5554
return nil, &rolayer{
5655
key: "",
@@ -72,8 +71,8 @@ func (i *ImageService) GetImageAndReleasableLayer(ctx context.Context, refOrID s
7271
return nil, nil, err
7372
}
7473
if img != nil {
75-
if !system.IsOSSupported(img.OperatingSystem()) {
76-
return nil, nil, system.ErrNotSupportedOperatingSystem
74+
if err := dimage.CheckOS(img.OperatingSystem()); err != nil {
75+
return nil, nil, err
7776
}
7877

7978
roLayer, err := newROLayerForImage(ctx, &imgDesc, i, opts.Platform)
@@ -161,8 +160,8 @@ Please notify the image author to correct the configuration.`,
161160
}
162161
}
163162

164-
if !system.IsOSSupported(img.OperatingSystem()) {
165-
return nil, system.ErrNotSupportedOperatingSystem
163+
if err := dimage.CheckOS(img.OperatingSystem()); err != nil {
164+
return nil, err
166165
}
167166

168167
imgDesc, err := i.resolveDescriptor(ctx, name)

daemon/images/image_builder.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/docker/docker/pkg/progress"
1919
"github.com/docker/docker/pkg/streamformatter"
2020
"github.com/docker/docker/pkg/stringid"
21-
"github.com/docker/docker/pkg/system"
2221
registrypkg "github.com/docker/docker/registry"
2322
"github.com/opencontainers/go-digest"
2423
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -208,8 +207,8 @@ func (i *ImageService) GetImageAndReleasableLayer(ctx context.Context, refOrID s
208207
if opts.Platform != nil {
209208
os = opts.Platform.OS
210209
}
211-
if !system.IsOSSupported(os) {
212-
return nil, nil, system.ErrNotSupportedOperatingSystem
210+
if err := image.CheckOS(os); err != nil {
211+
return nil, nil, err
213212
}
214213
lyr, err := newROLayerForImage(nil, i.layerStore)
215214
return nil, lyr, err
@@ -224,8 +223,8 @@ func (i *ImageService) GetImageAndReleasableLayer(ctx context.Context, refOrID s
224223
return nil, nil, err
225224
}
226225
if img != nil {
227-
if !system.IsOSSupported(img.OperatingSystem()) {
228-
return nil, nil, system.ErrNotSupportedOperatingSystem
226+
if err := image.CheckOS(img.OperatingSystem()); err != nil {
227+
return nil, nil, err
229228
}
230229
lyr, err := newROLayerForImage(img, i.layerStore)
231230
return img, lyr, err
@@ -236,8 +235,8 @@ func (i *ImageService) GetImageAndReleasableLayer(ctx context.Context, refOrID s
236235
if err != nil {
237236
return nil, nil, err
238237
}
239-
if !system.IsOSSupported(img.OperatingSystem()) {
240-
return nil, nil, system.ErrNotSupportedOperatingSystem
238+
if err := image.CheckOS(img.OperatingSystem()); err != nil {
239+
return nil, nil, err
241240
}
242241
lyr, err := newROLayerForImage(img, i.layerStore)
243242
return img, lyr, err

daemon/images/image_import.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/docker/docker/image"
1717
"github.com/docker/docker/layer"
1818
"github.com/docker/docker/pkg/archive"
19-
"github.com/docker/docker/pkg/system"
2019
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2120
)
2221

@@ -32,8 +31,8 @@ func (i *ImageService) ImportImage(ctx context.Context, newRef reference.Named,
3231
def := platforms.DefaultSpec()
3332
platform = &def
3433
}
35-
if !system.IsOSSupported(platform.OS) {
36-
return "", errdefs.InvalidParameter(system.ErrNotSupportedOperatingSystem)
34+
if err := image.CheckOS(platform.OS); err != nil {
35+
return "", err
3736
}
3837

3938
config, err := dockerfile.BuildFromConfig(ctx, &container.Config{}, changes, platform.OS)

daemon/images/image_list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/docker/docker/container"
1414
"github.com/docker/docker/image"
1515
"github.com/docker/docker/layer"
16-
"github.com/docker/docker/pkg/system"
1716
)
1817

1918
var acceptedImageFilterTags = map[string]bool{
@@ -116,7 +115,7 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions)
116115
// Skip any images with an unsupported operating system to avoid a potential
117116
// panic when indexing through the layerstore. Don't error as we want to list
118117
// the other images. This should never happen, but here as a safety precaution.
119-
if !system.IsOSSupported(img.OperatingSystem()) {
118+
if err := image.CheckOS(img.OperatingSystem()); err != nil {
120119
continue
121120
}
122121

daemon/images/image_windows.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/docker/docker/image"
77
"github.com/docker/docker/layer"
8-
"github.com/docker/docker/pkg/system"
98
"github.com/pkg/errors"
109
)
1110

@@ -22,8 +21,8 @@ func (i *ImageService) GetLayerFolders(img *image.Image, rwLayer layer.RWLayer)
2221
for index := 1; index <= rd; index++ {
2322
// FIXME: why does this mutate the RootFS?
2423
img.RootFS.DiffIDs = img.RootFS.DiffIDs[:index]
25-
if !system.IsOSSupported(img.OperatingSystem()) {
26-
return nil, errors.Wrapf(system.ErrNotSupportedOperatingSystem, "cannot get layerpath for ImageID %s", img.RootFS.ChainID())
24+
if err := image.CheckOS(img.OperatingSystem()); err != nil {
25+
return nil, errors.Wrapf(err, "cannot get layerpath for ImageID %s", img.RootFS.ChainID())
2726
}
2827
layerPath, err := layer.GetLayerPath(i.layerStore, img.RootFS.ChainID())
2928
if err != nil {

daemon/oci_windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/docker/container"
1616
"github.com/docker/docker/daemon/config"
1717
"github.com/docker/docker/errdefs"
18+
"github.com/docker/docker/image"
1819
"github.com/docker/docker/oci"
1920
"github.com/docker/docker/pkg/sysinfo"
2021
"github.com/docker/docker/pkg/system"
@@ -33,8 +34,8 @@ func (daemon *Daemon) createSpec(ctx context.Context, daemonCfg *configStore, c
3334
if err != nil {
3435
return nil, err
3536
}
36-
if !system.IsOSSupported(img.OperatingSystem()) {
37-
return nil, system.ErrNotSupportedOperatingSystem
37+
if err := image.CheckOS(img.OperatingSystem()); err != nil {
38+
return nil, err
3839
}
3940

4041
s := oci.DefaultSpec()

distribution/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/docker/docker/image"
1717
"github.com/docker/docker/layer"
1818
"github.com/docker/docker/pkg/progress"
19-
"github.com/docker/docker/pkg/system"
2019
refstore "github.com/docker/docker/reference"
2120
registrypkg "github.com/docker/docker/registry"
2221
"github.com/opencontainers/go-digest"
@@ -152,8 +151,8 @@ func platformFromConfig(c []byte) (*ocispec.Platform, error) {
152151
if os == "" {
153152
os = runtime.GOOS
154153
}
155-
if !system.IsOSSupported(os) {
156-
return nil, errors.Wrapf(system.ErrNotSupportedOperatingSystem, "image operating system %q cannot be used on this platform", os)
154+
if err := image.CheckOS(os); err != nil {
155+
return nil, errors.Wrapf(err, "image operating system %q cannot be used on this platform", os)
157156
}
158157
return &ocispec.Platform{
159158
OS: os,

distribution/pull_v2.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/docker/docker/pkg/ioutils"
2828
"github.com/docker/docker/pkg/progress"
2929
"github.com/docker/docker/pkg/stringid"
30-
"github.com/docker/docker/pkg/system"
3130
refstore "github.com/docker/docker/reference"
3231
"github.com/docker/docker/registry"
3332
"github.com/opencontainers/go-digest"
@@ -517,7 +516,7 @@ func (p *puller) pullSchema1(ctx context.Context, ref reference.Reference, unver
517516
if platform != nil {
518517
// Early bath if the requested OS doesn't match that of the configuration.
519518
// This avoids doing the download, only to potentially fail later.
520-
if !system.IsOSSupported(platform.OS) {
519+
if err := image.CheckOS(platform.OS); err != nil {
521520
return "", "", fmt.Errorf("cannot download image with operating system %q when requesting %q", runtime.GOOS, platform.OS)
522521
}
523522
}
@@ -701,7 +700,7 @@ func (p *puller) pullSchema2Layers(ctx context.Context, target distribution.Desc
701700
if platform == nil {
702701
// Early bath if the requested OS doesn't match that of the configuration.
703702
// This avoids doing the download, only to potentially fail later.
704-
if !system.IsOSSupported(configPlatform.OS) {
703+
if err := image.CheckOS(configPlatform.OS); err != nil {
705704
return "", fmt.Errorf("cannot download image with operating system %q when requesting %q", configPlatform.OS, layerStoreOS)
706705
}
707706
layerStoreOS = configPlatform.OS
@@ -716,8 +715,10 @@ func (p *puller) pullSchema2Layers(ctx context.Context, target distribution.Desc
716715

717716
// Assume that the operating system is the host OS if blank, and validate it
718717
// to ensure we don't cause a panic by an invalid index into the layerstores.
719-
if layerStoreOS != "" && !system.IsOSSupported(layerStoreOS) {
720-
return "", system.ErrNotSupportedOperatingSystem
718+
if layerStoreOS != "" {
719+
if err := image.CheckOS(layerStoreOS); err != nil {
720+
return "", err
721+
}
721722
}
722723

723724
if p.config.DownloadManager != nil {

0 commit comments

Comments
 (0)