2017-02-11 00:48:15 +08:00
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
2018-04-12 22:20:36 +08:00
|
|
|
"context"
|
2017-02-11 00:48:15 +08:00
|
|
|
"encoding/json"
|
2018-03-29 02:14:57 +08:00
|
|
|
"fmt"
|
2017-05-09 23:56:44 +08:00
|
|
|
"io"
|
2017-02-24 06:56:48 +08:00
|
|
|
"io/ioutil"
|
2017-03-16 04:44:52 +08:00
|
|
|
"os"
|
2017-02-11 00:48:15 +08:00
|
|
|
"path/filepath"
|
2020-01-14 20:12:56 +08:00
|
|
|
"sort"
|
2019-01-19 04:39:58 +08:00
|
|
|
"time"
|
2017-02-11 00:48:15 +08:00
|
|
|
|
2020-10-10 02:34:19 +08:00
|
|
|
"github.com/containers/buildah/define"
|
2018-09-18 03:20:16 +08:00
|
|
|
"github.com/containers/buildah/docker"
|
2019-10-26 05:19:30 +08:00
|
|
|
"github.com/containers/image/v5/types"
|
2020-04-02 02:15:56 +08:00
|
|
|
encconfig "github.com/containers/ocicrypt/config"
|
2017-05-17 23:53:28 +08:00
|
|
|
"github.com/containers/storage"
|
2017-02-24 06:56:48 +08:00
|
|
|
"github.com/containers/storage/pkg/ioutils"
|
2019-07-18 16:42:09 +08:00
|
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-06-03 00:17:27 +08:00
|
|
|
"github.com/pkg/errors"
|
2018-10-03 22:05:46 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-11 03:45:06 +08:00
|
|
|
// Package is the name of this package, used in help output and to
|
|
|
|
// identify working containers.
|
2017-04-19 03:52:53 +08:00
|
|
|
Package = "buildah"
|
2018-02-14 04:29:38 +08:00
|
|
|
// Version for the Package. Bump version in contrib/rpm/buildah.spec
|
|
|
|
// too.
|
2020-09-04 05:18:29 +08:00
|
|
|
Version = "1.17.0-dev"
|
2017-06-15 01:31:43 +08:00
|
|
|
// The value we use to identify what type of information, currently a
|
|
|
|
// serialized Builder structure, we are using as per-container state.
|
|
|
|
// This should only be changed when we make incompatible changes to
|
|
|
|
// that data structure, as it's used to distinguish containers which
|
|
|
|
// are "ours" from ones that aren't.
|
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
|
|
|
containerType = Package + " 0.0.1"
|
2017-06-15 01:31:43 +08:00
|
|
|
// The file in the per-container directory which we use to store our
|
|
|
|
// per-container state. If it isn't there, then the container isn't
|
|
|
|
// one of our build containers.
|
|
|
|
stateFile = Package + ".json"
|
2017-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
Fix --pull=true||false and add --pull-never to bud and from (retry)
(Replaces #1873 as it had lint issues that were timing out tests that I couldn't
track down easily)
Prior to this fix, if someone did `buildah bud --pull=false .` and the image in
the Containerfile's FROM statement was not local, the build would fail. The same
build on Docker will succeed. In Docker, when `--pull` is set to false, it only
pulls the image from the registry if there was not one locally. Buildah would never
pull the image and if the image was not locally available, it would throw an error.
In certain Kubernetes environments, this was especially troublesome.
To retain the old `--pull=false` functionality, I've created a new `--pull-never`
option that fails if an image is not locally available just like the old
`--pull=false` option used to do.
In addition, if there was a newer version of the image on the repository than
the one locally, the `--pull=true` option would not pull the image as it should
have, this corrects that.
Changes both the from and bud commands.
Addresses: #1675
Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
Closes: #1959
Approved by: rhatdan
2019-10-31 22:15:56 +08:00
|
|
|
// PullPolicy takes the value PullIfMissing, PullAlways, PullIfNewer, or PullNever.
|
2020-10-10 02:34:19 +08:00
|
|
|
type PullPolicy = define.PullPolicy
|
2018-03-29 02:14:57 +08:00
|
|
|
|
2017-04-11 02:15:30 +08:00
|
|
|
const (
|
|
|
|
// PullIfMissing is one of the values that BuilderOptions.PullPolicy
|
|
|
|
// can take, signalling that the source image should be pulled from a
|
|
|
|
// registry if a local copy of it is not already present.
|
2020-10-10 02:34:19 +08:00
|
|
|
PullIfMissing = define.PullIfMissing
|
2017-04-11 02:15:30 +08:00
|
|
|
// PullAlways is one of the values that BuilderOptions.PullPolicy can
|
|
|
|
// take, signalling that a fresh, possibly updated, copy of the image
|
|
|
|
// should be pulled from a registry before the build proceeds.
|
2020-10-10 02:34:19 +08:00
|
|
|
PullAlways = define.PullAlways
|
Fix --pull=true||false and add --pull-never to bud and from (retry)
(Replaces #1873 as it had lint issues that were timing out tests that I couldn't
track down easily)
Prior to this fix, if someone did `buildah bud --pull=false .` and the image in
the Containerfile's FROM statement was not local, the build would fail. The same
build on Docker will succeed. In Docker, when `--pull` is set to false, it only
pulls the image from the registry if there was not one locally. Buildah would never
pull the image and if the image was not locally available, it would throw an error.
In certain Kubernetes environments, this was especially troublesome.
To retain the old `--pull=false` functionality, I've created a new `--pull-never`
option that fails if an image is not locally available just like the old
`--pull=false` option used to do.
In addition, if there was a newer version of the image on the repository than
the one locally, the `--pull=true` option would not pull the image as it should
have, this corrects that.
Changes both the from and bud commands.
Addresses: #1675
Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
Closes: #1959
Approved by: rhatdan
2019-10-31 22:15:56 +08:00
|
|
|
// PullIfNewer is one of the values that BuilderOptions.PullPolicy
|
|
|
|
// can take, signalling that the source image should only be pulled
|
|
|
|
// from a registry if a local copy is not already present or if a
|
|
|
|
// newer version the image is present on the repository.
|
2020-10-10 02:34:19 +08:00
|
|
|
PullIfNewer = define.PullIfNewer
|
2017-04-11 02:15:30 +08:00
|
|
|
// PullNever is one of the values that BuilderOptions.PullPolicy can
|
|
|
|
// take, signalling that the source image should not be pulled from a
|
|
|
|
// registry if a local copy of it is not already present.
|
2020-10-10 02:34:19 +08:00
|
|
|
PullNever = define.PullNever
|
2017-04-11 02:15:30 +08:00
|
|
|
)
|
|
|
|
|
2018-04-14 06:20:25 +08:00
|
|
|
// NetworkConfigurationPolicy takes the value NetworkDefault, NetworkDisabled,
|
|
|
|
// or NetworkEnabled.
|
|
|
|
type NetworkConfigurationPolicy int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NetworkDefault is one of the values that BuilderOptions.ConfigureNetwork
|
|
|
|
// can take, signalling that the default behavior should be used.
|
|
|
|
NetworkDefault NetworkConfigurationPolicy = iota
|
|
|
|
// NetworkDisabled is one of the values that BuilderOptions.ConfigureNetwork
|
|
|
|
// can take, signalling that network interfaces should NOT be configured for
|
|
|
|
// newly-created network namespaces.
|
|
|
|
NetworkDisabled
|
|
|
|
// NetworkEnabled is one of the values that BuilderOptions.ConfigureNetwork
|
|
|
|
// can take, signalling that network interfaces should be configured for
|
|
|
|
// newly-created network namespaces.
|
|
|
|
NetworkEnabled
|
|
|
|
)
|
|
|
|
|
|
|
|
// String formats a NetworkConfigurationPolicy as a string.
|
|
|
|
func (p NetworkConfigurationPolicy) String() string {
|
|
|
|
switch p {
|
|
|
|
case NetworkDefault:
|
|
|
|
return "NetworkDefault"
|
|
|
|
case NetworkDisabled:
|
|
|
|
return "NetworkDisabled"
|
|
|
|
case NetworkEnabled:
|
|
|
|
return "NetworkEnabled"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unknown NetworkConfigurationPolicy %d", p)
|
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// Builder objects are used to represent containers which are being used to
|
|
|
|
// build images. They also carry potential updates which will be applied to
|
|
|
|
// the image's configuration when the container's contents are used to build an
|
|
|
|
// image.
|
2017-02-11 00:48:15 +08:00
|
|
|
type Builder struct {
|
|
|
|
store storage.Store
|
|
|
|
|
2018-06-25 20:53:47 +08:00
|
|
|
// Args define variables that users can pass at build-time to the builder
|
|
|
|
Args map[string]string
|
2017-02-11 03:45:06 +08:00
|
|
|
// Type is used to help identify a build container's metadata. It
|
|
|
|
// should not be modified.
|
|
|
|
Type string `json:"type"`
|
|
|
|
// FromImage is the name of the source image which was used to create
|
|
|
|
// the container, if one was used. It should not be modified.
|
|
|
|
FromImage string `json:"image,omitempty"`
|
2017-03-16 05:19:29 +08:00
|
|
|
// FromImageID is the ID of the source image which was used to create
|
|
|
|
// the container, if one was used. It should not be modified.
|
|
|
|
FromImageID string `json:"image-id"`
|
2019-07-02 05:14:07 +08:00
|
|
|
// FromImageDigest is the digest of the source image which was used to
|
|
|
|
// create the container, if one was used. It should not be modified.
|
|
|
|
FromImageDigest string `json:"image-digest"`
|
2017-02-11 03:45:06 +08:00
|
|
|
// Config is the source image's configuration. It should not be
|
|
|
|
// modified.
|
|
|
|
Config []byte `json:"config,omitempty"`
|
|
|
|
// Manifest is the source image's manifest. It should not be modified.
|
|
|
|
Manifest []byte `json:"manifest,omitempty"`
|
|
|
|
|
|
|
|
// Container is the name of the build container. It should not be modified.
|
|
|
|
Container string `json:"container-name,omitempty"`
|
|
|
|
// ContainerID is the ID of the build container. It should not be modified.
|
|
|
|
ContainerID string `json:"container-id,omitempty"`
|
|
|
|
// MountPoint is the last location where the container's root
|
|
|
|
// filesystem was mounted. It should not be modified.
|
|
|
|
MountPoint string `json:"mountpoint,omitempty"`
|
2017-10-20 05:47:15 +08:00
|
|
|
// ProcessLabel is the SELinux process label associated with the container
|
|
|
|
ProcessLabel string `json:"process-label,omitempty"`
|
|
|
|
// MountLabel is the SELinux mount label associated with the container
|
|
|
|
MountLabel string `json:"mount-label,omitempty"`
|
2017-02-11 00:48:15 +08:00
|
|
|
|
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
|
|
|
// ImageAnnotations is a set of key-value pairs which is stored in the
|
2017-02-11 03:45:06 +08:00
|
|
|
// image's manifest.
|
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
|
|
|
ImageAnnotations map[string]string `json:"annotations,omitempty"`
|
|
|
|
// ImageCreatedBy is a description of how this container was built.
|
|
|
|
ImageCreatedBy string `json:"created-by,omitempty"`
|
2018-04-27 22:59:03 +08:00
|
|
|
// ImageHistoryComment is a description of how our added layers were built.
|
|
|
|
ImageHistoryComment string `json:"history-comment,omitempty"`
|
2017-02-11 00:48:15 +08:00
|
|
|
|
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
|
|
|
// Image metadata and runtime settings, in multiple formats.
|
2017-05-17 05:07:31 +08:00
|
|
|
OCIv1 v1.Image `json:"ociv1,omitempty"`
|
|
|
|
Docker docker.V2Image `json:"docker,omitempty"`
|
2018-03-07 07:13:24 +08:00
|
|
|
// DefaultMountsFilePath is the file path holding the mounts to be mounted in "host-path:container-path" format.
|
2017-11-08 06:44:24 +08:00
|
|
|
DefaultMountsFilePath string `json:"defaultMountsFilePath,omitempty"`
|
2018-03-07 07:13:24 +08:00
|
|
|
|
2018-05-12 01:00:14 +08:00
|
|
|
// Isolation controls how we handle "RUN" statements and the Run() method.
|
|
|
|
Isolation Isolation
|
2018-03-08 07:11:43 +08:00
|
|
|
// NamespaceOptions controls how we set up the namespaces for processes that we run in the container.
|
|
|
|
NamespaceOptions NamespaceOptions
|
2018-04-14 06:20:25 +08:00
|
|
|
// ConfigureNetwork controls whether or not network interfaces and
|
|
|
|
// routing are configured for a new network namespace (i.e., when not
|
|
|
|
// joining another's namespace and not just using the host's
|
|
|
|
// namespace), effectively deciding whether or not the process has a
|
|
|
|
// usable network.
|
|
|
|
ConfigureNetwork NetworkConfigurationPolicy
|
|
|
|
// CNIPluginPath is the location of CNI plugin helpers, if they should be
|
|
|
|
// run from a location other than the default location.
|
|
|
|
CNIPluginPath string
|
|
|
|
// CNIConfigDir is the location of CNI configuration files, if the files in
|
|
|
|
// the default configuration directory shouldn't be used.
|
|
|
|
CNIConfigDir string
|
2018-03-08 07:11:43 +08:00
|
|
|
// ID mapping options to use when running processes in the container with non-host user namespaces.
|
|
|
|
IDMappingOptions IDMappingOptions
|
2020-01-14 20:12:56 +08:00
|
|
|
// Capabilities is a list of capabilities to use when running commands in the container.
|
|
|
|
Capabilities []string
|
2019-01-19 04:39:58 +08:00
|
|
|
// PrependedEmptyLayers are history entries that we'll add to a
|
|
|
|
// committed image, after any history items that we inherit from a base
|
|
|
|
// image, but before the history item for the layer that we're
|
|
|
|
// committing.
|
|
|
|
PrependedEmptyLayers []v1.History
|
|
|
|
// AppendedEmptyLayers are history entries that we'll add to a
|
|
|
|
// committed image after the history item for the layer that we're
|
|
|
|
// committing.
|
|
|
|
AppendedEmptyLayers []v1.History
|
2019-09-07 03:07:18 +08:00
|
|
|
CommonBuildOpts *CommonBuildOptions
|
2018-06-09 00:55:46 +08:00
|
|
|
// TopLayer is the top layer of the image
|
|
|
|
TopLayer string
|
2018-08-24 00:55:16 +08:00
|
|
|
// Format for the build Image
|
|
|
|
Format string
|
2019-04-29 21:41:18 +08:00
|
|
|
// TempVolumes are temporary mount points created during container runs
|
|
|
|
TempVolumes map[string]bool
|
2019-08-10 04:21:24 +08:00
|
|
|
// ContentDigester counts the digest of all Add()ed content
|
|
|
|
ContentDigester CompositeDigester
|
2019-09-07 03:07:18 +08:00
|
|
|
// Devices are the additional devices to add to the containers
|
2020-09-24 23:40:57 +08:00
|
|
|
Devices ContainerDevices
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:12:16 +08:00
|
|
|
// BuilderInfo are used as objects to display container information
|
|
|
|
type BuilderInfo struct {
|
|
|
|
Type string
|
|
|
|
FromImage string
|
|
|
|
FromImageID string
|
2019-07-02 05:14:07 +08:00
|
|
|
FromImageDigest string
|
2017-12-18 22:12:16 +08:00
|
|
|
Config string
|
|
|
|
Manifest string
|
|
|
|
Container string
|
|
|
|
ContainerID string
|
|
|
|
MountPoint string
|
|
|
|
ProcessLabel string
|
|
|
|
MountLabel string
|
|
|
|
ImageAnnotations map[string]string
|
|
|
|
ImageCreatedBy string
|
|
|
|
OCIv1 v1.Image
|
|
|
|
Docker docker.V2Image
|
|
|
|
DefaultMountsFilePath string
|
2018-05-12 01:00:14 +08:00
|
|
|
Isolation string
|
2018-03-08 07:11:43 +08:00
|
|
|
NamespaceOptions NamespaceOptions
|
2020-01-14 20:12:56 +08:00
|
|
|
Capabilities []string
|
2018-04-14 06:20:25 +08:00
|
|
|
ConfigureNetwork string
|
|
|
|
CNIPluginPath string
|
|
|
|
CNIConfigDir string
|
2018-03-08 07:11:43 +08:00
|
|
|
IDMappingOptions IDMappingOptions
|
2019-01-19 04:39:58 +08:00
|
|
|
History []v1.History
|
2020-09-24 23:40:57 +08:00
|
|
|
Devices ContainerDevices
|
2017-12-18 22:12:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuildInfo gets a pointer to a Builder object and returns a BuilderInfo object from it.
|
|
|
|
// This is used in the inspect command to display Manifest and Config as string and not []byte.
|
|
|
|
func GetBuildInfo(b *Builder) BuilderInfo {
|
2019-01-19 04:39:58 +08:00
|
|
|
history := copyHistory(b.OCIv1.History)
|
|
|
|
history = append(history, copyHistory(b.PrependedEmptyLayers)...)
|
|
|
|
history = append(history, copyHistory(b.AppendedEmptyLayers)...)
|
2020-01-14 20:12:56 +08:00
|
|
|
sort.Strings(b.Capabilities)
|
2017-12-18 22:12:16 +08:00
|
|
|
return BuilderInfo{
|
|
|
|
Type: b.Type,
|
|
|
|
FromImage: b.FromImage,
|
|
|
|
FromImageID: b.FromImageID,
|
2019-07-02 05:14:07 +08:00
|
|
|
FromImageDigest: b.FromImageDigest,
|
2017-12-18 22:12:16 +08:00
|
|
|
Config: string(b.Config),
|
|
|
|
Manifest: string(b.Manifest),
|
|
|
|
Container: b.Container,
|
|
|
|
ContainerID: b.ContainerID,
|
|
|
|
MountPoint: b.MountPoint,
|
|
|
|
ProcessLabel: b.ProcessLabel,
|
2018-10-20 02:49:51 +08:00
|
|
|
MountLabel: b.MountLabel,
|
2017-12-18 22:12:16 +08:00
|
|
|
ImageAnnotations: b.ImageAnnotations,
|
|
|
|
ImageCreatedBy: b.ImageCreatedBy,
|
|
|
|
OCIv1: b.OCIv1,
|
|
|
|
Docker: b.Docker,
|
|
|
|
DefaultMountsFilePath: b.DefaultMountsFilePath,
|
2018-05-12 01:00:14 +08:00
|
|
|
Isolation: b.Isolation.String(),
|
2018-03-08 07:11:43 +08:00
|
|
|
NamespaceOptions: b.NamespaceOptions,
|
2018-04-14 06:20:25 +08:00
|
|
|
ConfigureNetwork: fmt.Sprintf("%v", b.ConfigureNetwork),
|
|
|
|
CNIPluginPath: b.CNIPluginPath,
|
|
|
|
CNIConfigDir: b.CNIConfigDir,
|
2018-03-08 07:11:43 +08:00
|
|
|
IDMappingOptions: b.IDMappingOptions,
|
2020-01-14 20:12:56 +08:00
|
|
|
Capabilities: b.Capabilities,
|
2019-01-19 04:39:58 +08:00
|
|
|
History: history,
|
2019-09-07 03:07:18 +08:00
|
|
|
Devices: b.Devices,
|
2017-12-18 22:12:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 07:13:24 +08:00
|
|
|
// CommonBuildOptions are resources that can be defined by flags for both buildah from and build-using-dockerfile
|
2018-02-14 03:58:56 +08:00
|
|
|
type CommonBuildOptions struct {
|
2018-06-16 00:28:03 +08:00
|
|
|
// AddHost is the list of hostnames to add to the build container's /etc/hosts.
|
2018-02-14 03:58:56 +08:00
|
|
|
AddHost []string
|
2018-03-07 07:13:24 +08:00
|
|
|
// CgroupParent is the path to cgroups under which the cgroup for the container will be created.
|
2018-02-14 03:58:56 +08:00
|
|
|
CgroupParent string
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUPeriod limits the CPU CFS (Completely Fair Scheduler) period
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUPeriod uint64
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUQuota limits the CPU CFS (Completely Fair Scheduler) quota
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUQuota int64
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUShares (relative weight
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUShares uint64
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUSetCPUs in which to allow execution (0-3, 0,1)
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUSetCPUs string
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUSetMems memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems.
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUSetMems string
|
2019-04-13 07:03:39 +08:00
|
|
|
// HTTPProxy determines whether *_proxy env vars from the build host are passed into the container.
|
|
|
|
HTTPProxy bool
|
2018-03-07 07:13:24 +08:00
|
|
|
// Memory is the upper limit (in bytes) on how much memory running containers can use.
|
2018-02-14 03:58:56 +08:00
|
|
|
Memory int64
|
2019-04-07 00:03:58 +08:00
|
|
|
// DNSSearch is the list of DNS search domains to add to the build container's /etc/resolv.conf
|
|
|
|
DNSSearch []string
|
|
|
|
// DNSServers is the list of DNS servers to add to the build container's /etc/resolv.conf
|
|
|
|
DNSServers []string
|
|
|
|
// DNSOptions is the list of DNS
|
|
|
|
DNSOptions []string
|
2018-03-07 07:13:24 +08:00
|
|
|
// MemorySwap limits the amount of memory and swap together.
|
2018-02-14 03:58:56 +08:00
|
|
|
MemorySwap int64
|
2018-03-07 07:13:24 +08:00
|
|
|
// LabelOpts is the a slice of fields of an SELinux context, given in "field:pair" format, or "disable".
|
|
|
|
// Recognized field names are "role", "type", and "level".
|
|
|
|
LabelOpts []string
|
2020-08-11 17:28:41 +08:00
|
|
|
// OmitTimestamp forces epoch 0 as created timestamp to allow for
|
|
|
|
// deterministic, content-addressable builds.
|
|
|
|
OmitTimestamp bool
|
2018-03-07 07:13:24 +08:00
|
|
|
// SeccompProfilePath is the pathname of a seccomp profile.
|
2018-02-14 03:58:56 +08:00
|
|
|
SeccompProfilePath string
|
2018-03-07 07:13:24 +08:00
|
|
|
// ApparmorProfile is the name of an apparmor profile.
|
|
|
|
ApparmorProfile string
|
|
|
|
// ShmSize is the "size" value to use when mounting an shmfs on the container's /dev/shm directory.
|
2018-02-23 01:41:22 +08:00
|
|
|
ShmSize string
|
2018-03-07 07:13:24 +08:00
|
|
|
// Ulimit specifies resource limit options, in the form type:softlimit[:hardlimit].
|
|
|
|
// These types are recognized:
|
2019-11-17 00:31:41 +08:00
|
|
|
// "core": maximum core dump size (ulimit -c)
|
2018-03-07 07:13:24 +08:00
|
|
|
// "cpu": maximum CPU time (ulimit -t)
|
|
|
|
// "data": maximum size of a process's data segment (ulimit -d)
|
|
|
|
// "fsize": maximum size of new files (ulimit -f)
|
|
|
|
// "locks": maximum number of file locks (ulimit -x)
|
|
|
|
// "memlock": maximum amount of locked memory (ulimit -l)
|
|
|
|
// "msgqueue": maximum amount of data in message queues (ulimit -q)
|
|
|
|
// "nice": niceness adjustment (nice -n, ulimit -e)
|
|
|
|
// "nofile": maximum number of open files (ulimit -n)
|
|
|
|
// "nproc": maximum number of processes (ulimit -u)
|
|
|
|
// "rss": maximum size of a process's (ulimit -m)
|
|
|
|
// "rtprio": maximum real-time scheduling priority (ulimit -r)
|
|
|
|
// "rttime": maximum amount of real-time execution between blocking syscalls
|
|
|
|
// "sigpending": maximum number of pending signals (ulimit -i)
|
|
|
|
// "stack": maximum stack size (ulimit -s)
|
2018-02-14 03:58:56 +08:00
|
|
|
Ulimit []string
|
2018-03-07 07:13:24 +08:00
|
|
|
// Volumes to bind mount into the container
|
2018-02-23 01:41:22 +08:00
|
|
|
Volumes []string
|
2018-02-14 03:58:56 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// BuilderOptions are used to initialize a new Builder.
|
2017-02-11 00:48:15 +08:00
|
|
|
type BuilderOptions struct {
|
2018-06-25 20:53:47 +08:00
|
|
|
// Args define variables that users can pass at build-time to the builder
|
|
|
|
Args map[string]string
|
2017-02-11 03:45:06 +08:00
|
|
|
// FromImage is the name of the image which should be used as the
|
|
|
|
// starting point for the container. It can be set to an empty value
|
|
|
|
// or "scratch" to indicate that the container should not be based on
|
|
|
|
// an image.
|
|
|
|
FromImage string
|
|
|
|
// Container is a desired name for the build container.
|
|
|
|
Container string
|
2017-04-11 02:15:30 +08:00
|
|
|
// PullPolicy decides whether or not we should pull the image that
|
|
|
|
// we're using as a base image. It should be PullIfMissing,
|
|
|
|
// PullAlways, or PullNever.
|
2018-03-29 02:14:57 +08:00
|
|
|
PullPolicy PullPolicy
|
2017-02-11 03:45:06 +08:00
|
|
|
// Registry is a value which is prepended to the image's name, if it
|
|
|
|
// needs to be pulled and the image name alone can not be resolved to a
|
2017-07-31 22:44:21 +08:00
|
|
|
// reference to a source image. No separator is implicitly added.
|
2017-02-11 03:45:06 +08:00
|
|
|
Registry string
|
2019-03-14 04:03:13 +08:00
|
|
|
// BlobDirectory is the name of a directory in which we'll attempt
|
2018-10-18 06:06:16 +08:00
|
|
|
// to store copies of layer blobs that we pull down, if any. It should
|
|
|
|
// already exist.
|
2019-03-14 04:03:13 +08:00
|
|
|
BlobDirectory string
|
2017-02-11 03:45:06 +08:00
|
|
|
// Mount signals to NewBuilder() that the container should be mounted
|
|
|
|
// immediately.
|
|
|
|
Mount bool
|
|
|
|
// SignaturePolicyPath specifies an override location for the signature
|
|
|
|
// policy which should be used for verifying the new image as it is
|
|
|
|
// being written. Except in specific circumstances, no value should be
|
|
|
|
// specified, indicating that the shared, system-wide default policy
|
|
|
|
// should be used.
|
2017-02-11 00:48:15 +08:00
|
|
|
SignaturePolicyPath string
|
2017-05-09 23:56:44 +08:00
|
|
|
// ReportWriter is an io.Writer which will be used to log the reading
|
|
|
|
// of the source image from a registry, if we end up pulling the image.
|
|
|
|
ReportWriter io.Writer
|
2017-07-21 08:02:11 +08:00
|
|
|
// github.com/containers/image/types SystemContext to hold credentials
|
|
|
|
// and other authentication/authorization information.
|
|
|
|
SystemContext *types.SystemContext
|
2018-03-07 07:13:24 +08:00
|
|
|
// DefaultMountsFilePath is the file path holding the mounts to be
|
|
|
|
// mounted in "host-path:container-path" format
|
2017-11-08 06:44:24 +08:00
|
|
|
DefaultMountsFilePath string
|
2018-05-12 01:00:14 +08:00
|
|
|
// Isolation controls how we handle "RUN" statements and the Run()
|
|
|
|
// method.
|
|
|
|
Isolation Isolation
|
2018-03-08 07:11:43 +08:00
|
|
|
// NamespaceOptions controls how we set up namespaces for processes that
|
|
|
|
// we might need to run using the container's root filesystem.
|
|
|
|
NamespaceOptions NamespaceOptions
|
2018-04-14 06:20:25 +08:00
|
|
|
// ConfigureNetwork controls whether or not network interfaces and
|
|
|
|
// routing are configured for a new network namespace (i.e., when not
|
|
|
|
// joining another's namespace and not just using the host's
|
|
|
|
// namespace), effectively deciding whether or not the process has a
|
|
|
|
// usable network.
|
|
|
|
ConfigureNetwork NetworkConfigurationPolicy
|
|
|
|
// CNIPluginPath is the location of CNI plugin helpers, if they should be
|
|
|
|
// run from a location other than the default location.
|
|
|
|
CNIPluginPath string
|
|
|
|
// CNIConfigDir is the location of CNI configuration files, if the files in
|
|
|
|
// the default configuration directory shouldn't be used.
|
|
|
|
CNIConfigDir string
|
2018-03-08 07:11:43 +08:00
|
|
|
// ID mapping options to use if we're setting up our own user namespace.
|
|
|
|
IDMappingOptions *IDMappingOptions
|
2020-01-14 20:12:56 +08:00
|
|
|
// Capabilities is a list of capabilities to use when
|
2018-06-05 05:36:26 +08:00
|
|
|
// running commands in the container.
|
2020-01-14 20:12:56 +08:00
|
|
|
Capabilities []string
|
2018-03-08 07:11:43 +08:00
|
|
|
CommonBuildOpts *CommonBuildOptions
|
2018-08-24 00:55:16 +08:00
|
|
|
// Format for the container image
|
|
|
|
Format string
|
2019-09-07 03:07:18 +08:00
|
|
|
// Devices are the additional devices to add to the containers
|
2020-09-24 23:40:57 +08:00
|
|
|
Devices ContainerDevices
|
2020-01-14 20:12:56 +08:00
|
|
|
//DefaultEnv for containers
|
|
|
|
DefaultEnv []string
|
2019-11-28 00:31:02 +08:00
|
|
|
// MaxPullRetries is the maximum number of attempts we'll make to pull
|
|
|
|
// any one image from the external registry if the first attempt fails.
|
|
|
|
MaxPullRetries int
|
|
|
|
// PullRetryDelay is how long to wait before retrying a pull attempt.
|
|
|
|
PullRetryDelay time.Duration
|
2020-04-02 02:15:56 +08:00
|
|
|
// OciDecryptConfig contains the config that can be used to decrypt an image if it is
|
|
|
|
// encrypted if non-nil. If nil, it does not attempt to decrypt an image.
|
|
|
|
OciDecryptConfig *encconfig.DecryptConfig
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// ImportOptions are used to initialize a Builder from an existing container
|
|
|
|
// which was created elsewhere.
|
2017-02-25 04:18:35 +08:00
|
|
|
type ImportOptions struct {
|
|
|
|
// Container is the name of the build container.
|
|
|
|
Container string
|
|
|
|
// SignaturePolicyPath specifies an override location for the signature
|
|
|
|
// policy which should be used for verifying the new image as it is
|
|
|
|
// being written. Except in specific circumstances, no value should be
|
|
|
|
// specified, indicating that the shared, system-wide default policy
|
|
|
|
// should be used.
|
|
|
|
SignaturePolicyPath string
|
|
|
|
}
|
|
|
|
|
2017-05-19 05:38:41 +08:00
|
|
|
// ImportFromImageOptions are used to initialize a Builder from an image.
|
|
|
|
type ImportFromImageOptions struct {
|
|
|
|
// Image is the name or ID of the image we'd like to examine.
|
|
|
|
Image string
|
|
|
|
// SignaturePolicyPath specifies an override location for the signature
|
|
|
|
// policy which should be used for verifying the new image as it is
|
|
|
|
// being written. Except in specific circumstances, no value should be
|
|
|
|
// specified, indicating that the shared, system-wide default policy
|
|
|
|
// should be used.
|
|
|
|
SignaturePolicyPath string
|
2017-06-29 05:07:58 +08:00
|
|
|
// github.com/containers/image/types SystemContext to hold information
|
|
|
|
// about which registries we should check for completing image names
|
|
|
|
// that don't include a domain portion.
|
|
|
|
SystemContext *types.SystemContext
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// NewBuilder creates a new build container.
|
2018-04-12 22:20:36 +08:00
|
|
|
func NewBuilder(ctx context.Context, store storage.Store, options BuilderOptions) (*Builder, error) {
|
|
|
|
return newBuilder(ctx, store, options)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 04:18:35 +08:00
|
|
|
// ImportBuilder creates a new build configuration using an already-present
|
|
|
|
// container.
|
2018-04-12 22:20:36 +08:00
|
|
|
func ImportBuilder(ctx context.Context, store storage.Store, options ImportOptions) (*Builder, error) {
|
|
|
|
return importBuilder(ctx, store, options)
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 05:38:41 +08:00
|
|
|
// ImportBuilderFromImage creates a new builder configuration using an image.
|
|
|
|
// The returned object can be modified and examined, but it can not be saved
|
|
|
|
// or committed because it is not associated with a working container.
|
2018-04-12 22:20:36 +08:00
|
|
|
func ImportBuilderFromImage(ctx context.Context, store storage.Store, options ImportFromImageOptions) (*Builder, error) {
|
|
|
|
return importBuilderFromImage(ctx, store, options)
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// OpenBuilder loads information about a build container given its name or ID.
|
2017-02-11 00:48:15 +08:00
|
|
|
func OpenBuilder(store storage.Store, container string) (*Builder, error) {
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := store.ContainerDirectory(container)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-24 06:56:48 +08:00
|
|
|
buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile))
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
2018-10-03 22:05:46 +08:00
|
|
|
return nil, errors.Wrapf(err, "error reading %q", filepath.Join(cdir, stateFile))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
b := &Builder{}
|
2018-10-03 22:05:46 +08:00
|
|
|
if err = json.Unmarshal(buildstate, &b); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing %q, read from %q", string(buildstate), filepath.Join(cdir, stateFile))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
2017-02-11 03:45:06 +08:00
|
|
|
if b.Type != containerType {
|
2018-10-03 22:05:46 +08:00
|
|
|
return nil, errors.Errorf("container %q is not a %s container (is a %q container)", container, Package, b.Type)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
b.store = store
|
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
|
|
|
b.fixupConfig()
|
2017-02-11 00:48:15 +08:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:32:16 +08:00
|
|
|
// OpenBuilderByPath loads information about a build container given a
|
|
|
|
// path to the container's root filesystem
|
2017-02-11 00:48:15 +08:00
|
|
|
func OpenBuilderByPath(store storage.Store, path string) (*Builder, error) {
|
|
|
|
containers, err := store.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
abs, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
2018-10-03 22:05:46 +08:00
|
|
|
return nil, errors.Wrapf(err, "error turning %q into an absolute path", path)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
builderMatchesPath := func(b *Builder, path string) bool {
|
2017-05-28 18:52:18 +08:00
|
|
|
return (b.MountPoint == path)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
for _, container := range containers {
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := store.ContainerDirectory(container.ID)
|
2017-03-16 04:42:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile))
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
2018-10-18 04:58:32 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
logrus.Debugf("error reading %q: %v, ignoring container %q", filepath.Join(cdir, stateFile), err, container.ID)
|
|
|
|
continue
|
|
|
|
}
|
2018-10-03 22:05:46 +08:00
|
|
|
return nil, errors.Wrapf(err, "error reading %q", filepath.Join(cdir, stateFile))
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
b := &Builder{}
|
2017-04-04 05:44:23 +08:00
|
|
|
err = json.Unmarshal(buildstate, &b)
|
2017-02-11 03:45:06 +08:00
|
|
|
if err == nil && b.Type == containerType && builderMatchesPath(b, abs) {
|
2017-02-11 00:48:15 +08:00
|
|
|
b.store = store
|
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
|
|
|
b.fixupConfig()
|
2017-02-11 00:48:15 +08:00
|
|
|
return b, nil
|
|
|
|
}
|
2018-10-03 22:05:46 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error parsing %q, read from %q: %v", string(buildstate), filepath.Join(cdir, stateFile), err)
|
|
|
|
} else if b.Type != containerType {
|
|
|
|
logrus.Debugf("container %q is not a %s container (is a %q container)", container.ID, Package, b.Type)
|
|
|
|
}
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
return nil, storage.ErrContainerUnknown
|
|
|
|
}
|
|
|
|
|
2017-03-16 04:44:52 +08:00
|
|
|
// OpenAllBuilders loads all containers which have a state file that we use in
|
|
|
|
// their data directory, typically so that they can be listed.
|
|
|
|
func OpenAllBuilders(store storage.Store) (builders []*Builder, err error) {
|
|
|
|
containers, err := store.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := store.ContainerDirectory(container.ID)
|
2017-03-16 04:44:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile))
|
2018-10-18 04:58:32 +08:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
logrus.Debugf("error reading %q: %v, ignoring container %q", filepath.Join(cdir, stateFile), err, container.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, errors.Wrapf(err, "error reading %q", filepath.Join(cdir, stateFile))
|
2017-03-16 04:44:52 +08:00
|
|
|
}
|
|
|
|
b := &Builder{}
|
2017-04-04 05:44:23 +08:00
|
|
|
err = json.Unmarshal(buildstate, &b)
|
2017-03-16 04:44:52 +08:00
|
|
|
if err == nil && b.Type == containerType {
|
|
|
|
b.store = store
|
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
|
|
|
b.fixupConfig()
|
2017-03-16 04:44:52 +08:00
|
|
|
builders = append(builders, b)
|
2018-10-03 22:05:46 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error parsing %q, read from %q: %v", string(buildstate), filepath.Join(cdir, stateFile), err)
|
|
|
|
} else if b.Type != containerType {
|
|
|
|
logrus.Debugf("container %q is not a %s container (is a %q container)", container.ID, Package, b.Type)
|
2017-03-16 04:44:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return builders, nil
|
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// Save saves the builder's current state to the build container's metadata.
|
|
|
|
// This should not need to be called directly, as other methods of the Builder
|
|
|
|
// object take care of saving their state.
|
2017-02-11 00:48:15 +08:00
|
|
|
func (b *Builder) Save() error {
|
|
|
|
buildstate, err := json.Marshal(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := b.store.ContainerDirectory(b.ContainerID)
|
2017-02-24 06:56:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 22:05:46 +08:00
|
|
|
if err = ioutils.AtomicWriteFile(filepath.Join(cdir, stateFile), buildstate, 0600); err != nil {
|
|
|
|
return errors.Wrapf(err, "error saving builder state to %q", filepath.Join(cdir, stateFile))
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|