forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_server.go
280 lines (234 loc) · 7.41 KB
/
grpc_server.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
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
package api
import (
"context"
"errors"
"fmt"
"time"
"github.com/golang/protobuf/ptypes/duration"
"github.com/linkerd/linkerd2/controller/k8s"
pkgK8s "github.com/linkerd/linkerd2/pkg/k8s"
"github.com/linkerd/linkerd2/pkg/prometheus"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/linkerd/linkerd2/viz/metrics-api/util"
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
// Server specifies the interface the Viz metric API server should implement
type Server interface {
pb.ApiServer
}
type grpcServer struct {
pb.UnimplementedApiServer
prometheusAPI promv1.API
k8sAPI *k8s.API
controllerNamespace string
clusterDomain string
ignoredNamespaces []string
}
type podReport struct {
lastReport time.Time
processStartTimeSeconds time.Time
}
const (
podQuery = "max(process_start_time_seconds{%s}) by (pod, namespace)"
k8sClientSubsystemName = "kubernetes"
k8sClientCheckDescription = "linkerd viz can talk to Kubernetes"
promClientSubsystemName = "prometheus"
promClientCheckDescription = "linkerd viz can talk to Prometheus"
)
// NewGrpcServer creates a new instance of the Api server and registers it
// with Prometheus.
func NewGrpcServer(
promAPI promv1.API,
k8sAPI *k8s.API,
controllerNamespace string,
clusterDomain string,
ignoredNamespaces []string,
) *grpc.Server {
server := &grpcServer{
prometheusAPI: promAPI,
k8sAPI: k8sAPI,
controllerNamespace: controllerNamespace,
clusterDomain: clusterDomain,
ignoredNamespaces: ignoredNamespaces,
}
s := prometheus.NewGrpcServer(grpc.MaxConcurrentStreams(0))
pb.RegisterApiServer(s, server)
return s
}
func (s *grpcServer) ListPods(ctx context.Context, req *pb.ListPodsRequest) (*pb.ListPodsResponse, error) {
log.Debugf("ListPods request: %+v", req)
targetOwner := req.GetSelector().GetResource()
// Reports is a map from instance name to the absolute time of the most recent
// report from that instance and its process start time
reports := make(map[string]podReport)
labelSelector := labels.Everything()
if s := req.GetSelector().GetLabelSelector(); s != "" {
var err error
labelSelector, err = labels.Parse(s)
if err != nil {
return nil, fmt.Errorf("invalid label selector %q: %w", s, err)
}
}
nsQuery := ""
namespace := ""
if targetOwner.GetNamespace() != "" {
namespace = targetOwner.GetNamespace()
} else if targetOwner.GetType() == pkgK8s.Namespace {
namespace = targetOwner.GetName()
}
if namespace != "" {
nsQuery = fmt.Sprintf("namespace=\"%s\"", namespace)
}
processStartTimeQuery := fmt.Sprintf(podQuery, nsQuery)
// Query Prometheus for all pods present
vec, err := s.queryProm(ctx, processStartTimeQuery)
if err != nil && !errors.Is(err, ErrNoPrometheusInstance) {
return nil, err
}
for _, sample := range vec {
pod := string(sample.Metric["pod"])
timestamp := sample.Timestamp
reports[pod] = podReport{
lastReport: time.Unix(0, int64(timestamp)*int64(time.Millisecond)),
processStartTimeSeconds: time.Unix(0, int64(sample.Value)*int64(time.Second)),
}
}
var pods []*corev1.Pod
if namespace != "" {
pods, err = s.k8sAPI.Pod().Lister().Pods(namespace).List(labelSelector)
} else {
pods, err = s.k8sAPI.Pod().Lister().List(labelSelector)
}
if err != nil {
return nil, err
}
podList := make([]*pb.Pod, 0)
for _, pod := range pods {
if s.shouldIgnore(pod) {
continue
}
ownerKind, ownerName := s.k8sAPI.GetOwnerKindAndName(ctx, pod, false)
// filter out pods without matching owner
if targetOwner.GetNamespace() != "" && targetOwner.GetNamespace() != pod.GetNamespace() {
continue
}
if targetOwner.GetType() != "" && targetOwner.GetType() != ownerKind {
continue
}
if targetOwner.GetName() != "" && targetOwner.GetName() != ownerName {
continue
}
updated, added := reports[pod.Name]
item := util.K8sPodToPublicPod(*pod, ownerKind, ownerName)
item.Added = added
if added {
since := time.Since(updated.lastReport)
item.SinceLastReport = &duration.Duration{
Seconds: int64(since / time.Second),
Nanos: int32(since % time.Second),
}
sinceStarting := time.Since(updated.processStartTimeSeconds)
item.Uptime = &duration.Duration{
Seconds: int64(sinceStarting / time.Second),
Nanos: int32(sinceStarting % time.Second),
}
}
podList = append(podList, item)
}
rsp := pb.ListPodsResponse{Pods: podList}
log.Debugf("ListPods response: %s", rsp.String())
return &rsp, nil
}
func (s *grpcServer) SelfCheck(ctx context.Context, in *pb.SelfCheckRequest) (*pb.SelfCheckResponse, error) {
k8sClientCheck := &pb.CheckResult{
SubsystemName: k8sClientSubsystemName,
CheckDescription: k8sClientCheckDescription,
Status: pb.CheckStatus_OK,
}
_, err := s.k8sAPI.Pod().Lister().List(labels.Everything())
if err != nil {
k8sClientCheck.Status = pb.CheckStatus_ERROR
k8sClientCheck.FriendlyMessageToUser = fmt.Sprintf("Error calling the Kubernetes API: %s", err)
}
response := &pb.SelfCheckResponse{
Results: []*pb.CheckResult{
k8sClientCheck,
},
}
if s.prometheusAPI != nil {
promClientCheck := &pb.CheckResult{
SubsystemName: promClientSubsystemName,
CheckDescription: promClientCheckDescription,
Status: pb.CheckStatus_OK,
}
_, err = s.queryProm(ctx, fmt.Sprintf(podQuery, ""))
if err != nil {
promClientCheck.Status = pb.CheckStatus_ERROR
promClientCheck.FriendlyMessageToUser = fmt.Sprintf("Error calling Prometheus from the control plane: %s", err)
}
response.Results = append(response.Results, promClientCheck)
}
return response, nil
}
func (s *grpcServer) shouldIgnore(pod *corev1.Pod) bool {
for _, namespace := range s.ignoredNamespaces {
if pod.Namespace == namespace {
return true
}
}
return false
}
func (s *grpcServer) ListServices(ctx context.Context, req *pb.ListServicesRequest) (*pb.ListServicesResponse, error) {
log.Debugf("ListServices request: %+v", req)
services, err := s.k8sAPI.GetServices(req.Namespace, "")
if err != nil {
return nil, err
}
svcs := make([]*pb.Service, 0)
for _, svc := range services {
svcs = append(svcs, &pb.Service{
Name: svc.GetName(),
Namespace: svc.GetNamespace(),
})
}
return &pb.ListServicesResponse{Services: svcs}, nil
}
// validateTimeWindow returns an error if the Prometheus scrape interval
// is longer than the query time window. This is an opportunistic, best-effort
// validation: if we cannot determine the Prometheus scrape interval for any
// reason, we do not return an error.
func (s *grpcServer) validateTimeWindow(ctx context.Context, window string) error {
config, err := s.prometheusAPI.Config(ctx)
if err != nil {
return nil
}
type PrometheusConfig struct {
Global map[string]string
}
var prom PrometheusConfig
err = yaml.Unmarshal([]byte(config.YAML), &prom)
if err != nil {
return nil
}
scrape_interval_str, found := prom.Global["scrape_interval"]
if !found {
return nil
}
scrape_interval, err := time.ParseDuration(scrape_interval_str)
if err != nil {
return nil
}
t, err := time.ParseDuration(window)
if err != nil {
return err
}
if t < scrape_interval {
return fmt.Errorf("time window (%s) must be at least as long as the Prometheus scrape interval (%s)", window, scrape_interval)
}
return nil
}