diff --git a/pkg/config/config.go b/pkg/config/config.go index 15e797b36..1a5370a39 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -563,6 +563,10 @@ func NewConfig(userConfigPath string) (*Config, error) { return nil, err } + if err := config.setupEnv(); err != nil { + return nil, err + } + return config, nil } @@ -1187,3 +1191,23 @@ func (c *Config) ImageCopyTmpDir() (string, error) { return "", errors.Errorf("invalid image_copy_tmp_dir value %q (relative paths are not accepted)", c.Engine.ImageCopyTmpDir) } + +// setupEnv sets the environment variables for the engine +func (c *Config) setupEnv() error { + for _, env := range c.Engine.Env { + splitEnv := strings.SplitN(env, "=", 2) + if len(splitEnv) != 2 { + logrus.Warnf("invalid environment variable for engine %s, valid configuration is KEY=value pair", env) + continue + } + // skip if the env is already defined + if _, ok := os.LookupEnv(splitEnv[0]); ok { + logrus.Debugf("environment variable %s is already defined, skip the settings from containers.conf", splitEnv[0]) + continue + } + if err := os.Setenv(splitEnv[0], splitEnv[1]); err != nil { + return err + } + } + return nil +} diff --git a/pkg/config/config_local_test.go b/pkg/config/config_local_test.go index 154c65eab..a49445ca1 100644 --- a/pkg/config/config_local_test.go +++ b/pkg/config/config_local_test.go @@ -203,12 +203,15 @@ var _ = Describe("Config Local", func() { It("should return containers engine env", func() { // Given - expectedEnv := []string{"http_proxy=internal.proxy.company.com", "foo=bar"} + expectedEnv := []string{"super=duper", "foo=bar"} // When config, err := NewConfig("testdata/containers_default.conf") // Then gomega.Expect(err).To(gomega.BeNil()) gomega.Expect(config.Engine.Env).To(gomega.BeEquivalentTo(expectedEnv)) + gomega.Expect(os.Getenv("super")).To(gomega.BeEquivalentTo("duper")) + gomega.Expect(os.Getenv("foo")).To(gomega.BeEquivalentTo("bar")) + }) It("Expect Remote to be False", func() { diff --git a/pkg/config/testdata/containers_default.conf b/pkg/config/testdata/containers_default.conf index d54db1514..902568efb 100644 --- a/pkg/config/testdata/containers_default.conf +++ b/pkg/config/testdata/containers_default.conf @@ -137,7 +137,7 @@ conmon_path = [ # For example "http_proxy=internal.proxy.company.com". # Note these environment variables will not be used within the container. # Set the env section under [containers] table, if you want to set environment variables for the container. -env = ["http_proxy=internal.proxy.company.com", "foo=bar"] +env = ["super=duper", "foo=bar"] # Container init binary #init_path = "/usr/libexec/podman/catatonit"