2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-07-26 16:42:54 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-03 14:35:18 -05:00
|
|
|
etcd "go.etcd.io/etcd/clientv3"
|
2019-07-26 16:42:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var errEtcdUnreachable = errors.New("etcd is unreachable, please check your endpoints")
|
|
|
|
|
|
|
|
func etcdErrToErr(err error, etcdEndpoints []string) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch err {
|
|
|
|
case context.DeadlineExceeded:
|
2019-12-02 12:28:01 -05:00
|
|
|
return fmt.Errorf("%w %s", errEtcdUnreachable, etcdEndpoints)
|
2019-07-26 16:42:54 -04:00
|
|
|
default:
|
2019-12-02 12:28:01 -05:00
|
|
|
return fmt.Errorf("unexpected error %w from etcd, please check your endpoints %s", err, etcdEndpoints)
|
2019-07-26 16:42:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 16:06:05 -05:00
|
|
|
func saveKeyEtcdWithTTL(ctx context.Context, client *etcd.Client, key string, data []byte, ttl int64) error {
|
2019-07-26 16:42:54 -04:00
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
|
|
|
defer cancel()
|
2020-11-04 16:06:05 -05:00
|
|
|
lease, err := client.Grant(timeoutCtx, ttl)
|
|
|
|
if err != nil {
|
|
|
|
return etcdErrToErr(err, client.Endpoints())
|
|
|
|
}
|
|
|
|
_, err = client.Put(timeoutCtx, key, string(data), etcd.WithLease(lease.ID))
|
|
|
|
return etcdErrToErr(err, client.Endpoints())
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveKeyEtcd(ctx context.Context, client *etcd.Client, key string, data []byte, opts ...options) error {
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
if len(opts) > 0 {
|
|
|
|
return saveKeyEtcdWithTTL(ctx, client, key, data, opts[0].ttl)
|
|
|
|
}
|
2019-07-26 16:42:54 -04:00
|
|
|
_, err := client.Put(timeoutCtx, key, string(data))
|
|
|
|
return etcdErrToErr(err, client.Endpoints())
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteKeyEtcd(ctx context.Context, client *etcd.Client, key string) error {
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := client.Delete(timeoutCtx, key)
|
|
|
|
return etcdErrToErr(err, client.Endpoints())
|
|
|
|
}
|
|
|
|
|
|
|
|
func readKeyEtcd(ctx context.Context, client *etcd.Client, key string) ([]byte, error) {
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
resp, err := client.Get(timeoutCtx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, etcdErrToErr(err, client.Endpoints())
|
|
|
|
}
|
|
|
|
if resp.Count == 0 {
|
|
|
|
return nil, errConfigNotFound
|
|
|
|
}
|
|
|
|
for _, ev := range resp.Kvs {
|
|
|
|
if string(ev.Key) == key {
|
|
|
|
return ev.Value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errConfigNotFound
|
|
|
|
}
|