pull_v2_windows.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/log"
  14. "github.com/containerd/containerd/platforms"
  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/pkg/system"
  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. if (manifestDescriptor.Platform.Architecture == runtime.GOARCH) &&
  66. ((p.OS != "" && manifestDescriptor.Platform.OS == p.OS) || // Explicit user request for an OS we know we support
  67. (p.OS == "" && system.IsOSSupported(manifestDescriptor.Platform.OS))) { // No user requested OS, but one we can support
  68. if strings.EqualFold("windows", manifestDescriptor.Platform.OS) {
  69. if err := checkImageCompatibility("windows", manifestDescriptor.Platform.OSVersion); err != nil {
  70. continue
  71. }
  72. foundWindowsMatch = true
  73. }
  74. matches = append(matches, manifestDescriptor)
  75. 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())
  76. } else {
  77. 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())
  78. }
  79. }
  80. if foundWindowsMatch {
  81. sort.Stable(manifestsByVersion{osVersion, matches})
  82. }
  83. return matches
  84. }
  85. func versionMatch(actual, expected string) bool {
  86. // Check whether the version matches up to the build, ignoring UBR
  87. return strings.HasPrefix(actual, expected+".")
  88. }
  89. type manifestsByVersion struct {
  90. version string
  91. list []manifestlist.ManifestDescriptor
  92. }
  93. func (mbv manifestsByVersion) Less(i, j int) bool {
  94. // TODO: Split version by parts and compare
  95. // TODO: Prefer versions which have a greater version number
  96. // Move compatible versions to the top, with no other ordering changes
  97. return (strings.EqualFold("windows", mbv.list[i].Platform.OS) && !strings.EqualFold("windows", mbv.list[j].Platform.OS)) ||
  98. (versionMatch(mbv.list[i].Platform.OSVersion, mbv.version) && !versionMatch(mbv.list[j].Platform.OSVersion, mbv.version))
  99. }
  100. func (mbv manifestsByVersion) Len() int {
  101. return len(mbv.list)
  102. }
  103. func (mbv manifestsByVersion) Swap(i, j int) {
  104. mbv.list[i], mbv.list[j] = mbv.list[j], mbv.list[i]
  105. }
  106. // checkImageCompatibility blocks pulling incompatible images based on a later OS build
  107. // Fixes https://github.com/moby/moby/issues/36184.
  108. func checkImageCompatibility(imageOS, imageOSVersion string) error {
  109. if imageOS == "windows" {
  110. hostOSV := osversion.Get()
  111. splitImageOSVersion := strings.Split(imageOSVersion, ".") // eg 10.0.16299.nnnn
  112. if len(splitImageOSVersion) >= 3 {
  113. if imageOSBuild, err := strconv.Atoi(splitImageOSVersion[2]); err == nil {
  114. if imageOSBuild > int(hostOSV.Build) {
  115. 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())
  116. log.G(context.TODO()).Debugf(errMsg)
  117. return errors.New(errMsg)
  118. }
  119. }
  120. }
  121. }
  122. return nil
  123. }
  124. func formatPlatform(platform ocispec.Platform) string {
  125. if platform.OS == "" {
  126. platform = platforms.DefaultSpec()
  127. }
  128. return fmt.Sprintf("%s %s", platforms.Format(platform), osversion.Get().ToString())
  129. }