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

     1  package etcdv3
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	clientv3 "go.etcd.io/etcd/client/v3"
     9  
    10  	"github.com/projecteru2/core/log"
    11  )
    12  
    13  // GetDeployStatus get deploy status from store
    14  func (m *Mercury) GetDeployStatus(ctx context.Context, appname, entryname string) (map[string]int, error) {
    15  	// 手动加 / 防止不精确
    16  	key := filepath.Join(workloadDeployPrefix, appname, entryname) + "/"
    17  	resp, err := m.Get(ctx, key, clientv3.WithPrefix(), clientv3.WithKeysOnly())
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	if resp.Count == 0 {
    22  		log.WithFunc("store.etcdv3.GetDeployStatus").Warnf(ctx, "Deploy status not found %s.%s", appname, entryname)
    23  	}
    24  
    25  	deployCount := m.doGetDeployStatus(ctx, resp)
    26  	processingCount, err := m.doLoadProcessing(ctx, appname, entryname)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	// node count: deploy count + processing count
    32  	nodeCount := map[string]int{}
    33  	for node, count := range deployCount {
    34  		nodeCount[node] = count
    35  	}
    36  	for node, count := range processingCount {
    37  		nodeCount[node] += count
    38  	}
    39  
    40  	return nodeCount, nil
    41  }
    42  
    43  // doGetDeployStatus returns how many workload have been deployed on each node
    44  func (m *Mercury) doGetDeployStatus(_ context.Context, resp *clientv3.GetResponse) map[string]int {
    45  	nodesCount := map[string]int{}
    46  	for _, ev := range resp.Kvs {
    47  		key := string(ev.Key)
    48  		parts := strings.Split(key, "/")
    49  		nodename := parts[len(parts)-2]
    50  		if _, ok := nodesCount[nodename]; !ok {
    51  			nodesCount[nodename] = 1
    52  			continue
    53  		}
    54  		nodesCount[nodename]++
    55  	}
    56  
    57  	return nodesCount
    58  }