containers / podman

Podman: A tool for managing OCI containers and pods.
https://podman.io
Apache License 2.0
24.03k stars 2.43k forks source link

quadlet creates invalid unit file name for templated network #23215

Open Fl0GUI opened 5 months ago

Fl0GUI commented 5 months ago

Issue Description

when trying to create a templated network using quadlet, the resulting unit file doesn't have the @ in the correct place to be a templated unit. For example take the file foo@.network:

[Network]
NetworkName=%i

quadlet will generate foo@-network.service just like the documentation says

... and the generated service file is $NAME-network.service.

Where name is foo@ in this case. But looking at the documentation of systemd.unit

A template unit must have a single "@" at the end of unit name prefix

The prefix being .service. So this unit file does not work, cannot be instanciated, and I only got it to run with systemctl start foo@-network.service. The result of which is that -network is used as the network name, which podman sees as a command line option:

Error: unknown shorthand flag: 'n' in -network

Steps to reproduce the issue

Steps to reproduce the issue

  1. Create the file foo@.network with the content as shown in the description. Place this in ~/.config/containers/systemd.
  2. Run systemctl --user daemon-reload.
  3. See in systemctl --user list-unit-files that the name is foo@-network.service
  4. Try to detemplate it like systemctl --user start foo@bar-network.service or just try systemctl --user start foo@-network.service

Describe the results you received

Either Unit foo@bar-network.service could not be found. from systemctl, or Error: unknown shorthand flag: 'n' in -network from podman.

Describe the results you expected

I would expect the resulting unit file to have a valid format, eg. foo-network@.service. The @ should be right before .service. With this unit file as a result the network should be possible to create.

podman info output

$ podman version
Client:       Podman Engine
Version:      5.1.1
API Version:  5.1.1
Go Version:   go1.22.3
Built:        Tue Jun  4 02:00:00 2024
OS/Arch:      linux/amd64

### Podman in a container

No

### Privileged Or Rootless

Rootless

### Upstream Latest Release

Yes

### Additional environment details

$ systemctl --version systemd 255 (255.6-1.fc40) +PAM +AUDIT +SELINUX -APPARMOR +IMA +SMACK +SECCOMP -GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN -IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 +PWQUALITY +P11KIT +QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +BPF_FRAMEWORK +XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified $ cat /etc/fedora-release Fedora release 40 (Forty)



### Additional information

It seems only expected that this would happen assuming a naive implementation.
A workaround is to not use templated networks.
Fl0GUI commented 5 months ago

Pretty sure the issue happens on

https://github.com/containers/podman/blob/c5841b000358fa1dff27c23e5839af404b20f395/pkg/systemd/quadlet/quadlet.go#L866-L868

and the implementation of

https://github.com/containers/podman/blob/c5841b000358fa1dff27c23e5839af404b20f395/pkg/systemd/quadlet/quadlet.go#L373-L382

ygalblum commented 4 months ago

Thanks for reporting this. While I agree this is an issue, I wonder what is your use case here. Are you planning on linking different networks to different .container files? If yes, then, currently, linking between templated Qualdet files does not work. You may see #17744 for details.

Fl0GUI commented 4 months ago

Thank you for the links. It's an interesting discussion but I must admit I'm not familiar enough with systems units to contribute to it.

As for my use case; I'm setting up my home servers with a couple of domains. Each domain corresponds to a podman network that has the relevant services running under it. Services that are directly related are put in a pod (eg web server + database.) Services could join multiple networks if it makes sense. I have traefik connected to all the networks and doing all the routing.

Each network is the same except for the name, so a template would be nice to use instead of copy pasting the network file.

if templates aren't that supported maybe it should be documented as such. I was referencing the man page for quadlet but I can't check it rn.

With that being said, I'm fine with this issue being closed.

github-actions[bot] commented 3 months ago

A friendly reminder that this issue had no activity for 30 days.

rhatdan commented 3 months ago

@ygalblum Did the changes to allow named services help here?

ygalblum commented 3 months ago

@rhatdan unfortunately no. The issue here is with templates and overriding the name is not supposed to resolve templates