2020-02-04 21:00:03 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-24 12:38:18 +08:00
|
|
|
"fmt"
|
2020-02-04 21:00:03 +08:00
|
|
|
"github.com/coreos/etcd/clientv3"
|
2020-11-25 16:39:38 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-02-04 21:00:03 +08:00
|
|
|
)
|
|
|
|
|
2020-02-24 12:38:18 +08:00
|
|
|
// EtcdKeyType etcd key type
|
|
|
|
type EtcdKeyType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ServiceCheckEtcdKey source check etcd key
|
|
|
|
ServiceCheckEtcdKey EtcdKeyType = iota
|
|
|
|
// ShareResultEtcdKey share result etcd key
|
|
|
|
ShareResultEtcdKey
|
|
|
|
//BackupRestoreEtcdKey backup restore etcd key
|
|
|
|
BackupRestoreEtcdKey
|
|
|
|
)
|
|
|
|
|
2020-02-04 21:00:03 +08:00
|
|
|
// EtcdHandler defines handler methods about k8s pods.
|
|
|
|
type EtcdHandler struct {
|
|
|
|
etcdCli *clientv3.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEtcdHandler creates a new PodHandler.
|
|
|
|
func NewEtcdHandler(etcdCli *clientv3.Client) *EtcdHandler {
|
|
|
|
return &EtcdHandler{etcdCli}
|
|
|
|
}
|
|
|
|
|
2021-05-12 11:39:38 +08:00
|
|
|
// CleanAllServiceData -
|
2020-02-24 12:38:18 +08:00
|
|
|
func (h *EtcdHandler) CleanAllServiceData(keys []string) {
|
2020-02-04 21:00:03 +08:00
|
|
|
for _, key := range keys {
|
2020-02-24 12:38:18 +08:00
|
|
|
h.cleanEtcdByKey(key, ServiceCheckEtcdKey, ShareResultEtcdKey, BackupRestoreEtcdKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanServiceCheckData clean service check etcd data
|
|
|
|
func (h *EtcdHandler) CleanServiceCheckData(key string) {
|
|
|
|
h.cleanEtcdByKey(key, ServiceCheckEtcdKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *EtcdHandler) cleanEtcdByKey(key string, keyTypes ...EtcdKeyType) {
|
|
|
|
if key == "" {
|
|
|
|
logrus.Warn("get empty etcd data key, ignore it")
|
2020-02-04 21:00:03 +08:00
|
|
|
}
|
2020-02-24 12:38:18 +08:00
|
|
|
for _, keyType := range keyTypes {
|
|
|
|
prefix := ""
|
|
|
|
switch keyType {
|
|
|
|
case ServiceCheckEtcdKey:
|
|
|
|
prefix = fmt.Sprintf("/servicecheck/%s", key)
|
|
|
|
case ShareResultEtcdKey:
|
|
|
|
prefix = fmt.Sprintf("/rainbond/shareresult/%s", key)
|
|
|
|
case BackupRestoreEtcdKey:
|
|
|
|
prefix = fmt.Sprintf("/rainbond/backup_restore/%s", key)
|
|
|
|
}
|
|
|
|
h.cleanEtcdData(prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-02-04 21:00:03 +08:00
|
|
|
|
2020-02-24 12:38:18 +08:00
|
|
|
func (h *EtcdHandler) cleanEtcdData(prefix string) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
logrus.Debugf("ready for delete etcd key:%s", prefix)
|
|
|
|
_, err := h.etcdCli.Delete(ctx, prefix)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("delete etcd key[%s] failed: %s", prefix, err.Error())
|
|
|
|
}
|
2020-02-04 21:00:03 +08:00
|
|
|
}
|