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