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

add 'docker network' support #1262

Merged
merged 9 commits into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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