MCPcopy
hub / github.com/moby/moby / deduceContainerPlatform

Function deduceContainerPlatform

daemon/migration.go:51–96  ·  view source on GitHub ↗

deduceContainerPlatform tries to deduce `ctr`'s platform. If both `ctr.OS` and `ctr.ImageManifest` are empty, assume the image comes from a pre-OS times and use the host platform to match the behavior of [container.FromDisk]. Otherwise: - `ctr.ImageManifest.Platform` is used, if it exists and is not

(
	ctx context.Context,
	migration platformReader,
	ctr *container.Container,
)

Source from the content-addressed store, hash-verified

49// `ctr.ImageID`) is used – this looks for the best *present* matching manifest in
50// the store.
51func deduceContainerPlatform(
52 ctx context.Context,
53 migration platformReader,
54 ctr *container.Container,
55) (ocispec.Platform, error) {
56 if ctr.OS == "" && ctr.ImageManifest == nil { //nolint:staticcheck // ignore SA1019 because we are testing deprecated field migration
57 return platforms.DefaultSpec(), nil
58 }
59
60 var errs []error
61 isValidPlatform := func(p ocispec.Platform) bool {
62 return p.OS != "" && p.Architecture != ""
63 }
64
65 if ctr.ImageManifest != nil {
66 if ctr.ImageManifest.Platform != nil {
67 return *ctr.ImageManifest.Platform, nil
68 }
69
70 if ctr.ImageManifest != nil {
71 p, err := migration.ReadPlatformFromConfigByImageManifest(ctx, *ctr.ImageManifest)
72 if err != nil {
73 errs = append(errs, err)
74 } else {
75 if isValidPlatform(p) {
76 return p, nil
77 }
78 errs = append(errs, errors.New("malformed image config obtained by ImageManifestDescriptor"))
79 }
80 }
81 }
82
83 if ctr.ImageID != "" {
84 p, err := migration.ReadPlatformFromImage(ctx, ctr.ImageID)
85 if err != nil {
86 errs = append(errs, err)
87 } else {
88 if isValidPlatform(p) {
89 return p, nil
90 }
91 errs = append(errs, errors.New("malformed image config obtained by image id"))
92 }
93 }
94
95 return ocispec.Platform{}, errors.Wrap(multierror.Join(errs...), "cannot deduce the container platform")
96}
97
98type daemonPlatformReader struct {
99 imageService ImageService

Callers 1

migrateContainerOSFunction · 0.85

Calls 4

JoinFunction · 0.92
NewMethod · 0.65
ReadPlatformFromImageMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…