Skip to content

Commit

Permalink
feature: add pouch network manager
Browse files Browse the repository at this point in the history
Add pouch network manager.
With libnetwork to implement the network manager in pouch.
Use git submodule to manager the libnetwork package.

Signed-off-by: Rudy Zhang <[email protected]>
  • Loading branch information
rudyfly committed Dec 26, 2017
1 parent 5823749 commit bdd4252
Show file tree
Hide file tree
Showing 26 changed files with 1,254 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "extra/libnetwork"]
path = extra/libnetwork
url = https://github.com/docker/libnetwork
25 changes: 16 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
GOBUILD=go build
GOCLEAN=go clean
GOTEST=go test
GOPATH=$(shell go env GOPATH)
GOPACKAGES=$(shell go list ./... | grep -v /vendor/ | sed 's/^_//')
ORIG_GOPATH=$(shell go env GOPATH)
GOPATH=$(ORIG_GOPATH):$(ORIG_GOPATH)/src/github.com/docker/libnetwork/Godeps/_workspace
GOPACKAGES=$(shell go list ./... | grep -v /vendor/ | grep -v /extra/ | sed 's/^_//')

# Binary name of CLI and Daemon
BINARY_NAME=pouchd
Expand All @@ -13,7 +14,13 @@ CLI_BINARY_NAME=pouch
DESTDIR=/usr/local

.PHONY: build
build: server client
build: pre server client

.PHONY: pre
pre:
@ mkdir -p $(ORIG_GOPATH)/src/github.com/docker
@ - [ -L $(ORIG_GOPATH)/src/github.com/docker/libnetwork ] && rm -f $(ORIG_GOPATH)/src/github.com/docker/libnetwork
@ - ln -s $(shell pwd)/extra/libnetwork $(ORIG_GOPATH)/src/github.com/docker/libnetwork

.PHONY: server
server: modules
Expand All @@ -36,27 +43,27 @@ check: fmt lint vet validate-swagger
.PHONY: fmt
fmt: ## run go fmt
@echo $@
@test -z "$$(gofmt -s -l . 2>/dev/null | grep -Fv 'vendor/' | grep -v ".pb.go$$" | tee /dev/stderr)" || \
@test -z "$$(gofmt -s -l . 2>/dev/null | grep -Fv 'vendor/' | grep -Fv 'extra/' | grep -v ".pb.go$$" | tee /dev/stderr)" || \
(echo "please format Go code with 'gofmt -s -w'" && false)
@test -z "$$(find . -path ./vendor -prune -o ! -name timestamp.proto ! -name duration.proto -name '*.proto' -type f -exec grep -Hn -e "^ " {} \; | tee /dev/stderr)" || \
@test -z "$$(find . -path ./vendor -prune -o ! -path ./extra -prune -o ! -name timestamp.proto ! -name duration.proto -name '*.proto' -type f -exec grep -Hn -e "^ " {} \; | tee /dev/stderr)" || \
(echo "please indent proto files with tabs only" && false)
@test -z "$$(find . -path ./vendor -prune -o -name '*.proto' -type f -exec grep -Hn "Meta meta = " {} \; | grep -v '(gogoproto.nullable) = false' | tee /dev/stderr)" || \
@test -z "$$(find . -path ./vendor -prune -o ! -path ./extra -prune -o -name '*.proto' -type f -exec grep -Hn "Meta meta = " {} \; | grep -v '(gogoproto.nullable) = false' | tee /dev/stderr)" || \
(echo "meta fields in proto files must have option (gogoproto.nullable) = false" && false)

.PHONY: lint
lint: ## run go lint
@echo $@
@test -z "$$(golint ./... | grep -Fv 'vendor/' | grep -v ".pb.go:" | tee /dev/stderr)"
@test -z "$$(golint ./... | grep -Fv 'vendor/' | grep -Fv 'extra' | grep -v ".pb.go:" | tee /dev/stderr)"

.PHONY: vet
vet: # run go vet
@echo $@
@test -z "$$(go vet ${GOPACKAGES} 2>&1 | grep -v "unrecognized printf verb 'r'" | egrep -v '(exit status 1)' | tee /dev/stderr)"

.PHONY: unit-test
unit-test: ## run go test
unit-test: pre ## run go test
@echo $@
@go test `go list ./... | grep -v 'github.com/alibaba/pouch/test'`
@go test `go list ./... | grep -v 'github.com/alibaba/pouch/test' | grep -v 'github.com/alibaba/pouch/extra'`

.PHONY: validate-swagger
validate-swagger: ## run swagger validate
Expand Down
35 changes: 35 additions & 0 deletions apis/server/network_bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package server

