Commit Graph

94 Commits

Author SHA1 Message Date
Wei Fu
ee0ed75d64 internal/cri: simplify netns setup with pinned userns
Motivation:

For pod-level user namespaces, it's impossible to force the container runtime
to join an existing network namespace after creating a new user namespace.

According to the capabilities section in [user_namespaces(7)][1], a network
namespace created by containerd is owned by the root user namespace. When the
container runtime (like runc or crun) creates a new user namespace, it becomes
a child of the root user namespace. Processes within this child user namespace
are not permitted to access resources owned by the parent user namespace.

If the network namespace is not owned by the new user namespace, the container
runtime will fail to mount /sys due to the [sysfs: Restrict mounting sysfs][2]
patch.

Referencing the [cap_capable][3] function in Linux, a process can access a
resource if:

* The resource is owned by the process's user namespace, and the process has
the required capability.

* The resource is owned by a child of the process's user namespace, and the
owner's user namespace was created by the process's UID.

In the context of pod-level user namespaces, the CRI plugin delegates the
creation of the network namespace to the container runtime when running the
pause container. After the pause container is initialized, the CRI plugin pins
the pause container's network namespace into `/run/netns` and then executes
the `CNI_ADD` command over it.

However, if the pause container is terminated during the pinning process, the
CRI plugin might encounter a PID cycle, leading to the `CNI_ADD` command
operating on an incorrect network namespace.

Moreover, rolling back the `RunPodSandbox` API is complex due to the delegation
of network namespace creation. As highlighted in issue #10363, the CRI plugin
can lose IP information after a containerd restart, making it challenging to
maintain robustness in the RunPodSandbox API.

Solution:

Allow containerd to create a new user namespace and then create the network
namespace within that user namespace. This way, the CRI plugin can force the
container runtime to join both the user namespace and the network namespace.
Since the network namespace is owned by the newly created user namespace,
the container runtime will have the necessary permissions to mount `/sys` on
the container's root filesystem. As a result, delegation of network namespace
creation is no longer needed.

NOTE:

* The CRI plugin does not need to pin the newly created user namespace as it
does with the network namespace, because the kernel allows retrieving a user
namespace reference via [ioctl_ns(2)][4]. As a result, the podsandbox
implementation can obtain the user namespace using the `netnsPath` parameter.

[1]: <https://man7.org/linux/man-pages/man7/user_namespaces.7.html>
[2]: <7dc5dbc879>
[3]: <2c85ebc57b/security/commoncap.c (L65)>
[4]: <https://man7.org/linux/man-pages/man2/ioctl_ns.2.html>

Signed-off-by: Wei Fu <fuweid89@gmail.com>
2024-09-11 07:21:43 +08:00
Maksym Pavlenko
26b48a6b7a
Merge pull request #10594 from AkihiroSuda/cri-remove-disableCgroup
CRI: remove `disable_cgroup`
2024-09-03 18:25:42 +00:00
James Sturtevant
f6677a4ec5
Cumulative stats can't decrease
During removal of the container a stat value might be reported as zero; in this case the caluclation could end up with an extremely large number.  If the cumulative stat decreases report zero.

Signed-off-by: James Sturtevant <jstur@microsoft.com>
2024-08-28 23:23:30 +00:00
Akihiro Suda
6f3833f258
CRI: remove disable_cgroup
`disable_cgroup` was implemenetd in containerd/cri PR 970 (Nov 2018)
for supporting very early version of Usernetes on cgroup v1 hosts,
when most distros were still not ready to support cgroup v2.

This configuration is no longer needed, as cgroup v2 delegation is
now supported on almost all distros.

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2024-08-15 06:08:30 +09:00
Akhil Mohan
ebc47359ea
use format string when using printf like commands
As per https://github.com/golang/go/issues/60529, printf like commands with
non-constant format strings and no args give an error in govet

Signed-off-by: Akhil Mohan <akhilerm@gmail.com>
2024-08-14 17:04:53 +05:30
Maksym Pavlenko
0b02e0c225
Merge pull request #7616 from swagatbora90/trace-cri-runtime
Add tracing spans to CRI runtime service apis
2024-08-09 18:24:47 +00:00
Sebastiaan van Stijn
9776047243
migrate to github.com/moby/sys/userns
Commit 8437c567d8 migrated the use of the
userns package to the github.com/moby/sys/user module.

