pull_v2_windows.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package distribution // import "github.com/docker/docker/distribution"
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "runtime"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "github.com/Microsoft/hcsshim/osversion"
  13. "github.com/containerd/containerd/platforms"
  14. "github.com/containerd/log"
  15. "github.com/docker/distribution"
  16. "github.com/docker/distribution/manifest/manifestlist"
  17. "github.com/docker/distribution/manifest/schema2"
  18. "github.com/docker/distribution/registry/client/transport"
  19. "github.com/docker/docker/image"
  20. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  21. )
  22. var _ distribution.Describable = &layerDescriptor{}
  23. func (ld *layerDescriptor) Descriptor() distribution.Descriptor {
  24. if ld.src.MediaType == schema2.MediaTypeForeignLayer && len(ld.src.URLs) > 0 {
  25. return ld.src
  26. }
  27. return distribution.Descriptor{}
  28. }
  29. func (ld *layerDescriptor) open(ctx context.Context) (distribution.ReadSeekCloser, error) {
  30. blobs := ld.repo.Blobs(ctx)
  31. rsc, err := blobs.Open(ctx, ld.digest)
  32. if len(ld.src.URLs) == 0 {
  33. return rsc, err
  34. }
  35. // We're done if the registry has this blob.
  36. if err == nil {
  37. // Seek does an HTTP GET. If it succeeds, the blob really is accessible.
  38. if _, err = rsc.Seek(0, io.SeekStart); err == nil {
  39. return rsc, nil
  40. }
  41. rsc.Close()
  42. }
  43. // Find the first URL that results in a 200 result code.
  44. for _, url := range ld.src.URLs {
  45. log.G(ctx).Debugf("Pulling %v from foreign URL %v", ld.digest, url)
  46. rsc = transport.NewHTTPReadSeeker(http.DefaultClient, url, nil)
  47. // Seek does an HTTP GET. If it succeeds, the blob really is accessible.
  48. _, err = rsc.Seek(0, io.SeekStart)
  49. if err == nil {
  50. break
  51. }
  52. log.G(ctx).Debugf("Download for %v failed: %v", ld.digest, err)
  53. rsc.Close()
  54. rsc = nil
  55. }
  56. return rsc, err
  57. }
  58. func filterManifests(manifests []manifestlist.ManifestDescriptor, p ocispec.Platform) []manifestlist.ManifestDescriptor {
  59. version := osversion.Get()
  60. osVersion := fmt.Sprintf("%d.%d.%d", version.MajorVersion, version.MinorVersion, version.Build)
  61. log.G(context.TODO()).Debugf("will prefer Windows entries with version %s", osVersion)
  62. var matches []manifestlist.ManifestDescriptor
  63. foundWindowsMatch := false
  64. for _, manifestDescriptor := range manifests {
  65. skip := func() {
  66. log.G(context.TODO()).Debugf("ignoring %s/%s %s with media type %s, digest %s", manifestDescriptor.Platform.OS, manifestDescriptor.Platform.Architecture, manifestDescriptor.Platform.OSVersion, manifestDescriptor.MediaType, manifestDescriptor.Digest.String())
  67. }
  68. // TODO(thaJeztah): should we also check for the user-provided architecture (if any)?
  69. if manifestDescriptor.Platform.Architecture != runtime.GOARCH {
  70. skip()
  71. continue
  72. }
  73. os := manifestDescriptor.Platform.OS
  74. if p.OS != "" {
  75. // Explicit user request for an OS
  76. os = p.OS
  77. }
  78. if err := image.CheckOS(os); err != nil {
  79. skip()
  80. continue
  81. }
  82. // TODO(thaJeztah): should we also take the user-provided platform into account (if any)?
  83. if strings.EqualFold("windows", manifestDescriptor.Platform.OS) {
  84. if err := checkImageCompatibility("windows", manifestDescriptor.Platform.OSVersion); err != nil {
  85. skip()
  86. continue
  87. }
  88. foundWindowsMatch = true
  89. }
  90. matches = append(matches, manifestDescriptor)
  91. log.G(context.TODO()).Debugf("found match %s/%s %s with media type %s, digest %s", manifestDescriptor.Platform.OS, runtime.GOARCH, manifestDescriptor.Platform.OSVersion, manifestDescriptor.MediaType, manifestDescriptor.Digest.String())
  92. }
  93. if foundWindowsMatch {
  94. sort.Stable(manifestsByVersion{osVersion, matches})
  95. }
  96. return matches
  97. }
  98. func versionMatch(actual, expected string) bool {
  99. // Check whether the version matches up to the build, ignoring UBR
  100. return strings.HasPrefix(actual, expected+".")
  101. }
  102. type manifestsByVersion struct {
  103. version string
  104. list []manifestlist.ManifestDescriptor
  105. }
  106. func (mbv manifestsByVersion) Less(i, j int) bool {
  107. // TODO: Split version by parts and compare
  108. // TODO: Prefer versions which have a greater version number
  109. // Move compatible versions to the top, with no other ordering changes
  110. return (strings.EqualFold("windows", mbv.list[i].Platform.OS) && !strings.EqualFold("windows", mbv.list[j].Platform.OS)) ||
  111. (versionMatch(mbv.list[i].Platform.OSVersion, mbv.version) && !versionMatch(mbv.list[j].Platform.OSVersion, mbv.version))
  112. }
  113. func (mbv manifestsByVersion) Len() int {
  114. return len(mbv.list)
  115. }
  116. func (mbv manifestsByVersion) Swap(i, j int) {
  117. mbv.list[i], mbv.list[j] = mbv.list[j], mbv.list[i]
  118. }
  119. // checkImageCompatibility blocks pulling incompatible images based on a later OS build
  120. // Fixes https://github.com/moby/moby/issues/36184.
  121. func checkImageCompatibility(imageOS, imageOSVersion string) error {
  122. if imageOS == "windows" {
  123. hostOSV := osversion.Get()
  124. splitImageOSVersion := strings.Split(imageOSVersion, ".") // eg 10.0.16299.nnnn
  125. if len(splitImageOSVersion) >= 3 {
  126. if imageOSBuild, err := strconv.Atoi(splitImageOSVersion[2]); err == nil {
  127. if imageOSBuild > int(hostOSV.Build) {
  128. errMsg := fmt.Sprintf("a Windows version %s.%s.%s-based image is incompatible with a %s host", splitImageOSVersion[0], splitImageOSVersion[1], splitImageOSVersion[2], hostOSV.ToString())
  129. log.G(context.TODO()).Debugf(errMsg)
  130. return errors.New(errMsg)
  131. }
  132. }
  133. }
  134. }
  135. return nil
  136. }
  137. func formatPlatform(platform ocispec.Platform) string {
  138. if platform.OS == "" {
  139. platform = platforms.DefaultSpec()
  140. }
  141. return fmt.Sprintf("%s %s", platforms.Format(platform), osversion.Get().ToString())
  142. }