Commit Graph

305 Commits

Author SHA1 Message Date
Nalin Dahyabhai 32c68951a4 imagebuildah: handle --manifest directly
When we're told to add built images to a manifest list, manipulate the
list ourselves, so that if we're creating a list, we won't have a
partially-populated list if some of the builds fail.

This also lets us include all of the platform information (including
variant info, which we can't sniff out after the fact) that we were
given when we started building the images.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-08-17 11:05:28 -04:00
Nalin Dahyabhai 8148edf47d executor: make sure imageMap is updated with terminatedStage
When the terminatedStage map has its entry for a stage set, make sure
that we set the corresponding entry in the imageMap while holding the
mutex for it, eliminating the sliver of time when the first is set but
the second isn't, which could bite StageExecutor.Execute(), which waits
for the first and then reads the second.

Make terminatedStage record the error if a stage doesn't complete
successfully, and have waitForStage() return that error if it's set.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-08-16 13:51:54 -04:00
Nalin Dahyabhai 34d6ee13b8 imagebuildah: move multiple-platform building internal
Move multiple-platform build juggling logic from the CLI wrapper
directly into the imagebuildah package, to make using it easier for
packages that consume us as a library.

This requires reading Dockerfiles into byte slices so that we can
re-parse them for each per-platform build, rather than parsing them
directly, as we used to, since building modifies the parsed tree.

When building for multiple platforms, prefix progress log messages with
the platform description.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-08-11 15:24:56 -04:00
openshift-ci[bot] 88f55c8f15
Merge pull request #3409 from ashley-cui/sshagent
Implement SSH RUN mount
2021-08-06 22:35:40 +00:00
Ashley Cui 3a5635f90b Implement SSH RUN mount
Allow ssh socket from host or certain ssh keys to be exposed to a
certain RUN instruction, but not any other instructions, as well as not
showing up in the final image.
This is done by spawining a new agent from buildah and mounting
the listening socket inside the run. SSH_AUTH_SOCK inside the container
will be set to the socket mountpoint. The defualt mountpoint is
/run/buildkit/ssh_agent.{i}

Signed-off-by: Ashley Cui <acui@redhat.com>
2021-08-06 09:00:06 -04:00
Nalin Dahyabhai ae08e01e49 bud: teach --platform to take a list
Add a pkg/parse.PlatformsFromOptions() which understands a "variant"
value as an optional third value in an OS/ARCH[/VARIANT] argument value,
which accepts a comma-separated list of them, and which returns a list
of platforms.

Teach "from" and "pull" about the --platform option and add integration
tests for them, warning if --platform was given multiple values.

Add a define.BuildOptions.JobSemaphore which an imagebuildah executor
will use in preference to one that it might allocate for itself.

In main(), allocate a JobSemaphore if the number of jobs is not 0 (which
we treat as "unlimited", and continue to allow executors to do).

In addManifest(), take a lock on the manifest list's image ID so that we
don't overwrite changes that another thread might be making while we're
attempting to make changes to it.  In main(), create an empty list if
the list doesn't already exist before we start down this path, so that
we don't get two threads trying to create that manifest list at the same
time later on.  Two processes could still try to create the same list
twice, but it's an incremental improvement.

Finally, if we've been given multiple platforms to build for, run their
builds concurrently and gather up their results.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-08-04 15:23:10 -04:00
Nalin Dahyabhai 14965cde13 imagebuildah.stageExecutor.prepare(): remove pseudonym check
In prepare(), don't check if the image name that it's passed is a
pseudonym for the result of a stage in the Dockerfile.  Its callers
already did that.

When execute() knows that the image it's told to use as a base is a
pseudonym for the result of another stage in the Dockerfile, force the
pull policy to "never" to prevent an error when --pull-always=true.

Make imagebuildah.Mount a type alias instead of its own type, since we
never needed it to be a distinct type.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-07-21 10:31:17 -04:00
Daniel J Walsh eadb10a123
Switch rusagelogfile to use options.Out
When vendoring into Podman, Buildah is output log-rusage on
the server side of podman-remote.  It is using os.Stdout rather
then options.Out by default.

[NO TESTS NEEDED] This can only be tested on the Podman side,
existing tests should show if this breaks anything.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-06-28 13:45:43 -04:00
Nalin Dahyabhai 054d13f062 imagebuildah: use the specified logger for logging preprocessing warnings
Remove some redundancy in preprocessContainerfileContents(), have it
search the $PATH for "cpp", and have it log warning messages containing
errors output by the preprocessor using the specified logrus logger.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-06-15 14:03:09 -04:00
Dan Čermák a6b3b6f672
Add rusage-logfile flag to optionally send rusage to a file
Currently the rusage is reported to stdout and rather cumbersome to parse. The
new flag rusage-logfile can be used to specify a file to which the log will be
written instead.

Signed-off-by: Dan Čermák <dcermak@suse.com>
2021-06-02 08:07:37 +02:00
Nalin Dahyabhai b361fe9a2b imagebuildah: redo step logging
* Don't try to count COMMIT as a step; it's very confusing and doesn't
  match the behavior of traditional docker build.
* Include the step count for the stage, which is easy if we're not
  trying to predict COMMIT, which we don't always do, because we don't
  always have to, in multi-stage builds.
* In multi-stage builds, prefix the stage number and stage count, which
  is fun to see when we're building stages in parallel.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-06-01 09:52:11 -04:00
Daniel J Walsh a9c22e1001
Don't blow up if cpp detects errors
Currently the /usr/bin/cpp will blow up if a user adds a
comment to a containerfile that is not a preprocessor.
Since the Containerfile.in could include other Containerfile
which may have comments, begining with `#` this can cause
problems.

If we just warn on these errors, we can successfully process
all of the containerfiles.

Fixes: https://github.com/containers/buildah/issues/3229

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-05-19 15:14:15 -04:00
Daniel J Walsh d7d078561c
Send logrus messages back to caller when building
We want Info, Warning and Debug logrus messages to be writen to the
buildah stderr. this way when podman-remote is using build, it will
get the messages back on the client side.

[NO TESTS NEEDED] Since this will be tested in Podman.

Fixes: https://github.com/containers/buildah/issues/3214

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-05-15 03:55:05 -04:00
OpenShift Merge Robot 162fbaf909
Merge pull request #3221 from rhatdan/error
Check earlier for bad image tags name
2021-05-14 07:06:33 -04:00
Daniel J Walsh eca0c9cda4
Check earlier for bad image tags name
Fixes: https://github.com/containers/buildah/issues/3134

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-05-12 17:07:14 -04:00
Valentin Rothberg 5aeecd2454 buildah bud: fix containers/podman/issues/10307
Fix a regression in `buildah bud`.  In case of a cache hit or an
intermediate image, the pull policy should be set to "never" so that we
enforce a local-only lookup without even attempting to reach out to a
registry.

The regression was detected in Podman which defaults to --layers=true
which ultimately broke when setting --pull or --pull-always.  In case of
a cache hit, the image reference refers to a local image which conflicts
with an "always" pull policy.

Also extend an existing bud test to make sure we don't regress on it in
the future.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-05-12 15:50:49 +02:00
Nalin Dahyabhai 9b59b480bf imagebuildah: ignore signatures when tagging images
We re-tag images produced for Dockerfiles which contain no instructions
by "copying" them from their current names to their destination names,
letting the lower storage libraries deduplicate them into the same image
record.

When the source included signatures, this would break because
containers-storage can't guarantee that the compressed version of the
blob it will produce for a given layer will have the same digest that
the version referenced by the manifest had, so the image library would
refuse to "copy" them.

When the source and destination are the same, though, the
RemoveSignatures option doesn't cause the signatures to be deleted, but
it does bypass that check in the image library, so toggling it on works
around the problem.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-05-04 20:47:22 -04:00
Valentin Rothberg a23a49b227 update to latest libimage
Update Buildah to the latest libimage.  Migrating Podman over to
libimage entailed a number of fixes and changes to libimage which
we need to account for in Buildah.

Most notably:

 * `(*Runtime).LookupImage()` now returns `storage.ErrImageUnknown`
   instead of `nil` in case no matching image is found.

 * `(*Runtime).LookupImage()` now does quite a bit more work finding
   a local image and will also look at the repotags (or digests) of
   all local images if needed.

 * The signature of `(*Runtime).RemoveImages()` was changed and now
   returns a slice of reports and errors.  The reports aggregate the
   data of a removed image which allows the function to be used by
   `podman image prune` which is also interested in the size of the
   removed data.  The slice of errors is also needed in Podman which
   needs to have a closer look at _all_ rmi errors in order to determine
   the appropriate exit code (Docker compat).

 * `libimage/types` has been removed.  Pull policies have been merged
   into already existing logic in `pkg/config`.

Please refer to containers/podman/pull/10147 for a more detailed
changelog.

[NO NEW TESTS NEEDED]

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-05-04 08:45:07 +02:00
Nalin Dahyabhai 8f08ca71f9 imagebuildah: replace archive with chrootarchive
Use the chrootarchive package in places where we where using its
non-chrooted counterpart before.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-04-27 16:54:32 -04:00
Nalin Dahyabhai 6eea0b9516 imagebuildah.StageExecutor.volumeCacheSaveVFS(): set up bind mounts
When building with non-overlay drivers, we archive the contents of
volumes before RUN instructions and restore their contents when the RUN
instruction is finished.

The buildah.Run() ("buildah run") logic defaults to creating a copy of
the contents of built-in volumes and bind-mounting them when running
subcommands, but does not restore the contents afterward because the
rootfs's contents aren't changed.  The rootfs's contents do get changed
by buildah.Add() ("buildah add/copy"), and the volumes go out of sync
with the rootfs because we don't really know what to do then.

The save/restore logic in imagebuildah for non-overlay paths wasn't
adding mounts to the list used by buildah.Run(), so it was continuing to
set up bind mounts even in cases where our cache was invalidated, which
produced this kind of loss of synchronization.  The overlay path wasn't
affected because it adds overlay mounts to the list we pass to
buildah.Run(), and buildah.Run() doesn't override mount points.  So to
avoid this for the VFS methods, add bind mounts to the rootfs, which
we're updating anyway.  They're basically redundant, but they work
around the problem.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-04-27 16:54:32 -04:00
Nalin Dahyabhai 8d4a5248fa imagebuildah: create volume directories when using overlays
Saving a volume cache using VFS had the side-effect of creating the
volume's directory, so have the overlay path for it do so as well.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-04-27 15:13:21 -04:00
Nalin Dahyabhai 395f467c31 imagebuildah: drop resolveSymlink()
resolveSymlink() was parsing the combined stdout and stderr from its
child process, which would include warning messages the storage library
printed to stderr during the child process's init() functions.
Instead of fixing just that, drop resolveSymlink() and replace it with a
call to copier.Eval().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-04-27 15:13:21 -04:00
OpenShift Merge Robot 9428d03194
Merge pull request #3177 from ashley-cui/secrets
Implement RUN secrets for buildah bud
2021-04-26 08:37:00 -04:00
Ashley Cui c8002d9739 Add support for secret mounts
Add support for secrets. Secrets is a two-part flag that allows secret files to
be accessed for a certain RUN instruction, but not any other
instructions, as well as now showing up in the final image.

Signed-off-by: Ashley Cui <acui@redhat.com>
2021-04-23 09:19:43 -04:00
Valentin Rothberg fb331c1861 restore push/pull and util API
Restore the push and pull API that commit dcd2a92e56 removed.
These changes would break vendoring into openshift/builder due
to build errors.

For the same reason, restore `util.FindImage` and `util.AddImageNames`
but deprecate the `findRegistry` argument.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-04-23 12:50:31 +02:00
OpenShift Merge Robot ad6b9902cc
Merge pull request #3161 from rhatdan/copier
Fix copier when using globs
2021-04-22 13:40:29 -04:00
Valentin Rothberg dcd2a92e56 use new containers/common/libimage package
Move all code related handling container image over to the new
`libimage` package in containers/common.  The new package is an
attempt to consolidate the code across the containers tools under the
github.com/containers umbrella.

The new `libimage` packages provides functionality to perform all kinds
of operations for managing images such as local lookups, pushing,
pulling, listing, removing, etc.

The following packages have been moved over the containers/common:

`manifests` -> `common/image/manifests`
`pkg/manifests` -> `common/pkg/manifests`
`pkg/supplemented` -> `common/pkg/supplemented`

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-04-22 17:39:00 +02:00
Daniel J Walsh 37e9d254cc
Fix copier when using globs
In Docker if you are copying more then one object, and
one of them is successful, then the command is successful. Currently in
buildah each glob has to be successful. This PR matches Buildah to
Docker.

Fixes: https://github.com/containers/podman/issues/9594

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-04-22 10:39:23 -04:00
Daniel J Walsh 73f2f84595
Output names of multiple tags in buildah bud
Buildah currently handles multiple tags when building, but does not
report it to the user by default. This reports the tags back to the
user.

Removed some unused code from commit.go, that would blow up if a user
specified AdditionalTags to the commit command, even though this is not
exposed to the user currently.  In a previous try to fix this, the
removed code was causing breakage, and I did not see a real purpose in
the code.

Fixes: https://github.com/containers/buildah/issues/3084

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-04-21 08:06:53 -04:00
TomSweeneyRedHat cc734103a0 Fix arg missing warning in bud
If a Containerfile had an ARG specified with a value and it was
referenced later in a multifrom scenario, a Warning would be raised
unless there was a `--build-arg` option specified for the argument.

This change removes the warning if the ARG has a value specified
in the Containerfile.  We still need the warning however, if it
is not specified via a `--build-arg` or within the Containerfile.

Fixes:  #3020

Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
2021-04-17 20:35:07 -04:00
Daniel J Walsh d3778cb784
Lookup Containerfile if user specifies a directory
Currently if the user specifies a --file path
and path is a directory, we only append on Dockerfile.
This PR searches for Containerfile and then Dockerfile.

Fixes: https://github.com/containers/buildah/issues/3078

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-03-15 07:57:30 -04:00
Nalin Dahyabhai ba11a4ceeb imagebuildah: use overlay for volumes when using overlay
When we're using the overlay driver (which means we know overlay is
available), use it to make volumes appear to be writeable during RUN
instructions instead of saving/restoring their contents.

