-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nixos/oci-containers: support rootless containers & healthchecks #368565
Open
Ma27
wants to merge
1
commit into
NixOS:master
Choose a base branch
from
flyingcircusio:podman-rootless-and-healthchecks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+186
−25
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ let | |
{ ... }: | ||
{ | ||
|
||
config = { | ||
podman = mkIf (cfg.backend == "podman") { }; | ||
}; | ||
|
||
options = { | ||
|
||
image = mkOption { | ||
|
@@ -280,6 +284,43 @@ let | |
''; | ||
}; | ||
|
||
podman = mkOption { | ||
type = types.nullOr ( | ||
types.submodule { | ||
options = { | ||
sdnotify = mkOption { | ||
default = "conmon"; | ||
type = types.enum [ | ||
"conmon" | ||
"healthy" | ||
"container" | ||
]; | ||
description = '' | ||
The way how `podman` should notify systemd that the unit is ready. There are | ||
[three options](https://docs.podman.io/en/latest/markdown/podman-run.1.html#sdnotify-container-conmon-healthy-ignore): | ||
|
||
* `conmon`: marks the unit as ready when the container has started. | ||
* `healthy`: marks the unit as ready when the [container's healthcheck](https://docs.podman.io/en/stable/markdown/podman-healthcheck-run.1.html) passes. | ||
* `container`: `NOTIFY_SOCKET` is passed into the container and the process inside the container needs to indicate on its own that it's ready. | ||
''; | ||
}; | ||
user = mkOption { | ||
default = "root"; | ||
type = types.str; | ||
description = '' | ||
The user under which the container should run. | ||
''; | ||
}; | ||
}; | ||
} | ||
); | ||
default = null; | ||
description = '' | ||
Podman-specific settings in OCI containers. These must be null when using | ||
the `docker` backend. | ||
''; | ||
}; | ||
|
||
pull = mkOption { | ||
type = | ||
with types; | ||
|
@@ -372,16 +413,20 @@ let | |
${container.imageStream} | ${cfg.backend} load | ||
''} | ||
${optionalString (cfg.backend == "podman") '' | ||
rm -f /run/podman-${escapedName}.ctr-id | ||
rm -f /run/${escapedName}/ctr-id | ||
''} | ||
''; | ||
}; | ||
|
||
effectiveUser = container.podman.user or "root"; | ||
dependOnLingerService = | ||
cfg.backend == "podman" && effectiveUser != "root" && config.users.users.${effectiveUser}.linger; | ||
in | ||
{ | ||
wantedBy = [ ] ++ optional (container.autoStart) "multi-user.target"; | ||
wants = lib.optional ( | ||
container.imageFile == null && container.imageStream == null | ||
) "network-online.target"; | ||
wants = | ||
lib.optional (container.imageFile == null && container.imageStream == null) "network-online.target" | ||
++ lib.optional dependOnLingerService "linger-users.service"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still kinda amazing, how this is the result of an RFC that has
(emphasis mine) as explicit goal. |
||
after = | ||
lib.optionals (cfg.backend == "docker") [ | ||
"docker.service" | ||
|
@@ -391,9 +436,15 @@ let | |
++ lib.optionals (container.imageFile == null && container.imageStream == null) [ | ||
"network-online.target" | ||
] | ||
++ dependsOn; | ||
++ dependsOn | ||
++ lib.optional dependOnLingerService "linger-users.service"; | ||
requires = dependsOn; | ||
environment = proxy_env; | ||
environment = lib.mkMerge [ | ||
proxy_env | ||
(mkIf (cfg.backend == "podman" && container.podman.user != "root") { | ||
HOME = config.users.users.${container.podman.user}.home; | ||
}) | ||
]; | ||
|
||
path = | ||
if cfg.backend == "docker" then | ||
|
@@ -417,9 +468,9 @@ let | |
++ optional (container.entrypoint != null) "--entrypoint=${escapeShellArg container.entrypoint}" | ||
++ optional (container.hostname != null) "--hostname=${escapeShellArg container.hostname}" | ||
++ lib.optionals (cfg.backend == "podman") [ | ||
"--cidfile=/run/podman-${escapedName}.ctr-id" | ||
"--cgroups=no-conmon" | ||
"--sdnotify=conmon" | ||
"--cidfile=/run/${escapedName}/ctr-id" | ||
"--cgroups=enabled" | ||
"--sdnotify=${container.podman.sdnotify}" | ||
"-d" | ||
"--replace" | ||
] | ||
|
@@ -447,13 +498,13 @@ let | |
|
||
preStop = | ||
if cfg.backend == "podman" then | ||
"podman stop --ignore --cidfile=/run/podman-${escapedName}.ctr-id" | ||
"podman stop --ignore --cidfile=/run/${escapedName}/ctr-id" | ||
else | ||
"${cfg.backend} stop ${name} || true"; | ||
|
||
postStop = | ||
if cfg.backend == "podman" then | ||
"podman rm -f --ignore --cidfile=/run/podman-${escapedName}.ctr-id" | ||
"podman rm -f --ignore --cidfile=/run/${escapedName}/ctr-id" | ||
else | ||
"${cfg.backend} rm -f ${name} || true"; | ||
|
||
|
@@ -483,6 +534,9 @@ let | |
Environment = "PODMAN_SYSTEMD_UNIT=podman-${name}.service"; | ||
Type = "notify"; | ||
NotifyAccess = "all"; | ||
Delegate = mkIf (container.podman.sdnotify == "healthy") true; | ||
User = effectiveUser; | ||
RuntimeDirectory = escapedName; | ||
}; | ||
}; | ||
|
||
|
@@ -531,17 +585,46 @@ in | |
|
||
assertions = | ||
let | ||
toAssertion = | ||
_: | ||
{ imageFile, imageStream, ... }: | ||
toAssertions = | ||
name: | ||
{ | ||
assertion = imageFile == null || imageStream == null; | ||
|
||
message = "You can only define one of imageFile and imageStream"; | ||
}; | ||
|
||
imageFile, | ||
imageStream, | ||
podman, | ||
... | ||
}: | ||
[ | ||
{ | ||
assertion = imageFile == null || imageStream == null; | ||
|
||
message = "virtualisation.oci-containers.containers.${name}: You can only define one of imageFile and imageStream"; | ||
} | ||
{ | ||
assertion = cfg.backend == "docker" -> podman == null; | ||
message = "virtualisation.oci-containers.containers.${name}: Cannot set `podman` option if backend is `docker`."; | ||
} | ||
]; | ||
in | ||
lib.mapAttrsToList toAssertion cfg.containers; | ||
concatMap (name: toAssertions name cfg.containers.${name}) (lib.attrNames cfg.containers); | ||
|
||
warnings = mkIf (cfg.backend == "podman") ( | ||
lib.foldlAttrs ( | ||
warnings: name: | ||
{ podman, ... }: | ||
let | ||
inherit (config.users.users.${podman.user}) linger; | ||
in | ||
warnings | ||
++ lib.optional (podman.user != "root" && linger && podman.sdnotify == "conmon") '' | ||
Podman container ${name} is configured as rootless (user ${podman.user}) | ||
with `--sdnotify=conmon`, but lingering for this user is turned on. | ||
'' | ||
++ lib.optional (podman.user != "root" && !linger && podman.sdnotify == "healthy") '' | ||
Podman container ${name} is configured as rootless (user ${podman.user}) | ||
with `--sdnotify=healthy`, but lingering for this user is turned off. | ||
'' | ||
) [ ] cfg.containers | ||
); | ||
} | ||
(lib.mkIf (cfg.backend == "podman") { | ||
virtualisation.podman.enable = true; | ||
|
@@ -551,5 +634,4 @@ in | |
}) | ||
] | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this probably requires some testing before merging. I haven't really verified if all the settings are correct for this case.