refactor weaveCIDRsFromConfig to return an error message
This commit is contained in:
1 parent
9619a27f8c
commit
c6fa9cb17e
4 files changed
+25
-26
No files matched your search
@@ -46,7 +46,9 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if cidrs, ok := i.proxy.weaveCIDRsFromConfig(container.Config, container.HostConfig); ok {
|
||||
if cidrs, err := i.proxy.weaveCIDRsFromConfig(container.Config, container.HostConfig); err != nil {
|
||||
Info.Printf("Ignoring container due to %s", err)
|
||||
} else {
|
||||
Info.Printf("Creating container with WEAVE_CIDR \"%s\"", strings.Join(cidrs, " "))
|
||||
if container.HostConfig == nil {
|
||||
container.HostConfig = &docker.HostConfig{}
|
||||
|
||||
@@ -30,8 +30,10 @@ func (i *createExecInterceptor) InterceptRequest(r *http.Request) error {
|
||||
}
|
||||
|
||||
_, hasWeaveWait := container.Volumes["/w"]
|
||||
cidrs, hasCIDRs := i.proxy.weaveCIDRsFromConfig(container.Config, container.HostConfig)
|
||||
if hasCIDRs && hasWeaveWait {
|
||||
cidrs, err := i.proxy.weaveCIDRsFromConfig(container.Config, container.HostConfig)
|
||||
if err != nil {
|
||||
Info.Printf("Ignoring container %s due to %s", container.ID, err)
|
||||
} else if hasWeaveWait {
|
||||
Info.Printf("Exec in container %s with WEAVE_CIDR \"%s\"", container.ID, strings.Join(cidrs, " "))
|
||||
cmd := append(weaveWaitEntrypoint, "-s")
|
||||
options.Cmd = append(cmd, options.Cmd...)
|
||||
|
||||
+15
-16
@@ -2,6 +2,7 @@ package proxy
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -22,6 +23,10 @@ var (
|
||||
containerCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/create$")
|
||||
containerStartRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/(re)?start$")
|
||||
execCreateRegexp = regexp.MustCompile("^(/v[0-9\\.]*)?/containers/[^/]*/exec$")
|
||||
|
||||
ErrInvalidNetworkMode = errors.New("--net option")
|
||||
ErrWeaveCIDRNone = errors.New("WEAVE_CIDR=none")
|
||||
ErrNoDefaultIPAM = errors.New("--no-default-ipam option")
|
||||
)
|
||||
|
||||
type Proxy struct {
|
||||
@@ -115,28 +120,22 @@ func (proxy *Proxy) ListenAndServe() error {
|
||||
return (&http.Server{Handler: proxy}).Serve(listener)
|
||||
}
|
||||
|
||||
func (proxy *Proxy) weaveCIDRsFromConfig(config *docker.Config, hostConfig *docker.HostConfig) ([]string, bool) {
|
||||
if !validNetworkMode(hostConfig) {
|
||||
return nil, false
|
||||
func (proxy *Proxy) weaveCIDRsFromConfig(config *docker.Config, hostConfig *docker.HostConfig) ([]string, error) {
|
||||
if hostConfig != nil &&
|
||||
hostConfig.NetworkMode != "" &&
|
||||
hostConfig.NetworkMode != "bridge" {
|
||||
return nil, ErrInvalidNetworkMode
|
||||
}
|
||||
for _, e := range config.Env {
|
||||
if strings.HasPrefix(e, "WEAVE_CIDR=") {
|
||||
if e[11:] == "none" {
|
||||
return nil, false
|
||||
return nil, ErrWeaveCIDRNone
|
||||
}
|
||||
return strings.Fields(e[11:]), true
|
||||
return strings.Fields(e[11:]), nil
|
||||
}
|
||||
}
|
||||
return nil, !proxy.NoDefaultIPAM
|
||||
}
|
||||
|
||||
func validNetworkMode(hostConfig *docker.HostConfig) bool {
|
||||
return networkMode(hostConfig) == "bridge"
|
||||
}
|
||||
|
||||
func networkMode(hostConfig *docker.HostConfig) string {
|
||||
if hostConfig == nil || hostConfig.NetworkMode == "" {
|
||||
return "bridge"
|
||||
if proxy.NoDefaultIPAM {
|
||||
return nil, ErrNoDefaultIPAM
|
||||
}
|
||||
return hostConfig.NetworkMode
|
||||
return nil, nil
|
||||
}
|
||||
@@ -21,13 +21,9 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !validNetworkMode(container.HostConfig) {
|
||||
Debug.Printf("Ignoring container %s with --net=%s", container.ID, networkMode(container.HostConfig))
|
||||
return nil
|
||||
}
|
||||
cidrs, ok := i.proxy.weaveCIDRsFromConfig(container.Config, container.HostConfig)
|
||||
if !ok {
|
||||
Debug.Print("No Weave CIDR, ignoring")
|
||||
cidrs, err := i.proxy.weaveCIDRsFromConfig(container.Config, container.HostConfig)
|
||||
if err != nil {
|
||||
Info.Printf("Ignoring container %s due to %s", container.ID, err)
|
||||
return nil
|
||||
}
|
||||
Info.Printf("Attaching container %s with WEAVE_CIDR \"%s\" to weave network", container.ID, strings.Join(cidrs, " "))
|
||||
|
||||
Reference in new issue
Block a user