From 70f55e0840c17372ff69a9376e2c22bbeb2ec5e9 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 21 Oct 2024 15:36:32 -0400 Subject: [PATCH] Honor users requests in quadlet files Fixes: https://github.com/containers/podman/issues/24322 Signed-off-by: Daniel J Walsh --- pkg/systemd/quadlet/quadlet.go | 45 ++++++++----------- .../e2e/quadlet/build.multiple-tags.container | 1 + test/e2e/quadlet/network.build | 5 +++ test/e2e/quadlet/network.kube | 2 + test/e2e/quadlet/volume.build | 4 ++ test/e2e/quadlet/volume.pod | 2 + 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/pkg/systemd/quadlet/quadlet.go b/pkg/systemd/quadlet/quadlet.go index 3e957200de..7ed9deff2b 100644 --- a/pkg/systemd/quadlet/quadlet.go +++ b/pkg/systemd/quadlet/quadlet.go @@ -876,6 +876,21 @@ func ConvertContainer(container *parser.UnitFile, isUser bool, unitsInfoMap map[ return service, nil } +func defaultServiceGroup(service *parser.UnitFile, remainAfterExit bool) { + // The default syslog identifier is the exec basename (podman) which isn't very useful here + if _, ok := service.Lookup(ServiceGroup, "SyslogIdentifier"); !ok { + service.Set(ServiceGroup, "SyslogIdentifier", "%N") + } + if _, ok := service.Lookup(ServiceGroup, "Type"); !ok { + service.Set(ServiceGroup, "Type", "oneshot") + } + if remainAfterExit { + if _, ok := service.Lookup(ServiceGroup, "RemainAfterExit"); !ok { + service.Set(ServiceGroup, "RemainAfterExit", "yes") + } + } +} + // Convert a quadlet network file (unit file with a Network group) to a systemd // service file (unit file with Service group) based on the options in the // Network group. @@ -976,12 +991,7 @@ func ConvertNetwork(network *parser.UnitFile, name string, unitsInfoMap map[stri service.AddCmdline(ServiceGroup, "ExecStart", podman.Args) - service.Setv(ServiceGroup, - "Type", "oneshot", - "RemainAfterExit", "yes", - - // The default syslog identifier is the exec basename (podman) which isn't very useful here - "SyslogIdentifier", "%N") + defaultServiceGroup(service, true) // Store the name of the created resource unitInfo.ResourceName = networkName @@ -1124,12 +1134,7 @@ func ConvertVolume(volume *parser.UnitFile, name string, unitsInfoMap map[string service.AddCmdline(ServiceGroup, "ExecStart", podman.Args) - service.Setv(ServiceGroup, - "Type", "oneshot", - "RemainAfterExit", "yes", - - // The default syslog identifier is the exec basename (podman) which isn't very useful here - "SyslogIdentifier", "%N") + defaultServiceGroup(service, true) // Store the name of the created resource unitInfo.ResourceName = volumeName @@ -1342,12 +1347,7 @@ func ConvertImage(image *parser.UnitFile, unitsInfoMap map[string]*UnitInfo, isU service.AddCmdline(ServiceGroup, "ExecStart", podman.Args) - service.Setv(ServiceGroup, - "Type", "oneshot", - "RemainAfterExit", "yes", - - // The default syslog identifier is the exec basename (podman) which isn't very useful here - "SyslogIdentifier", "%N") + defaultServiceGroup(service, true) if name, ok := image.Lookup(ImageGroup, KeyImageTag); ok && len(name) > 0 { imageName = name @@ -1475,14 +1475,7 @@ func ConvertBuild(build *parser.UnitFile, unitsInfoMap map[string]*UnitInfo, isU service.AddCmdline(ServiceGroup, "ExecStart", podman.Args) - service.Setv(ServiceGroup, - "Type", "oneshot", - "RemainAfterExit", "yes", - - // The default syslog identifier is the exec basename (podman) - // which isn't very useful here - "SyslogIdentifier", "%N") - + defaultServiceGroup(service, false) return service, nil } diff --git a/test/e2e/quadlet/build.multiple-tags.container b/test/e2e/quadlet/build.multiple-tags.container index d8ba492104..61f853bfa0 100644 --- a/test/e2e/quadlet/build.multiple-tags.container +++ b/test/e2e/quadlet/build.multiple-tags.container @@ -1,4 +1,5 @@ ## assert-podman-final-args localhost/imagename:v1 +## assert-key-is "Service" "RemainAfterExit" "" [Container] Image=multiple-tags.build diff --git a/test/e2e/quadlet/network.build b/test/e2e/quadlet/network.build index bb7b8edff4..efc3eb68ff 100644 --- a/test/e2e/quadlet/network.build +++ b/test/e2e/quadlet/network.build @@ -1,8 +1,13 @@ ## assert-podman-final-args-regex /.*/podman-e2e-.*/subtest-.*/quadlet ## assert-podman-args "--tag" "localhost/imagename" ## assert-podman-args "--network" "host" +## assert-key-is "Service" "Type" "oneshot" +## assert-key-is "Service" "RemainAfterExit" "no" [Build] ImageTag=localhost/imagename SetWorkingDirectory=unit Network=host + +[Service] +RemainAfterExit=no diff --git a/test/e2e/quadlet/network.kube b/test/e2e/quadlet/network.kube index 7b5493244d..4153105f0f 100644 --- a/test/e2e/quadlet/network.kube +++ b/test/e2e/quadlet/network.kube @@ -1,4 +1,6 @@ ## assert-podman-args --network basic +## assert-key-is "Service" "Type" "oneshot" +## assert-key-is "Service" "RemainAfterExit" "yes" [Kube] Yaml=deployment.yml diff --git a/test/e2e/quadlet/volume.build b/test/e2e/quadlet/volume.build index bcbbf37157..8a8777da1a 100644 --- a/test/e2e/quadlet/volume.build +++ b/test/e2e/quadlet/volume.build @@ -4,6 +4,7 @@ ## assert-podman-args -v named:/container/named ## assert-podman-args -v systemd-basic:/container/quadlet ## assert-podman-args -v %h/container:/container/volume4 +## assert-key-is "Service" "Type" "notify" [Build] ImageTag=localhost/imagename @@ -15,3 +16,6 @@ Volume=/container/empty Volume=named:/container/named Volume=basic.volume:/container/quadlet Volume=%h/container:/container/volume4 + +[Service] +Type=notify diff --git a/test/e2e/quadlet/volume.pod b/test/e2e/quadlet/volume.pod index 1c1b6578df..3149d50b30 100644 --- a/test/e2e/quadlet/volume.pod +++ b/test/e2e/quadlet/volume.pod @@ -4,6 +4,8 @@ ## assert-podman-pre-args -v named:/container/named ## assert-podman-pre-args -v systemd-basic:/container/quadlet ## assert-podman-pre-args -v %h/container:/container/volume4 +## assert-key-is "Service" "Type" "oneshot" +## assert-key-is "Service" "RemainAfterExit" "yes" [Pod] Volume=/host/dir:/container/volume