image_commit.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package containerd
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "runtime"
  10. "strings"
  11. "time"
  12. "github.com/containerd/containerd/content"
  13. "github.com/containerd/containerd/diff"
  14. cerrdefs "github.com/containerd/containerd/errdefs"
  15. "github.com/containerd/containerd/images"
  16. "github.com/containerd/containerd/leases"
  17. "github.com/containerd/containerd/mount"
  18. "github.com/containerd/containerd/rootfs"
  19. "github.com/containerd/containerd/snapshots"
  20. "github.com/containerd/log"
  21. "github.com/docker/docker/api/types/backend"
  22. "github.com/docker/docker/image"
  23. imagespec "github.com/docker/docker/image/spec/specs-go/v1"
  24. "github.com/docker/docker/internal/compatcontext"
  25. "github.com/docker/docker/pkg/archive"
  26. "github.com/opencontainers/go-digest"
  27. "github.com/opencontainers/image-spec/identity"
  28. "github.com/opencontainers/image-spec/specs-go"
  29. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  30. )
  31. /*
  32. This code is based on `commit` support in nerdctl, under Apache License
  33. https://github.com/containerd/nerdctl/blob/master/pkg/imgutil/commit/commit.go
  34. with adaptations to match the Moby data model and services.
  35. */
  36. // CommitImage creates a new image from a commit config.
  37. func (i *ImageService) CommitImage(ctx context.Context, cc backend.CommitConfig) (image.ID, error) {
  38. container := i.containers.Get(cc.ContainerID)
  39. cs := i.client.ContentStore()
  40. var parentManifest ocispec.Manifest
  41. var parentImage imagespec.DockerOCIImage
  42. // ImageManifest can be nil when committing an image with base FROM scratch
  43. if container.ImageManifest != nil {
  44. imageManifestBytes, err := content.ReadBlob(ctx, cs, *container.ImageManifest)
  45. if err != nil {
  46. return "", err
  47. }
  48. if err := json.Unmarshal(imageManifestBytes, &parentManifest); err != nil {
  49. return "", err
  50. }
  51. imageConfigBytes, err := content.ReadBlob(ctx, cs, parentManifest.Config)
  52. if err != nil {
  53. return "", err
  54. }
  55. if err := json.Unmarshal(imageConfigBytes, &parentImage); err != nil {
  56. return "", err
  57. }
  58. }
  59. var (
  60. differ = i.client.DiffService()
  61. sn = i.client.SnapshotService(container.Driver)
  62. )
  63. // Don't gc me and clean the dirty data after 1 hour!
  64. ctx, release, err := i.client.WithLease(ctx, leases.WithRandomID(), leases.WithExpiration(1*time.Hour))
  65. if err != nil {
  66. return "", fmt.Errorf("failed to create lease for commit: %w", err)
  67. }
  68. defer func() {
  69. if err := release(compatcontext.WithoutCancel(ctx)); err != nil {
  70. log.G(ctx).WithError(err).Warn("failed to release lease created for commit")
  71. }
  72. }()
  73. diffLayerDesc, diffID, err := createDiff(ctx, cc.ContainerID, sn, cs, differ)
  74. if err != nil {
  75. return "", fmt.Errorf("failed to export layer: %w", err)
  76. }
  77. imageConfig := generateCommitImageConfig(parentImage, diffID, cc)
  78. layers := parentManifest.Layers
  79. if diffLayerDesc != nil {
  80. rootfsID := identity.ChainID(imageConfig.RootFS.DiffIDs).String()
  81. if err := i.applyDiffLayer(ctx, rootfsID, cc.ContainerID, sn, differ, *diffLayerDesc); err != nil {
  82. return "", fmt.Errorf("failed to apply diff: %w", err)
  83. }
  84. layers = append(layers, *diffLayerDesc)
  85. }
  86. commitManifestDesc, err := writeContentsForImage(ctx, container.Driver, cs, imageConfig, layers)
  87. if err != nil {
  88. return "", err
  89. }
  90. // image create
  91. img := images.Image{
  92. Name: danglingImageName(commitManifestDesc.Digest),
  93. Target: commitManifestDesc,
  94. CreatedAt: time.Now(),
  95. Labels: map[string]string{
  96. imageLabelClassicBuilderParent: cc.ParentImageID,
  97. },
  98. }
  99. if _, err := i.client.ImageService().Update(ctx, img); err != nil {
  100. if !cerrdefs.IsNotFound(err) {
  101. return "", err
  102. }
  103. if _, err := i.client.ImageService().Create(ctx, img); err != nil {
  104. return "", fmt.Errorf("failed to create new image: %w", err)
  105. }
  106. }
  107. id := image.ID(img.Target.Digest)
  108. c8dImg, err := i.NewImageManifest(ctx, img, commitManifestDesc)
  109. if err != nil {
  110. return id, err
  111. }
  112. if err := c8dImg.Unpack(ctx, container.Driver); err != nil && !cerrdefs.IsAlreadyExists(err) {
  113. return id, fmt.Errorf("failed to unpack image: %w", err)
  114. }
  115. return id, nil
  116. }
  117. // generateCommitImageConfig generates an OCI Image config based on the
  118. // container's image and the CommitConfig options.
  119. func generateCommitImageConfig(baseConfig imagespec.DockerOCIImage, diffID digest.Digest, opts backend.CommitConfig) imagespec.DockerOCIImage {
  120. if opts.Author == "" {
  121. opts.Author = baseConfig.Author
  122. }
  123. createdTime := time.Now()
  124. arch := baseConfig.Architecture
  125. if arch == "" {
  126. arch = runtime.GOARCH
  127. log.G(context.TODO()).Warnf("assuming arch=%q", arch)
  128. }
  129. os := baseConfig.OS
  130. if os == "" {
  131. os = runtime.GOOS
  132. log.G(context.TODO()).Warnf("assuming os=%q", os)
  133. }
  134. log.G(context.TODO()).Debugf("generateCommitImageConfig(): arch=%q, os=%q", arch, os)
  135. diffIds := baseConfig.RootFS.DiffIDs
  136. if diffID != "" {
  137. diffIds = append(diffIds, diffID)
  138. }
  139. return imagespec.DockerOCIImage{
  140. Image: ocispec.Image{
  141. Platform: ocispec.Platform{
  142. Architecture: arch,
  143. OS: os,
  144. },
  145. Created: &createdTime,
  146. Author: opts.Author,
  147. RootFS: ocispec.RootFS{
  148. Type: "layers",
  149. DiffIDs: diffIds,
  150. },
  151. History: append(baseConfig.History, ocispec.History{
  152. Created: &createdTime,
  153. CreatedBy: strings.Join(opts.ContainerConfig.Cmd, " "),
  154. Author: opts.Author,
  155. Comment: opts.Comment,
  156. EmptyLayer: diffID == "",
  157. }),
  158. },
  159. Config: containerConfigToDockerOCIImageConfig(opts.Config),
  160. }
  161. }
  162. // writeContentsForImage will commit oci image config and manifest into containerd's content store.
  163. func writeContentsForImage(ctx context.Context, snName string, cs content.Store, newConfig imagespec.DockerOCIImage, layers []ocispec.Descriptor) (ocispec.Descriptor, error) {
  164. newConfigJSON, err := json.Marshal(newConfig)
  165. if err != nil {
  166. return ocispec.Descriptor{}, err
  167. }
  168. configDesc := ocispec.Descriptor{
  169. MediaType: ocispec.MediaTypeImageConfig,
  170. Digest: digest.FromBytes(newConfigJSON),
  171. Size: int64(len(newConfigJSON)),
  172. }
  173. newMfst := struct {
  174. MediaType string `json:"mediaType,omitempty"`
  175. ocispec.Manifest
  176. }{
  177. MediaType: ocispec.MediaTypeImageManifest,
  178. Manifest: ocispec.Manifest{
  179. Versioned: specs.Versioned{
  180. SchemaVersion: 2,
  181. },
  182. Config: configDesc,
  183. Layers: layers,
  184. },
  185. }
  186. newMfstJSON, err := json.MarshalIndent(newMfst, "", " ")
  187. if err != nil {
  188. return ocispec.Descriptor{}, err
  189. }
  190. newMfstDesc := ocispec.Descriptor{
  191. MediaType: ocispec.MediaTypeImageManifest,
  192. Digest: digest.FromBytes(newMfstJSON),
  193. Size: int64(len(newMfstJSON)),
  194. }
  195. // new manifest should reference the layers and config content
  196. labels := map[string]string{
  197. "containerd.io/gc.ref.content.0": configDesc.Digest.String(),
  198. }
  199. for i, l := range layers {
  200. labels[fmt.Sprintf("containerd.io/gc.ref.content.%d", i+1)] = l.Digest.String()
  201. }
  202. err = content.WriteBlob(ctx, cs, newMfstDesc.Digest.String(), bytes.NewReader(newMfstJSON), newMfstDesc, content.WithLabels(labels))
  203. if err != nil {
  204. return ocispec.Descriptor{}, err
  205. }
  206. // config should reference to snapshotter
  207. labelOpt := content.WithLabels(map[string]string{
  208. fmt.Sprintf("containerd.io/gc.ref.snapshot.%s", snName): identity.ChainID(newConfig.RootFS.DiffIDs).String(),
  209. })
  210. err = content.WriteBlob(ctx, cs, configDesc.Digest.String(), bytes.NewReader(newConfigJSON), configDesc, labelOpt)
  211. if err != nil {
  212. return ocispec.Descriptor{}, err
  213. }
  214. return newMfstDesc, nil
  215. }
  216. // createDiff creates a layer diff into containerd's content store.
  217. // If the diff is empty it returns nil empty digest and no error.
  218. func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs content.Store, comparer diff.Comparer) (*ocispec.Descriptor, digest.Digest, error) {
  219. newDesc, err := rootfs.CreateDiff(ctx, name, sn, comparer)
  220. if err != nil {
  221. return nil, "", err
  222. }
  223. ra, err := cs.ReaderAt(ctx, newDesc)
  224. if err != nil {
  225. return nil, "", fmt.Errorf("failed to read diff archive: %w", err)
  226. }
  227. defer ra.Close()
  228. empty, err := archive.IsEmpty(content.NewReader(ra))
  229. if err != nil {
  230. return nil, "", fmt.Errorf("failed to check if archive is empty: %w", err)
  231. }
  232. if empty {
  233. return nil, "", nil
  234. }
  235. info, err := cs.Info(ctx, newDesc.Digest)
  236. if err != nil {
  237. return nil, "", fmt.Errorf("failed to get content info: %w", err)
  238. }
  239. diffIDStr, ok := info.Labels["containerd.io/uncompressed"]
  240. if !ok {
  241. return nil, "", fmt.Errorf("invalid differ response with no diffID")
  242. }
  243. diffID, err := digest.Parse(diffIDStr)
  244. if err != nil {
  245. return nil, "", err
  246. }
  247. return &ocispec.Descriptor{
  248. MediaType: ocispec.MediaTypeImageLayerGzip,
  249. Digest: newDesc.Digest,
  250. Size: info.Size,
  251. }, diffID, nil
  252. }
  253. // applyDiffLayer will apply diff layer content created by createDiff into the snapshotter.
  254. func (i *ImageService) applyDiffLayer(ctx context.Context, name string, containerID string, sn snapshots.Snapshotter, differ diff.Applier, diffDesc ocispec.Descriptor) (retErr error) {
  255. var (
  256. key = uniquePart() + "-" + name
  257. mounts []mount.Mount
  258. err error
  259. )
  260. info, err := sn.Stat(ctx, containerID)
  261. if err != nil {
  262. return err
  263. }
  264. mounts, err = sn.Prepare(ctx, key, info.Parent)
  265. if err != nil {
  266. return fmt.Errorf("failed to prepare snapshot: %w", err)
  267. }
  268. defer func() {
  269. if retErr != nil {
  270. // NOTE: the snapshotter should be hold by lease. Even
  271. // if the cleanup fails, the containerd gc can delete it.
  272. if err := sn.Remove(ctx, key); err != nil {
  273. log.G(ctx).Warnf("failed to cleanup aborted apply %s: %s", key, err)
  274. }
  275. }
  276. }()
  277. if _, err = differ.Apply(ctx, diffDesc, mounts); err != nil {
  278. return err
  279. }
  280. if !i.idMapping.Empty() {
  281. // The rootfs of the container is remapped if an id mapping exists, we
  282. // need to "unremap" it before committing the snapshot
  283. rootPair := i.idMapping.RootPair()
  284. usernsID := fmt.Sprintf("%s-%d-%d", key, rootPair.UID, rootPair.GID)
  285. remappedID := usernsID + remapSuffix
  286. if err = sn.Commit(ctx, name+"-pre", key); err != nil {
  287. if cerrdefs.IsAlreadyExists(err) {
  288. return nil
  289. }
  290. return err
  291. }
  292. mounts, err = sn.Prepare(ctx, remappedID, name+"-pre")
  293. if err != nil {
  294. return err
  295. }
  296. if err := i.unremapRootFS(ctx, mounts); err != nil {
  297. return err
  298. }
  299. if err := sn.Commit(ctx, name, remappedID); err != nil {
  300. return err
  301. }
  302. key = remappedID
  303. }
  304. if err = sn.Commit(ctx, name, key); err != nil {
  305. if cerrdefs.IsAlreadyExists(err) {
  306. return nil
  307. }
  308. return err
  309. }
  310. return nil
  311. }
  312. // copied from github.com/containerd/containerd/rootfs/apply.go
  313. func uniquePart() string {
  314. t := time.Now()
  315. var b [3]byte
  316. // Ignore read failures, just decreases uniqueness
  317. rand.Read(b[:])
  318. return fmt.Sprintf("%d-%s", t.Nanosecond(), base64.URLEncoding.EncodeToString(b[:]))
  319. }
  320. // CommitBuildStep is used by the builder to create an image for each step in
  321. // the build.
  322. //
  323. // This method is different from CreateImageFromContainer:
  324. // - it doesn't attempt to validate container state
  325. // - it doesn't send a commit action to metrics
  326. // - it doesn't log a container commit event
  327. //
  328. // This is a temporary shim. Should be removed when builder stops using commit.
  329. func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
  330. ctr := i.containers.Get(c.ContainerID)
  331. if ctr == nil {
  332. // TODO: use typed error
  333. return "", fmt.Errorf("container not found: %s", c.ContainerID)
  334. }
  335. c.ContainerMountLabel = ctr.MountLabel
  336. c.ContainerOS = ctr.OS
  337. c.ParentImageID = string(ctr.ImageID)
  338. return i.CommitImage(ctx, c)
  339. }