some refactoring and tidying

- some inlining
- compile regexps once
- consistent error messages
- always mention container id in startContainerInterceptor errors
This commit is contained in:
Matthias Radestock authored and Paul Bellamy committed 2015-05-19 10:34:46 +01:00
1 parent ca4b8e6f8e
commit 126d4f4079
4 files changed
+51 -84

No files matched your search

+21 -25
View File
@@ -9,6 +9,8 @@ import (
. "github.com/weaveworks/weave/common"
)
var containerIDRegexp = regexp.MustCompile("^/v[0-9\\.]*/containers/([^/]*)/.*")
type startContainerInterceptor struct {
client *docker.Client
withDNS bool
@@ -18,39 +20,33 @@ func (i *startContainerInterceptor) InterceptRequest(r *http.Request) (*http.Req
return r, nil
}
func (i *startContainerInterceptor) InterceptResponse(res *http.Response) (*http.Response, error) {
containerID := containerFromPath(res.Request.URL.Path)
func (i *startContainerInterceptor) InterceptResponse(r *http.Response) (*http.Response, error) {
containerID := ""
if subs := containerIDRegexp.FindStringSubmatch(r.Request.URL.Path); subs == nil {
Warning.Printf("No container id found in request with path %s", r.Request.URL.Path)
return r, nil
} else {
containerID = subs[1]
}
container, err := i.client.InspectContainer(containerID)
if err != nil {
Warning.Print("Error Inspecting Container: ", err)
return res, nil
Warning.Printf("Error inspecting container %s: %v", containerID, err)
return r, nil
}
if cidrs, ok := weaveCIDRsFromConfig(container.Config); ok {
Info.Printf("Container %s was started with CIDR \"%s\"", containerID, strings.Join(cidrs, " "))
if err := i.attachContainerToWeave(containerID, cidrs); err != nil {
Warning.Print("Attaching container to weave failed: ", err)
return res, nil
}
} else {
cidrs, ok := weaveCIDRsFromConfig(container.Config)
if !ok {
Debug.Print("No Weave CIDR, ignoring")
return r, nil
}
return res, nil
}
func containerFromPath(path string) string {
if subs := regexp.MustCompile("^/v[0-9\\.]*/containers/([^/]*)/.*").FindStringSubmatch(path); subs != nil {
return subs[1]
}
return ""
}
func (i *startContainerInterceptor) attachContainerToWeave(containerID string, cidrs []string) error {
Info.Printf("Container %s was started with CIDR \"%s\"", containerID, strings.Join(cidrs, " "))
args := []string{"attach"}
args = append(args, cidrs...)
args = append(args, containerID)
_, err := callWeave(args...)
return err
if _, err := callWeave(args...); err != nil {
Warning.Printf("Attaching container %s to weave failed: %v", containerID, err)
return r, nil
}
return r, nil
}