Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the environment variables for the engine #828

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion pkg/config/config_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/testdata/containers_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down