-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
202 lines (165 loc) · 4.23 KB
/
rpc.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
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
package rpc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/sagacious-labs/k8trics/pkg/protos/v1alpha1/api"
"github.com/sagacious-labs/k8trics/pkg/store"
"github.com/sagacious-labs/k8trics/pkg/utils"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// WatchDataResponse represents the response of the watch RPCs
type WatchDataResponse struct {
Data map[string]interface{} `json:"data,omitempty"`
}
// HyperionApply is a wrapper around hyperion's `Apply` RPC
func HyperionApply(ctx context.Context, req *api.ApplyRequest, host string) (*api.ApplyResponse, error) {
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := api.NewHyperionAPIServiceClient(conn)
res, err := client.Apply(ctx, req)
if err != nil {
return nil, err
}
return res, nil
}
// HyperionDelete is a wrapper around hyperion's `Delete` RPC
func HyperionDelete(ctx context.Context, req *api.DeleteRequest, host string) (*api.DeleteResponse, error) {
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := api.NewHyperionAPIServiceClient(conn)
res, err := client.Delete(ctx, req)
if err != nil {
return nil, err
}
return res, nil
}
// HyperionGet is a wrapper around hyperion's `Get` RPC
func HyperionGet(ctx context.Context, req *api.GetRequest, host string) (*api.GetResponse, error) {
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := api.NewHyperionAPIServiceClient(conn)
res, err := client.Get(ctx, req)
if err != nil {
return nil, err
}
return res, nil
}
// HyperionList is a wrapper around hyperion's `List` RPC
func HyperionList(ctx context.Context, req *api.ListRequest, host string) (chan *api.GetResponse, error) {
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := api.NewHyperionAPIServiceClient(conn)
res, err := client.List(ctx, req)
if err != nil {
return nil, err
}
ch := make(chan *api.GetResponse, 8)
go func() {
for {
item, err := res.Recv()
if err == io.EOF {
close(ch)
return
}
if err != nil {
fmt.Println("[List Error]: ", err)
continue
}
ch <- item
}
}()
return ch, nil
}
// HyperionWatchData is a wrapper around hyperion's `WatchData` RPC
func HyperionWatchData(ctx context.Context, req *api.WatchDataRequest, host string) (chan *WatchDataResponse, error) {
store, ok := ctx.Value("pod_store").(*store.PodStore)
if !ok {
return nil, errors.New("pod store not found")
}
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := api.NewHyperionAPIServiceClient(conn)
res, err := client.WatchData(ctx, req)
if err != nil {
return nil, err
}
ch := make(chan *WatchDataResponse, 8)
go func() {
for {
item, err := res.Recv()
if err != nil {
if err == io.EOF {
close(ch)
}
return
}
data := parseWatchDataJSON(item.Data)
cid, ok := data["container_id"].(string)
if !ok {
logrus.Warn("container_id not found in the retrieved data")
continue
}
pod, ok := store.GetByContainerID(cid)
if !ok {
logrus.Warn("no pod found for container id: ", cid)
continue
}
data["name"] = utils.TrimPodTemplateHash(&pod.Pod)
ch <- &WatchDataResponse{
Data: data,
}
}
}()
return ch, nil
}
// HyperionWatchLog is a wrapper around hyperion's `WatchLog` RPC
func HyperionWatchLog(ctx context.Context, req *api.WatchLogRequest, host string) (chan string, error) {
conn, err := grpc.Dial(host, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := api.NewHyperionAPIServiceClient(conn)
res, err := client.WatchLog(ctx, req)
if err != nil {
return nil, err
}
ch := make(chan string, 8)
go func() {
for {
item, err := res.Recv()
if err != nil {
if err == io.EOF {
close(ch)
}
return
}
ch <- parseWatchLog(item.Data)
}
}()
return ch, nil
}
// parseWatchDataJSON takes in a slice of byte and converts it into
// map[string]interface{}
//
// If the method fails then it returns an empty map
func parseWatchDataJSON(byt []byte) (mp map[string]interface{}) {
_ = json.Unmarshal(byt, &mp)
return
}
func parseWatchLog(byt []byte) string {
return string(byt)
}