-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcluster.go
More file actions
135 lines (119 loc) · 4.38 KB
/
cluster.go
File metadata and controls
135 lines (119 loc) · 4.38 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package orchestration
import (
"context"
"fmt"
"math/rand"
"net/url"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/beekeeper/pkg/bee"
"github.com/ethersphere/beekeeper/pkg/k8s"
"github.com/ethersphere/beekeeper/pkg/swap"
)
type Cluster interface {
Accounting(ctx context.Context) (accounting ClusterAccounting, err error)
AddNodeGroup(name string, o NodeGroupOptions)
Addresses(ctx context.Context) (addrs map[string]NodeGroupAddresses, err error)
Balances(ctx context.Context) (balances ClusterBalances, err error)
FlattenAccounting(ctx context.Context) (accounting NodeGroupAccounting, err error)
FlattenBalances(ctx context.Context) (balances NodeGroupBalances, err error)
FlattenOverlays(ctx context.Context, exclude ...string) (map[string]swarm.Address, error)
FlattenSettlements(ctx context.Context) (settlements NodeGroupSettlements, err error)
FlattenTopologies(ctx context.Context) (topologies map[string]bee.Topology, err error)
FullNodeNames() (names []string)
ShuffledFullNodeClients(ctx context.Context, r *rand.Rand) ([]*bee.Client, error)
GlobalReplicationFactor(ctx context.Context, a swarm.Address) (grf int, err error)
LightNodeNames() (names []string)
Name() string
NodeGroup(name string) (ng NodeGroup, err error)
NodeGroups() (l map[string]NodeGroup)
NodeGroupsSorted() (l []string)
NodeNames() (names []string)
Nodes() map[string]Node
NodesClients(ctx context.Context) (map[string]*bee.Client, error)
NodesClientsAll(ctx context.Context) (map[string]*bee.Client, error)
Overlays(ctx context.Context, exclude ...string) (overlays ClusterOverlays, err error)
Peers(ctx context.Context, exclude ...string) (peers ClusterPeers, err error)
RandomNode(ctx context.Context, r *rand.Rand) (node Node, err error)
Settlements(ctx context.Context) (settlements ClusterSettlements, err error)
Size() (size int)
Topologies(ctx context.Context) (topologies ClusterTopologies, err error)
}
// ClusterOptions represents Bee cluster options
type ClusterOptions struct {
Annotations map[string]string
APIDomain string
APIDomainInternal string
APIInsecureTLS bool
APIScheme string
K8SClient *k8s.Client
SwapClient swap.Client
Labels map[string]string
Namespace string
DisableNamespace bool
}
// ClusterAddresses represents addresses of all nodes in the cluster
type ClusterAddresses map[string]NodeGroupAddresses
// ClusterAccounting represents accounting of all nodes in the cluster
type ClusterAccounting map[string]NodeGroupAccounting
// ClusterBalances represents balances of all nodes in the cluster
type ClusterBalances map[string]NodeGroupBalances
// ClusterOverlays represents overlay addresses of all nodes in the cluster
type ClusterOverlays map[string]NodeGroupOverlays
// ClusterPeers represents peers of all nodes in the cluster
type ClusterPeers map[string]NodeGroupPeers
// ClusterSettlements represents settlements of all nodes in the cluster
type ClusterSettlements map[string]NodeGroupSettlements
// ClusterTopologies represents Kademlia topology of all nodes in the cluster
type ClusterTopologies map[string]NodeGroupTopologies
// RandomOverlay returns a random overlay from a random NodeGroup
func (c ClusterOverlays) Random(r *rand.Rand) (nodeGroup string, nodeName string, overlay swarm.Address) {
i := r.Intn(len(c))
var (
ng, name string
ngo NodeGroupOverlays
o swarm.Address
)
for n, v := range c {
if i == 0 {
ng = n
ngo = v
break
}
i--
}
i = r.Intn(len(ngo))
for n, v := range ngo {
if i == 0 {
name = n
o = v
break
}
i--
}
return ng, name, o
}
// ApiURL generates URL for node's API
func (c ClusterOptions) ApiURL(name string, inCluster bool) (u *url.URL, err error) {
apiDomain := c.APIDomain
apiScheme := c.APIScheme
if inCluster {
apiDomain = c.APIDomainInternal
apiScheme = "http"
}
if c.DisableNamespace {
u, err = url.Parse(fmt.Sprintf("%s://%s.%s", apiScheme, name, apiDomain))
} else {
u, err = url.Parse(fmt.Sprintf("%s://%s.%s.%s", apiScheme, name, c.Namespace, apiDomain))
}
if err != nil {
return nil, fmt.Errorf("bad API url for node %s: %w", name, err)
}
return
}
// IngressHost generates host for node's API ingress
func (c ClusterOptions) IngressHost(name string) string {
if c.DisableNamespace {
return fmt.Sprintf("%s.%s", name, c.APIDomain)
}
return fmt.Sprintf("%s.%s.%s", name, c.Namespace, c.APIDomain)
}