-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathruntime.go
More file actions
60 lines (49 loc) · 1.53 KB
/
Copy pathruntime.go
File metadata and controls
60 lines (49 loc) · 1.53 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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"errors"
"fmt"
runtimev1 "github.com/agntcy/dir/api/runtime/v1"
"github.com/agntcy/dir/client/streaming"
)
func (c *Client) GetWorkload(ctx context.Context, workloadID string) (*runtimev1.Workload, error) {
workload, err := c.DiscoveryServiceClient.GetWorkload(ctx, &runtimev1.GetWorkloadRequest{
Id: workloadID,
})
if err != nil {
return nil, fmt.Errorf("failed to get workload %s: %w", workloadID, err)
}
return workload, nil
}
func (c *Client) ListWorkloadsStream(ctx context.Context, labels map[string]string) (streaming.StreamResult[runtimev1.Workload], error) {
stream, err := c.DiscoveryServiceClient.ListWorkloads(ctx, &runtimev1.ListWorkloadsRequest{
Labels: labels,
})
if err != nil {
return nil, fmt.Errorf("failed to create list workload stream: %w", err)
}
//nolint:wrapcheck
return streaming.ProcessServerStream(ctx, stream)
}
func (c *Client) ListWorkloads(ctx context.Context, labels map[string]string) ([]*runtimev1.Workload, error) {
// Use channel to communicate error safely (no race condition)
result, err := c.ListWorkloadsStream(ctx, labels)
if err != nil {
return nil, err
}
// Check for results
var errs error
var workloads []*runtimev1.Workload
for {
select {
case err := <-result.ErrCh():
errs = errors.Join(errs, err)
case resp := <-result.ResCh():
workloads = append(workloads, resp)
case <-result.DoneCh():
return workloads, errs
}
}
}