-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathstamper.go
More file actions
296 lines (242 loc) · 7.37 KB
/
stamper.go
File metadata and controls
296 lines (242 loc) · 7.37 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package stamper
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/ethersphere/bee/v2/pkg/postage"
"github.com/ethersphere/beekeeper/pkg/bee"
"github.com/ethersphere/beekeeper/pkg/bee/api"
"github.com/ethersphere/beekeeper/pkg/k8s"
"github.com/ethersphere/beekeeper/pkg/logging"
"github.com/ethersphere/beekeeper/pkg/swap"
)
type Option func(*options)
type options struct {
batchIDs []string
postageLabels []string
}
func WithBatchIDs(batchIds []string) Option {
return func(o *options) {
o.batchIDs = batchIds
}
}
func WithPostageLabels(postageLabels []string) Option {
return func(o *options) {
o.postageLabels = postageLabels
}
}
type ClientConfig struct {
Log logging.Logger
Namespace string
HTTPClient *http.Client
K8sClient *k8s.Client
SwapClient swap.BlockTimeFetcher
BeeClients map[string]*bee.Client
LabelSelector string
InCluster bool
}
type Client struct {
log logging.Logger
namespace string
k8sClient *k8s.Client
swapClient swap.BlockTimeFetcher
httpClient *http.Client
beeClients map[string]*bee.Client
labelSelector string
inCluster bool
}
func New(cfg *ClientConfig) *Client {
if cfg == nil {
return nil
}
if cfg.Log == nil {
cfg.Log = logging.New(io.Discard, 0)
}
if cfg.SwapClient == nil {
cfg.SwapClient = &swap.NotSet{}
}
if cfg.HTTPClient == nil {
cfg.HTTPClient = &http.Client{}
}
return &Client{
log: cfg.Log,
namespace: cfg.Namespace,
k8sClient: cfg.K8sClient,
swapClient: cfg.SwapClient,
beeClients: cfg.BeeClients,
labelSelector: cfg.LabelSelector,
inCluster: cfg.InCluster,
httpClient: cfg.HTTPClient,
}
}
// Create creates a postage batch.
func (s *Client) Create(ctx context.Context, duration time.Duration, depth uint16, postageLabel string) error {
if duration == 0 {
return fmt.Errorf("duration must be greater than 0")
}
if depth <= postage.BucketDepth {
return fmt.Errorf("depth must be greater than %d", postage.BucketDepth)
}
s.log.WithFields(map[string]interface{}{
"duration": duration,
"depth": depth,
}).Info("creating postage batch on nodes")
nodes, err := s.getNodes(ctx)
if err != nil {
return fmt.Errorf("get nodes: %w", err)
}
blockTime, err := s.swapClient.FetchBlockTime(ctx, swap.WithOffset(1000))
if err != nil {
return fmt.Errorf("fetching block time: %w", err)
}
for _, node := range nodes {
if err := node.Create(ctx, duration, depth, postageLabel, blockTime); err != nil {
s.log.Errorf("node %s create postage batch: %v", node.name, err)
}
}
return nil
}
// Dilute dilutes a postage batch.
func (s *Client) Dilute(ctx context.Context, usageThreshold float64, dilutionDepth uint16, opts ...Option) error {
s.log.WithFields(map[string]interface{}{
"usageThreshold": usageThreshold,
"dilutionDepth": dilutionDepth,
}).Info("diluting postage batch on nodes")
nodes, err := s.getNodes(ctx)
if err != nil {
return fmt.Errorf("get nodes: %w", err)
}
for _, node := range nodes {
if err := node.Dilute(ctx, usageThreshold, dilutionDepth, processOptions(opts...)); err != nil {
s.log.Errorf("node %s dilute postage batch: %v", node.name, err)
}
}
return nil
}
// Set sets the topup and dilution parameters.
func (s *Client) Set(ctx context.Context, ttlThreshold time.Duration, topupTo time.Duration, usageThreshold float64, dilutionDepth uint16, opts ...Option) error {
s.log.WithFields(map[string]interface{}{
"ttlThreshold": ttlThreshold,
"topupTo": topupTo,
"usageThreshold": usageThreshold,
"dilutionDepth": dilutionDepth,
}).Info("setting topup and dilution on postage batch on nodes")
nodes, err := s.getNodes(ctx)
if err != nil {
return fmt.Errorf("get nodes: %w", err)
}
blockTime, err := s.swapClient.FetchBlockTime(ctx, swap.WithOffset(1000))
if err != nil {
return fmt.Errorf("fetching block time: %w", err)
}
for _, node := range nodes {
if err := node.Set(ctx, ttlThreshold, topupTo, usageThreshold, dilutionDepth, blockTime, processOptions(opts...)); err != nil {
s.log.Errorf("node %s set postage batch: %v", node.name, err)
}
}
return nil
}
// Topup tops up a postage batch.
func (s *Client) Topup(ctx context.Context, ttlThreshold time.Duration, topupTo time.Duration, opts ...Option) (err error) {
s.log.WithFields(map[string]interface{}{
"ttlThreshold": ttlThreshold,
"topupTo": topupTo,
}).Info("topup postage batch on nodes")
nodes, err := s.getNodes(ctx)
if err != nil {
return fmt.Errorf("get nodes: %w", err)
}
blockTime, err := s.swapClient.FetchBlockTime(ctx, swap.WithOffset(1000))
if err != nil {
return fmt.Errorf("fetching block time: %w", err)
}
for _, node := range nodes {
if err := node.Topup(ctx, ttlThreshold, topupTo, blockTime, processOptions(opts...)); err != nil {
s.log.Errorf("node %s topup postage batch: %v", node.name, err)
}
}
return nil
}
func (sc *Client) getNodes(ctx context.Context) (nodes []node, err error) {
if sc.namespace != "" {
return sc.getNamespaceNodes(ctx)
}
if sc.beeClients == nil {
return nil, fmt.Errorf("bee clients not provided")
}
nodes = make([]node, 0, len(sc.beeClients))
for nodeName, beeClient := range sc.beeClients {
nodes = append(nodes, *newNodeInfo(beeClient.API(), nodeName, sc.log))
}
return nodes, nil
}
func (sc *Client) getNamespaceNodes(ctx context.Context) (nodes []node, err error) {
if sc.namespace == "" {
return nil, fmt.Errorf("namespace not provided")
}
if sc.k8sClient == nil {
return nil, fmt.Errorf("k8s client not provided")
}
if sc.inCluster {
nodes, err = sc.getServiceNodes(ctx)
} else {
nodes, err = sc.getIngressNodes(ctx)
}
if err != nil {
return nil, fmt.Errorf("get nodes: %w", err)
}
return nodes, nil
}
func (sc *Client) getServiceNodes(ctx context.Context) ([]node, error) {
svcNodes, err := sc.k8sClient.Service.GetNodes(ctx, sc.namespace, sc.labelSelector)
if err != nil {
return nil, fmt.Errorf("list api services: %w", err)
}
nodes := make([]node, len(svcNodes))
for i, node := range svcNodes {
parsedURL, err := url.Parse(node.Endpoint)
if err != nil {
return nil, fmt.Errorf("extract base URL: %w", err)
}
apiClient, err := api.NewClient(parsedURL, sc.httpClient)
if err != nil {
return nil, fmt.Errorf("create api client: %w", err)
}
nodes[i] = *newNodeInfo(apiClient, node.Name, sc.log)
}
return nodes, nil
}
func (sc *Client) getIngressNodes(ctx context.Context) ([]node, error) {
ingressNodes, err := sc.k8sClient.Ingress.GetNodes(ctx, sc.namespace, sc.labelSelector)
if err != nil {
return nil, fmt.Errorf("list ingress api nodes hosts: %w", err)
}
ingressRouteNodes, err := sc.k8sClient.IngressRoute.GetNodes(ctx, sc.namespace, sc.labelSelector)
if err != nil {
return nil, fmt.Errorf("list ingress route api nodes hosts: %w", err)
}
allNodes := append(ingressNodes, ingressRouteNodes...)
nodes := make([]node, len(allNodes))
for i, node := range allNodes {
apiURL, err := url.Parse(fmt.Sprintf("http://%s", node.Host))
if err != nil {
return nil, fmt.Errorf("extract base URL: %w", err)
}
apiClient, err := api.NewClient(apiURL, sc.httpClient)
if err != nil {
return nil, fmt.Errorf("create api client: %w", err)
}
nodes[i] = *newNodeInfo(apiClient, node.Name, sc.log)
}
return nodes, nil
}
func processOptions(opts ...Option) *options {
o := &options{}
for _, opt := range opts {
opt(o)
}
return o
}