Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #114 from StrongMonkey/proxy-fix
Browse files Browse the repository at this point in the history
set proxy only for system container and override it
  • Loading branch information
Craig Jellick authored Dec 16, 2016
2 parents 91b2665 + b5a565c commit f4f3c0c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func DoInstanceActivate(instance model.Instance, host model.Host, progress *prog
return errors.Wrap(err, constants.DoInstanceActivateError+"failed to set up networking")
}

setupProxy(instance, &config)
setupProxy(instance, &config, getHostEntries())

setupCattleConfigURL(instance, &config)

Expand Down
54 changes: 51 additions & 3 deletions core/compute/compute_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,47 @@ func setupHeathConfig(instanceFields model.InstanceFields, config *container.Con
config.Healthcheck = healthConfig
}

func setupProxy(instance model.Instance, config *container.Config) {
for _, i := range constants.HTTPProxyList {
config.Env = append(config.Env, fmt.Sprintf("%v=%v", i, os.Getenv(i)))
func setupProxy(instance model.Instance, config *container.Config, hostEntries map[string]string) {
// only setup envs for system container
if instance.System {
envMap := map[string]string{}
envList := []string{}
for _, env := range config.Env {
/*
3 case:
1. foo=bar. Parse as normal
2. foo=. Parse as foo=
3. foo. Parse as foo
*/
part := strings.SplitN(env, "=", 2)
if len(part) == 1 {

if strings.Contains(env, "=") {
//case 2
envMap[part[0]] = ""
} else {
envList = append(envList, env)
}
} else if len(part) == 2 {
envMap[part[0]] = part[1]
}
}
for _, key := range constants.HTTPProxyList {
if hostEntries[key] != "" {
envMap[key] = hostEntries[key]
}
}
envs := []string{}
for _, env := range envList {
if _, ok := envMap[env]; ok {
continue
}
envs = append(envs, env)
}
for key, value := range envMap {
envs = append(envs, fmt.Sprintf("%v=%v", key, value))
}
config.Env = envs
}
}

Expand Down Expand Up @@ -342,3 +380,13 @@ func isRunning(dockerClient *client.Client, container types.Container) (bool, er
}
return false, err
}

func getHostEntries() map[string]string {
data := map[string]string{}
for _, env := range constants.HTTPProxyList {
if os.Getenv(env) != "" {
data[env] = os.Getenv(env)
}
}
return data
}
36 changes: 36 additions & 0 deletions core/compute/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,42 @@ func (s *ComputeTestSuite) TestDefaultValue(c *check.C) {
c.Assert(actual, check.Equals, "supersecretkey")
}

func (s *ComputeTestSuite) TestSetupProxy(c *check.C) {
instance := model.Instance{System: true}

// test case 1: test override by host_entries
config1 := &container.Config{Env: []string{"no_proxy=bar"}}
setupProxy(instance, config1, map[string]string{"no_proxy": "bar1"})
c.Assert(config1.Env, check.DeepEquals, []string{"no_proxy=bar1"})

// test case 2: ignore empty no_proxy value
config2 := &container.Config{Env: []string{}}
setupProxy(instance, config2, map[string]string{"no_proxy": ""})
c.Assert(config2.Env, check.DeepEquals, []string{})

// test case 3: no-equal case
config3 := &container.Config{Env: []string{"no_proxy=foo"}}
setupProxy(instance, config3, map[string]string{})
c.Assert(config3.Env, check.DeepEquals, []string{"no_proxy=foo"})

// test case 4: normal case
config4 := &container.Config{Env: []string{}}
setupProxy(instance, config4, map[string]string{"no_proxy": "foo"})
c.Assert(config4.Env, check.DeepEquals, []string{"no_proxy=foo"})

// test case 5: override no-equal case
config5 := &container.Config{Env: []string{"no_proxy"}}
setupProxy(instance, config5, map[string]string{"no_proxy": "foo"})
c.Assert(config5.Env, check.DeepEquals, []string{"no_proxy=foo"})

// test case 6: override non-setting
config6 := &container.Config{Env: []string{"no_proxy", "http_proxy=", "https_proxy=foo"}}
setupProxy(instance, config6, map[string]string{})
c.Assert(utils.SearchInList(config6.Env, "no_proxy"), check.Equals, true)
c.Assert(utils.SearchInList(config6.Env, "http_proxy="), check.Equals, true)
c.Assert(utils.SearchInList(config6.Env, "https_proxy=foo"), check.Equals, true)
}

func setupDeviceOptionsTest(hostConfig *container.HostConfig, instance *model.Instance, mockDevice string) {
deviceOptions := instance.Data.Fields.BlkioDeviceOptions

Expand Down
1 change: 1 addition & 0 deletions model/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Instance struct {
Token string
MilliCPUReservation int64
MemoryReservation int64
System bool
}

type InstanceFieldsData struct {
Expand Down

0 comments on commit f4f3c0c

Please sign in to comment.