implemented api handlers
This commit is contained in:
parent
ab12dd67d4
commit
372915e24d
15 changed files with 302 additions and 73 deletions
|
|
@ -2,6 +2,7 @@ package types
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
|
|
@ -26,20 +27,19 @@ func NewContext(w http.ResponseWriter, r *http.Request, data *ContextData, log l
|
|||
Request: r,
|
||||
Response: w,
|
||||
Data: data,
|
||||
logger: log,
|
||||
Log: log,
|
||||
}
|
||||
}
|
||||
|
||||
// Context is the context object shared between http handlers
|
||||
type Context struct {
|
||||
Body []byte
|
||||
Status int
|
||||
|
||||
Request *http.Request
|
||||
Response http.ResponseWriter
|
||||
|
||||
Data *ContextData
|
||||
logger logr.Logger
|
||||
Data *ContextData
|
||||
Log logr.Logger
|
||||
}
|
||||
|
||||
// Method returns the request method
|
||||
|
|
@ -70,7 +70,7 @@ func (c *Context) Var(name string) (ret string) {
|
|||
}
|
||||
|
||||
func (c *Context) log() {
|
||||
c.logger.Info("access",
|
||||
c.Log.Info("access",
|
||||
"addr", c.Request.RemoteAddr,
|
||||
"method", c.Request.Method,
|
||||
"url", c.Request.URL.Path,
|
||||
|
|
@ -79,23 +79,38 @@ func (c *Context) log() {
|
|||
}
|
||||
|
||||
func (c *Context) NotFound() {
|
||||
c.Status = http.StatusNotFound
|
||||
c.Response.WriteHeader(http.StatusNotFound)
|
||||
c.Response.Write([]byte(`{"message":"not found","status":404}`))
|
||||
c.log()
|
||||
c.Err(ErrNotFound)
|
||||
}
|
||||
|
||||
//TODO func (c *Context) MethodNotAllowed() {
|
||||
// c.Status = http.StatusMethodNotAllowed
|
||||
// c.Response.WriteHeader(http.StatusMethodNotAllowed)
|
||||
// c.Response.Write([]byte(`{"message":"method not allowed","status":405}`))
|
||||
// c.log()
|
||||
//}
|
||||
|
||||
// OK is a empty HTTP 200 JSON response
|
||||
func (c *Context) OK() {
|
||||
c.Response.Write([]byte("{}"))
|
||||
c.log()
|
||||
}
|
||||
|
||||
func (c *Context) ReadBody() (b []byte, err error) {
|
||||
b, err = io.ReadAll(c.Request.Body)
|
||||
c.Request.Body.Close()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Context) Body(b []byte) {
|
||||
c.Response.Write(b)
|
||||
c.log()
|
||||
}
|
||||
|
||||
// JSON is a json response
|
||||
func (c *Context) JSON(v interface{}) {
|
||||
err := json.NewEncoder(c.Response).Encode(v)
|
||||
if err != nil {
|
||||
c.Err(ise)
|
||||
c.Err(ErrISE)
|
||||
return
|
||||
}
|
||||
c.log()
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ type Err struct {
|
|||
|
||||
// Error returns the error string
|
||||
func (a Err) Error() string {
|
||||
return strconv.Itoa(a.status) + ": " + a.err
|
||||
return `{"message":"` + a.err + `","status":` + strconv.Itoa(a.status) + `}`
|
||||
}
|
||||
|
||||
// Status returns the HTTP status code
|
||||
|
|
@ -35,11 +35,11 @@ func newError(err string, status int) Error {
|
|||
}
|
||||
|
||||
var (
|
||||
badreq = newError("bad request", http.StatusBadRequest)
|
||||
invalid = newError("invalid data", http.StatusBadRequest)
|
||||
forbid = newError("forbidden", http.StatusForbidden)
|
||||
nf = newError("resource not found", http.StatusNotFound)
|
||||
mna = newError("method not allowed", http.StatusMethodNotAllowed)
|
||||
exists = newError("resource already exists", http.StatusConflict)
|
||||
ise = newError("internal server error", http.StatusInternalServerError)
|
||||
ErrBadreq = newError("bad request", http.StatusBadRequest)
|
||||
ErrInvalid = newError("invalid data", http.StatusBadRequest)
|
||||
ErrForbidden = newError("forbidden", http.StatusForbidden)
|
||||
ErrNotFound = newError("not found", http.StatusNotFound)
|
||||
ErrMNA = newError("method not allowed", http.StatusMethodNotAllowed)
|
||||
ErrExists = newError("resource already exists", http.StatusConflict)
|
||||
ErrISE = newError("internal server error", http.StatusInternalServerError)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
package schema
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
/*
|
||||
healthCheckNodePort
|
||||
|
||||
|
|
@ -30,3 +36,33 @@ type Server struct {
|
|||
IP string `json:"ip,omitempty"`
|
||||
Port int `json:"port,omitempty"`
|
||||
}
|
||||
|
||||
func (lb LoadBalancer) Validate() error {
|
||||
if lb.Name == "" {
|
||||
return errors.New("LoadBalancer.Name can not be empty")
|
||||
}
|
||||
if lb.IP == "" {
|
||||
return errors.New("LoadBalancer.IP can not be empty")
|
||||
}
|
||||
for i, p := range lb.Ports {
|
||||
if p.Port < 0 || p.Port > 65535 {
|
||||
return fmt.Errorf("LoadBalancer.Ports[%d].Port is invalid (range 0-65535", i)
|
||||
}
|
||||
for j, s := range p.Servers {
|
||||
if s.Name == "" {
|
||||
return fmt.Errorf("LoadBalancer.Ports[%d].Servers[%d].Name can not be empty", i, j)
|
||||
}
|
||||
if s.IP == "" {
|
||||
return fmt.Errorf("LoadBalancer.Ports[%d].Servers[%d].IP can not be empty", i, j)
|
||||
}
|
||||
if s.Port < 0 || s.Port > 65535 {
|
||||
return fmt.Errorf("LoadBalancer.Ports[%d].Servers[%d].Port is invalid (range 0-65535)", i, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lb LoadBalancer) JSON() ([]byte, error) {
|
||||
return json.Marshal(lb)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,12 @@ package server
|
|||
import (
|
||||
//"golang.org/x/crypto/bcrypt"
|
||||
//"net/http"
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/schema"
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/cluster"
|
||||
)
|
||||
|
||||
func authHandler(h types.CtxHandler) types.CtxHandler {
|
||||
|
|
@ -22,10 +26,115 @@ func healthzHandler(ctx *types.Context) {
|
|||
ctx.OK()
|
||||
}
|
||||
|
||||
func lbListHandler(ctx *types.Context) {
|
||||
ctx.OK()
|
||||
func name(c, n string) string {
|
||||
return c + "/" + n
|
||||
}
|
||||
|
||||
func lbname(c, n string) string {
|
||||
return c + "_" + n
|
||||
}
|
||||
|
||||
func lbHandler(ctx *types.Context) {
|
||||
ctx.OK()
|
||||
cl := ctx.Var("cluster")
|
||||
n := ctx.Var("name")
|
||||
name := name(cl, n)
|
||||
switch ctx.Method() {
|
||||
case "GET":
|
||||
b, err := ctx.Data.DB.GetLB(name)
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error reading loadbalancer", "cluster", cl, "name", n)
|
||||
if err == cluster.ErrKeyNotFound {
|
||||
ctx.Err(types.ErrNotFound)
|
||||
return
|
||||
}
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
ctx.Body(b)
|
||||
case "POST":
|
||||
// TODO
|
||||
// handle if loadbalancer already exists
|
||||
// decode existing LB and read Name + IP from there
|
||||
b, err := ctx.ReadBody()
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error reading request body", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
var lb schema.LoadBalancer
|
||||
err = json.Unmarshal(b, &lb)
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error decoding request body", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrInvalid)
|
||||
return
|
||||
}
|
||||
lb.Name = lbname(cl, n)
|
||||
lb.IP, err = ctx.Data.Alloc.AllocIP(context.Background(), name)
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error allocating IP for loadbalancer", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
err = lb.Validate()
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error validating loadbalancer", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrInvalid)
|
||||
return
|
||||
}
|
||||
b, err = lb.JSON()
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error encoding loadbalancer", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
err = ctx.Data.DB.SetLB(name, b)
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error writing loadbalancer", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
ctx.OK()
|
||||
case "DELETE":
|
||||
err := ctx.Data.Alloc.FreeIP(context.Background(), name)
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error freeing IP of loadbalancer", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
err = ctx.Data.DB.DeleteLB(name)
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error deleting loadbalancer", "cluster", cl, "name", n)
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
ctx.OK()
|
||||
}
|
||||
}
|
||||
|
||||
func lbList(lbs map[string][]byte) (m map[string]json.RawMessage) {
|
||||
m = make(map[string]json.RawMessage)
|
||||
for k, v := range lbs {
|
||||
m[k] = json.RawMessage(v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func lbClusterListHandler(ctx *types.Context) {
|
||||
cl := ctx.Var("cluster")
|
||||
m, err := ctx.Data.DB.GetLBs(cl)
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error reading loadbalancers")
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
ctx.JSON(lbList(m))
|
||||
}
|
||||
func lbListHandler(ctx *types.Context) {
|
||||
m, err := ctx.Data.DB.GetLBs("")
|
||||
if err != nil {
|
||||
ctx.Log.Error(err, "error reading loadbalancers")
|
||||
ctx.Err(types.ErrISE)
|
||||
return
|
||||
}
|
||||
ctx.JSON(lbList(m))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,11 @@ var Routes = []types.Route{
|
|||
authHandler(lbListHandler),
|
||||
[]string{"GET"},
|
||||
},
|
||||
{
|
||||
v1 + "/lb/{cluster:[a-zA-Z0-9-]+$}",
|
||||
authHandler(lbClusterListHandler),
|
||||
[]string{"GET"},
|
||||
},
|
||||
{
|
||||
v1 + "/lb/{cluster:[a-zA-Z0-9-]+}/{name:[a-zA-Z0-9-]+$}",
|
||||
authHandler(lbHandler),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue