added etcd kv

This commit is contained in:
ston1th 2020-11-15 22:02:23 +01:00
commit fd9f8fc1fd
8 changed files with 54 additions and 71 deletions

View file

@ -1,6 +1,7 @@
package etcd
import (
"context"
"errors"
"git.giftfish.de/ston1th/vipman/pkg/cluster"
@ -9,16 +10,19 @@ import (
"github.com/go-logr/logr"
)
var ErrKeyNotFound = errors.New("key not found")
type Cluster struct {
logr.Logger
cluster.KV
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
@ -28,13 +32,36 @@ type Cluster struct {
func NewCluster(log logr.Logger) cluster.Cluster {
return &Cluster{
Logger: log,
KV: &kv{},
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()