Skip to content

Commit

Permalink
quadlet: Handle booleans that have defaults better
Browse files Browse the repository at this point in the history
The ReadOnly and the RunInit keys affect options that have a variable
default (configurable in containers.conf). This means we need to
handle them a bit differently in quadlet to allow overriding the
default. For example, we can't assume ReadOnly=false doesn't need to
add any argument because no argument may mean readonly=true if the default
is changed.

We now don't add any argument (leaving the default) if the key is not specified,
or we always add an argument (--foo or --foo=false) if the key is specified (overriding whatever the default is).

Signed-off-by: Alexander Larsson <[email protected]>
  • Loading branch information
alexlarsson committed Dec 20, 2022
1 parent dd428af commit 0cf3668
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pkg/systemd/quadlet/podmancmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func (c *PodmanCmdline) addAnnotations(annotations map[string]string) {
c.addKeys("--annotation", annotations)
}

func (c *PodmanCmdline) addBool(arg string, val bool) {
if val {
c.add(arg)
} else {
c.addf("%s=false", arg)
}
}

func NewPodmanCmdline(args ...string) *PodmanCmdline {
c := &PodmanCmdline{
Args: make([]string, 0),
Expand Down
12 changes: 6 additions & 6 deletions pkg/systemd/quadlet/quadlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ func ConvertContainer(container *parser.UnitFile, isUser bool) (*parser.UnitFile
addNetworks(container, ContainerGroup, service, podman)

// Run with a pid1 init to reap zombies by default (as most apps don't do that)
runInit := container.LookupBooleanWithDefault(ContainerGroup, KeyRunInit, false)
if runInit {
podman.add("--init")
runInit, ok := container.LookupBoolean(ContainerGroup, KeyRunInit)
if ok {
podman.addBool("--init", runInit)
}

// By default we handle startup notification with conmon, but allow passing it to the container with Notify=yes
Expand Down Expand Up @@ -345,9 +345,9 @@ func ConvertContainer(container *parser.UnitFile, isUser bool) (*parser.UnitFile
podman.addf("--cap-add=%s", strings.ToLower(caps))
}

readOnly := container.LookupBooleanWithDefault(ContainerGroup, KeyReadOnly, false)
if readOnly {
podman.add("--read-only")
readOnly, ok := container.LookupBoolean(ContainerGroup, KeyReadOnly)
if ok {
podman.addBool("--read-only", readOnly)
}

volatileTmp := container.LookupBooleanWithDefault(ContainerGroup, KeyVolatileTmp, false)
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/quadlet/basepodman.container
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
Image=localhost/imagename

# Disable all default features to get as empty podman run command as we can
ReadOnly=no
NoNewPrivileges=no
DropCapability=
RunInit=no
VolatileTmp=no
Timezone=
1 change: 1 addition & 0 deletions test/e2e/quadlet/readwrite-notmpfs.container
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## assert-podman-args "--read-only=false"
## !assert-podman-args "--read-only"
## !assert-podman-args "--tmpfs" "/tmp:rw,size=512M,mode=1777"

Expand Down
1 change: 1 addition & 0 deletions test/e2e/quadlet/readwrite.container
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## !assert-podman-args "--read-only"
## assert-podman-args "--read-only=false"
## assert-podman-args "--tmpfs" "/tmp:rw,size=512M,mode=1777"

[Container]
Expand Down

0 comments on commit 0cf3668

Please sign in to comment.