Skip to content

Commit

Permalink
Merge pull request #7355 from rhatdan/env
Browse files Browse the repository at this point in the history
Use environment from containers.conf
  • Loading branch information
openshift-merge-robot authored Aug 28, 2020
2 parents bd43f81 + 60fe961 commit 522a32f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
29 changes: 7 additions & 22 deletions cmd/podman/common/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,11 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
// ENVIRONMENT VARIABLES
//
// Precedence order (higher index wins):
// 1) env-host, 2) image data, 3) env-file, 4) env
env := map[string]string{
"container": "podman",
}
// 1) containers.conf (EnvHost, EnvHTTP, Env) 2) image data, 3 User EnvHost/EnvHTTP, 4) env-file, 5) env
// containers.conf handled and image data handled on the server side
// user specified EnvHost and EnvHTTP handled on Server Side relative to Server
// env-file and env handled on client side
var env map[string]string

// First transform the os env into a map. We need it for the labels later in
// any case.
Expand All @@ -319,24 +320,8 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
return errors.Wrap(err, "error parsing host environment variables")
}

if c.EnvHost {
env = envLib.Join(env, osEnv)
} else if c.HTTPProxy {
for _, envSpec := range []string{
"http_proxy",
"HTTP_PROXY",
"https_proxy",
"HTTPS_PROXY",
"ftp_proxy",
"FTP_PROXY",
"no_proxy",
"NO_PROXY",
} {
if v, ok := osEnv[envSpec]; ok {
env[envSpec] = v
}
}
}
s.EnvHost = c.EnvHost
s.HTTPProxy = c.HTTPProxy

// env-file overrides any previous variables
for _, f := range c.EnvFile {
Expand Down
40 changes: 36 additions & 4 deletions pkg/specgen/generate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generate

import (
"context"
"os"

"github.com/containers/image/v5/manifest"
"github.com/containers/podman/v2/libpod"
Expand Down Expand Up @@ -62,14 +63,24 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
if err != nil {
return nil, err
}
// Get Default Environment
defaultEnvs, err := envLib.ParseSlice(rtc.Containers.Env)
// First transform the os env into a map. We need it for the labels later in
// any case.
osEnv, err := envLib.ParseSlice(os.Environ())
if err != nil {
return nil, errors.Wrap(err, "Env fields in containers.conf failed to parse")
return nil, errors.Wrap(err, "error parsing host environment variables")
}

// Get Default Environment from containers.conf
defaultEnvs, err := envLib.ParseSlice(rtc.GetDefaultEnv())
if err != nil {
return nil, errors.Wrap(err, "error parsing fields in containers.conf")
}
if defaultEnvs["containers"] == "" {
defaultEnvs["containers"] = "podman"
}
var envs map[string]string

// Image Environment defaults
if newImage != nil {
// Image envs from the image if they don't exist
// already, overriding the default environments
Expand All @@ -82,9 +93,30 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
if err != nil {
return nil, errors.Wrap(err, "Env fields from image failed to parse")
}
defaultEnvs = envLib.Join(defaultEnvs, envs)
}

// Caller Specified defaults
if s.EnvHost {
defaultEnvs = envLib.Join(defaultEnvs, osEnv)
} else if s.HTTPProxy {
for _, envSpec := range []string{
"http_proxy",
"HTTP_PROXY",
"https_proxy",
"HTTPS_PROXY",
"ftp_proxy",
"FTP_PROXY",
"no_proxy",
"NO_PROXY",
} {
if v, ok := osEnv[envSpec]; ok {
defaultEnvs[envSpec] = v
}
}
}

s.Env = envLib.Join(envLib.Join(defaultEnvs, envs), s.Env)
s.Env = envLib.Join(defaultEnvs, s.Env)

// Labels and Annotations
annotations := make(map[string]string)
Expand Down
7 changes: 7 additions & 0 deletions pkg/specgen/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ type ContainerBasicConfig struct {
// image's configuration.
// Optional.
Command []string `json:"command,omitempty"`
// EnvHost indicates that the host environment should be added to container
// Optional.
EnvHost bool `json:"env_host,omitempty"`
// EnvHTTPProxy indicates that the http host proxy environment variables
// should be added to container
// Optional.
HTTPProxy bool `json:"httpproxy,omitempty"`
// Env is a set of environment variables that will be set in the
// container.
// Optional.
Expand Down

0 comments on commit 522a32f

Please sign in to comment.