cleanup, ensure haproxy is running and disallow writes to followers

This commit is contained in:
ston1th 2021-10-09 19:03:08 +02:00
commit fef86deac8
9 changed files with 49 additions and 183 deletions

View file

@ -104,8 +104,9 @@ func NewServer(c *config.Config, log logr.Logger) (*Server, error) {
return s, nil
}
func (s *Server) UpdateDB(db *db.DB) error {
func (s *Server) UpdateDB(db *db.DB, leader bool) error {
s.Data.DB = db
s.Data.Leader = leader
if s.Data.Net == nil {
na, err := netalloc.NewGenericAlloc(db, s.cidr)
if err != nil {

View file

@ -14,9 +14,10 @@ import (
)
type ContextData struct {
Net netalloc.Allocator
DB *db.DB
Auth *Auth
Net netalloc.Allocator
DB *db.DB
Auth *Auth
Leader bool
}
func NewContext(w http.ResponseWriter, r *http.Request, data *ContextData, log logr.Logger) *Context {
@ -42,6 +43,10 @@ type Context struct {
Log logr.Logger
}
func (c *Context) Leader() bool {
return c.Data.Leader
}
// Method returns the request method
func (c *Context) Method() string {
return c.Request.Method

View file

@ -43,4 +43,5 @@ var (
ErrMNA = newError("method not allowed", http.StatusMethodNotAllowed)
ErrExists = newError("resource already exists", http.StatusConflict)
ErrISE = newError("internal server error", http.StatusInternalServerError)
ErrFollower = newError("follower write forbidden", http.StatusForbidden)
)

View file

@ -4,6 +4,7 @@ import (
//"golang.org/x/crypto/bcrypt"
//"net/http"
"encoding/json"
"errors"
"net"
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
@ -11,6 +12,8 @@ import (
"git.giftfish.de/ston1th/haproxy-lb/pkg/cluster"
)
var errFollower = errors.New("follower write")
func authHandler(h types.CtxHandler) types.CtxHandler {
return func(ctx *types.Context) {
user, pass, ok := ctx.Request.BasicAuth()
@ -65,6 +68,11 @@ func lbHandler(ctx *types.Context) {
}
ctx.Body(b)
case "POST":
if !ctx.Leader() {
ctx.Log.Error(errFollower, "error writing config: i am a follower", "cluster", cl, "name", n)
ctx.Err(types.ErrFollower)
return
}
b, err := ctx.ReadBody()
if err != nil {
ctx.Log.Error(err, "error reading request body", "cluster", cl, "name", n)
@ -123,6 +131,11 @@ func lbHandler(ctx *types.Context) {
}
ctx.OK()
case "DELETE":
if !ctx.Leader() {
ctx.Log.Error(errFollower, "error writing config: i am a follower", "cluster", cl, "name", n)
ctx.Err(types.ErrFollower)
return
}
if !ctx.Data.DB.LBExists(name) {
ctx.Err(types.ErrNotFound)
return
@ -169,6 +182,11 @@ func lbClusterHandler(ctx *types.Context) {
case "GET":
ctx.JSON(lbList(m))
case "DELETE":
if !ctx.Leader() {
ctx.Log.Error(errFollower, "error writing config: i am a follower", "cluster", cl, "name", name)
ctx.Err(types.ErrFollower)
return
}
for name, _ := range m {
cidr, err := ctx.Data.DB.GetCIDR(name)
if err != nil && err != cluster.ErrKeyNotFound {