This avoids having to copy the contents of the volume directory before
each RUN instruction, and having to remove and extract the contents
after each RUN instruction, which should be faster, particularly if the
amount of content in that volume location is large.

For empty directories, it will at least avoid adding an "opaque"
notation for the directory in a layer that might otherwise be empty.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2021-03-05 15:57:55 -05:00
Daniel J Walsh 514a3f1a91
Shrink the vendoring size of pkc/cli
This PR removes the pkg/auth which brings in docker/docker
since it really is not needed, and was only there to help users
discover the settings of where the authfile was, when the environment
variables were set.  Would almost never be of any value.

Move imagebuildah.BuildOptions to define.BuildOptions

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-03-03 14:18:35 -05:00
OpenShift Merge Robot 06d974b700
Merge pull request #3004 from jmguzik/support-for-chmod-flag-add-and-copy
Chmod flag for add and copy
2021-02-24 09:35:22 -05:00
OpenShift Merge Robot b51f63a3b6
Merge pull request #3036 from rhatdan/version
Make sure we set the buildah version label
2021-02-24 09:28:21 -05:00
Jakub Guzik d0917fa7e6 buildah add/copy --chmod dockerfile implementation
Signed-off-by: Jakub Guzik <jakubmguzik@gmail.com>
2021-02-24 08:40:04 +01:00
Daniel J Walsh 3b954c2989
Make sure we set the buildah version label
Currently if the buildah image has a label that was built with
an older version of buildah, we don't update to the latest version.

