Skip to content

Commit 2b647bf

Browse files
Merge pull request #25964 from SuNNjek/quadlet-pod-labels
Add Label to quadlet pod
2 parents 83d989f + 802fc15 commit 2b647bf

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

cmd/quadlet/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ func process() bool {
736736
case strings.HasSuffix(unit.Filename, ".build"):
737737
service, warnings, err = quadlet.ConvertBuild(unit, unitsInfoMap, isUserFlag)
738738
case strings.HasSuffix(unit.Filename, ".pod"):
739-
service, err = quadlet.ConvertPod(unit, unit.Filename, unitsInfoMap, isUserFlag)
739+
service, warnings, err = quadlet.ConvertPod(unit, unit.Filename, unitsInfoMap, isUserFlag)
740740
default:
741741
Logf("Unsupported file type %q", unit.Filename)
742742
continue

docs/source/markdown/podman-systemd.unit.5.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ Valid options for `[Pod]` are listed below:
10081008
| HostName=name | --hostname=name |
10091009
| IP=192.5.0.1 | --ip 192.5.0.1 |
10101010
| IP6=2001:db8::1 | --ip6 2001:db8::1 |
1011+
| Label="XYZ" | --label "XYZ" |
10111012
| Network=host | --network host |
10121013
| NetworkAlias=name | --network-alias name |
10131014
| PodmanArgs=\-\-cpus=2 | --cpus=2 |
@@ -1093,6 +1094,13 @@ Equivalent to the Podman `--ip` option.
10931094
Specify a static IPv6 address for the pod, for example **fd46:db93:aa76:ac37::10**.
10941095
Equivalent to the Podman `--ip6` option.
10951096

1097+
### `Label=`
1098+
1099+
Set one or more OCI labels on the pod. The format is a list of
1100+
`key=value` items, similar to `Environment`.
1101+
1102+
This key can be listed multiple times.
1103+
10961104
### `Network=`
10971105

10981106
Specify a custom network for the pod.

pkg/systemd/quadlet/quadlet.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ var (
450450
KeyHostName: true,
451451
KeyIP: true,
452452
KeyIP6: true,
453+
KeyLabel: true,
453454
KeyNetwork: true,
454455
KeyNetworkAlias: true,
455456
KeyPodName: true,
@@ -1622,17 +1623,19 @@ func getServiceName(quadletUnitFile *parser.UnitFile, groupName string, defaultE
16221623
return removeExtension(quadletUnitFile.Filename, "", defaultExtraSuffix)
16231624
}
16241625

1625-
func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*UnitInfo, isUser bool) (*parser.UnitFile, error) {
1626+
func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*UnitInfo, isUser bool) (*parser.UnitFile, error, error) {
1627+
var warn, warnings error
1628+
16261629
unitInfo, ok := unitsInfoMap[podUnit.Filename]
16271630
if !ok {
1628-
return nil, fmt.Errorf("internal error while processing pod %s", podUnit.Filename)
1631+
return nil, warnings, fmt.Errorf("internal error while processing pod %s", podUnit.Filename)
16291632
}
16301633

16311634
service := podUnit.Dup()
16321635
service.Filename = unitInfo.ServiceFileName()
16331636

16341637
if err := translateUnitDependencies(service, unitsInfoMap); err != nil {
1635-
return nil, err
1638+
return nil, warnings, err
16361639
}
16371640

16381641
addDefaultDependencies(service, isUser)
@@ -1642,7 +1645,7 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
16421645
}
16431646

16441647
if err := checkForUnknownKeys(podUnit, PodGroup, supportedPodKeys); err != nil {
1645-
return nil, err
1648+
return nil, warnings, err
16461649
}
16471650

16481651
// Derive pod name from unit name (with added prefix), or use user-provided name.
@@ -1701,13 +1704,17 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
17011704
)
17021705

17031706
if err := handleUserMappings(podUnit, PodGroup, execStartPre, true); err != nil {
1704-
return nil, err
1707+
return nil, warnings, err
17051708
}
17061709

17071710
handlePublishPorts(podUnit, PodGroup, execStartPre)
17081711

1712+
labels, warn := podUnit.LookupAllKeyVal(PodGroup, KeyLabel)
1713+
warnings = errors.Join(warnings, warn)
1714+
execStartPre.addLabels(labels)
1715+
17091716
if err := addNetworks(podUnit, PodGroup, service, unitsInfoMap, execStartPre); err != nil {
1710-
return nil, err
1717+
return nil, warnings, err
17111718
}
17121719

17131720
stringsKeys := map[string]string{
@@ -1728,7 +1735,7 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
17281735
lookupAndAddAllStrings(podUnit, PodGroup, allStringsKeys, execStartPre)
17291736

17301737
if err := addVolumes(podUnit, service, PodGroup, unitsInfoMap, execStartPre); err != nil {
1731-
return nil, err
1738+
return nil, warnings, err
17321739
}
17331740

17341741
execStartPre.add("--infra-name", fmt.Sprintf("%s-infra", podName))
@@ -1745,7 +1752,7 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
17451752
"PIDFile", "%t/%N.pid",
17461753
)
17471754

1748-
return service, nil
1755+
return service, warnings, nil
17491756
}
17501757

17511758
func handleUser(unitFile *parser.UnitFile, groupName string, podman *PodmanCmdline) error {

test/e2e/quadlet/label.pod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## assert-podman-pre-args "--label" "org.foo.Arg0=arg0"
2+
## assert-podman-pre-args "--label" "org.foo.Arg1=arg1"
3+
## assert-podman-pre-args "--label" "org.foo.Arg2=arg 2"
4+
## assert-podman-pre-args "--label" "org.foo.Arg3=arg3"
5+
6+
[Pod]
7+
Label=org.foo.Arg1=arg1 "org.foo.Arg2=arg 2" \
8+
org.foo.Arg3=arg3
9+
10+
Label=org.foo.Arg0=arg0

test/e2e/quadlet_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ BOGUS=foo
10781078
Entry("Pod - Host", "host.pod"),
10791079
Entry("Pod - HostName", "hostname.pod"),
10801080
Entry("Pod - IP", "ip.pod"),
1081+
Entry("Pod - Label", "label.pod"),
10811082
Entry("Pod - Name", "name.pod"),
10821083
Entry("Pod - Network", "network.pod"),
10831084
Entry("Pod - PodmanArgs", "podmanargs.pod"),

0 commit comments

Comments
 (0)