Merge pull request #25844 from ygalblum/quadlet-network-delete

Quadlet - allow deleting the network when stopping the service
This commit is contained in:
openshift-merge-bot[bot] 2025-04-14 17:57:45 +00:00 committed by GitHub
commit 09c2546b27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 95 additions and 17 deletions

View File

@ -1349,7 +1349,7 @@ particularly interesting when using special options to control network creation,
Valid options for `[Network]` are listed below: Valid options for `[Network]` are listed below:
| **[Network] options** | **podman network create equivalent** | | **[Network] options** | **podman network create equivalent** |
|-------------------------------------|--------------------------------------| |-------------------------------------|-----------------------------------------------------------------|
| ContainersConfModule=/etc/nvd\.conf | --module=/etc/nvd\.conf | | ContainersConfModule=/etc/nvd\.conf | --module=/etc/nvd\.conf |
| DisableDNS=true | --disable-dns | | DisableDNS=true | --disable-dns |
| DNS=192.168.55.1 | --dns=192.168.55.1 | | DNS=192.168.55.1 | --dns=192.168.55.1 |
@ -1361,6 +1361,7 @@ Valid options for `[Network]` are listed below:
| IPRange=192.168.55.128/25 | --ip-range 192.168.55.128/25 | | IPRange=192.168.55.128/25 | --ip-range 192.168.55.128/25 |
| IPv6=true | --ipv6 | | IPv6=true | --ipv6 |
| Label="XYZ" | --label "XYZ" | | Label="XYZ" | --label "XYZ" |
| NetworkDeleteOnStop=true | Add ExecStopPost to delete the network when the unit is stopped |
| NetworkName=foo | podman network create foo | | NetworkName=foo | podman network create foo |
| Options=isolate=true | --opt isolate=true | | Options=isolate=true | --opt isolate=true |
| PodmanArgs=--dns=192.168.55.1 | --dns=192.168.55.1 | | PodmanArgs=--dns=192.168.55.1 | --dns=192.168.55.1 |
@ -1447,6 +1448,10 @@ Set one or more OCI labels on the network. The format is a list of
This key can be listed multiple times. This key can be listed multiple times.
### `NetworkDeleteOnStop=` (defaults to `false`)
When set to `true` the network is deleted when the service is stopped
### `NetworkName=` ### `NetworkName=`
The (optional) name of the Podman network. If this is not specified, the default value of The (optional) name of the Podman network. If this is not specified, the default value of

View File

@ -126,6 +126,7 @@ const (
KeyMount = "Mount" KeyMount = "Mount"
KeyNetwork = "Network" KeyNetwork = "Network"
KeyNetworkAlias = "NetworkAlias" KeyNetworkAlias = "NetworkAlias"
KeyNetworkDeleteOnStop = "NetworkDeleteOnStop"
KeyNetworkName = "NetworkName" KeyNetworkName = "NetworkName"
KeyNoNewPrivileges = "NoNewPrivileges" KeyNoNewPrivileges = "NoNewPrivileges"
KeyNotify = "Notify" KeyNotify = "Notify"
@ -327,6 +328,7 @@ var (
KeyIPv6: true, KeyIPv6: true,
KeyInternal: true, KeyInternal: true,
KeyNetworkName: true, KeyNetworkName: true,
KeyNetworkDeleteOnStop: true,
KeyOptions: true, KeyOptions: true,
KeyServiceName: true, KeyServiceName: true,
KeySubnet: true, KeySubnet: true,
@ -948,6 +950,12 @@ func ConvertNetwork(network *parser.UnitFile, name string, unitsInfoMap map[stri
// Need the containers filesystem mounted to start podman // Need the containers filesystem mounted to start podman
service.Add(UnitGroup, "RequiresMountsFor", "%t/containers") service.Add(UnitGroup, "RequiresMountsFor", "%t/containers")
if network.LookupBooleanWithDefault(NetworkGroup, KeyNetworkDeleteOnStop, false) {
serviceStopPostCmd := createBasePodmanCommand(network, NetworkGroup)
serviceStopPostCmd.add("network", "rm", networkName)
service.AddCmdline(ServiceGroup, "ExecStopPost", serviceStopPostCmd.Args)
}
podman := createBasePodmanCommand(network, NetworkGroup) podman := createBasePodmanCommand(network, NetworkGroup)
podman.add("network", "create", "--ignore") podman.add("network", "create", "--ignore")

View File

@ -0,0 +1,7 @@
## assert-podman-stop-post-args "network"
## assert-podman-stop-post-args "rm"
## assert-podman-stop-post-final-args "test-network"
[Network]
NetworkName=test-network
NetworkDeleteOnStop=true

View File

@ -1017,6 +1017,7 @@ BOGUS=foo
Entry("Network - subnet, gateway and range", "subnet-trio.network"), Entry("Network - subnet, gateway and range", "subnet-trio.network"),
Entry("Network - global args", "globalargs.network"), Entry("Network - global args", "globalargs.network"),
Entry("Network - Containers Conf Modules", "containersconfmodule.network"), Entry("Network - Containers Conf Modules", "containersconfmodule.network"),
Entry("Network - Delete on stop", "delete.network"),
Entry("Image - Basic", "basic.image"), Entry("Image - Basic", "basic.image"),
Entry("Image - Architecture", "arch.image"), Entry("Image - Architecture", "arch.image"),

View File

@ -517,6 +517,63 @@ EOF
run_podman network rm $network_name run_podman network rm $network_name
} }
@test "quadlet - network delete with dependencies" {
# Save the unit name to use as the network for the container
local network_name=$(safename)
local quadlet_network_unit=dep_$(safename).network
local quadlet_network_file=$PODMAN_TMPDIR/${quadlet_network_unit}
cat > $quadlet_network_file <<EOF
[Network]
NetworkName=${network_name}
NetworkDeleteOnStop=true
EOF
local quadlet_tmpdir=$(mktemp -d --tmpdir=$PODMAN_TMPDIR quadlet.XXXXXX)
# Have quadlet create the systemd unit file for the network unit
run_quadlet "$quadlet_network_file" "$quadlet_tmpdir"
# Save the network service name since the variable will be overwritten
local network_service=$QUADLET_SERVICE_NAME
local quadlet_container_file=$PODMAN_TMPDIR/user_$(safename).container
cat > $quadlet_container_file <<EOF
[Container]
Image=$IMAGE
Exec=top
Network=$quadlet_network_unit
EOF
run_quadlet "$quadlet_container_file" "$quadlet_tmpdir"
# Save the container service name for readability
local container_service=$QUADLET_SERVICE_NAME
# Network should not exist
run_podman 1 network exists $network_name
# Start the container service
service_setup $container_service
# Network system unit should be active
run systemctl show --property=ActiveState "$network_service"
assert "$output" = "ActiveState=active" \
"network should be active via dependency"
# Network should exist
run_podman network exists $network_name
# Stop the Network Service
service_cleanup $network_service inactive
# Container system unit should be active
run systemctl show --property=ActiveState "$container_service"
assert "$output" = "ActiveState=failed" \
"container service should be failed via dependency"
# Network should not exist
run_podman 1 network exists $network_name
}
# A quadlet container depends on a quadlet network # A quadlet container depends on a quadlet network
@test "quadlet - network dependency" { @test "quadlet - network dependency" {
# Save the unit name to use as the network for the container # Save the unit name to use as the network for the container