This PR Will cause the new version to use the current label.

Fixes:https://github.com/containers/buildah/issues/3035

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-02-23 16:31:25 -05:00
caiges 8a5447968b
Fix reaping of stages with no instructions
Signed-off-by: caiges <caigesn@gmail.com>
2021-02-19 23:28:48 -07:00
Daniel J Walsh 4704e6cb3f
Eliminate the use of containers/building import in pkg subdirs
We want to shrink the size of the import when importing pkg from
buildah. This should help us shrink the size of the golang bindings
in podman.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-02-12 12:43:08 -05:00
Antonio Terceiro ab8e351c60 Fix caching layers with build args
This fixes a regression introduced in
9b299588c0.

ib.Run() is only really needed in the ARG step. On all the other steps,
it can cause potentially expensive commands to be executed unecessarily.

Closes https://github.com/containers/buildah/issues/2992

Signed-off-by: Antonio Terceiro <antonio.terceiro@linaro.org>
Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
2021-02-10 14:42:22 -03:00
Valentin Rothberg 2301d148da --iidfile: print hash prefix
Prefix the image ID with the hash prefix when using `--iidfile` to be
compatible with Docker.  The absence of the hash can cause
docker-compose to error out.

Reported-in: github.com/containers/podman/issues/9260
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-02-08 12:24:56 +01:00
Daniel J Walsh 5b350b9a3f
Finish plumbing for buildah bud --manifest
Buildah bud --manifest XYZ was not working.

