Commit Graph

227 Commits

Author SHA1 Message Date
Justin Chadwell
4dcbc22d31 chore: move linux-specific oci spec to spec_linux.go
These functions are unused on windows and so cause linting issues.

Signed-off-by: Justin Chadwell <me@jedevc.com>
2023-11-02 10:45:34 +00:00
Justin Chadwell
a9b4dd5090 chore: fix windows variable naming issues
Signed-off-by: Justin Chadwell <me@jedevc.com>
2023-11-02 10:45:34 +00:00
Sebastiaan van Stijn
253c678a3f migrate to github.com/moby/sys/user
This migrates uses of github.com/opencontainers/runc/libcontainer/user
to the new github.com/moby/sys/user module, which was extracted from
runc at commit [opencontainers/runc@a3a0ec4].

This is the initial release of the module, which is a straight copy, but
some changes may be made in the next release (such as fixing camel-casing
in some fields and functions (Uid -> UID).

[opencontainers/runc@a3a0ec4]: a3a0ec48c4

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-10-26 23:31:02 +02:00
Mark Yen
f9ccb09ef9 Rename cgroupNamespaceSupported, add details
This responds to review feedback from
https://github.com/moby/buildkit/pull/4308#pullrequestreview-1658246195

Signed-off-by: Mark Yen <mark.yen@suse.com>
2023-10-04 14:28:12 -07:00
Mark Yen
d48bf06e18 Don't support cgroupns on cgroups v1
Fixes #4108

Signed-off-by: Mark Yen <mark.yen@suse.com>
2023-10-03 16:58:10 -07:00
Justin Chadwell
8bfd280ab7 Add options to specify containerd runtime
Co-authored-by: Marat Radchenko <marat@slonopotamus.org>
Signed-off-by: Justin Chadwell <me@jedevc.com>
2023-09-26 14:20:35 +01:00
CrazyMax
0074631e0e chore: use bklog instead of logrus
Co-authored-by: Justin Chadwell <me@jedevc.com>
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-09-05 12:01:22 +01:00
Gabriel Adrian Samfira
cc5657c600 Cleanup args, rename jobDetails
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2023-08-30 13:40:43 +03:00
Gabriel Adrian Samfira
b6b322b327 Implement executor on Windows
This change splits the containerdexecutor.Run() function into smaller
pieces and enables it to run on Windows.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2023-08-15 12:35:45 +03:00
Tõnis Tiigi
408e43248f Merge pull request #2376 from akhramov/feature/freebsd-port
FreeBSD port
2023-08-02 12:11:34 +03:00
Erik Sipsma
fa11bf9e57 executor: fix resource sampler goroutine leak
Before this, the runc executor did not close the cgroupRecord when the
container exited non-zero, which resulted in goroutines leaking.

Signed-off-by: Erik Sipsma <erik@sipsma.dev>
2023-07-31 07:52:54 -07:00
Artem Khramov
c415d85fe0 FreeBSD port
Buildkit code is mostly generic enough to support FreeBSD, however
there are some quirks / infrastructural pieces that need to be
addressed for full support, to name some

-    contenthash.NewFromStat attempts to set Devmajor / Devminor for
    regular files, assuming that RDev is zero for regular
    files. Unlike on Linux, it's not the case for FreeBSD.

-    containerdexecutor.Run uses bind mounts for rootfs. Bind mounts
    are not supported in FreeBSD and we should use nullfs instead

-    There is no CI job to run tests on FreeBSD

-    Some dependencies weren't ported

This change ports buildkit to FreeBSD

Signed-off-by: Artem Khramov <akhramov@pm.me>
Co-authored-by: Akihiro Suda <suda.kyoto@gmail.com>
2023-07-28 17:02:53 +03:00
Sebastiaan van Stijn
ce332e1952 executor/resource: stub out NewSysSampler on Windows
Commit 509cfa3916 introduced the SysSampler,
which measures resource consumption. However, for this it depends on
prometheus' procfs. That package does not have build-tags but is a Linux-only
implementation, which (by default) attempts to access `/proc`;
https://github.com/prometheus/procfs/blob/v0.9.0/fs.go#L26-L33
https://github.com/prometheus/procfs/blob/v0.9.0/internal/fs/fs.go#L23-L24

This patch splits the implementation of "resource" into platform-specific
files, and stubs out the NewSysSampler() on non-Linux platforms.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-20 14:28:53 +02:00
Tõnis Tiigi
f21a96cdff Merge pull request #4003 from tonistiigi/oci-cgroupns
oci: make sure cgroupns is enabled if supported
2023-07-10 13:57:51 -07:00
Alex Couture-Beil
86a740ff8c use sampler lock instead
Signed-off-by: Alex Couture-Beil <alex@earthly.dev>
2023-07-10 10:57:32 -07:00
Alex Couture-Beil
4f0a1f88e3 sampler data race fix
Protect against data race occuring while both sampler.Run and sub.Close
accesses the same data from multiple threads.

Signed-off-by: Alex Couture-Beil <alex@earthly.dev>
2023-07-10 09:14:49 -07:00
Tonis Tiigi
c96364913e oci: make sure cgroupns is enabled if supported
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-07-07 15:15:55 -07:00
Tonis Tiigi
fca346fff8 allow ENOTSUP for PSI cgroup files
If kernel is configured with CONFIG_PSI_DEFAULT_DISABLED then PSI
files exist but are unreadable.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-07-06 22:49:39 -07:00
Tonis Tiigi
8ffc03b8f0 move flightcontrol to use generics
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-06-28 23:44:05 -07:00
Tonis Tiigi
262b70801e resources: make maxsamples configurable
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-06-08 15:52:03 -07:00
Tonis Tiigi
509cfa3916 llbsolver: add systemusage samples to provenance attestation
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-06-08 15:52:02 -07:00
Tonis Tiigi
32dcdff1a0 resources: store sys cpu usage per step
This can be used to convert step usage to relative units.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-06-08 15:51:35 -07:00
Tonis Tiigi
6a2f92d08f resources: add sampler for periodic stat reads
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-06-08 15:51:35 -07:00
Tonis Tiigi
963f16179f resources: CNI network usage sampling support
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-06-08 15:51:35 -07:00
Tonis Tiigi
6e87e4b455 resources: add build step resource tracking via cgroups
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-06-08 15:51:31 -07:00
Erik Sipsma
9b0bdb6006 Prevent overflow of runc exit code.
It's possible for the Status field of runc.ExitError to be set to -1, in
which case conversion to uint32 results in the error message to say that
the container exited with code 4294967295 (2^32-1).

Signed-off-by: Erik Sipsma <erik@sipsma.dev>
2023-03-31 19:31:55 -07:00
coryb
6073f5821e runc worker: fix sigkill handling
This fixes the incorrect kill handling introduced in
b76f8c0248.  We need to send the
SIGKILL to the in-container process, not the runc process.  This patch
adds an abstraction over the kill handling:
  * for `runc run` processes use `runc kill`
  * for `runc exec` processes, read pid (in host PID namespace) from
    pidfile created by `runc exec`, then send the signal directly to
    that process.
Also use the kill abstraction when we receive a SIGKILL over the
signal channel for containers created by gateway NewContainer

Signed-off-by: coryb <cbennett@netflix.com>
2023-03-31 12:56:41 -07:00
coryb
b76f8c0248 fix process termination handling for runc exec
This patch makes the process handling consistent between runc.Run and
runc.Exec usage.  Previously runc.Run would use context.Background
for the runc.Run process and would monitor the request context for
shutdown requests, sending a SIGKILL to the container pid1 process. This
allowed runc.Run to gracefully shutdown and reap child processes.  This
logic was not used for runc.Exec where instead we were passing in the
request context to runc.Exec, and if that request context was cancelled
the runc process would immediately terminate preventing runc from reaping
the child process.  In this scenario the extra pid will remain forever
and then when the pid1 process will get wedged in zap_pid_ns_processes
syscall upon shutdown waiting fo the zombie pid to exit.

With this fix both runc.Run and runc.Exec will use context.Background
for runc processes and monitor the request context for shutdown request
triggering a SIGKILL to the pid being monitored by runc.

Signed-off-by: coryb <cbennett@netflix.com>
2023-03-17 12:46:19 -07:00
coryb
a8aa7b200c use bklog.G(ctx) instead of logrus directly
Signed-off-by: coryb <cbennett@netflix.com>
2023-03-15 12:07:44 -07:00
Akihiro Suda
2fed51aea3 rootless: fix up unprivileged mount opts
Port https://github.com/moby/moby/blob/v23.0.1/daemon/oci_linux.go#L430-L460

> // Get the set of mount flags that are set on the mount that contains the given
> // path and are locked by CL_UNPRIVILEGED. This is necessary to ensure that
> // bind-mounting "with options" will not fail with user namespaces, due to
> // kernel restrictions that require user namespace mounts to preserve
> // CL_UNPRIVILEGED locked flags.

Fix issue 3098

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2023-03-09 21:09:58 +09:00
Tõnis Tiigi
9ea86dddc8 Merge pull request #3544 from gabriel-samfira/fix-tracing-listener
[Windows] Fix tracing listener
2023-03-08 15:46:40 +00:00
Akihiro Suda
a0ef27c593 Ensures that the primary GID is also included in the additional GIDs
Apply `ensureAdditionalGids()` from 3eda46af12

(CVE-2023-25173, https://github.com/advisories/GHSA-hmfx-3pcx-653p)

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2023-02-18 00:20:46 +09:00
Gabriel Adrian Samfira
b133b13652 Add platform tracing socket paths and mounts
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2023-02-09 11:42:11 -08:00
Gabriel Adrian Samfira
7d6bee2627 Fix tracing listener on Windows
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2023-01-29 03:37:15 +02:00
Tõnis Tiigi
30cd3b4c0c Merge pull request #3142 from thaJeztah/simplify_resolvconf
executor/oci: GetResolvConf(): simplify handling of resolv.conf
2023-01-05 20:53:52 -08:00
Tõnis Tiigi
e0220af506 Merge pull request #3254 from corhere/c8dexecutor-inprocess-lifecycle-hook
[RFC] containerdexecutor: add network namespace callback
2022-12-22 12:44:31 -08:00
Cory Snider
b5fdf90aee containerdexecutor: add network namespace callback
In order to support identity mapping and user namespaces, the Moby
project needs to defer the creation of a container's network namespace
to the runtime and hook into the container lifecycle to configure the
network namespace before the user binary is started. The standard way to
do so is by configuring a `createRuntime` OCI lifecycle hook, in which
the OCI runtime executes a specified process in the runtime environment
after the container has been created and before it is started. In the
case of Moby the network namespace needs to be configured from the
daemon process, which necessitates that the hook process communicate
with the daemon process. This is complicated and slow. All the hook
process does is inform the daemon of the container's PID and wait until
the daemon has finished applying the network namespace configuration.

There is an alternative to the `createRuntime` OCI hook which containerd
clients can take advantage of. The `container.NewTask` method is
directly analogous to the OCI create operation, and the `task.Start`
method is directly analogous to the OCI start operation. Any operations
performed between the `NewTask` and `Start` calls are therefore directly
analogous to `createRuntime` OCI hooks, without needing to execute any
external processes! Provide a mechanism for network.Namespace instances
to register a callback function which can be used to configure a
container's network namespace instead of, or in addition to,
`createRuntime` OCI hooks.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-12-08 19:46:23 -05:00
Justin Chadwell
6b265b1bae gateway: add RemoveMountStubsRecursive to exec meta options
This allows a frontend to request a specific for stubs removal.

By default, if not specified, this will revert to the previous
behaviour. New gateway clients however will set the property to the
desired recursive removal mode.

This property needs to be set for both components that call the
executor: for ExecOp, as well as for the StartContainer API.

Signed-off-by: Justin Chadwell <me@jedevc.com>
2022-12-07 19:06:47 +00:00
Justin Chadwell
2cae26f618 executor: stubs cleaner should recursively remove empty directory mounts
Follows-up removing directory stub mounts by following up the parent
chain, and removing any additionally created directories.

Signed-off-by: Justin Chadwell <me@jedevc.com>
2022-11-24 13:15:51 +00:00
Justin Chadwell
6778973776 executor: stubs cleaner should remove empty directory mounts
On Linux, an empty directory is usually 4096 bytes, not 0, so we need an
additional explicit check here.

Signed-off-by: Justin Chadwell <me@jedevc.com>
2022-11-23 18:13:22 +00:00
Akihiro Suda
0b5a315c22 MountStubsCleaner: preserve timestamps
Fix issue 3148

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2022-11-18 18:31:38 +09:00
Cory Snider
f448313827 containerdexecutor: clean up task if Start() fails
Deleting a containerd task whose status is Created fails with a
"precondition failed" error. This is because (aside from Windows) a
process is spawned when the task is created, and deleting the task while
the process is running would leak the process if it was allowed.
Change the deferred `task.Delete` call to pass the `WithProcessKill`
delete option so the cleanup has a chance to succeed in the event that
the `p.Start` call inside `runProcess` returns an error.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-02 18:29:34 -04:00
Akihiro Suda
bd57e5f6b8 Make SELinux labels opt-in (--oci-worker-selinux=<BOOL>)
Fix issue 3202

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2022-11-02 14:33:01 +09:00
Sebastiaan van Stijn
f04dadd761 executor/oci: GetResolvConf(): simplify handling of resolv.conf
We only need the content here, not the checksum, so simplifying the code by
just using os.ReadFile(), using resolvconf.Path() for the location.

Also reversing the logic for custom options; The existing code was always
parsing the host's resolv.conf to read the nameservers, searchdomain and
options, but those options were only needed if they were not configured in
DNSConfig. This patch reverses the logic to only parse the resolv.conf if
no options are present in the passed DNSConfig.

There's some more optimisations to make (but changes in libnetwork are needed
for that); resolvconf.FilterResolvDNS() still parse the resolv.conf file, even
if we just generated it (in which case we're generating a resolv.conf, after
which we're parsing the generated file again to update it, which is not ideal).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-02 02:18:05 +02:00
Aaron Lehmann
588a72d04c Add network namespace pool support
This adds netNSPoolSize pool options which allow setting a target
network namespace pool size. buildkitd will create this number of
network namespaces at startup (without blocking). When a container
execution finishes, the network namespace gets returned to the pool. If
the pool goes above the target size, there is a grace period to allow
network namespaces to be reused, and if this passes without reuse, the
extra namespaces will be released.

Signed-off-by: Aaron Lehmann <alehmann@netflix.com>
2022-09-14 07:08:31 -07:00
Alex Suraci
e3c9e064a9 cniprovider: pass hostname as K8S_POD_NAME arg
Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
2022-08-21 15:43:12 -04:00
Eng Zer Jun
be6501b654 test: use T.TempDir to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-07-24 02:27:26 +08:00
Sebastiaan van Stijn
dfb08e32ac executor/oci: replace deprecated types.IP
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-05-10 22:53:12 +02:00
CrazyMax
574d992882 lint: fix deprecated
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2022-04-18 18:47:17 +02:00
Sebastiaan van Stijn
21e9e9641e Remove uses of deprecated io/ioutil
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-10 00:57:07 +01:00