After further discussion with maintainers, it was decided to move the
userns package to a separate module, as it has no direct relation with
"user" operations (other than having "user" in its name).

This patch migrates our code to use the new module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-08-08 12:48:54 +02:00
Swagat Bora
83c26962c2 Remove extra span created in the instrumented service layer
Signed-off-by: Swagat Bora <sbora@amazon.com>
2024-08-05 02:34:27 +00:00
Swagat Bora
c0cdcb34f1 Add spans to CRI runtime service and related client methods
This adds otel spans to CRI service mainly targeting mutating apis which includes:
* Sandbox apis - RunPodSandbox, StopPodSandbox, RemovePodSandbox
* Container apis - CreateContainer, StartContainer, StopContainer, RemoveContainer
* Attach, Exec and Exec Sync
* Containerd client methods: container.go, client.go, process.go and task.go

Signed-off-by: Swagat Bora <sbora@amazon.com>
2024-08-05 02:34:20 +00:00
Maksym Pavlenko
fec24e209d
Merge pull request #10520 from saschagrunert/cri
Make `StopPodSandbox` RPC idempotent
2024-07-30 20:12:50 +00:00
Sascha Grunert
a97b11898a
Make StopContainer RPC idempotent
Similar to container removal, the stop of a container should be a noop if
the container has not been found.

Found during: https://github.com/kubernetes-sigs/cri-tools/pull/1536

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2024-07-30 11:47:25 +02:00
Sascha Grunert
c6cea95d95
Make StopPodSandbox RPC idempotent
Similar to sandbox removal, the stop of a sandbox should be a noop if
the sandbox has not been found.

Found during: https://github.com/kubernetes-sigs/cri-tools/pull/1535

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2024-07-30 10:40:16 +02:00
Sebastiaan van Stijn
8437c567d8
pkg/userns: deprecate and migrate to github.com/moby/sys/user/userns
The userns package in libcontainer was integrated into the moby/sys/user
module at commit [3778ae603c706494fd1e2c2faf83b406e38d687d][1].

This patch deprecates the containerd fork of that package, and adds it as
an alias for the moby/sys/user/userns package.

[1]: 3778ae603c

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-07-26 09:47:50 +02:00
Shingo Omura
ee2c0d9e74
KEP-3619: implement features.supplemental_groups_policy in RuntimeStatus
Signed-off-by: Shingo Omura <everpeace@gmail.com>
2024-07-23 01:01:36 +09:00
Eric Lin
807f32573f cri: optimize ListPodSandboxStats with parallelism
Signed-off-by: Eric Lin <linxiulei@gmail.com>
2024-07-12 09:02:33 +00:00
Maksym Pavlenko
ac0f34f0ec
Merge pull request #10378 from akhilerm/use-marshall-from-type-url
use typeurl funcs for marshalling anypb.Any
2024-07-11 16:15:20 +00:00
Davanum Srinivas
0772d0a37b
Fix for [cri] ttrpc: closed during ListPodSandboxStats
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
2024-07-10 20:42:06 -04:00
Akhil Mohan
300fd770a0
use typeurl funcs for marshalling anypb.Any
Signed-off-by: Akhil Mohan <akhilerm@gmail.com>
2024-07-10 22:26:27 +05:30
Fu Wei
1fb1882c7d
Merge pull request #10375 from linxiulei/podstats
cri: get pid count from container metrics
2024-07-01 21:32:17 +00:00
Samuel Karp
ebcbbe5643
Merge pull request #10401 from samuelkarp/nri-panic
cri: ensure NRI API never has nil CRI
2024-07-01 16:37:46 +00:00
Eric Lin
f6e731c809 cri: get pid count from container metrics
This reduces latency of calling ListPodSandboxStats() by avoiding calling
shim API Task().

Signed-off-by: Eric Lin <linxiulei@gmail.com>
2024-07-01 15:34:47 +00:00
Fu Wei
f5f11658c5
Merge pull request #9903 from abel-von/add-update-resource-only
sandbox: Add Update API for sandbox controller
2024-07-01 15:16:21 +00:00
Samuel Karp
10aec359a0
cri: ensure NRI API never has nil CRI
A nil CRIImplementation field can cause a nil pointer dereference and
panic during startup recovery.