The manifest was never created. This PR Finishes
the plumbing and allows users to create a manifest
while building an image in one single command.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-02-05 12:36:48 -05:00
Urvashi Mohnani 28453dc3c3 Fix build arg check
Fix the check on build args to be the length of the map
and not whether the map is nil. The nil check was causing
the cache layer to not be used as it would give a false
result.

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
2021-02-04 14:14:59 -05:00
Urvashi Mohnani 9b299588c0 Rebuild layer if a change in ARG is detected
Check whether the ARG in the containerfile is changed by
either the --build-arg flag or local environment and use
the cached layer or rebuild the layer accordingly.
Add tests for this use case as well.

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
2021-01-28 08:29:19 -05:00
Daniel J Walsh 7acdfe8e8d
Add support for --manifest flags
This patch allows users to build a multi arch image with simple commands
using emulation software.

```
buildah bud --arch arm --manifest myimage /tmp/mysrc
buildah bud --arch amd64 --manifest myimage /tmp/mysrc
buildah bud --arch s390x --manifest myimage /tmp/mysrc
```

And something like this for buildah commit
```
build() {
	ctr=$(./bin/buildah from --arch $1 ubi8)
	./bin/buildah run $ctr dnf install -y iputils
	./bin/buildah commit --manifest ubi8ping $ctr
}
build arm
build amd64
build s390x
```

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-01-07 14:41:02 -05:00
TomSweeneyRedHat 7724292a4d Allow FROM to be overriden with from option
These changes will allow the "FROM" statement in a Containerfile
to be overridden with a new `--from` option.  If I have this Dockerfile.fedora

