github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/store/etcdv3/pod.go (about)

     1  package etcdv3
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/cockroachdb/errors"
     9  	"github.com/projecteru2/core/types"
    10  
    11  	clientv3 "go.etcd.io/etcd/client/v3"
    12  )
    13  
    14  // AddPod add a pod
    15  // save it to etcd
    16  // storage path in etcd is `/pod/info/:podname`
    17  func (m *Mercury) AddPod(ctx context.Context, name, desc string) (*types.Pod, error) {
    18  	key := fmt.Sprintf(podInfoKey, name)
    19  	pod := &types.Pod{Name: name, Desc: desc}
    20  
    21  	bytes, err := json.Marshal(pod)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	resp, err := m.BatchCreate(ctx, map[string]string{key: string(bytes)})
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	if !resp.Succeeded {
    30  		return nil, types.ErrTxnConditionFailed
    31  	}
    32  	return pod, err
    33  }
    34  
    35  // RemovePod if the pod has no nodes left, otherwise return an error
    36  func (m *Mercury) RemovePod(ctx context.Context, podname string) error {
    37  	key := fmt.Sprintf(podInfoKey, podname)
    38  
    39  	ns, err := m.GetNodesByPod(ctx, &types.NodeFilter{Podname: podname, All: true})
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	if l := len(ns); l != 0 {
    45  		return errors.Wrapf(types.ErrPodHasNodes, "pod %s still has %d nodes, delete them first", podname, l)
    46  	}
    47  
    48  	resp, err := m.Delete(ctx, key)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	if resp.Deleted != 1 {
    53  		return errors.Wrapf(types.ErrPodNotFound, "podname: %s", podname)
    54  	}
    55  	return nil
    56  }
    57  
    58  // GetPod get a pod from etcd
    59  // storage path in etcd is `/pod/info/:podname`
    60  func (m *Mercury) GetPod(ctx context.Context, name string) (*types.Pod, error) {
    61  	key := fmt.Sprintf(podInfoKey, name)
    62  
    63  	ev, err := m.GetOne(ctx, key)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	pod := &types.Pod{}
    69  	if err = json.Unmarshal(ev.Value, pod); err != nil {
    70  		return nil, err
    71  	}
    72  	return pod, err
    73  }
    74  
    75  // GetAllPods get all pods in etcd
    76  // any error will break and return error immediately
    77  // storage path in etcd is `/pod`
    78  func (m *Mercury) GetAllPods(ctx context.Context) ([]*types.Pod, error) {
    79  	resp, err := m.Get(ctx, fmt.Sprintf(podInfoKey, ""), clientv3.WithPrefix())
    80  	if err != nil {
    81  		return []*types.Pod{}, err
    82  	}
    83  
    84  	pods := []*types.Pod{}
    85  	for _, ev := range resp.Kvs {
    86  		pod := &types.Pod{}
    87  		if err := json.Unmarshal(ev.Value, pod); err != nil {
    88  			return pods, err
    89  		}
    90  		pods = append(pods, pod)
    91  	}
    92  	return pods, nil
    93  }