initial commit
This commit is contained in:
commit
7715fcf373
37 changed files with 2957 additions and 0 deletions
82
pkg/etcd/cluster.go
Normal file
82
pkg/etcd/cluster.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.giftfish.de/ston1th/vipman/pkg/cluster"
|
||||
//"go.etcd.io/etcd/clientv3"
|
||||
"github.com/coreos/etcd/clientv3"
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
var ErrKeyNotFound = errors.New("key not found")
|
||||
|
||||
type Cluster struct {
|
||||
logr.Logger
|
||||
cli *clientv3.Client
|
||||
ctx context.Context
|
||||
|
||||
stepdown chan struct{}
|
||||
stop chan struct{}
|
||||
done chan struct{}
|
||||
callbacks cluster.Callbacks
|
||||
|
||||
kvPrefix string
|
||||
id string
|
||||
keyID string
|
||||
session bool
|
||||
cancelSession func()
|
||||
}
|
||||
|
||||
func NewCluster(log logr.Logger) cluster.Cluster {
|
||||
return &Cluster{
|
||||
Logger: log,
|
||||
ctx: context.Background(),
|
||||
stepdown: make(chan struct{}, 1),
|
||||
stop: make(chan struct{}, 1),
|
||||
done: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cluster) Get(k string) (v []byte, err error) {
|
||||
r, err := c.cli.Get(c.ctx, c.kvPrefix+"/"+k)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(r.Kvs) == 0 {
|
||||
err = ErrKeyNotFound
|
||||
return
|
||||
}
|
||||
v = r.Kvs[0].Value
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Cluster) Set(k string, v []byte) (err error) {
|
||||
_, err = c.cli.Put(c.ctx, c.kvPrefix+"/"+k, string(v))
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Cluster) Delete(k string) (err error) {
|
||||
_, err = c.cli.Delete(c.ctx, c.kvPrefix+"/"+k)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Cluster) SetCallbacks(callbacks cluster.Callbacks) error {
|
||||
c.callbacks = callbacks
|
||||
return c.checkConfig()
|
||||
}
|
||||
|
||||
func (c *Cluster) ID() string {
|
||||
return c.id
|
||||
}
|
||||
|
||||
func (c *Cluster) checkConfig() error {
|
||||
if c.callbacks.Leader == nil {
|
||||
return errors.New("Leader callback is nil")
|
||||
}
|
||||
if c.callbacks.Follower == nil {
|
||||
return errors.New("Follower callback is nil")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue