Skip to content

Commit 8a2d75a

Browse files
committed
config: add env_merge support to containers.conf
Allow users to set default env_merge values in containers.conf. Works the same as the --env-merge CLI option. Fixes: containers/podman#28410 Signed-off-by: Jiwoo Ahn <ikwydls1314@gmail.com>
1 parent adb645f commit 8a2d75a

9 files changed

Lines changed: 38 additions & 0 deletions

File tree

common/docs/containers.conf.5.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ overwriting values specified on the command line when the container was created.
226226

227227
Pass all host environment variables into the container.
228228

229+
**env_merge**=[]
230+
231+
Preprocess default environment variables for the container.
232+
Works the same as the **--env-merge** command line option.
233+
The value of existing variables in the image will be used for substitution.
234+
235+
Example: `env_merge=["PATH=${PATH}:/custom/bin"]`
236+
229237
**host_containers_internal_ip**=""
230238

231239
Set the IP address the container should expect to connect to the host. The IP

common/pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ type ContainersConfig struct {
142142
// EnvHost Pass all host environment variables into the container.
143143
EnvHost bool `toml:"env_host,omitempty"`
144144

145+
// EnvMerge preprocess the environment variables list for the container.
146+
EnvMerge configfile.Slice `toml:"env_merge,omitempty"`
147+
145148
// HostContainersInternalIP is used to set a specific host.containers.internal ip.
146149
HostContainersInternalIP string `toml:"host_containers_internal_ip,omitempty"`
147150

common/pkg/config/config_local_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,4 +664,15 @@ var _ = Describe("Config Local", func() {
664664
// Then
665665
gomega.Expect(err).To(gomega.HaveOccurred())
666666
})
667+
It("should parse env_merge from config file", func() {
668+
// Given
669+
config, err := newLocked(&Options{}, testConfigPath(""))
670+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
671+
gomega.Expect(config.Containers.EnvMerge.Get()).To(gomega.BeEmpty())
672+
// When
673+
config2, err := newLocked(&Options{}, testConfigPath("testdata/containers_default.conf"))
674+
// Then
675+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
676+
gomega.Expect(config2.Containers.EnvMerge.Get()).To(gomega.Equal([]string{"FOO=bar"}))
677+
})
667678
})

common/pkg/config/containers.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ default_sysctls = [
137137
#
138138
#env_host = false
139139

140+
# Preprocess default environment variables for the container.
141+
#
142+
#env_merge = []
143+
140144
# Set the IP address the container should expect to connect to the host. The IP
141145
# address is used by Podman to automatically add the `host.containers.internal`
142146
# and `host.docker.internal` hostnames to the container's `/etc/hosts` file. It

common/pkg/config/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ func defaultConfig() (*Config, error) {
223223
EnableLabeling: selinux.GetEnabled(),
224224
Env: configfile.NewSlice(defaultContainerEnv),
225225
EnvHost: false,
226+
EnvMerge: configfile.Slice{},
226227
HTTPProxy: true,
227228
IPCNS: "shareable",
228229
Init: false,

common/pkg/config/modules_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ var _ = Describe("Config Modules", func() {
5757
gomega.Expect(err).ToNot(gomega.HaveOccurred())
5858
gomega.Expect(c.Containers.InitPath).To(gomega.Equal("etc four"))
5959
gomega.Expect(c.Containers.Env.Get()).To(gomega.Equal([]string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "usr share only"}))
60+
gomega.Expect(c.Containers.EnvMerge.Get()).To(gomega.Equal([]string{
61+
"PATH=/base/bin:${PATH}",
62+
"PATH=/usr/local/share/bin:${PATH}",
63+
"etc only env_merge conf",
64+
}))
6065
gomega.Expect(c.Network.DefaultNetwork).To(gomega.Equal("etc only conf"))
6166
gomega.Expect(c.LoadedModules()).To(gomega.HaveLen(3)) // 3 modules are getting loaded!
6267

common/pkg/config/testdata/containers_default.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ env = [
6565
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
6666
]
6767

68+
env_merge = ["FOO=bar"]
69+
6870
# Run an init inside the container that forwards signals and reaps processes.
6971
init = false
7072

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[network]
22
default_network="etc only conf"
3+
4+
[containers]
5+
env_merge=["PATH=/usr/local/share/bin:${PATH}", "etc only env_merge conf", {append=true}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[containers]
22
env=["usr share only", {append=true}]
3+
env_merge=["PATH=/base/bin:${PATH}"]

0 commit comments

Comments
 (0)