Skip to content

Commit d03d994

Browse files
Merge pull request #25975 from ygalblum/quadlet-consolidate-add-keys
Quadlet - use helper function for handling key=val type keys
2 parents 4be34de + 633f727 commit d03d994

File tree

2 files changed

+41
-53
lines changed

2 files changed

+41
-53
lines changed

pkg/systemd/quadlet/podmancmdline.go

-12
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@ func (c *PodmanCmdline) addKeys(arg string, keys map[string]string) {
4545
}
4646
}
4747

48-
func (c *PodmanCmdline) addEnv(env map[string]string) {
49-
c.addKeys("--env", env)
50-
}
51-
52-
func (c *PodmanCmdline) addLabels(labels map[string]string) {
53-
c.addKeys("--label", labels)
54-
}
55-
56-
func (c *PodmanCmdline) addAnnotations(annotations map[string]string) {
57-
c.addKeys("--annotation", annotations)
58-
}
59-
6048
func (c *PodmanCmdline) addBool(arg string, val bool) {
6149
if val {
6250
c.add(arg)

pkg/systemd/quadlet/quadlet.go

+41-41
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,6 @@ func ConvertContainer(container *parser.UnitFile, isUser bool, unitsInfoMap map[
601601
service.Set(ServiceGroup, "KillMode", "mixed")
602602
}
603603

604-
// Read env early so we can override it below
605-
podmanEnv, warn := container.LookupAllKeyVal(ContainerGroup, KeyEnvironment)
606-
warnings = errors.Join(warnings, warn)
607-
608604
// Need the containers filesystem mounted to start podman
609605
service.Add(UnitGroup, "RequiresMountsFor", "%t/containers")
610606

@@ -814,9 +810,12 @@ func ConvertContainer(container *parser.UnitFile, isUser bool, unitsInfoMap map[
814810

815811
update, ok := container.Lookup(ContainerGroup, KeyAutoUpdate)
816812
if ok && len(update) > 0 {
817-
podman.addLabels(map[string]string{
818-
autoUpdateLabel: update,
819-
})
813+
podman.addKeys(
814+
"--label",
815+
map[string]string{
816+
autoUpdateLabel: update,
817+
},
818+
)
820819
}
821820

822821
exposedPorts := container.LookupAll(ContainerGroup, KeyExposeHostPort)
@@ -832,15 +831,13 @@ func ConvertContainer(container *parser.UnitFile, isUser bool, unitsInfoMap map[
832831

833832
handlePublishPorts(container, ContainerGroup, podman)
834833

835-
podman.addEnv(podmanEnv)
836-
837-
labels, warn := container.LookupAllKeyVal(ContainerGroup, KeyLabel)
838-
warnings = errors.Join(warnings, warn)
839-
podman.addLabels(labels)
840-
841-
annotations, warn := container.LookupAllKeyVal(ContainerGroup, KeyAnnotation)
834+
keyValKeys := map[string]string{
835+
KeyEnvironment: "--env",
836+
KeyLabel: "--label",
837+
KeyAnnotation: "--annotation",
838+
}
839+
warn = lookupAndAddKeyVals(container, ContainerGroup, keyValKeys, podman)
842840
warnings = errors.Join(warnings, warn)
843-
podman.addAnnotations(annotations)
844841

845842
masks := container.LookupAllArgs(ContainerGroup, KeyMask)
846843
for _, mask := range masks {
@@ -1041,17 +1038,12 @@ func ConvertNetwork(network *parser.UnitFile, name string, unitsInfoMap map[stri
10411038
return nil, warnings, fmt.Errorf("cannot set gateway or range without subnet")
10421039
}
10431040

1044-
networkOptions, warn := network.LookupAllKeyVal(NetworkGroup, KeyOptions)
1045-
warnings = errors.Join(warnings, warn)
1046-
if len(networkOptions) > 0 {
1047-
podman.addKeys("--opt", networkOptions)
1041+
keyValKeys := map[string]string{
1042+
KeyOptions: "--opt",
1043+
KeyLabel: "--label",
10481044
}
1049-
1050-
labels, warn := network.LookupAllKeyVal(NetworkGroup, KeyLabel)
1045+
warn = lookupAndAddKeyVals(network, NetworkGroup, keyValKeys, podman)
10511046
warnings = errors.Join(warnings, warn)
1052-
if len(labels) > 0 {
1053-
podman.addLabels(labels)
1054-
}
10551047

10561048
handlePodmanArgs(network, NetworkGroup, podman)
10571049

@@ -1111,9 +1103,6 @@ func ConvertVolume(volume *parser.UnitFile, name string, unitsInfoMap map[string
11111103
// Need the containers filesystem mounted to start podman
11121104
service.Add(UnitGroup, "RequiresMountsFor", "%t/containers")
11131105

1114-
labels, warn := volume.LookupAllKeyVal(VolumeGroup, "Label")
1115-
warnings = errors.Join(warnings, warn)
1116-
11171106
podman := createBasePodmanCommand(volume, VolumeGroup)
11181107

11191108
podman.add("volume", "create", "--ignore")
@@ -1200,7 +1189,11 @@ func ConvertVolume(volume *parser.UnitFile, name string, unitsInfoMap map[string
12001189
podman.add("--opt", opts.String())
12011190
}
12021191

1203-
podman.addLabels(labels)
1192+
keyValKeys := map[string]string{
1193+
KeyLabel: "--label",
1194+
}
1195+
warn = lookupAndAddKeyVals(volume, VolumeGroup, keyValKeys, podman)
1196+
warnings = errors.Join(warnings, warn)
12041197

12051198
handlePodmanArgs(volume, VolumeGroup, podman)
12061199

@@ -1516,18 +1509,13 @@ func ConvertBuild(build *parser.UnitFile, unitsInfoMap map[string]*UnitInfo, isU
15161509
}
15171510
lookupAndAddAllStrings(build, BuildGroup, allStringKeys, podman)
15181511

1519-
annotations, warn := build.LookupAllKeyVal(BuildGroup, KeyAnnotation)
1520-
warnings = errors.Join(warnings, warn)
1521-
1522-
podman.addAnnotations(annotations)
1523-
1524-
podmanEnv, warn := build.LookupAllKeyVal(BuildGroup, KeyEnvironment)
1525-
warnings = errors.Join(warnings, warn)
1526-
podman.addEnv(podmanEnv)
1527-
1528-
labels, warn := build.LookupAllKeyVal(BuildGroup, KeyLabel)
1512+
keyValKeys := map[string]string{
1513+
KeyEnvironment: "--env",
1514+
KeyLabel: "--label",
1515+
KeyAnnotation: "--annotation",
1516+
}
1517+
warn = lookupAndAddKeyVals(build, BuildGroup, keyValKeys, podman)
15291518
warnings = errors.Join(warnings, warn)
1530-
podman.addLabels(labels)
15311519

15321520
if err := addNetworks(build, BuildGroup, service, unitsInfoMap, podman); err != nil {
15331521
return nil, warnings, err
@@ -1709,9 +1697,11 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
17091697

17101698
handlePublishPorts(podUnit, PodGroup, execStartPre)
17111699

1712-
labels, warn := podUnit.LookupAllKeyVal(PodGroup, KeyLabel)
1700+
keyValKeys := map[string]string{
1701+
KeyLabel: "--label",
1702+
}
1703+
warn = lookupAndAddKeyVals(podUnit, PodGroup, keyValKeys, execStartPre)
17131704
warnings = errors.Join(warnings, warn)
1714-
execStartPre.addLabels(labels)
17151705

17161706
if err := addNetworks(podUnit, PodGroup, service, unitsInfoMap, execStartPre); err != nil {
17171707
return nil, warnings, err
@@ -2368,3 +2358,13 @@ func translateUnitDependencies(serviceUnitFile *parser.UnitFile, unitsInfoMap ma
23682358
}
23692359
return nil
23702360
}
2361+
2362+
func lookupAndAddKeyVals(unit *parser.UnitFile, group string, keys map[string]string, podman *PodmanCmdline) error {
2363+
var warnings error
2364+
for key, flag := range keys {
2365+
keyVals, warn := unit.LookupAllKeyVal(group, key)
2366+
warnings = errors.Join(warnings, warn)
2367+
podman.addKeys(flag, keyVals)
2368+
}
2369+
return warnings
2370+
}

0 commit comments

Comments
 (0)