more work done

This commit is contained in:
ston1th 2021-03-06 16:51:36 +01:00
commit 6fda229a8e
23 changed files with 384 additions and 177 deletions

View file

@ -3,15 +3,15 @@ package api
import (
"context"
stdtls "crypto/tls"
"io/ioutil"
"log"
"net"
"net/http"
"path/filepath"
"time"
"git.giftfish.de/ston1th/haproxy-lb/pkg/alloc"
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
serverv1 "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/server"
"git.giftfish.de/ston1th/haproxy-lb/pkg/config"
"git.giftfish.de/ston1th/haproxy-lb/pkg/db"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
@ -23,14 +23,19 @@ type Server struct {
mux *mux.Router
log logr.Logger
Alloc *alloc.Alloc
DB *db.DB
cidrs []string
gw string
Auth []config.BasicAuth
init chan struct{}
stop chan struct{}
stopKeyReset chan struct{}
cert string
key string
laddr string
debug bool
}
type notFoundHandler struct {
@ -42,19 +47,21 @@ func (nf *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// NewHTTPServer returns a new HTTPServer
func NewServer(log logr.Logger) *Server {
func NewServer(c *config.Config, log logr.Logger) *Server {
s := &Server{
mux: mux.NewRouter(),
log: log.WithName("api"),
mux: mux.NewRouter(),
log: log.WithName("api"),
cidrs: c.VIP.VirtualIPs,
gw: c.VIP.Gateway,
Auth: c.Server.BasicAuth,
init: make(chan struct{}),
stop: make(chan struct{}),
stopKeyReset: make(chan struct{}),
cert: filepath.Join(core.C.DataDir, core.C.HTTP.Cert),
key: filepath.Join(core.C.DataDir, core.C.HTTP.Key),
laddr: net.JoinHostPort(core.C.HTTP.Address, core.C.HTTP.Port),
debug: core.C.Debug,
cert: c.Server.TLS.Cert,
key: c.Server.TLS.Key,
laddr: c.Server.Listen,
}
s.mux.NotFoundHandler = &notFoundHandler{s}
for _, v := range serverv1.Routes {
@ -64,11 +71,28 @@ func NewServer(log logr.Logger) *Server {
return s
}
func (s *Server) UpdateDB(db *db.DB) error {
s.db = db
if s.Alloc == nil {
alloc, err := alloc.NewAlloc(db, s.cidrs, s.gw)
if err != nil {
return err
}
s.alloc = alloc
close(s.init)
} else {
s.Alloc.UpdateDB(db)
}
return nil
}
func (s *Server) start() error {
l, err := tls.Listen("tcp", h.laddr, cfg)
<-s.init
l, err := net.Listen("tcp", h.laddr)
if err != nil {
return err
}
// TODO tls config
h.srv = &http.Server{
Handler: h.mux,
TLSConfig: cfg,

View file

@ -2,7 +2,6 @@ package types
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-logr/logr"
@ -16,7 +15,7 @@ func NewContext(w http.ResponseWriter, r *http.Request, log logr.Logger) *Contex
Status: http.StatusOK,
Request: r,
Response: w,
log: log,
logger: log,
}
}
@ -27,7 +26,7 @@ type Context struct {
Request *http.Request
Response http.ResponseWriter
log logr.Logger
logger logr.Logger
}
// Method returns the request method
@ -58,7 +57,7 @@ func (c *Context) Var(name string) (ret string) {
}
func (c *Context) log() {
c.log.Info("access",
c.logger.Info("access",
"addr", c.Request.RemoteAddr,
"method", c.Request.Method,
"url", c.Request.URL,

45
pkg/api/types/error.go Normal file
View file

@ -0,0 +1,45 @@
package types
import (
"net/http"
"strconv"
)
// Error interface is a custom api error
type Error interface {
Error() string
Status() int
}
// Err implements the Error interface
type Err struct {
err string
status int
}
// Error returns the error string
func (a Err) Error() string {
return strconv.Itoa(a.status) + ": " + a.err
}
// Status returns the HTTP status code
func (a Err) Status() int {
return a.status
}
func newError(err string, status int) Error {
return Err{
err: err,
status: status,
}
}
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)
)

View file

@ -2,6 +2,6 @@ package types
type Route struct {
Path string
Handler http.HandlerFunc
Handler CtxHandler
Methods []string
}

View file

@ -5,6 +5,8 @@ healthCheckNodePort
*/
const Version = "v1"
type LoadBalancer struct {
Name string `json:"name"`
IP string `json:"ip,omitempty"`

View file

@ -1,10 +1,12 @@
package server
import (
"git.gitfish.de/ston1th/haproxy-lb/pkg/config"
"git.giftfish.de/ston1th/haproxy-lb/pkg/config"
"golang.org/x/crypto/bcrypt"
weakrand "math/rand"
"net/http"
//"net/http"
"encoding/hex"
"strings"
"sync"
"time"
)
@ -90,7 +92,7 @@ func (a *auth) Auth(user, pass, path string) bool {
a.bcryptMtx.Unlock()
authOk = err == nil
u.cache.set(cacheKey, authOk)
a.cache.set(cacheKey, authOk)
}
return authOk && valid && strings.HasPrefix(path, creds.Prefix)
@ -103,10 +105,10 @@ type creds struct {
func newAuth(conf config.Config) (a *auth) {
a = &auth{
users: make(map[string]creds),
users: make(map[string]*creds),
cache: newCache(),
}
for _, u := range conf.BasicAuth {
for _, u := range conf.Server.BasicAuth {
a.users[u.User] = &creds{u.Hash, u.Prefix}
}
return

View file

@ -2,7 +2,7 @@ package server
import (
//"golang.org/x/crypto/bcrypt"
"net/http"
//"net/http"
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
)
@ -21,3 +21,11 @@ func healthzHandler(ctx *types.Context) {
// TODO maybe report etcd/raft stats
ctx.OK()
}
func lbListHandler(ctx *types.Context) {
ctx.OK()
}
func lbHandler(ctx *types.Context) {
ctx.OK()
}