image_commit.go 13 KB

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