diff --git a/Makefile b/Makefile index e8eb5b4..e603d46 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ BUILD=build -v -trimpath VERSION=$(shell cat VERSION) GCFLAGS=-gcflags '-e' LDFLAGS=-ldflags '-X main.version=$(VERSION) -s -w' -PROGRAM=vipman -CMD=-o vipman cmd/vipman/main.go +PROGRAM=haproxy-lb +CMD=-o haproxy-lb cmd/haproxy-lb/main.go ENV=CGO_ENABLED=0 GO111MODULE=on all: $(PROGRAM) diff --git a/cmd/vipman/main.go b/cmd/haproxy-lb/main.go similarity index 72% rename from cmd/vipman/main.go rename to cmd/haproxy-lb/main.go index fb93d8a..bdba936 100644 --- a/cmd/vipman/main.go +++ b/cmd/haproxy-lb/main.go @@ -6,11 +6,11 @@ import ( "os/signal" "syscall" - "git.giftfish.de/ston1th/vipman/pkg/cluster" - "git.giftfish.de/ston1th/vipman/pkg/config" - "git.giftfish.de/ston1th/vipman/pkg/controller" - "git.giftfish.de/ston1th/vipman/pkg/etcd" - "git.giftfish.de/ston1th/vipman/pkg/raft" + "git.giftfish.de/ston1th/haproxy-lb/pkg/cluster" + "git.giftfish.de/ston1th/haproxy-lb/pkg/config" + "git.giftfish.de/ston1th/haproxy-lb/pkg/controller" + "git.giftfish.de/ston1th/haproxy-lb/pkg/etcd" + "git.giftfish.de/ston1th/haproxy-lb/pkg/raft" "github.com/go-logr/logr" "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" @@ -24,10 +24,10 @@ var ( func main() { klog.InitFlags(nil) - flag.StringVar(&configFile, "config", "vipman.yaml", "vipman config file") + flag.StringVar(&configFile, "config", "haproxy-lb.yaml", "haproxy-lb config file") flag.Parse() log = klogr.New().WithName("main") - log.Info("starting vipman", "version", version) + log.Info("starting haproxy-lb", "version", version) cfg, err := config.ParseFile(configFile) if err != nil { klog.Fatalf("init failed: %s", err) @@ -66,7 +66,7 @@ func main() { sigs := make(chan os.Signal) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) <-sigs - log.Info("vipman shutdown") + log.Info("haproxy-lb shutdown") c.Stop() - log.Info("vipman stopped") + log.Info("haproxy-lb stopped") } diff --git a/go.mod b/go.mod index f9bf906..672fc31 100644 --- a/go.mod +++ b/go.mod @@ -1,30 +1,29 @@ -module git.giftfish.de/ston1th/vipman +module git.giftfish.de/ston1th/haproxy-lb -go 1.15 +go 1.16 require ( - git.giftfish.de/ston1th/raftbbolt v0.0.0-20201114165203-b85c94bfc9b0 + git.giftfish.de/ston1th/raftbbolt v0.0.0-20201114165203-b85c94bfc9b0 // indirect github.com/coreos/etcd v3.3.25+incompatible github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect - github.com/envoyproxy/go-control-plane v0.9.4 // indirect - github.com/go-logr/logr v0.3.0 + github.com/go-logr/logr v0.4.0 // indirect github.com/gogo/protobuf v1.3.1 // indirect - github.com/golang/protobuf v1.4.3 // indirect - github.com/google/go-cmp v0.5.0 // indirect - github.com/google/uuid v1.1.2 // indirect - github.com/gorilla/mux v1.8.0 - github.com/hashicorp/go-hclog v0.15.0 - github.com/hashicorp/raft v1.2.0 - github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc - github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7 - github.com/vishvananda/netlink v1.1.0 + github.com/google/uuid v1.2.0 // indirect + github.com/gorilla/mux v1.8.0 // indirect + github.com/hashicorp/go-hclog v0.15.0 // indirect + github.com/hashicorp/raft v1.2.0 // indirect + github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc // indirect + github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7 // indirect + github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721 // indirect + github.com/vishvananda/netlink v1.1.0 // indirect go.etcd.io/etcd v3.3.25+incompatible - go.uber.org/zap v1.16.0 - google.golang.org/grpc v1.26.0 // indirect - gopkg.in/yaml.v2 v2.3.0 - k8s.io/klog/v2 v2.4.0 + go.uber.org/zap v1.16.0 // indirect + golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect + google.golang.org/genproto v0.0.0-20210303154014-9728d6b83eeb // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/klog/v2 v2.6.0 // indirect ) replace github.com/coreos/bbolt => go.etcd.io/bbolt v1.3.5 diff --git a/go.sum b/go.sum index 6fa3992..939bb56 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,7 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/coreos/etcd v0.5.0-alpha.5 h1:0Qi6Jzjk2CDuuGlIeecpu+em2nrjhOgz2wsIwCmQHmc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/coreos/etcd v3.3.25+incompatible h1:0GQEw6h3YnuOVdtwygkIfJ+Omx0tZ8/QkVyXI4LkbeY= github.com/coreos/etcd v3.3.25+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= @@ -25,17 +24,19 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -github.com/go-logr/logr v0.3.0 h1:q4c+kbcR0d5rSurhBR8dIgieOaYpXtsdTYfx22Cu6rs= -github.com/go-logr/logr v0.3.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= +github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc= +github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= @@ -45,8 +46,8 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= -github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -55,6 +56,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= @@ -63,6 +66,7 @@ github.com/hashicorp/go-hclog v0.15.0 h1:qMuK0wxsoW4D0ddCCYwPSTm4KQv1X1ke3WmPWZ0 github.com/hashicorp/go-hclog v0.15.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0= github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-msgpack v0.5.5 h1:i9R9JSrqIz0QVLz3sz+i3YJdT7TTSLcfLLzJi9aZTuI= github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= github.com/hashicorp/go-msgpack v1.1.5 h1:9byZdVjKTe5mce63pRVNP1L7UAmdHOTEMGehn6KvJWs= github.com/hashicorp/go-msgpack v1.1.5/go.mod h1:gWVc3sv/wbDmR3rQsj1CAktEZzoz1YNK9NfGLXJ69/4= @@ -74,6 +78,7 @@ github.com/hashicorp/raft v1.2.0 h1:mHzHIrF0S91d3A7RPBvuqkgB4d/7oFJZyvf1Q4m7GA0= github.com/hashicorp/raft v1.2.0/go.mod h1:vPAJM8Asw6u8LxC3eJCUZmRP/E4QmUGE1R7g7k8sG/8= github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea/go.mod h1:pNv7Wc3ycL6F5oOWn+tPGo2gWD4a5X+yp/ntwdKLjRk= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -86,12 +91,16 @@ github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcME github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc h1:m7rJJJeXrYCFpsxXYapkDW53wJCDmf9bsIXUg0HoeQY= github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc/go.mod h1:eOj1DDj3NAZ6yv+WafaKzY37MFZ58TdfIhQ+8nQbiis= +github.com/mdlayher/ethernet v0.0.0-20190313224307-5b5fc417d966 h1:O3p5UmisBhl3V6lgs4Vdfg8HpjzbWJPyOfGLdwVJSmI= github.com/mdlayher/ethernet v0.0.0-20190313224307-5b5fc417d966/go.mod h1:5s5p/sMJ6sNsFl6uCh85lkFGV8kLuIYJCRJLavVJwvg= github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7 h1:lez6TS6aAau+8wXUP3G9I3TGlmPFEq2CTxBaRqY6AGE= github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7/go.mod h1:U6ZQobyTjI/tJyq2HG+i/dfSoFUt8/aZCM+GKtmFk/Y= +github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d h1:rjAS0af7FIYCScTtEU5KjIldC6qVaEScUJhABHC+ccM= github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d/go.mod h1:r1fbeITl2xL/zLbVnNHFyOzQJTgr/3fpf1lJX/cjzR8= github.com/mdlayher/raw v0.0.0-20190606142536-fef19f00fc18 h1:zwOa3e/13D6veNIz6zzuqrd3eZEMF0dzD0AQWKcYSs4= github.com/mdlayher/raw v0.0.0-20190606142536-fef19f00fc18/go.mod h1:7EpbotpCmVZcu+KCX4g9WaRNuu11uyhiW7+Le1dKawg= +github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721 h1:RlZweED6sbSArvlE924+mUcZuXKLBHA35U7LN621Bws= +github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -105,14 +114,16 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0= go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= -go.etcd.io/etcd v0.5.0-alpha.5 h1:VOolFSo3XgsmnYDLozjvZ6JL6AAwIDu1Yx1y+4EYLDo= go.etcd.io/etcd v3.3.25+incompatible h1:V1RzkZJj9LqsJRy+TUBgpWSbZXITLB819lstuTFoZOY= go.etcd.io/etcd v3.3.25+incompatible/go.mod h1:yaeTdrJi5lOmYerz05bd8+V7KubZs8YSFZfzsF9A6aI= go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= @@ -123,13 +134,20 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9E go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g= +golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -141,10 +159,15 @@ golang.org/x/net v0.0.0-20190419010253-1f3472d942ba/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -154,11 +177,19 @@ golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606122018-79a91cf218c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -168,33 +199,36 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20210303154014-9728d6b83eeb h1:hcskBH5qZCOa7WpTUFUFvoebnSFZBYpjykLtjIp9DVk= +google.golang.org/genproto v0.0.0-20210303154014-9728d6b83eeb/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= -google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg= -google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o= -google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/grpc v1.36.0 h1:o1bcQ6imQMIOpdrO3SWf2z5RV72WbDwdXuK0MDlc8As= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.24.0 h1:UhZDfRO8JRQru4/+LlLE0BRKGF8L+PICnvYZmx/fEGA= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= @@ -202,11 +236,10 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= -k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8= -k8s.io/klog/v2 v2.4.0 h1:7+X0fUguPyrKEC4WjH8iGDg3laWgMo5tMnRTIGTTxGQ= -k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y= +k8s.io/klog/v2 v2.6.0 h1:c1wFxejFMBkp/VxCdc6kYdgrBkC2gzmcl6afuJAkJyU= +k8s.io/klog/v2 v2.6.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec= diff --git a/pkg/alloc/alloc.go b/pkg/alloc/alloc.go index cd3f6dc..ca14261 100644 --- a/pkg/alloc/alloc.go +++ b/pkg/alloc/alloc.go @@ -4,18 +4,21 @@ import ( "context" "fmt" "net" + "strings" + "sync" - "git.giftfish.de/ston1th/haproxy-lb/pkg/cluster" + "git.giftfish.de/ston1th/haproxy-lb/pkg/db" "github.com/mikioh/ipaddr" ) type Alloc struct { - kv cluster.KV + sync.Mutex + db *db.DB pool []*net.IPNet gateway *net.IPNet } -func NewAlloc(kv cluster.KV, cidrs []string, gateway string) (*Alloc, error) { +func NewAlloc(db *db.DB, cidrs []string, gateway string) (*Alloc, error) { pool, err := parseRange(cidrs) if err != nil { return nil, err @@ -24,26 +27,27 @@ func NewAlloc(kv cluster.KV, cidrs []string, gateway string) (*Alloc, error) { if err != nil { return nil, err } - return &Alloc{kv, pool, gw}, nil + return &Alloc{db: db, pool: pool, gateway: gw}, nil +} + +func (a *Alloc) UpdateDB(db *db.DB) { + a.Lock() + a.db = db + a.Unlock() } func (a *Alloc) AllocIP(ctx context.Context, name string) (addr string, err error) { + a.Lock() + defer a.Unlock() for _, cidr := range a.pool { - c := ipaddr.NewCursor([]ipaddr.Prefix{*ipaddr.NewPrefix(a.gateway)}) + c := ipaddr.NewCursor([]ipaddr.Prefix{*ipaddr.NewPrefix(cidr)}) for pos := c.First(); pos != nil; pos = c.Next() { ip := pos.IP.String() - v, _ := a.kv.Get(ip) - vs := string(v) - if v == "" { - err = a.kv.Set(ip, []byte(name)) + if !a.db.IPExists(ip) { + err = a.db.SetIP(ip, name) if err != nil { return } - err = a.kv.Set(name, []byte(ip)) - if err != nil { - a.kv.Delete(ip) - return - } var gw *net.IPNet *gw = *a.gateway gw.IP = pos.IP @@ -52,28 +56,21 @@ func (a *Alloc) AllocIP(ctx context.Context, name string) (addr string, err erro } } } - err = fmt.Errorf("no available IPs in pool") + err = fmt.Errorf("no more IPs available in pool") return } func (a *Alloc) FreeIP(ctx context.Context, name string) (err error) { + a.Lock() + defer a.Unlock() if name == "" { return } - v, err := a.kv.Get(name) + ip, err := a.db.GetName(name) if err != nil { return } - vs := string(v) - err = a.kv.Delete(vs) - if err != nil { - return - } - err = a.kv.Delete(name) - if err != nil { - a.kv.Set(vs, []byte(name)) - } - return + return a.db.DeleteIP(ip, name) } func parseRange(cidrs []string) (nets []*net.IPNet, err error) { diff --git a/pkg/api/server.go b/pkg/api/server.go index 100ed76..bbd8ef4 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -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 = ¬FoundHandler{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, diff --git a/pkg/api/types/context.go b/pkg/api/types/context.go index 19d3023..806a081 100644 --- a/pkg/api/types/context.go +++ b/pkg/api/types/context.go @@ -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, diff --git a/pkg/api/types/error.go b/pkg/api/types/error.go new file mode 100644 index 0000000..acfac67 --- /dev/null +++ b/pkg/api/types/error.go @@ -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) +) diff --git a/pkg/api/types/types.go b/pkg/api/types/types.go index e57c3ec..34815e8 100644 --- a/pkg/api/types/types.go +++ b/pkg/api/types/types.go @@ -2,6 +2,6 @@ package types type Route struct { Path string - Handler http.HandlerFunc + Handler CtxHandler Methods []string } diff --git a/pkg/api/v1/schema/schema.go b/pkg/api/v1/schema/schema.go index cd6f015..3a23917 100644 --- a/pkg/api/v1/schema/schema.go +++ b/pkg/api/v1/schema/schema.go @@ -5,6 +5,8 @@ healthCheckNodePort */ +const Version = "v1" + type LoadBalancer struct { Name string `json:"name"` IP string `json:"ip,omitempty"` diff --git a/pkg/api/v1/server/auth.go b/pkg/api/v1/server/auth.go index 404eea9..3687e1e 100644 --- a/pkg/api/v1/server/auth.go +++ b/pkg/api/v1/server/auth.go @@ -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 diff --git a/pkg/api/v1/server/handler.go b/pkg/api/v1/server/handler.go index 73e3088..0ff4d4e 100644 --- a/pkg/api/v1/server/handler.go +++ b/pkg/api/v1/server/handler.go @@ -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() +} diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 62aa492..387e2db 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -3,7 +3,7 @@ package cluster import ( "context" - "git.giftfish.de/ston1th/vipman/pkg/config" + "git.giftfish.de/ston1th/haproxy-lb/pkg/config" "github.com/go-logr/logr" ) @@ -25,6 +25,7 @@ type CallbackContext interface { logr.Logger KV ID() string + Fatal(error, string) } type Callbacks struct { diff --git a/pkg/config/config.go b/pkg/config/config.go index e86965d..89b1085 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,7 +17,9 @@ const ( RaftLogLevel = "off" EtcdLogLevel = "error" - EtcdPrefix = "/vipman" + EtcdPrefix = "/haproxy-lb" + + HAProxyConfigFile = "/etc/haproxy/haproxy.cf" ) func ParseFile(file string) (cfg *Config, err error) { @@ -39,8 +41,14 @@ func ParseFile(file string) (cfg *Config, err error) { } func Validate(c *Config) error { - if len(c.VirtualIPs) == 0 { - return errors.New("missing virtualIPs config") + if c.HAProxyConfig == "" { + c.HAProxyConfig = HAProxyConfigFile + } + if len(c.VIP.VirtualIPs) == 0 { + return errors.New("missing server.virtualIPs config") + } + if c.VIP.Gateway == "" { + return errors.New("missing server.gateway config") } if c.LeaderHook != "" { if !filepath.IsAbs(c.LeaderHook) { @@ -110,10 +118,10 @@ func Validate(c *Config) error { if raft != nil && etcd != nil { return errors.New("only one cluster config allowed: raft or etcd") } - if len(c.BasicAuth) == 0 { + if len(c.Server.BasicAuth) == 0 { return errors.New("missing basic auth configuration for api users") } - for _, u := range c.BasicAuth { + for _, u := range c.Server.BasicAuth { if u.User == "" { return errors.New("basic auth username can not be empty") } @@ -126,13 +134,25 @@ func Validate(c *Config) error { } type Config struct { - VirtualIPs []string `yaml:"virtualIPs"` - Interface string `yaml:"interface,omitempty"` - Label string `yaml:"label,omitempty"` - LeaderHook string `yaml:"leaderHook,omitempty"` - FollowerHook string `yaml:"followerHook,omitempty"` - Cluster Cluster `yaml:"cluster"` - BasicAuth []BasicAuth `yaml:"basicAuth"` + LeaderHook string `yaml:"leaderHook,omitempty"` + FollowerHook string `yaml:"followerHook,omitempty"` + HAProxyConfig string `yaml:"haproxyConfig,omitempty"` + VIP VIP `yaml:"vip"` + Cluster Cluster `yaml:"cluster"` + Server Server `yaml:"server"` +} + +type Server struct { + Listen string `yaml:"listen"` + TLS TLS `yaml:"tls"` + BasicAuth []BasicAuth `yaml:"basicAuth"` +} + +type VIP struct { + VirtualIPs []string `yaml:"virtualIPs"` + Gateway string `yaml:"gateway"` + Interface string `yaml:"interface,omitempty"` + Label string `yaml:"label,omitempty"` } type BasicAuth struct { diff --git a/pkg/controller/vip.go b/pkg/controller/vip.go index 61f07e5..0e0f826 100644 --- a/pkg/controller/vip.go +++ b/pkg/controller/vip.go @@ -5,62 +5,33 @@ import ( "os/exec" "time" - "git.giftfish.de/ston1th/haproxy-kb/pkg/cluster" - "git.giftfish.de/ston1th/haproxy-kb/pkg/config" - "git.giftfish.de/ston1th/haproxy-kb/pkg/haproxy" - "git.giftfish.de/ston1th/haproxy-kb/pkg/vip" + "git.giftfish.de/ston1th/haproxy-lb/pkg/api" + "git.giftfish.de/ston1th/haproxy-lb/pkg/cluster" + "git.giftfish.de/ston1th/haproxy-lb/pkg/config" + "git.giftfish.de/ston1th/haproxy-lb/pkg/db" + "git.giftfish.de/ston1th/haproxy-lb/pkg/haproxy" + "git.giftfish.de/ston1th/haproxy-lb/pkg/vip" "github.com/go-logr/logr" ) -type UpdateAction int - -const ( - Add UpdateAction = iota - Delete -) - -type ConfigUpdate struct { - Action UpdateAction - Config haproxy.LoadBalancer -} - -func readConfigUpdates(c chan ConfigUpdate) (cu []ConfigUpdate) { - for len(c) > 0 { - select { - case config := <-c: - cu = append(cu, config) - } - } - return -} - -func applyConfigUpdates(cc cluster.CallbackContext, cu []ConfigUpdate) error { - for _, c := range cu { - switch c.Action { - case Add: - addIPs(cc, n, []string{c.Config.IP}) - cc.Set(c.Config.IP, nil) - case Delete: - cc.Delete(c.Config.IP) - deleteIPs(cc, n, []string{c.Config.IP}) - } - } -} - -func NewLBController(cfg *config.Config) (callbacks cluster.Callbacks, err error) { - n, err := vip.NewNetworkWithLabel(cfg.Interface, cfg.Label) +func NewLBController(cfg *config.Config, srv *api.Server, log logr.Logger) (callbacks cluster.Callbacks, err error) { + n, err := vip.NewNetworkWithLabel(cfg.VIP.Interface, cfg.VIP.Label) if err != nil { return } - //TODO - ha, err := haproxy.NewHAProxyManager("") + ha, err := haproxy.NewHAProxyManager(cfg.HAProxyConfig) if err != nil { return } - configUpdate := make(chan ConfigUpdate, 100) callbacks = cluster.Callbacks{ Leader: func(ctx context.Context, cc cluster.CallbackContext) { + db := db.New(cc) + err := srv.UpdateDB(db) + if err != nil { + cc.Fatal(err, "error initialising api server as leader") + return + } t := time.NewTicker(time.Second * 10) if cfg.LeaderHook != "" { go func() { @@ -72,13 +43,20 @@ func NewLBController(cfg *config.Config) (callbacks cluster.Callbacks, err error } for { cc.Info("leading", "id", cc.ID()) + ips, err := db.GetIPs() + if err != nil { + cc.Error("error updating haproxy config", err) + <-t.C + continue + } + addIPs(cc, n, ips) + cfg, err := cc.Get("config") if err != nil { cc.Error("error getting config key", err) <-t.C continue } - addIPs(cc, n, cfg.VirtualIPs) var lbs haproxy.Config err = ha.UpdateConfig(ctx, lbs) if err != nil { @@ -89,7 +67,7 @@ func NewLBController(cfg *config.Config) (callbacks cluster.Callbacks, err error select { case <-ctx.Done(): t.Stop() - deleteIPs(cc, n, cfg.VirtualIPs) + deleteIPs(cc, n, ips) cc.Info("leading canceled", "id", cc.ID()) return case <-t.C: @@ -97,6 +75,12 @@ func NewLBController(cfg *config.Config) (callbacks cluster.Callbacks, err error } }, Follower: func(ctx context.Context, cc cluster.CallbackContext) { + db := db.New(cc) + err := srv.UpdateDB(db) + if err != nil { + cc.Fatal(err, "error initialising api server as leader") + return + } t := time.NewTicker(time.Second * 10) if cfg.FollowerHook != "" { go func() { @@ -109,7 +93,13 @@ func NewLBController(cfg *config.Config) (callbacks cluster.Callbacks, err error for { cc.Info("following", "id", cc.ID()) //TODO - //deleteIPs(cc, n, cfg.VirtualIPs) + //ips, err := db.GetIPs() + //if err != nil { + // cc.Error("error getting config key", err) + // <-t.C + // continue + //} + //deleteIPs(cc, n, ips) select { case <-ctx.Done(): t.Stop() @@ -120,7 +110,12 @@ func NewLBController(cfg *config.Config) (callbacks cluster.Callbacks, err error } }, Cleanup: func(ctx context.Context, cc cluster.CallbackContext) { - deleteIPs(cc, n, cfg.VirtualIPs) + db := db.New(cc) + ips, err := db.GetIPs() + if err != nil { + return + } + deleteIPs(cc, n, ips) }, } return diff --git a/pkg/db/db.go b/pkg/db/db.go new file mode 100644 index 0000000..1a4c28e --- /dev/null +++ b/pkg/db/db.go @@ -0,0 +1,56 @@ +package db + +import ( + "git.giftfish.de/ston1th/haproxy-lb/pkg/cluster" +) + +type DB struct { + kv cluster.KV +} + +func New(kv cluster.KV) *DB { + return &DB{kv} +} + +const ( + ipPrefix = "/ip/" + namePrefix = "/name/" +) + +func (db *DB) GetIPs() (ips []string, err error) { + m, err := db.kv.GetPrefix(namePrefix) + if err != nil { + return + } + for _, v := range m { + ips = append(ips, string(v)) + } + return +} +func (db *DB) GetIP(ip string) (string, error) { + v, err := db.kv.Get(ipPrefix + ip) + return string(v), err +} +func (db *DB) IPExists(ip string) bool { + _, err := db.GetIP(ipPrefix + ip) + return err == nil +} +func (db *DB) SetIP(ip, name string) error { + err := db.kv.Set(ipPrefix+ip, []byte(name)) + if err != nil { + return err + } + return db.kv.Set(namePrefix+name, []byte(ip)) +} +func (db *DB) DeleteIP(ip, name string) error { + err := db.kv.Delete(ipPrefix + ip) + if err != nil { + return err + } + return db.kv.Delete(namePrefix + name) +} + +func (db *DB) GetName(name string) (string, error) { + v, err := db.kv.Get(namePrefix + name) + return string(v), err +} diff --git a/pkg/etcd/cluster.go b/pkg/etcd/cluster.go index b2478f2..fb70245 100644 --- a/pkg/etcd/cluster.go +++ b/pkg/etcd/cluster.go @@ -5,13 +5,16 @@ import ( "errors" "strings" - "git.giftfish.de/ston1th/vipman/pkg/cluster" + "git.giftfish.de/ston1th/haproxy-lb/pkg/cluster" //"go.etcd.io/etcd/clientv3" "github.com/coreos/etcd/clientv3" "github.com/go-logr/logr" ) -var ErrKeyNotFound = errors.New("key not found") +var ( + ErrKeyNotFound = errors.New("etcd: key not found") + ErrPrefixNotFound = errors.New("etcd: prefix not found") +) type Cluster struct { logr.Logger @@ -59,7 +62,7 @@ func (c *Cluster) GetPrefix(k string) (m map[string][]byte, err error) { return } if len(r.Kvs) == 0 { - err = ErrKeyNotFound + err = ErrPrefixNotFound return } m = make(map[string][]byte) diff --git a/pkg/etcd/etcd.go b/pkg/etcd/etcd.go index f5df23a..00fc5b1 100644 --- a/pkg/etcd/etcd.go +++ b/pkg/etcd/etcd.go @@ -6,8 +6,8 @@ import ( "fmt" "time" - "git.giftfish.de/ston1th/vipman/pkg/config" - "git.giftfish.de/ston1th/vipman/pkg/util" + "git.giftfish.de/ston1th/haproxy-lb/pkg/config" + "git.giftfish.de/ston1th/haproxy-lb/pkg/util" "github.com/coreos/etcd/clientv3" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -198,6 +198,11 @@ func (c *Cluster) Stepdown() { } } +func (c *Cluster) Fatal(err error, msg string) { + c.Error(err, "a fatal error occurred", "message", msg) + c.Stop() +} + func (c *Cluster) Stop() { if !c.session { c.cancelSession() diff --git a/pkg/haproxy/haproxy.go b/pkg/haproxy/haproxy.go index c29ce04..f48c77b 100644 --- a/pkg/haproxy/haproxy.go +++ b/pkg/haproxy/haproxy.go @@ -7,12 +7,13 @@ import ( "io" "io/ioutil" "os" + "os/exec" "sync" "text/template" ) type ServiceManager interface { - Reload() + Reload(context.Context) error } type HAProxyManager struct { @@ -25,7 +26,7 @@ type HAProxyManager struct { } func NewHAProxyManager(configFile string) (*HAProxyManager, error) { - t, err := template.New("haproxy").Parse(haproxyTemplate) + t, err := template.New("haproxy").Parse(haproxyConfigTemplate) if err != nil { return nil, err } @@ -74,5 +75,5 @@ func (ha *HAProxyManager) UpdateConfig(ctx context.Context, lbs Config) error { } file.Close() ha.hash = hash - return ha.serviceManager.Reload() + return ha.serviceManager.Reload(ctx) } diff --git a/pkg/raft/cluster.go b/pkg/raft/cluster.go index e581102..f99f13d 100644 --- a/pkg/raft/cluster.go +++ b/pkg/raft/cluster.go @@ -3,7 +3,7 @@ package raft import ( "errors" - "git.giftfish.de/ston1th/vipman/pkg/cluster" + "git.giftfish.de/ston1th/haproxy-lb/pkg/cluster" "github.com/go-logr/logr" "github.com/hashicorp/raft" ) diff --git a/pkg/raft/kv.go b/pkg/raft/kv.go index 9a7a949..fc201e3 100644 --- a/pkg/raft/kv.go +++ b/pkg/raft/kv.go @@ -2,13 +2,18 @@ package raft import ( "errors" + "strings" "sync" "git.giftfish.de/ston1th/raftbbolt/msgpack" "github.com/hashicorp/raft" ) -var ErrNotLeader = errors.New("not leader") +var ( + ErrNotLeader = errors.New("raft: not leader") + ErrKeyNotFound = errors.New("raft: key not found") + ErrPrefixNotFound = errors.New("raft: prefix not found") +) type kvm map[string][]byte @@ -21,7 +26,11 @@ type kv struct { func (kv *kv) Get(k string) ([]byte, error) { kv.mu.Lock() defer kv.mu.Unlock() - return kv.m[k], nil + v, ok := kv.m[k] + if !ok { + return nil, ErrKeyNotFound + } + return v, nil } func (kv *kv) GetPrefix(pk string) (m map[string][]byte, err error) { @@ -33,6 +42,9 @@ func (kv *kv) GetPrefix(pk string) (m map[string][]byte, err error) { m[k] = v } } + if len(m) == 0 { + err = ErrPrefixNotFound + } return } diff --git a/pkg/raft/raft.go b/pkg/raft/raft.go index afbdad9..58470a5 100644 --- a/pkg/raft/raft.go +++ b/pkg/raft/raft.go @@ -8,10 +8,10 @@ import ( "path/filepath" "time" + "git.giftfish.de/ston1th/haproxy-lb/pkg/config" + "git.giftfish.de/ston1th/haproxy-lb/pkg/raft/tls" + "git.giftfish.de/ston1th/haproxy-lb/pkg/util" "git.giftfish.de/ston1th/raftbbolt" - "git.giftfish.de/ston1th/vipman/pkg/config" - "git.giftfish.de/ston1th/vipman/pkg/raft/tls" - "git.giftfish.de/ston1th/vipman/pkg/util" hclog "github.com/hashicorp/go-hclog" "github.com/hashicorp/raft" ) @@ -187,6 +187,11 @@ func (c *Cluster) Stepdown() { } } +func (c *Cluster) Fatal(err error, msg string) { + c.Error(err, "a fatal error occurred", "message", msg) + c.Stop() +} + func (c *Cluster) Stop() { close(c.stop) <-c.done diff --git a/pkg/raft/tls/stream.go b/pkg/raft/tls/stream.go index bde01f1..834df88 100644 --- a/pkg/raft/tls/stream.go +++ b/pkg/raft/tls/stream.go @@ -6,7 +6,7 @@ import ( "net" "time" - "git.giftfish.de/ston1th/vipman/pkg/config" + "git.giftfish.de/ston1th/haproxy-lb/pkg/config" "github.com/hashicorp/raft" )