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

@ -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 {