mirror of
				https://github.com/minio/minio.git
				synced 2025-10-31 08:11:19 +01:00 
			
		
		
		
	Due to botched upstream renames of project repositories and incomplete migration to go.mod support, our current dependency version of `go.mod` had bugs i.e it was using commits from master branch which didn't have the required fixes present in release-3.4 branches which leads to some rare bugs https://github.com/etcd-io/etcd/pull/11477 provides a workaround for now and we should migrate to this. release-3.5 eventually claims to fix all of this properly until then we cannot use /v3 import right now
		
			
				
	
	
		
			87 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| /*
 | |
|  * 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"
 | |
| 
 | |
| 	etcd "go.etcd.io/etcd/clientv3"
 | |
| )
 | |
| 
 | |
| 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:
 | |
| 		return fmt.Errorf("%w %s", errEtcdUnreachable, etcdEndpoints)
 | |
| 	default:
 | |
| 		return fmt.Errorf("unexpected error %w from etcd, please check your endpoints %s", err, etcdEndpoints)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func saveKeyEtcdWithTTL(ctx context.Context, client *etcd.Client, key string, data []byte, ttl int64) error {
 | |
| 	timeoutCtx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
 | |
| 	defer cancel()
 | |
| 	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)
 | |
| 	}
 | |
| 	_, 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
 | |
| }
 |