swarm IDs: Convert Swarm ID to Container ID in API Proxy.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2015-05-11 16:40:42 -07:00
parent b6a7c3d7f5
commit dde47ee650
3 changed files with 22 additions and 12 deletions

View File

@ -478,12 +478,15 @@ func ping(c *context, w http.ResponseWriter, r *http.Request) {
// Proxy a request to the right node
func proxyContainer(c *context, w http.ResponseWriter, r *http.Request) {
container, err := getContainerFromVars(c, mux.Vars(r))
name, container, err := getContainerFromVars(c, mux.Vars(r))
if err != nil {
httpError(w, err.Error(), http.StatusNotFound)
return
}
// Set the full container ID in the proxied URL path.
r.URL.Path = strings.Replace(r.URL.Path, name, container.Id, 1)
if err := proxy(c.tlsConfig, container.Engine.Addr, w, r); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
}
@ -579,11 +582,13 @@ func postCommit(c *context, w http.ResponseWriter, r *http.Request) {
vars["name"] = r.Form.Get("container")
// get container
container, err := getContainerFromVars(c, vars)
name, container, err := getContainerFromVars(c, vars)
if err != nil {
httpError(w, err.Error(), http.StatusNotFound)
return
}
// Set the full container ID in the proxied URL path.
r.URL.RawQuery = strings.Replace(r.URL.RawQuery, name, container.Id, 1)
cb := func(resp *http.Response) {
if resp.StatusCode == http.StatusCreated {
@ -599,7 +604,7 @@ func postCommit(c *context, w http.ResponseWriter, r *http.Request) {
// POST /containers/{name:.*}/rename
func postRenameContainer(c *context, w http.ResponseWriter, r *http.Request) {
container, err := getContainerFromVars(c, mux.Vars(r))
_, container, err := getContainerFromVars(c, mux.Vars(r))
if err != nil {
httpError(w, err.Error(), http.StatusNotFound)
return
@ -622,11 +627,13 @@ func postRenameContainer(c *context, w http.ResponseWriter, r *http.Request) {
// Proxy a hijack request to the right node
func proxyHijack(c *context, w http.ResponseWriter, r *http.Request) {
container, err := getContainerFromVars(c, mux.Vars(r))
name, container, err := getContainerFromVars(c, mux.Vars(r))
if err != nil {
httpError(w, err.Error(), http.StatusNotFound)
return
}
// Set the full container ID in the proxied URL path.
r.URL.Path = strings.Replace(r.URL.Path, name, container.Id, 1)
if err := hijack(c.tlsConfig, container.Engine.Addr, w, r); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)

View File

@ -26,25 +26,24 @@ func newClientAndScheme(tlsConfig *tls.Config) (*http.Client, string) {
return &http.Client{}, "http"
}
func getContainerFromVars(c *context, vars map[string]string) (*cluster.Container, error) {
func getContainerFromVars(c *context, vars map[string]string) (string, *cluster.Container, error) {
if name, ok := vars["name"]; ok {
if container := c.cluster.Container(name); container != nil {
return container, nil
return name, container, nil
}
return nil, fmt.Errorf("No such container: %s", name)
return name, nil, fmt.Errorf("No such container: %s", name)
}
if ID, ok := vars["execid"]; ok {
for _, container := range c.cluster.Containers() {
for _, execID := range container.Info.ExecIDs {
if ID == execID {
return container, nil
return "", container, nil
}
}
}
return nil, fmt.Errorf("Exec %s not found", ID)
return "", nil, fmt.Errorf("Exec %s not found", ID)
}
return nil, errors.New("Not found")
return "", nil, errors.New("Not found")
}
// from https://github.com/golang/go/blob/master/src/net/http/httputil/reverseproxy.go#L82

View File

@ -16,7 +16,7 @@ function teardown() {
docker_swarm run -d busybox true
# Create a container and get its Swarm ID back.
id=$(docker_swarm run -d busybox true)
id=$(docker_swarm run -d -i busybox sh -c "head -n 1; echo output")
swarm_id=$(docker_swarm inspect -f '{{ index .Config.Labels "com.docker.swarm.id" }}' "$id")
# Make sure we got a valid Swarm ID.
@ -25,6 +25,10 @@ function teardown() {
# API operations should work with Swarm IDs as well as Container IDs.
[[ $(docker_swarm inspect -f "{{ .Id }}" "$swarm_id") == "$id" ]]
# These should work with a Swarm ID.
docker_swarm logs "$swarm_id"
docker_swarm commit "$swarm_id"
attach_output=$(echo input | docker_swarm attach "$swarm_id")
# `docker ps` should be able to filter by Swarm ID using the label.
[[ $(docker_swarm ps -a -q --no-trunc --filter="label=com.docker.swarm.id=$swarm_id") == "$id" ]]