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

support docker info proxy details #1176

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 4 additions & 1 deletion pkg/cluster/internal/providers/docker/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ func runArgsForLoadBalancer(cfg *config.Cluster, name string, args []string) []s
}

func getProxyEnv(cfg *config.Cluster) (map[string]string, error) {
envs := common.GetProxyEnvs(cfg)
envs, err := common.GetProxyEnvs(cfg)
if err != nil {
return nil, err
}
// Specifically add the docker network subnets to NO_PROXY if we are using a proxy
if len(envs) > 0 {
// Docker default bridge network is named "bridge" (https://docs.docker.com/network/bridge/#use-the-default-bridge-network)
Expand Down
53 changes: 49 additions & 4 deletions pkg/cluster/internal/providers/provider/common/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"os"
"strings"

"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"

"sigs.k8s.io/kind/pkg/internal/apis/config"
)

Expand All @@ -34,11 +37,11 @@ const (

// GetProxyEnvs returns a map of proxy environment variables to their values
// If proxy settings are set, NO_PROXY is modified to include the cluster subnets
func GetProxyEnvs(cfg *config.Cluster) map[string]string {
return getProxyEnvs(cfg, os.Getenv)
func GetProxyEnvs(cfg *config.Cluster) (map[string]string, error) {
return getProxyEnvs(cfg, os.Getenv, exec.DefaultCmder)
}

func getProxyEnvs(cfg *config.Cluster, getEnv func(string) string) map[string]string {
func getProxyEnvs(cfg *config.Cluster, getEnv func(string) string, cmder exec.Cmder) (map[string]string, error) {
envs := make(map[string]string)
for _, name := range []string{HTTPProxy, HTTPSProxy, NOProxy} {
val := getEnv(name)
Expand All @@ -50,6 +53,18 @@ func getProxyEnvs(cfg *config.Cluster, getEnv func(string) string) map[string]st
envs[strings.ToLower(name)] = val
}
}

// if we didn't detect proxy environment variable settings, probe docker
// we prefer the proxy env to allow more explicit per-invocation override
// of proxy settings
if len(envs) < 1 {
e, err := getProxyEnvFromDocker(cmder)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: probably re-work this so some providers don't do this? need to think about this more, but that shouldn't be hard and it's academic at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it be an interface getProxyEnvFromProvider?

if err != nil {
return nil, err
}
envs = e
}

// Specifically add the cluster subnets to NO_PROXY if we are using a proxy
if len(envs) > 0 {
noProxy := envs[NOProxy]
Expand All @@ -60,5 +75,35 @@ func getProxyEnvs(cfg *config.Cluster, getEnv func(string) string) map[string]st
envs[NOProxy] = noProxy
envs[strings.ToLower(NOProxy)] = noProxy
}
return envs
return envs, nil
}

// obtains proxy information from docker via docker info against cmder
func getProxyEnvFromDocker(cmder exec.Cmder) (map[string]string, error) {
// get raw fields
lines, err := exec.OutputLines(
cmder.Command(
"docker", "info",
"--format",
// one per line, upper(key)=value
"HTTP_PROXY={{.HTTPProxy}}\nHTTPS_PROXY={{.HTTPSProxy}}\nNO_PROXY={{.NoProxy}}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this format stable?
This is what I have in my local machine

$ docker info --format "HTTP_PROXY={{.HTTPProxy}}\nHTTPS_PROXY={{.HTTPSProxy}}\nNO_PROXY={{.NoProxy}}"
HTTP_PROXY=\nHTTPS_PROXY=\nNO_PROXY=
$

),
)
if err != nil {
return nil, errors.Wrap(err, "failed to get proxy information from docker")
}
// parse out environment settings from the format we specified
envs := make(map[string]string)
for _, line := range lines {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check that the number of lines is 3 and the names of the variables []string{HTTPProxy, HTTPSProxy, NOProxy}
to be more robust.
If docker outputs whatever message instead of the variables we are going to return this wrong info

Copy link
Member Author

@BenTheElder BenTheElder Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • we control the format
  • error output goes to stderr, not stdout

parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
return nil, errors.Errorf("failed to parse docker proxy information %q", line)
}
name, val := parts[0], parts[1]
if val != "" {
envs[name] = val
envs[strings.ToLower(name)] = val
}
}
return envs, nil
}
70 changes: 56 additions & 14 deletions pkg/cluster/internal/providers/provider/common/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@ limitations under the License.
package common

import (
"errors"
"testing"

"sigs.k8s.io/kind/pkg/exec"

"sigs.k8s.io/kind/pkg/internal/apis/config"
"sigs.k8s.io/kind/pkg/internal/assert"
)

func TestGetProxyEnvs(t *testing.T) {
t.Parallel()
// first test the public method
cfg := &config.Cluster{}
config.SetDefaultsCluster(cfg)
envs := GetProxyEnvs(cfg)
// GetProxyEnvs should always reutrn a valid map
if envs == nil {
t.Errorf("GetProxyEnvs returned nil but should not")
}

// now test the internal one (with all of the logic)
cases := []struct {
name string
cluster *config.Cluster
env map[string]string
want map[string]string
name string
cluster *config.Cluster
env map[string]string
dockerInfoOut string
dockerInfoErr error
want map[string]string
expectError bool
}{
{
name: "No environment variables",
Expand All @@ -51,6 +49,17 @@ func TestGetProxyEnvs(t *testing.T) {
}(),
want: map[string]string{},
},
{
name: "No environment variables, but docker config",
cluster: func() *config.Cluster {
c := config.Cluster{}
c.Networking.ServiceSubnet = "10.0.0.0/24"
c.Networking.PodSubnet = "12.0.0.0/24"
return &c
}(),
dockerInfoOut: "HTTP_PROXY=5.5.5.5\nHTTPS_PROXY=5.5.5.5\nNO_PROXY=localhost",
want: map[string]string{"HTTPS_PROXY": "5.5.5.5", "https_proxy": "5.5.5.5", "HTTP_PROXY": "5.5.5.5", "http_proxy": "5.5.5.5", "NO_PROXY": "localhost,10.0.0.0/24,12.0.0.0/24", "no_proxy": "localhost,10.0.0.0/24,12.0.0.0/24"},
},
{
name: "HTTP_PROXY environment variables",
cluster: func() *config.Cluster {
Expand Down Expand Up @@ -91,17 +100,50 @@ func TestGetProxyEnvs(t *testing.T) {
},
want: map[string]string{"HTTPS_PROXY": "5.5.5.5", "https_proxy": "5.5.5.5", "NO_PROXY": "8.8.8.8,10.0.0.0/24,12.0.0.0/24", "no_proxy": "8.8.8.8,10.0.0.0/24,12.0.0.0/24"},
},
{
name: "Invalid docker config",
cluster: func() *config.Cluster {
c := config.Cluster{}
c.Networking.ServiceSubnet = "10.0.0.0/24"
c.Networking.PodSubnet = "12.0.0.0/24"
return &c
}(),
dockerInfoOut: ".....",
expectError: true,
},
{
name: "Failed to exec docker info",
cluster: func() *config.Cluster {
c := config.Cluster{}
c.Networking.ServiceSubnet = "10.0.0.0/24"
c.Networking.PodSubnet = "12.0.0.0/24"
return &c
}(),
dockerInfoErr: errors.New("error"),
expectError: true,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
result := getProxyEnvs(tc.cluster, func(e string) string {
// fake out getting env
getEnvFake := func(e string) string {
if tc.env == nil {
return ""
}
return tc.env[e]
})
}
// fake out docker info --format ...
getProxyEnvFromDockerFake := &exec.FakeCmder{
FakeCmd: exec.FakeCmd{
Out: []byte(tc.dockerInfoOut),
Error: tc.dockerInfoErr,
},
}
// actuall test
result, err := getProxyEnvs(tc.cluster, getEnvFake, getProxyEnvFromDockerFake)
assert.ExpectError(t, tc.expectError, err)
assert.DeepEqual(t, tc.want, result)
})
}
Expand Down
74 changes: 74 additions & 0 deletions pkg/exec/fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package exec

import "io"

// FakeCmder implements a very simple Cmder fake that returns a copy of
// the FakeCmd each time
// WARNING: this API is not yet stable! It will change!
// TODO(bentheelder): design a more robust exec fake
type FakeCmder struct {
FakeCmd
}

// FakeCmd implements a very simple Cmd fake
// WARNING: this API is not yet stable! It will change!
// TODO(bentheelder): design a more robust exec fake
type FakeCmd struct {
Out []byte
ErrOut []byte
Error error
Stdout io.Writer
Stderr io.Writer
}

// Command implements Cmder.Command
func (f *FakeCmder) Command(name string, arg ...string) Cmd {
f2 := f.FakeCmd
return &f2
}

// Run implements Cmd.Run
func (f *FakeCmd) Run() error {
if f.Stdout != nil {
// TODO: how should the fake handle these errors?
_, _ = f.Stdout.Write(f.Out)
}
if f.Stderr != nil {
_, _ = f.Stderr.Write(f.ErrOut)
}
return f.Error
}

// SetEnv "implements" Cmd.SetEnv but discards the input
func (f *FakeCmd) SetEnv(e ...string) Cmd { return f }

// SetStdin "implements" Cmd.SetStdin but discards the input
func (f *FakeCmd) SetStdin(r io.Reader) Cmd { return f }

// SetStdout implements Cmd.SetStdout
func (f *FakeCmd) SetStdout(s io.Writer) Cmd {
f.Stdout = s
return f
}

// SetStderr implements Cmd.SetStderr
func (f *FakeCmd) SetStderr(s io.Writer) Cmd {
f.Stderr = s
return f
}
14 changes: 7 additions & 7 deletions pkg/exec/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func RunErrorForError(err error) *RunError {
// stderr + stdout, it scans these for lines and returns a slice of output lines
func CombinedOutputLines(cmd Cmd) (lines []string, err error) {
var buff bytes.Buffer
cmd.SetStdout(&buff)
cmd.SetStderr(&buff)
cmd = cmd.SetStdout(&buff)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, we need the assignment to make the code testable with the new fakeCmder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At one point we did. This is more correct regardless.

cmd = cmd.SetStderr(&buff)
err = cmd.Run()
scanner := bufio.NewScanner(&buff)
for scanner.Scan() {
Expand All @@ -77,7 +77,7 @@ func CombinedOutputLines(cmd Cmd) (lines []string, err error) {
// stdout, it scans these for lines and returns a slice of output lines
func OutputLines(cmd Cmd) (lines []string, err error) {
var buff bytes.Buffer
cmd.SetStdout(&buff)
cmd = cmd.SetStdout(&buff)
err = cmd.Run()
scanner := bufio.NewScanner(&buff)
for scanner.Scan() {
Expand All @@ -88,8 +88,8 @@ func OutputLines(cmd Cmd) (lines []string, err error) {

// InheritOutput sets cmd's output to write to the current process's stdout and stderr
func InheritOutput(cmd Cmd) Cmd {
cmd.SetStderr(os.Stderr)
cmd.SetStdout(os.Stdout)
cmd = cmd.SetStderr(os.Stderr)
cmd = cmd.SetStdout(os.Stdout)
return cmd
}

Expand All @@ -101,7 +101,7 @@ func RunWithStdoutReader(cmd Cmd, readerFunc func(io.Reader) error) error {
}
defer pw.Close()
defer pr.Close()
cmd.SetStdout(pw)
cmd = cmd.SetStdout(pw)

return errors.AggregateConcurrent([]func() error{
func() error {
Expand All @@ -121,7 +121,7 @@ func RunWithStdinWriter(cmd Cmd, writerFunc func(io.Writer) error) error {
}
defer pw.Close()
defer pr.Close()
cmd.SetStdin(pr)
cmd = cmd.SetStdin(pr)

return errors.AggregateConcurrent([]func() error{
func() error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/internal/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func StringEqual(t testingDotT, expected, result string) {
if expected != result {
t.Errorf("Strings did not match!")
t.Errorf("Expected: %q", expected)
t.Errorf("But got: %q", result)
t.Errorf("Received: %q", result)
}
}

Expand All @@ -49,7 +49,7 @@ func StringEqual(t testingDotT, expected, result string) {
func DeepEqual(t testingDotT, expected, result interface{}) {
if !reflect.DeepEqual(expected, result) {
t.Errorf("Result did not DeepEqual Expected!")
t.Errorf("Expected: %+v", expected)
t.Errorf("But got: %+v", result)
t.Errorf("Expected: %#+v", expected)
t.Errorf("Received: %#+v", result)
}
}