import (
"context"
"encoding/json"
"net/http"

"github.com/alibaba/pouch/apis/types"
)

func (s *Server) createNetwork(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
var networkCreateReq types.NetworkCreateRequest

if err := json.NewDecoder(req.Body).Decode(&networkCreateReq); err != nil {
resp.WriteHeader(http.StatusBadRequest)
return err
}

network, err := s.NetworkMgr.NetworkCreate(ctx, networkCreateReq)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
return err
}

networkCreateResp := types.NetworkCreateResponse{
ID: network.ID,
}

resp.WriteHeader(http.StatusCreated)
return json.NewEncoder(resp).Encode(networkCreateResp)
}

func (s *Server) deleteNetwork(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
return nil
}
4 changes: 4 additions & 0 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func initRoute(s *Server) http.Handler {
// metrics
r.Path("/metrics").Methods(http.MethodGet).Handler(prometheus.Handler())

// network
r.Path("/networks/create").Methods(http.MethodPost).Handler(s.filter(s.createNetwork))
r.Path("/networks/{id:.*}").Methods(http.MethodDelete).Handler(s.filter(s.deleteNetwork))

if s.Config.Debug {
profilerSetup(r)
}
Expand Down
1 change: 1 addition & 0 deletions apis/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Server struct {
SystemMgr mgr.SystemMgr
ImageMgr mgr.ImageMgr
VolumeMgr mgr.VolumeMgr
NetworkMgr mgr.NetworkMgr
listeners []net.Listener
}

Expand Down
126 changes: 125 additions & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,33 @@ paths:
schema:
$ref: "#/definitions/VolumeCreateConfig"
tags: ["Volume"]

/networks/create:
post:
summary: "Create a network"
operationId: "NetworkCreate"
consumes: ["application/json"]
produces: ["application/json"]
responses:
201:
description: "The network was created successfully"
schema:
$ref: "#/definitions/NetworkCreateResponse"
400:
description: "bad parameter"
schema:
$ref: "#/definitions/Error"
500:
description: "Server error"
schema:
$ref: "#/definitions/Error"
parameters:
- name: "NetworkCreateRequest"
in: "body"
required: true
description: "Network configuration"
schema:
$ref: "#/definitions/NetworkCreateRequest"
tags: ["Network"]
definitions:
Error:
type: "object"
Expand Down Expand Up @@ -1622,3 +1648,101 @@ definitions:
type: "string"
example: "4443"

NetworkCreateRequest:
type: "object"
description: "contains the request for the remote API: POST /networks/create"
properties:
Name:
description: "Name is the name of the network."
type: "string"
NetworkCreate:
$ref: "#/definitions/NetworkCreate"

NetworkCreateResponse:
type: "object"
description: "contains the response for the remote API: POST /networks/create"
properties:
ID:
description: "ID is the id of the network."
type: "string"
Warning:
description: "Warning means the message of create network result."
type: "string"

NetworkCreate:
type: "object"
description: "is the expected body of the \"create network\" http request message"
properties:
CheckDuplicate:
type: "boolean"
description: "CheckDuplicate is used to check the network is duplicate or not."
Driver:
type: "string"
description: "Driver means the network's driver."
Scope:
type: "string"
description: "Scope means the network's scope."
EnableIPv6:
type: "boolean"
IPAM:
type: "object"
$ref: "#/definitions/IPAM"
Internal:
type: "boolean"
description: "Internal checks the network is internal network or not."
Attachable:
type: "boolean"
description: "Attachable means the network can be attached or not."
Ingress:
type: "boolean"
description: "Ingress checks the network is ingress network or not."
ConfigOnly:
type: "boolean"
ConfigFrom:
type: "object"
$ref: "#/definitions/ConfigReference"
Options:
type: "object"
additionalProperties:
type: "string"
Labels:
type: "object"
additionalProperties:
type: "string"

IPAM:
type: "object"
description: "represents IP Address Management"
properties:
Driver:
type: "string"
Options:
type: "object"
additionalProperties:
type: "string"
Config:
type: "array"
items:
$ref: '#/definitions/IPAMConfig'

IPAMConfig:
description: "represents IPAM configurations"
type: "object"
properties:
Subnet:
type: "string"
IPRange:
type: "string"
Gateway:
type: "string"
AuxAddress:
type: "object"
additionalProperties:
type: "string"

ConfigReference:
type: "object"
description: "specifies the source which provides a network's configuration"
properties:
Network:
type: "string"
52 changes: 52 additions & 0 deletions apis/types/config_reference.go

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

Loading

0 comments on commit bdd4252

Please sign in to comment.