imagecontext.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package dockerfile
  2. import (
  3. "strconv"
  4. "strings"
  5. "github.com/docker/docker/api/types/backend"
  6. "github.com/docker/docker/builder"
  7. "github.com/docker/docker/builder/remotecontext"
  8. dockerimage "github.com/docker/docker/image"
  9. "github.com/pkg/errors"
  10. "github.com/sirupsen/logrus"
  11. "golang.org/x/net/context"
  12. )
  13. type buildStage struct {
  14. id string
  15. }
  16. func newBuildStage(imageID string) *buildStage {
  17. return &buildStage{id: imageID}
  18. }
  19. func (b *buildStage) ImageID() string {
  20. return b.id
  21. }
  22. func (b *buildStage) update(imageID string) {
  23. b.id = imageID
  24. }
  25. // buildStages tracks each stage of a build so they can be retrieved by index
  26. // or by name.
  27. type buildStages struct {
  28. sequence []*buildStage
  29. byName map[string]*buildStage
  30. }
  31. func newBuildStages() *buildStages {
  32. return &buildStages{byName: make(map[string]*buildStage)}
  33. }
  34. func (s *buildStages) getByName(name string) (*buildStage, bool) {
  35. stage, ok := s.byName[strings.ToLower(name)]
  36. return stage, ok
  37. }
  38. func (s *buildStages) get(indexOrName string) (*buildStage, error) {
  39. index, err := strconv.Atoi(indexOrName)
  40. if err == nil {
  41. if err := s.validateIndex(index); err != nil {
  42. return nil, err
  43. }
  44. return s.sequence[index], nil
  45. }
  46. if im, ok := s.byName[strings.ToLower(indexOrName)]; ok {
  47. return im, nil
  48. }
  49. return nil, nil
  50. }
  51. func (s *buildStages) validateIndex(i int) error {
  52. if i < 0 || i >= len(s.sequence)-1 {
  53. if i == len(s.sequence)-1 {
  54. return errors.New("refers to current build stage")
  55. }
  56. return errors.New("index out of bounds")
  57. }
  58. return nil
  59. }
  60. func (s *buildStages) add(name string, image builder.Image) error {
  61. stage := newBuildStage(image.ImageID())
  62. name = strings.ToLower(name)
  63. if len(name) > 0 {
  64. if _, ok := s.byName[name]; ok {
  65. return errors.Errorf("duplicate name %s", name)
  66. }
  67. s.byName[name] = stage
  68. }
  69. s.sequence = append(s.sequence, stage)
  70. return nil
  71. }
  72. func (s *buildStages) update(imageID string) {
  73. s.sequence[len(s.sequence)-1].update(imageID)
  74. }
  75. type getAndMountFunc func(string, bool) (builder.Image, builder.ReleaseableLayer, error)
  76. // imageSources mounts images and provides a cache for mounted images. It tracks
  77. // all images so they can be unmounted at the end of the build.
  78. type imageSources struct {
  79. byImageID map[string]*imageMount
  80. mounts []*imageMount
  81. getImage getAndMountFunc
  82. cache pathCache // TODO: remove
  83. }
  84. // TODO @jhowardmsft LCOW Support: Eventually, platform can be moved to options.Options.Platform,
  85. // and removed from builderOptions, but that can't be done yet as it would affect the API.
  86. func newImageSources(ctx context.Context, options builderOptions) *imageSources {
  87. getAndMount := func(idOrRef string, localOnly bool) (builder.Image, builder.ReleaseableLayer, error) {
  88. pullOption := backend.PullOptionNoPull
  89. if !localOnly {
  90. if options.Options.PullParent {
  91. pullOption = backend.PullOptionForcePull
  92. } else {
  93. pullOption = backend.PullOptionPreferLocal
  94. }
  95. }
  96. return options.Backend.GetImageAndReleasableLayer(ctx, idOrRef, backend.GetImageAndLayerOptions{
  97. PullOption: pullOption,
  98. AuthConfig: options.Options.AuthConfigs,
  99. Output: options.ProgressWriter.Output,
  100. Platform: options.Platform,
  101. })
  102. }
  103. return &imageSources{
  104. byImageID: make(map[string]*imageMount),
  105. getImage: getAndMount,
  106. }
  107. }
  108. func (m *imageSources) Get(idOrRef string, localOnly bool) (*imageMount, error) {
  109. if im, ok := m.byImageID[idOrRef]; ok {
  110. return im, nil
  111. }
  112. image, layer, err := m.getImage(idOrRef, localOnly)
  113. if err != nil {
  114. return nil, err
  115. }
  116. im := newImageMount(image, layer)
  117. m.Add(im)
  118. return im, nil
  119. }
  120. func (m *imageSources) Unmount() (retErr error) {
  121. for _, im := range m.mounts {
  122. if err := im.unmount(); err != nil {
  123. logrus.Error(err)
  124. retErr = err
  125. }
  126. }
  127. return
  128. }
  129. func (m *imageSources) Add(im *imageMount) {
  130. switch im.image {
  131. case nil:
  132. im.image = &dockerimage.Image{}
  133. default:
  134. m.byImageID[im.image.ImageID()] = im
  135. }
  136. m.mounts = append(m.mounts, im)
  137. }
  138. // imageMount is a reference to an image that can be used as a builder.Source
  139. type imageMount struct {
  140. image builder.Image
  141. source builder.Source
  142. layer builder.ReleaseableLayer
  143. }
  144. func newImageMount(image builder.Image, layer builder.ReleaseableLayer) *imageMount {
  145. im := &imageMount{image: image, layer: layer}
  146. return im
  147. }
  148. func (im *imageMount) Source() (builder.Source, error) {
  149. if im.source == nil {
  150. if im.layer == nil {
  151. return nil, errors.Errorf("empty context")
  152. }
  153. mountPath, err := im.layer.Mount()
  154. if err != nil {
  155. return nil, errors.Wrapf(err, "failed to mount %s", im.image.ImageID())
  156. }
  157. source, err := remotecontext.NewLazySource(mountPath)
  158. if err != nil {
  159. return nil, errors.Wrapf(err, "failed to create lazycontext for %s", mountPath)
  160. }
  161. im.source = source
  162. }
  163. return im.source, nil
  164. }
  165. func (im *imageMount) unmount() error {
  166. if im.layer == nil {
  167. return nil
  168. }
  169. if err := im.layer.Release(); err != nil {
  170. return errors.Wrapf(err, "failed to unmount previous build image %s", im.image.ImageID())
  171. }
  172. im.layer = nil
  173. return nil
  174. }
  175. func (im *imageMount) Image() builder.Image {
  176. return im.image
  177. }
  178. func (im *imageMount) Layer() builder.ReleaseableLayer {
  179. return im.layer
  180. }
  181. func (im *imageMount) ImageID() string {
  182. return im.image.ImageID()
  183. }