Prior to this change, the nri.API struct would have a nil cri
(CRIImplementation) field after nri.NewAPI until nri.Register was
called.  Register is called mid-way through initialization of the CRI
plugin, but recovery for containers occurs prior to that.  Container
recovery includes establishing new exit monitors for existing containers
that were discovered.  When a container exits, NRI plugins are given the
opportunity to be notified about the lifecycle event, and this is done
by accessing that CRIImplementation field inside the nri.API.  If a
container exits prior to nri.Register being called, access to the
CRIImplementation field can cause a panic.

Here's the call-path:

* The CRI plugin starts running
  [here](ae71819c4f/pkg/cri/server/service.go (L222))
* It then [calls into](ae71819c4f/pkg/cri/server/service.go (L227))
  `recover()` to recover state from previous runs of containerd
* `recover()` then attempts to recover all containers through
  [`loadContainer()`](ae7d74b9e2/internal/cri/server/restart.go (L175))
* When `loadContainer()` finds a container that is still running, it waits
  for the task (internal containerd object) to exit and sets up
  [exit monitoring](ae7d74b9e2/internal/cri/server/restart.go (L391))
* Any exit that then happens must be
  [handled](ae7d74b9e2/internal/cri/server/events.go (L145))
* Handling an exit includes
  [deleting the Task](ae7d74b9e2/internal/cri/server/events.go (L188))
  and specifying [`nri.WithContainerExit`](ae7d74b9e2/internal/cri/nri/nri_api_linux.go (L348))
  to [notify](ae7d74b9e2/internal/cri/nri/nri_api_linux.go (L356))
  any subscribed NRI plugins
* NRI plugins need to know information about the pod (not just the sandbox),
  so before a plugin is notified the NRI API package
  [queries the Sandbox Store](ae7d74b9e2/internal/cri/nri/nri_api_linux.go (L232))
  through the CRI implementation
* The `cri` implementation member field in the `nri.API` struct is set as part of the
  [`Register()`](ae7d74b9e2/internal/cri/nri/nri_api_linux.go (L66)) method
* The `nri.Register()` method is only called
  [much further down in the CRI `Run()` method](ae71819c4f/pkg/cri/server/service.go (L279))

Signed-off-by: Samuel Karp <samuelkarp@google.com>
2024-06-28 15:32:11 -07:00
Sebastiaan van Stijn
dd0542f7c1
cmd: don't alias context package, and use cliContext for cli.Context
Unfortunately, this is a rather large diff, but perhaps worth a one-time
"rip off the bandaid" for v2. This patch removes the use of "gocontext"
as alias for stdLib's "context", and uses "cliContext" for uses of
cli.context.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-06-20 02:15:13 +02:00
Abel Feng
15887d7efc sandbox: add update api for controller
Signed-off-by: Abel Feng <fshb1988@gmail.com>
2024-06-14 02:31:51 +00:00
Mike Brown
98ebb3ecff
Merge pull request #9737 from everpeace/kep-3169-SupplementalGroupsPolicy
KEP-3619: Fine-grained SupplementalGroups control
2024-06-13 16:59:20 +00:00
Reinhard Tartler
09d3e20d3a Allow running test in usernamespace
Debian has started building packages with usernamespaces
to disable network access and similar isolation features. The
containerd package executes a unit test that fails in that
scenario, see https://bugs.debian.org/1070411

The code contains a conditional on whether it is running in
usernamepsace. This commit expands the unit test to cover
this behavior; it was previously untested.

The easiest way to reproduce this issue is to prefix the test
invocaiton with 'unshare -nr go test [...]'

Signed-off-by: Reinhard Tartler <siretart@gmail.com>
2024-06-12 00:36:54 +00:00
Shingo Omura
8bcffa9446
KEP-3619: Fine grained SupplementalGroups control
Signed-off-by: Shingo Omura <everpeace@gmail.com>
2024-06-12 09:10:40 +09:00
Akihiro Suda
1529259077
Merge pull request #10275 from dmcgowan/imgcrypt-revendor
Re-enable imgcrypt
2024-05-29 16:54:47 +00:00
Fu Wei
5d2c988a5a
Merge pull request #10201 from abel-von/retry-remote-sandbox-wait
sandbox: do retry for wait to remote sandbox controller
2024-05-29 00:00:29 +00:00
Derek McGowan
da1d9672f6
Enable imgcrypt in cri pull
Signed-off-by: Derek McGowan <derek@mcg.dev>
2024-05-28 13:50:06 -07:00
Maksym Pavlenko
6383a1ce76
Merge pull request #10258 from bzsuni/bz/update/pause
update pause image to 3.10
2024-05-28 18:01:23 +00:00
Sebastiaan van Stijn
446e63579c
remove uses of platforms.Platform alias
Commit 3c8469a782 removed uses of the api
types.Platform type from public interfaces, instead using the type from
the OCI image spec.

