1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
package router
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/rootless-containers/rootlesskit/v2/pkg/api"
"github.com/rootless-containers/rootlesskit/v2/pkg/httputil"
"github.com/rootless-containers/rootlesskit/v2/pkg/port"
"github.com/rootless-containers/rootlesskit/v2/pkg/version"
)
// NetworkDriver is implemented by network.ParentDriver
type NetworkDriver interface {
Info(context.Context) (*api.NetworkDriverInfo, error)
}
// PortDriver is implemented by port.ParentDriver
type PortDriver interface {
Info(context.Context) (*api.PortDriverInfo, error)
port.Manager
}
type Backend struct {
StateDir string
ChildPID int
// NetworkDriver can be nil
NetworkDriver NetworkDriver
// PortDriver MUST be thread-safe.
// PortDriver can be nil
PortDriver PortDriver
}
func (b *Backend) onPortDriverNil(w http.ResponseWriter, r *http.Request) {
httputil.WriteError(w, r, errors.New("no PortDriver is available"), http.StatusBadRequest)
}
// GetPorts is handler for GET /v{N}/ports
func (b *Backend) GetPorts(w http.ResponseWriter, r *http.Request) {
if b.PortDriver == nil {
b.onPortDriverNil(w, r)
return
}
ports, err := b.PortDriver.ListPorts(context.TODO())
if err != nil {
httputil.WriteError(w, r, err, http.StatusInternalServerError)
return
}
m, err := json.Marshal(ports)
if err != nil {
httputil.WriteError(w, r, err, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(m)
}
// PostPort is the handler for POST /v{N}/ports
func (b *Backend) PostPort(w http.ResponseWriter, r *http.Request) {
if b.PortDriver == nil {
b.onPortDriverNil(w, r)
return
}
decoder := json.NewDecoder(r.Body)
var portSpec port.Spec
if err := decoder.Decode(&portSpec); err != nil {
httputil.WriteError(w, r, err, http.StatusBadRequest)
return
}
portStatus, err := b.PortDriver.AddPort(context.TODO(), portSpec)
if err != nil {
httputil.WriteError(w, r, err, http.StatusBadRequest)
return
}
m, err := json.Marshal(portStatus)
if err != nil {
httputil.WriteError(w, r, err, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(m)
}
// DeletePort is the handler for POST /v{N}/ports/{id}
func (b *Backend) DeletePort(w http.ResponseWriter, r *http.Request) {
if b.PortDriver == nil {
b.onPortDriverNil(w, r)
return
}
idStr, ok := mux.Vars(r)["id"]
if !ok {
httputil.WriteError(w, r, errors.New("id not specified"), http.StatusBadRequest)
return
}
id, err := strconv.Atoi(idStr)
if err != nil {
httputil.WriteError(w, r, fmt.Errorf("bad id %s: %w", idStr, err), http.StatusBadRequest)
return
}
if err := b.PortDriver.RemovePort(context.TODO(), id); err != nil {
httputil.WriteError(w, r, err, http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
func (b *Backend) GetInfo(w http.ResponseWriter, r *http.Request) {
info := &api.Info{
APIVersion: api.Version,
Version: version.Version,
StateDir: b.StateDir,
ChildPID: b.ChildPID,
}
if b.NetworkDriver != nil {
ndInfo, err := b.NetworkDriver.Info(context.Background())
if err != nil {
httputil.WriteError(w, r, err, http.StatusInternalServerError)
return
}
info.NetworkDriver = ndInfo
}
if b.PortDriver != nil {
pdInfo, err := b.PortDriver.Info(context.Background())
if err != nil {
httputil.WriteError(w, r, err, http.StatusInternalServerError)
return
}
info.PortDriver = pdInfo
}
m, err := json.Marshal(info)
if err != nil {
httputil.WriteError(w, r, err, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(m)
}
func AddRoutes(r *mux.Router, b *Backend) {
v1 := r.PathPrefix("/v1").Subrouter()
v1.Path("/ports").Methods("GET").HandlerFunc(b.GetPorts)
v1.Path("/ports").Methods("POST").HandlerFunc(b.PostPort)
v1.Path("/ports/{id}").Methods("DELETE").HandlerFunc(b.DeletePort)
v1.Path("/info").Methods("GET").HandlerFunc(b.GetInfo)
}
|