some api changes

This commit is contained in:
ston1th 2021-10-19 21:24:14 +02:00
commit 70fd3f71e5
11 changed files with 118 additions and 76 deletions

View file

@ -13,7 +13,7 @@ import (
var (
ErrClientIsNil = errors.New("client is nil")
base = "/" + schema.Version
base = schema.LBPath
)
type Client struct {
@ -32,7 +32,7 @@ func (c *Client) GetLoadBalancers(ctx context.Context) (lbs map[string]schema.Lo
}
func (c *Client) GetLoadBalancersWithCluster(ctx context.Context, cluster string) (lbs map[string]schema.LoadBalancer, err error) {
path := base + "/lb"
path := base
if cluster != "" {
path += "/" + cluster
}
@ -46,7 +46,7 @@ func (c *Client) GetLoadBalancersWithCluster(ctx context.Context, cluster string
}
func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb schema.LoadBalancer, err error) {
path := fmt.Sprintf("%s/lb/%s/%s", base, cluster, name)
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
req, err := c.c.NewRequest(ctx, "GET", path, nil)
if err != nil {
return
@ -57,7 +57,7 @@ func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb
func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, lb schema.LoadBalancer) (err error) {
buf := new(bytes.Buffer)
path := fmt.Sprintf("%s/lb/%s/%s", base, cluster, name)
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
err = lb.ValidateClient()
if err != nil {
return
@ -75,7 +75,7 @@ func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, l
}
func (c *Client) DeleteLoadBalancer(ctx context.Context, cluster, name string) (err error) {
path := fmt.Sprintf("%s/lb/%s/%s", base, cluster, name)
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
req, err := c.c.NewRequest(ctx, "DELETE", path, nil)
if err != nil {
return
@ -88,7 +88,7 @@ func (c *Client) DeleteLoadBalancersWithCluster(ctx context.Context, cluster str
if cluster == "" {
return errors.New("cluster value can not be empty")
}
path := base + "/lb/" + cluster
path := base + "/" + cluster
req, err := c.c.NewRequest(ctx, "DELETE", path, nil)
if err != nil {
return

View file

@ -12,7 +12,11 @@ healthCheckNodePort
*/
const Version = "v1"
const (
Version = "v1"
LBName = "lb"
LBPath = "/" + Version + "/" + LBName
)
type ProxyProtocol string

View file

@ -62,7 +62,7 @@ func lbHandler(ctx *types.Context) {
if err != nil {
ctx.Log.Error(err, "error reading loadbalancer", "cluster", cl, "name", n)
if err == cluster.ErrKeyNotFound {
ctx.Err(types.ErrNotFound)
ctx.Err(types.ErrLBNotFound)
return
}
ctx.Err(types.ErrISE)
@ -139,7 +139,7 @@ func lbHandler(ctx *types.Context) {
return
}
if !ctx.Data.DB.LBExists(name) {
ctx.Err(types.ErrNotFound)
ctx.Err(types.ErrLBNotFound)
return
}
cidr, err := ctx.Data.DB.GetCIDR(name)
@ -175,6 +175,10 @@ func lbList(lbs map[string][]byte) (m map[string]json.RawMessage) {
func lbClusterHandler(ctx *types.Context) {
cl := ctx.Var("cluster")
m, err := ctx.Data.DB.GetLBs(cl)
if err == cluster.ErrPrefixNotFound {
ctx.Err(types.ErrClusterNotFound)
return
}
if err != nil {
ctx.Log.Error(err, "error reading loadbalancers", "cluster", cl)
ctx.Err(types.ErrISE)
@ -214,6 +218,10 @@ func lbClusterHandler(ctx *types.Context) {
}
func lbListHandler(ctx *types.Context) {
m, err := ctx.Data.DB.GetLBs("")
if err == cluster.ErrPrefixNotFound {
ctx.Err(types.ErrLBsNotFound)
return
}
if err != nil {
ctx.Log.Error(err, "error reading loadbalancer clusters")
ctx.Err(types.ErrISE)

View file

@ -2,29 +2,33 @@ package server
import (
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
schemav1 "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/schema"
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/schema"
)
const v1 = "/" + schemav1.Version
const (
healthz = "/healthz"
lbcluster = schema.LBPath + "/{cluster:[a-zA-Z0-9-]+$}"
lbclustername = schema.LBPath + "/{cluster:[a-zA-Z0-9-]+}/{name:[a-zA-Z0-9-_]+$}"
)
var Routes = []types.Route{
{
"/healthz",
healthz,
healthzHandler,
[]string{"GET"},
},
{
v1 + "/lb",
schema.LBPath,
authHandler(lbListHandler),
[]string{"GET"},
},
{
v1 + "/lb/{cluster:[a-zA-Z0-9-]+$}",
lbcluster,
authHandler(lbClusterHandler),
[]string{"GET", "DELETE"},
},
{
v1 + "/lb/{cluster:[a-zA-Z0-9-]+}/{name:[a-zA-Z0-9-_]+$}",
lbclustername,
authHandler(lbHandler),
[]string{"GET", "POST", "DELETE"},
},