linter fixes

This commit is contained in:
ston1th 2021-11-07 13:30:22 +01:00
commit df5babf6c8
15 changed files with 49 additions and 55 deletions

View file

@ -7,19 +7,16 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"os"
"strconv"
"strings"
"sync"
"time"
)
type Client struct {
sync.Mutex
endpoint string
username string
password string
@ -193,13 +190,13 @@ func (c *Client) Do(r *http.Request, v interface{}) (resp *http.Response, err er
if err != nil {
return
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
resp.Body.Close()
return
}
resp.Body.Close()
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
resp.Body = io.NopCloser(bytes.NewReader(body))
if c.debugWriter != nil {
dumpResp, err := httputil.DumpResponse(resp, true)

View file

@ -14,6 +14,10 @@ func (e Error) Error() string {
return e.Err
}
func (e Error) Is(target error) bool {
return e.Err == target.Error()
}
func decodeResp(resp *http.Response, v interface{}) error {
if resp.Body == nil {
return nil

View file

@ -112,19 +112,19 @@ func NewServer(c *config.Config, log logr.Logger) (*Server, error) {
return s, nil
}
func (s *Server) UpdateDB(db *db.DB, leader bool) error {
s.Data.DB = db
func (s *Server) UpdateDB(kvdb *db.DB, leader bool) error {
s.Data.DB = kvdb
s.Data.Leader = leader
if s.store.Local != nil {
if s.Data.Net == nil {
na, err := netalloc.NewGenericAlloc(db, s.prefix)
na, err := netalloc.NewGenericAlloc(kvdb, s.prefix)
if err != nil {
return err
}
s.Data.Net = na
close(s.init)
} else {
s.Data.Net.SetStore(db)
s.Data.Net.SetStore(kvdb)
}
} else {
if s.Data.Net == nil {

View file

@ -1,11 +1,10 @@
package types
import (
"encoding/hex"
"git.giftfish.de/ston1th/haproxy-lb/pkg/config"
"golang.org/x/crypto/bcrypt"
weakrand "math/rand"
//"net/http"
"encoding/hex"
"strings"
"sync"
"time"
@ -28,11 +27,11 @@ func newCache() *cache {
}
}
func (c *cache) get(key string) (bool, bool) {
func (c *cache) get(key string) (v, ok bool) {
c.mtx.Lock()
defer c.mtx.Unlock()
v, ok := c.cache[key]
return v, ok
v, ok = c.cache[key]
return
}
func (c *cache) set(key string, value bool) {

View file

@ -8,6 +8,7 @@ import (
// Error interface is a custom api error
type Error interface {
Error() string
JSON() string
Status() int
}
@ -19,6 +20,11 @@ type Err struct {
// Error returns the error string
func (a Err) Error() string {
return a.err
}
// Error returns the error string as JSON format
func (a Err) JSON() string {
return `{"error":"` + a.err + `","status":` + strconv.Itoa(a.status) + `}`
}

View file

@ -27,11 +27,11 @@ func NewClient(c *apiclient.Client) (*Client, error) {
return &Client{c}, nil
}
func (c *Client) GetLoadBalancers(ctx context.Context) (lbs map[string]schema.LoadBalancer, err error) {
func (c *Client) GetLoadBalancers(ctx context.Context) (lbs map[string]*schema.LoadBalancer, err error) {
return c.GetLoadBalancersWithCluster(ctx, "")
}
func (c *Client) GetLoadBalancersWithCluster(ctx context.Context, cluster string) (lbs map[string]schema.LoadBalancer, err error) {
func (c *Client) GetLoadBalancersWithCluster(ctx context.Context, cluster string) (lbs map[string]*schema.LoadBalancer, err error) {
path := base
if cluster != "" {
path += "/" + cluster
@ -40,12 +40,12 @@ func (c *Client) GetLoadBalancersWithCluster(ctx context.Context, cluster string
if err != nil {
return
}
lbs = make(map[string]schema.LoadBalancer)
lbs = make(map[string]*schema.LoadBalancer)
_, err = c.c.Do(req, &lbs)
return
}
func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb schema.LoadBalancer, err error) {
func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb *schema.LoadBalancer, err error) {
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
req, err := c.c.NewRequest(ctx, "GET", path, nil)
if err != nil {
@ -55,7 +55,7 @@ func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb
return
}
func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, lb schema.LoadBalancer) (err error) {
func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, lb *schema.LoadBalancer) (err error) {
buf := new(bytes.Buffer)
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
err = lb.ValidateClient()

View file

@ -137,7 +137,7 @@ func (o Servers) RemoveByName(name string) Servers {
return append(o[:index], o[index+1:]...)
}
func (lb LoadBalancer) ValidateClient() error {
func (lb *LoadBalancer) ValidateClient() error {
for i, p := range lb.Ports {
if p.Port < 0 || p.Port > 65535 {
return fmt.Errorf("LoadBalancer.Ports[%d].Port is invalid (range 0-65535", i)
@ -157,7 +157,7 @@ func (lb LoadBalancer) ValidateClient() error {
return nil
}
func (lb LoadBalancer) ValidateServer() error {
func (lb *LoadBalancer) ValidateServer() error {
if lb.Name == "" {
return errors.New("LoadBalancer.Name can not be empty")
}
@ -183,6 +183,6 @@ func (lb LoadBalancer) ValidateServer() error {
return nil
}
func (lb LoadBalancer) JSON() ([]byte, error) {
func (lb *LoadBalancer) JSON() ([]byte, error) {
return json.Marshal(lb)
}

View file

@ -1,8 +1,6 @@
package server
import (
//"golang.org/x/crypto/bcrypt"
//"net/http"
"encoding/json"
"errors"
"net"

View file

@ -2,14 +2,14 @@ package config
import (
"crypto/x509"
"io/ioutil"
"os"
)
func LoadCertPool(file string) (p *x509.CertPool, err error) {
if file == "" {
return
}
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
return
}

View file

@ -140,10 +140,8 @@ func Validate(c *Config) error {
}
if etcd.ClusterName == "" {
return errors.New("missing etcd.clusterName config")
} else {
if strings.HasSuffix(etcd.ClusterName, "/") {
etcd.ClusterName = strings.TrimSuffix(etcd.ClusterName, "/")
}
} else if strings.HasSuffix(etcd.ClusterName, "/") {
etcd.ClusterName = strings.TrimSuffix(etcd.ClusterName, "/")
}
}
if raft == nil && etcd == nil {

View file

@ -83,7 +83,7 @@ func (db *DB) GetCIDR(name string) (cidr string, err error) {
func (db *DB) GetLBs(cl string) (lbs map[string][]byte, err error) {
lbs = make(map[string][]byte)
if cl != "" {
cl = cl + "/"
cl += "/"
}
m, err := db.kv.GetPrefix(lbPrefix + cl)
if err != nil {

View file

@ -217,15 +217,11 @@ func (c *Cluster) Stop() {
}
func (c *Cluster) leader(ctx context.Context) {
defer func() {
util.HandleCrash()
}()
defer util.HandleCrash()
c.callbacks.Leader(ctx, c)
}
func (c *Cluster) follower(ctx context.Context) {
defer func() {
util.HandleCrash()
}()
defer util.HandleCrash()
c.callbacks.Follower(ctx, c)
}

View file

@ -25,7 +25,7 @@ type ServiceManager interface {
type HAProxyManager struct {
// protects config update
sync.Mutex
mu sync.Mutex
configFile string
svc ServiceManager
@ -61,7 +61,7 @@ func (ha *HAProxyManager) checkConfig(ctx context.Context, lbs Config) (hash []b
return
}
hash = h.Sum(nil)
err = os.Chmod(tmp.Name(), 0640)
err = os.Chmod(tmp.Name(), 0o640)
if err != nil {
return
}
@ -95,7 +95,7 @@ func (ha *HAProxyManager) updateConfig(ctx context.Context, lbs Config) error {
return nil
}
ha.reload = true
file, err := os.OpenFile(ha.configFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
file, err := os.OpenFile(ha.configFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
}
@ -110,8 +110,8 @@ func (ha *HAProxyManager) updateConfig(ctx context.Context, lbs Config) error {
}
func (ha *HAProxyManager) UpdateConfig(ctx context.Context, lbs Config) error {
ha.Lock()
defer ha.Unlock()
ha.mu.Lock()
defer ha.mu.Unlock()
err := ha.updateConfig(ctx, lbs)
if err != nil {
return err

View file

@ -168,16 +168,12 @@ func (c *Cluster) Start(raftcfg *config.Config) error {
}
func (c *Cluster) leader(ctx context.Context) {
defer func() {
util.HandleCrash()
}()
defer util.HandleCrash()
c.callbacks.Leader(ctx, c)
}
func (c *Cluster) follower(ctx context.Context) {
defer func() {
util.HandleCrash()
}()
defer util.HandleCrash()
c.callbacks.Follower(ctx, c)
}

View file

@ -27,7 +27,7 @@ func DefaultInterface() (iface string, err error) {
}
type Network struct {
sync.Mutex
mu sync.Mutex
m map[string]struct{}
iface string
label string
@ -76,8 +76,8 @@ func (n *Network) HasIP(a *netlink.Addr) bool {
}
func (n *Network) AddIP(cidr string) error {
n.Lock()
defer n.Unlock()
n.mu.Lock()
defer n.mu.Unlock()
addr, err := netlink.ParseAddr(cidr)
if err != nil {
return err
@ -97,8 +97,8 @@ func (n *Network) AddIP(cidr string) error {
}
func (n *Network) DeleteIP(cidr string) error {
n.Lock()
defer n.Unlock()
n.mu.Lock()
defer n.mu.Unlock()
addr, err := netlink.ParseAddr(cidr)
if err != nil {
return err
@ -115,8 +115,8 @@ func (n *Network) DeleteIP(cidr string) error {
}
func (n *Network) GetIPs() (cidrs []string) {
n.Lock()
defer n.Unlock()
n.mu.Lock()
defer n.mu.Unlock()
cidrs = make([]string, len(n.m))
c := 0
for k := range n.m {