2017-02-25 04:18:35 +08:00
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
2018-04-12 22:20:36 +08:00
|
|
|
"context"
|
2022-07-06 17:14:06 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-04-12 22:20:36 +08:00
|
|
|
|
2021-02-07 06:49:40 +08:00
|
|
|
"github.com/containers/buildah/define"
|
2018-09-18 03:20:16 +08:00
|
|
|
"github.com/containers/buildah/docker"
|
|
|
|
"github.com/containers/buildah/util"
|
2019-11-01 03:18:10 +08:00
|
|
|
"github.com/containers/image/v5/image"
|
2019-10-26 05:19:30 +08:00
|
|
|
"github.com/containers/image/v5/manifest"
|
|
|
|
is "github.com/containers/image/v5/storage"
|
2019-11-01 03:18:10 +08:00
|
|
|
"github.com/containers/image/v5/transports"
|
2019-10-26 05:19:30 +08:00
|
|
|
"github.com/containers/image/v5/types"
|
2017-05-17 23:53:28 +08:00
|
|
|
"github.com/containers/storage"
|
2019-10-02 04:03:57 +08:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-02-25 04:18:35 +08:00
|
|
|
)
|
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
func importBuilderDataFromImage(ctx context.Context, store storage.Store, systemContext *types.SystemContext, imageID, containerName, containerID string) (*Builder, error) {
|
2018-06-12 06:23:48 +08:00
|
|
|
if imageID == "" {
|
2025-04-01 09:24:18 +08:00
|
|
|
return nil, errors.New("internal error: imageID is empty in importBuilderDataFromImage")
|
2018-06-12 06:23:48 +08:00
|
|
|
}
|
|
|
|
|
2023-11-15 00:55:28 +08:00
|
|
|
storeopts, err := storage.DefaultStoreOptions()
|
2019-03-25 18:23:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uidmap, gidmap := convertStorageIDMaps(storeopts.UIDMap, storeopts.GIDMap)
|
2017-02-25 04:18:35 +08:00
|
|
|
|
2018-06-12 06:23:48 +08:00
|
|
|
ref, err := is.Transport.ParseStoreReference(store, imageID)
|
|
|
|
if err != nil {
|
2022-07-06 17:14:06 +08:00
|
|
|
return nil, fmt.Errorf("no such image %q: %w", imageID, err)
|
2018-06-12 06:23:48 +08:00
|
|
|
}
|
2019-11-01 03:18:10 +08:00
|
|
|
src, err := ref.NewImageSource(ctx, systemContext)
|
|
|
|
if err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("instantiating image source: %w", err)
|
2018-06-12 06:23:48 +08:00
|
|
|
}
|
|
|
|
defer src.Close()
|
2018-06-12 06:41:11 +08:00
|
|
|
|
2019-11-01 03:18:10 +08:00
|
|
|
imageDigest := ""
|
2025-03-01 02:56:09 +08:00
|
|
|
unparsedTop := image.UnparsedInstance(src, nil)
|
|
|
|
manifestBytes, manifestType, err := unparsedTop.Manifest(ctx)
|
2019-11-01 03:18:10 +08:00
|
|
|
if err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("loading image manifest for %q: %w", transports.ImageName(ref), err)
|
2019-11-01 03:18:10 +08:00
|
|
|
}
|
|
|
|
if manifestDigest, err := manifest.Digest(manifestBytes); err == nil {
|
|
|
|
imageDigest = manifestDigest.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
var instanceDigest *digest.Digest
|
2025-03-01 02:56:09 +08:00
|
|
|
unparsedInstance := unparsedTop // for instanceDigest
|
2019-11-01 03:18:10 +08:00
|
|
|
if manifest.MIMETypeIsMultiImage(manifestType) {
|
|
|
|
list, err := manifest.ListFromBlob(manifestBytes, manifestType)
|
|
|
|
if err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("parsing image manifest for %q as list: %w", transports.ImageName(ref), err)
|
2019-11-01 03:18:10 +08:00
|
|
|
}
|
|
|
|
instance, err := list.ChooseInstance(systemContext)
|
|
|
|
if err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("finding an appropriate image in manifest list %q: %w", transports.ImageName(ref), err)
|
2019-11-01 03:18:10 +08:00
|
|
|
}
|
|
|
|
instanceDigest = &instance
|
2025-03-01 02:56:09 +08:00
|
|
|
unparsedInstance = image.UnparsedInstance(src, instanceDigest)
|
2019-11-01 03:18:10 +08:00
|
|
|
}
|
|
|
|
|
2025-03-01 02:56:09 +08:00
|
|
|
image, err := image.FromUnparsedImage(ctx, systemContext, unparsedInstance)
|
2019-11-01 03:18:10 +08:00
|
|
|
if err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("instantiating image for %q instance %q: %w", transports.ImageName(ref), instanceDigest, err)
|
2019-11-01 03:18:10 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 06:23:48 +08:00
|
|
|
imageName := ""
|
|
|
|
if img, err3 := store.Image(imageID); err3 == nil {
|
|
|
|
if len(img.Names) > 0 {
|
|
|
|
imageName = img.Names[0]
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
2018-06-12 06:23:48 +08:00
|
|
|
if img.TopLayer != "" {
|
|
|
|
layer, err4 := store.Layer(img.TopLayer)
|
|
|
|
if err4 != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("reading information about image's top layer: %w", err4)
|
2018-03-08 07:11:43 +08:00
|
|
|
}
|
2018-06-12 06:23:48 +08:00
|
|
|
uidmap, gidmap = convertStorageIDMaps(layer.UIDMap, layer.GIDMap)
|
2017-03-16 05:19:29 +08:00
|
|
|
}
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
2018-06-27 04:35:43 +08:00
|
|
|
defaultNamespaceOptions, err := DefaultNamespaceOptions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-06 04:36:49 +08:00
|
|
|
netInt, err := getNetworkInterface(store, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-25 04:18:35 +08:00
|
|
|
builder := &Builder{
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
store: store,
|
|
|
|
Type: containerType,
|
|
|
|
FromImage: imageName,
|
2017-05-19 05:38:41 +08:00
|
|
|
FromImageID: imageID,
|
2019-07-02 05:14:07 +08:00
|
|
|
FromImageDigest: imageDigest,
|
2017-05-19 05:38:41 +08:00
|
|
|
Container: containerName,
|
|
|
|
ContainerID: containerID,
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
ImageCreatedBy: "",
|
2018-06-27 04:35:43 +08:00
|
|
|
NamespaceOptions: defaultNamespaceOptions,
|
2021-02-07 06:49:40 +08:00
|
|
|
IDMappingOptions: define.IDMappingOptions{
|
2018-03-08 07:11:43 +08:00
|
|
|
HostUIDMapping: len(uidmap) == 0,
|
|
|
|
HostGIDMapping: len(uidmap) == 0,
|
|
|
|
UIDMap: uidmap,
|
|
|
|
GIDMap: gidmap,
|
|
|
|
},
|
2022-01-06 04:36:49 +08:00
|
|
|
NetworkInterface: netInt,
|
2023-08-12 08:25:21 +08:00
|
|
|
CommonBuildOpts: &CommonBuildOptions{},
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
2024-08-16 01:41:05 +08:00
|
|
|
if err := builder.initConfig(ctx, systemContext, image, nil); err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("preparing image configuration: %w", err)
|
2018-06-12 05:43:21 +08:00
|
|
|
}
|
2017-05-19 05:38:41 +08:00
|
|
|
|
|
|
|
return builder, nil
|
|
|
|
}
|
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
func importBuilder(ctx context.Context, store storage.Store, options ImportOptions) (*Builder, error) {
|
2017-05-19 05:38:41 +08:00
|
|
|
if options.Container == "" {
|
2022-07-06 17:14:06 +08:00
|
|
|
return nil, errors.New("container name must be specified")
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := store.Container(options.Container)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-14 04:03:13 +08:00
|
|
|
systemContext := getSystemContext(store, &types.SystemContext{}, options.SignaturePolicyPath)
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
builder, err := importBuilderDataFromImage(ctx, store, systemContext, c.ImageID, options.Container, c.ID)
|
2017-05-19 05:38:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-07 02:20:55 +08:00
|
|
|
if builder.FromImageID != "" {
|
|
|
|
if d, err2 := digest.Parse(builder.FromImageID); err2 == nil {
|
|
|
|
builder.Docker.Parent = docker.ID(d)
|
|
|
|
} else {
|
|
|
|
builder.Docker.Parent = docker.ID(digest.NewDigestFromHex(digest.Canonical.String(), builder.FromImageID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if builder.FromImage != "" {
|
|
|
|
builder.Docker.ContainerConfig.Image = builder.FromImage
|
|
|
|
}
|
2018-03-08 07:11:43 +08:00
|
|
|
builder.IDMappingOptions.UIDMap, builder.IDMappingOptions.GIDMap = convertStorageIDMaps(c.UIDMap, c.GIDMap)
|
2017-06-07 02:20:55 +08:00
|
|
|
|
2017-02-25 04:18:35 +08:00
|
|
|
err = builder.Save()
|
|
|
|
if err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("saving builder state: %w", err)
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder, nil
|
|
|
|
}
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2018-04-12 22:20:36 +08:00
|
|
|
func importBuilderFromImage(ctx context.Context, store storage.Store, options ImportFromImageOptions) (*Builder, error) {
|
2017-05-19 05:38:41 +08:00
|
|
|
if options.Image == "" {
|
2022-07-06 17:14:06 +08:00
|
|
|
return nil, errors.New("image name must be specified")
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
2019-03-14 04:03:13 +08:00
|
|
|
systemContext := getSystemContext(store, options.SystemContext, options.SignaturePolicyPath)
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2021-04-23 16:26:26 +08:00
|
|
|
_, img, err := util.FindImage(store, "", systemContext, options.Image)
|
2018-05-02 03:37:13 +08:00
|
|
|
if err != nil {
|
2022-07-06 17:14:06 +08:00
|
|
|
return nil, fmt.Errorf("importing settings: %w", err)
|
2018-05-02 03:37:13 +08:00
|
|
|
}
|
2017-05-19 05:38:41 +08:00
|
|
|
|
2018-05-02 03:37:13 +08:00
|
|
|
builder, err := importBuilderDataFromImage(ctx, store, systemContext, img.ID, "", "")
|
|
|
|
if err != nil {
|
2022-09-18 18:36:08 +08:00
|
|
|
return nil, fmt.Errorf("importing build settings from image %q: %w", options.Image, err)
|
2017-06-29 05:07:58 +08:00
|
|
|
}
|
2018-05-02 03:37:13 +08:00
|
|
|
|
2021-05-08 01:38:44 +08:00
|
|
|
builder.setupLogger()
|
2018-05-02 03:37:13 +08:00
|
|
|
return builder, nil
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|