For convenience, it also introduced an alias in the platforms package.
While this alias allows packages that already import containerd's
platforms package (now a separate module), it may also cause confusion
(it's not clear that it's an alias for the OCI type), and for packages
that do not depend on containerd's platforms package / module may now
be resulting in an extra dependency.

Let's remove the use of this alias, and instead use the OCI type directly.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-05-28 14:56:30 +02:00
bzsuni
22f2af40c0 update pause image to 3.10
Signed-off-by: bzsuni <bingzhe.sun@daocloud.io>
2024-05-25 08:17:46 +08:00
Mike Brown
87bab6cdc7
Merge pull request #10238 from MikeZappa87/feature/provideinternalloup
Add support to set loopback to up
2024-05-20 14:19:43 +00:00
Michael Zappa
332caf1a15 Provide ability to set lo up without CNI
Signed-off-by: Michael Zappa <michael.zappa@gmail.com>
2024-05-17 14:34:55 -06:00
Kazuyoshi Kato
24c2ae8115
Merge pull request #10026 from ningmingxiao/info_exited_event
internal/cri: log ctr's exit event on info level
2024-05-17 01:28:43 +00:00
Kazuyoshi Kato
ff66ec7a73
Merge pull request #10105 from fidencio/topic/fix-typos-in-image_pull_test.go
images: tests: Fix typos in the tests
2024-05-17 01:27:46 +00:00
Abel Feng
7cead88004 cri: restart created container with correct io type
Signed-off-by: Abel Feng <fshb1988@gmail.com>
2024-05-13 17:42:58 +08:00
Abel Feng
58be881890 sandbox: do retry for wait to remote sandbox controller
remote sandbox controller may restart, the Wait call should be retried
if it is an grpc disconnetion error.

Signed-off-by: Abel Feng <fshb1988@gmail.com>
2024-05-10 10:18:42 +08:00
Fu Wei
313fc12b8a
Merge pull request #9965 from abel-von/streaming-io
cri: support io by streaming API
2024-05-07 14:22:12 +00:00
Abel Feng
b8dfb4d8f5 cri: support io by streaming api
Signed-off-by: Abel Feng <fshb1988@gmail.com>
2024-05-07 20:26:40 +08:00
Phil Estes
04c7d6ccbf
Merge pull request #9960 from adrianreber/2024-03-12-criu-not-found
Return correct error if CRIU binary is missing
2024-05-06 19:41:38 +00:00
Derek McGowan
2ac2b9c909
Make api a Go sub-module
Allow the api to stay at the same v1 go package name and keep using a
1.x version number. This indicates the API is still at 1.x and allows
sharing proto types with containerd 1.6 and 1.7 releases.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2024-05-02 11:03:00 -07:00
Derek McGowan
e1b94c0e7d
Move protobuf package under pkg
Signed-off-by: Derek McGowan <derek@mcg.dev>
2024-05-02 10:52:03 -07:00
Derek McGowan
4a45507772
Move runc options to api directory
Signed-off-by: Derek McGowan <derek@mcg.dev>
2024-05-02 10:52:00 -07:00
Abel Feng
de38490ed6 sandbox: merge address and protocol to one url
Signed-off-by: Abel Feng <fshb1988@gmail.com>
2024-04-30 15:28:00 +08:00
Abel Feng
c3b306240e add task api endpoint in task create options
Signed-off-by: Abel Feng <fshb1988@gmail.com>
2024-04-30 15:22:44 +08:00
Abel Feng
b1fefccc78 sandbox: store endpoint in cri sandboxStore
Signed-off-by: Abel Feng <fshb1988@gmail.com>
2024-04-30 15:20:03 +08:00
Akihiro Suda
c27bcdc564
cri: introspectRuntimeFeatures: fix nil panic
Fix issue 10062

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2024-04-25 08:36:38 +09:00