-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
server.go
116 lines (94 loc) · 3.6 KB
/
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
/*
Copyright 2022 The KEDA Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metricsservice
import (
"context"
"fmt"
"net"
"google.golang.org/grpc"
"k8s.io/metrics/pkg/apis/external_metrics/v1beta1"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/kedacore/keda/v2/pkg/metricsservice/api"
"github.com/kedacore/keda/v2/pkg/scaling"
)
var log = logf.Log.WithName("grpc_server")
type GrpcServer struct {
server *grpc.Server
address string
scalerHandler *scaling.ScaleHandler
api.UnimplementedMetricsServiceServer
}
// GetMetrics returns metrics values in form of ExternalMetricValueList for specified ScaledObject reference
func (s *GrpcServer) GetMetrics(ctx context.Context, in *api.ScaledObjectRef) (*api.Response, error) {
response := api.Response{}
v1beta1ExtMetrics := &v1beta1.ExternalMetricValueList{}
extMetrics, exportedMetrics, err := (*s.scalerHandler).GetScaledObjectMetrics(ctx, in.Name, in.Namespace, in.MetricName)
response.PromMetrics = exportedMetrics
if err != nil {
return &response, fmt.Errorf("error when getting metric values %w", err)
}
err = v1beta1.Convert_external_metrics_ExternalMetricValueList_To_v1beta1_ExternalMetricValueList(extMetrics, v1beta1ExtMetrics, nil)
if err != nil {
return &response, fmt.Errorf("error when converting metric values %w", err)
}
log.V(1).WithValues("scaledObjectName", in.Name, "scaledObjectNamespace", in.Namespace, "metrics", v1beta1ExtMetrics).Info("Providing metrics")
response.Metrics = v1beta1ExtMetrics
return &response, nil
}
// NewGrpcServer creates a new instance of GrpcServer
func NewGrpcServer(scaleHandler *scaling.ScaleHandler, address string) GrpcServer {
// nosemgrep: go.grpc.security.grpc-server-insecure-connection.grpc-server-insecure-connection
gsrv := grpc.NewServer()
srv := GrpcServer{
server: gsrv,
address: address,
scalerHandler: scaleHandler,
}
api.RegisterMetricsServiceServer(gsrv, &srv)
return srv
}
func (s *GrpcServer) startServer() error {
lis, err := net.Listen("tcp", s.address)
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
if err := s.server.Serve(lis); err != nil {
return fmt.Errorf("failed to serve: %w", err)
}
return nil
}
// Start starts a new gRPC Metrics Service, this implements Runnable interface
// of controller-runtime Manager, so we can use mgr.Add() to start this component.
func (s *GrpcServer) Start(ctx context.Context) error {
errChan := make(chan error)
go func() {
log.Info("Starting Metrics Service gRPC Server", "address", s.address)
if err := s.startServer(); err != nil {
err := fmt.Errorf("unable to start Metrics Service gRPC server on address %s, error: %w", s.address, err)
log.Error(err, "error starting Metrics Service gRPC server")
errChan <- err
}
}()
select {
case err := <-errChan:
return err
case <-ctx.Done():
return nil
}
}
// NeedLeaderElection is needed to implement LeaderElectionRunnable interface
// of controller-runtime. This assures that the component is started/stoped
// when this particular instance is selected/deselected as a leader.
func (s *GrpcServer) NeedLeaderElection() bool {
return true
}