```
FROM fedora
```

This command will instead build an alpine image:

```
STEP 1: FROM alpine
Completed short name "alpine" with unqualified-search registries (origin: /etc/containers/registries.conf)
Getting image source signatures
Copying blob 188c0c94c7c5 done
Copying config d6e46aa247 done
Writing manifest to image destination
Storing signatures
STEP 2: COMMIT tom
--> d6e46aa2470
d6e46aa2470df1d32034c6707c8041158b652f38d2a9ae3d7ad7e7532d22ebe0
```

Addresses: #2404

Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
2020-12-17 19:30:53 -05:00
Daniel J Walsh fde2597af8
Add --ignorefile flag to use alternate .dockerignore flags
Allow users to override location of the .dockerignore file.
If user specified an --ignorefile flag, buildah will read the
file and pass in the exclude lines based on the .dockerignore
contents.

Also add a --contextdir flag to buildah copy and buildah add to
specify where the context directory is located.  For these two
commands the --ignorefile flag requires the --contextdir flag.

When the --ignorefile flag is passed in, the .dockerignore files
in the context directory will be ignored.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2020-11-24 15:28:07 -05:00
Nalin Dahyabhai e863665192 imagebuildah: disable pseudo-terminals for RUN
Always handle RUN instructions with no pseudo terminal, which matches
what I see with docker build 19.03.  Interactive 'buildah run' will
still have the same default behavior.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-11-17 11:24:50 -05:00
Nalin Dahyabhai a9c340541d intermediateImageExists: ignore images whose history we can't read
When attempting to read the history of an image in order to try to check
if it's suitable as a cache entry for an image we're building, if we
fail to read its history, ignore the image instead of failing.

If the image was pulled into local storage for a different architecture,
or it's a list with no entry in it for the local architecture, or if
it's a list which references an image for the local architecture that we
haven't pulled down, we'll get an error back from the image library, and
we don't want to fail because of that.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-11-16 17:56:18 -05:00
OpenShift Merge Robot 5368ec3211
Merge pull request #2764 from edigaryev/fix-build-dockerfiles-npe
Fix NPE when Dockerfile path contains non-directory entries
2020-11-10 22:14:30 +01:00