-
Notifications
You must be signed in to change notification settings - Fork 211
/
discover.go
50 lines (45 loc) · 1.15 KB
/
discover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cluster
import (
"fmt"
"sort"
"strconv"
"strings"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/spacemeshos/go-spacemesh/systest/testcontext"
)
func discoverNodes(ctx *testcontext.Context, kind string) ([]*NodeClient, error) {
deployments, err := ctx.Client.AppsV1().Deployments(ctx.Namespace).List(ctx,
apimetav1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", kind)})
if err != nil {
return nil, fmt.Errorf("failed to list pods app=%s: %w", kind, err)
}
var rst []*NodeClient
for _, deployment := range deployments.Items {
deployment := deployment
rst = append(rst, &NodeClient{
session: ctx,
Node: Node{
Name: deployment.Name,
P2P: 7513,
GRPC_PUB: 9092,
GRPC_PRIV: 9093,
},
})
}
sort.Slice(rst, func(i, j int) bool {
return decodeOrdinal(rst[i].Name) < decodeOrdinal(rst[j].Name)
})
return rst, nil
}
func decodeOrdinal(name string) int {
// expected name is boot-1-0 or poet-3213221321
parts := strings.Split(name, "-")
if len(parts) < 2 {
panic(fmt.Sprintf("unexpected name format %s", name))
}
ord, err := strconv.Atoi(parts[1])
if err != nil {
panic(err)
}
return ord
}