2019-07-26 16:42:54 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-07-07 22:04:29 -04:00
|
|
|
etcd "go.etcd.io/etcd/v3/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
|
|
|
|
}
|