-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
||
|
@@ -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) | ||
|
@@ -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) | ||
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] | ||
|
@@ -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}}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this format stable?
|
||
), | ||
) | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
} |
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
@@ -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() { | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 { | ||
|
@@ -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 { | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?