Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1262 from vieux/libnetwork
Browse files Browse the repository at this point in the history
add 'docker network' support
  • Loading branch information
abronan committed Oct 13, 2015
2 parents 2532d1b + ca9a234 commit 1e30ce2
Show file tree
Hide file tree
Showing 19 changed files with 724 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Godeps/_workspace/src/github.com/samalba/dockerclient/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(images)
}

// GET /networks
func getNetworks(c *context, w http.ResponseWriter, r *http.Request) {
out := []*dockerclient.NetworkResource{}
for _, network := range c.cluster.Networks() {
tmp := (*network).NetworkResource
tmp.Name = network.Engine.Name + "/" + network.Name
out = append(out, &tmp)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(out)
}

// GET /volumes
func getVolumes(c *context, w http.ResponseWriter, r *http.Request) {
volumes := struct {
Expand Down Expand Up @@ -415,6 +427,29 @@ func deleteContainers(c *context, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}

// POST /networks/create
func postNetworksCreate(c *context, w http.ResponseWriter, r *http.Request) {
var request dockerclient.NetworkCreate

if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
httpError(w, err.Error(), http.StatusBadRequest)
return
}

if request.Driver == "" {
request.Driver = "overlay"
}

response, err := c.cluster.CreateNetwork(&request)
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}

// POST /volumes
func postVolumes(c *context, w http.ResponseWriter, r *http.Request) {
var request dockerclient.VolumeCreateRequest
Expand Down Expand Up @@ -637,6 +672,27 @@ func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
json.NewEncoder(NewWriteFlusher(w)).Encode(out)
}

// DELETE /networks/{networkid:.*}
func deleteNetworks(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}

var id = mux.Vars(r)["networkid"]

if network := c.cluster.Networks().Get(id); network != nil {
if err := c.cluster.RemoveNetwork(network); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
httpError(w, fmt.Sprintf("No such network %s", id), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNoContent)
}

// DELETE /volumes/{names:.*}
func deleteVolumes(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
Expand All @@ -663,6 +719,20 @@ func ping(c *context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte{'O', 'K'})
}

// Proxy a request to the right node
func proxyNetwork(c *context, w http.ResponseWriter, r *http.Request) {
var id = mux.Vars(r)["networkid"]
if network := c.cluster.Networks().Get(id); network != nil {

// Set the network ID in the proxied URL path.
r.URL.Path = strings.Replace(r.URL.Path, id, network.ID, 1)

proxy(c.tlsConfig, network.Engine.Addr, w, r)
return
}
httpError(w, fmt.Sprintf("No such network: %s", id), http.StatusNotFound)
}

// Proxy a request to the right node
func proxyVolume(c *context, w http.ResponseWriter, r *http.Request) {
var name = mux.Vars(r)["volumename"]
Expand Down
58 changes: 32 additions & 26 deletions api/primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,47 @@ var routes = map[string]map[string]handler{
"/containers/{name:.*}/stats": proxyContainer,
"/containers/{name:.*}/attach/ws": proxyHijack,
"/exec/{execid:.*}/json": proxyContainer,
"/networks": getNetworks,
"/networks/{networkid:.*}": proxyNetwork,
"/volumes": getVolumes,
"/volumes/{volumename:.*}": proxyVolume,
},
"POST": {
"/auth": proxyRandom,
"/commit": postCommit,
"/build": postBuild,
"/images/create": postImagesCreate,
"/images/load": postImagesLoad,
"/images/{name:.*}/push": proxyImageTagOptional,
"/images/{name:.*}/tag": postTagImage,
"/containers/create": postContainersCreate,
"/containers/{name:.*}/kill": proxyContainerAndForceRefresh,
"/containers/{name:.*}/pause": proxyContainerAndForceRefresh,
"/containers/{name:.*}/unpause": proxyContainerAndForceRefresh,
"/containers/{name:.*}/rename": postRenameContainer,
"/containers/{name:.*}/restart": proxyContainerAndForceRefresh,
"/containers/{name:.*}/start": proxyContainerAndForceRefresh,
"/containers/{name:.*}/stop": proxyContainerAndForceRefresh,
"/containers/{name:.*}/wait": proxyContainerAndForceRefresh,
"/containers/{name:.*}/resize": proxyContainer,
"/containers/{name:.*}/attach": proxyHijack,
"/containers/{name:.*}/copy": proxyContainer,
"/containers/{name:.*}/exec": postContainersExec,
"/exec/{execid:.*}/start": postExecStart,
"/exec/{execid:.*}/resize": proxyContainer,
"/volumes": postVolumes,
"/auth": proxyRandom,
"/commit": postCommit,
"/build": postBuild,
"/images/create": postImagesCreate,
"/images/load": postImagesLoad,
"/images/{name:.*}/push": proxyImageTagOptional,
"/images/{name:.*}/tag": postTagImage,
"/containers/create": postContainersCreate,
"/containers/{name:.*}/kill": proxyContainerAndForceRefresh,
"/containers/{name:.*}/pause": proxyContainerAndForceRefresh,
"/containers/{name:.*}/unpause": proxyContainerAndForceRefresh,
"/containers/{name:.*}/rename": postRenameContainer,
"/containers/{name:.*}/restart": proxyContainerAndForceRefresh,
"/containers/{name:.*}/start": proxyContainerAndForceRefresh,
"/containers/{name:.*}/stop": proxyContainerAndForceRefresh,
"/containers/{name:.*}/wait": proxyContainerAndForceRefresh,
"/containers/{name:.*}/resize": proxyContainer,
"/containers/{name:.*}/attach": proxyHijack,
"/containers/{name:.*}/copy": proxyContainer,
"/containers/{name:.*}/exec": postContainersExec,
"/exec/{execid:.*}/start": postExecStart,
"/exec/{execid:.*}/resize": proxyContainer,
"/networks/create": postNetworksCreate,
"/networks/{networkid:.*}/connect": proxyNetwork,
"/networks/{networkid:.*}/disconnect": proxyNetwork,
"/volumes": postVolumes,
},
"PUT": {
"/containers/{name:.*}/archive": proxyContainer,
},
"DELETE": {
"/containers/{name:.*}": deleteContainers,
"/images/{name:.*}": deleteImages,
"/volumes/{name:.*}": deleteVolumes,
"/containers/{name:.*}": deleteContainers,
"/images/{name:.*}": deleteImages,
"/networks/{networkid:.*}": deleteNetworks,
"/volumes/{name:.*}": deleteVolumes,
},
"OPTIONS": {
"": optionsHandler,
Expand Down
Loading

0 comments on commit 1e30ce2

Please sign in to comment.