generateCommitImageConfig returns commit oci image config based on the container's image.
(ctx context.Context, container containerd.Container, img containerd.Image, diffID digest.Digest, opts *Opts)
| 235 | |
| 236 | // generateCommitImageConfig returns commit oci image config based on the container's image. |
| 237 | func generateCommitImageConfig(ctx context.Context, container containerd.Container, img containerd.Image, diffID digest.Digest, opts *Opts) (ocispec.Image, error) { |
| 238 | spec, err := container.Spec(ctx) |
| 239 | if err != nil { |
| 240 | return ocispec.Image{}, err |
| 241 | } |
| 242 | |
| 243 | baseConfig, _, err := imgutil.ReadImageConfig(ctx, img) // aware of img.platform |
| 244 | if err != nil { |
| 245 | return ocispec.Image{}, err |
| 246 | } |
| 247 | |
| 248 | // TODO(fuweid): support updating the USER/ENV/... fields? |
| 249 | if opts.Changes.CMD != nil { |
| 250 | baseConfig.Config.Cmd = opts.Changes.CMD |
| 251 | } |
| 252 | if opts.Changes.Entrypoint != nil { |
| 253 | baseConfig.Config.Entrypoint = opts.Changes.Entrypoint |
| 254 | } |
| 255 | if opts.Author == "" { |
| 256 | opts.Author = baseConfig.Author |
| 257 | } |
| 258 | |
| 259 | createdBy := "" |
| 260 | if spec.Process != nil { |
| 261 | createdBy = strings.Join(spec.Process.Args, " ") |
| 262 | } |
| 263 | |
| 264 | createdTime := time.Now() |
| 265 | arch := baseConfig.Architecture |
| 266 | if arch == "" { |
| 267 | arch = runtime.GOARCH |
| 268 | log.G(ctx).Warnf("assuming arch=%q", arch) |
| 269 | } |
| 270 | os := baseConfig.OS |
| 271 | if os == "" { |
| 272 | os = runtime.GOOS |
| 273 | log.G(ctx).Warnf("assuming os=%q", os) |
| 274 | } |
| 275 | log.G(ctx).Debugf("generateCommitImageConfig(): arch=%q, os=%q", arch, os) |
| 276 | return ocispec.Image{ |
| 277 | Platform: ocispec.Platform{ |
| 278 | Architecture: arch, |
| 279 | OS: os, |
| 280 | }, |
| 281 | |
| 282 | Created: &createdTime, |
| 283 | Author: opts.Author, |
| 284 | Config: baseConfig.Config, |
| 285 | RootFS: ocispec.RootFS{ |
| 286 | Type: "layers", |
| 287 | DiffIDs: append(baseConfig.RootFS.DiffIDs, diffID), |
| 288 | }, |
| 289 | History: append(baseConfig.History, ocispec.History{ |
| 290 | Created: &createdTime, |
| 291 | CreatedBy: createdBy, |
| 292 | Author: opts.Author, |
| 293 | Comment: opts.Message, |
| 294 | EmptyLayer: (diffID == emptyGZLayer), |
no test coverage detected
searching dependent graphs…