forked from cloudprober/cloudprober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudprober.go
325 lines (278 loc) · 9.35 KB
/
cloudprober.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2017-2019 The Cloudprober 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 cloudprober provides a prober for running a set of probes.
Cloudprober takes in a config proto which dictates what probes should be created
with what configuration, and manages the asynchronous fan-in/fan-out of the
metrics data from these probes.
*/
package cloudprober
import (
"context"
"crypto/tls"
"fmt"
"log/slog"
"net"
"net/http"
"net/http/pprof"
"os"
"strconv"
"strings"
"sync"
"github.com/cloudprober/cloudprober/config"
configpb "github.com/cloudprober/cloudprober/config/proto"
"github.com/cloudprober/cloudprober/config/runconfig"
"github.com/cloudprober/cloudprober/internal/servers"
"github.com/cloudprober/cloudprober/internal/sysvars"
"github.com/cloudprober/cloudprober/internal/tlsconfig"
"github.com/cloudprober/cloudprober/logger"
"github.com/cloudprober/cloudprober/prober"
"github.com/cloudprober/cloudprober/probes"
"github.com/cloudprober/cloudprober/surfacers"
"github.com/cloudprober/cloudprober/web"
"google.golang.org/grpc"
"google.golang.org/grpc/channelz/service"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
)
const (
sysvarsModuleName = "sysvars"
)
// Constants defining the default server host and port.
const (
DefaultServerHost = ""
DefaultServerPort = 9313
ServerHostEnvVar = "CLOUDPROBER_HOST"
ServerPortEnvVar = "CLOUDPROBER_PORT"
DisableHTTPDebugVar = "CLOUDPROBER_DISABLE_HTTP_PPROF"
)
// Global prober.Prober instance protected by a mutex.
var cloudProber struct {
prober *prober.Prober
defaultServerLn net.Listener
defaultGRPCLn net.Listener
configSource config.ConfigSource
config *configpb.ProberConfig
cancelInitCtx context.CancelFunc
sync.RWMutex
}
func getServerHost(c *configpb.ProberConfig) string {
serverHost := c.GetHost()
if serverHost == "" {
serverHost = DefaultServerHost
// If ServerHostEnvVar is defined, it will override the default
// server host.
if host := os.Getenv(ServerHostEnvVar); host != "" {
serverHost = host
}
}
return serverHost
}
func getDefaultServerPort(c *configpb.ProberConfig, l *logger.Logger) (int, error) {
if c.GetPort() != 0 {
return int(c.GetPort()), nil
}
// If ServerPortEnvVar is defined, it will override the default
// server port.
portStr := os.Getenv(ServerPortEnvVar)
if portStr == "" {
return DefaultServerPort, nil
}
if strings.HasPrefix(portStr, "tcp://") {
l.Warningf("%s environment variable likely set by Kubernetes (to %s), ignoring it", ServerPortEnvVar, portStr)
return DefaultServerPort, nil
}
port, err := strconv.ParseInt(portStr, 10, 32)
if err != nil {
return 0, fmt.Errorf("failed to parse default port from the env var: %s=%s", ServerPortEnvVar, portStr)
}
return int(port), nil
}
func initDefaultServer(c *configpb.ProberConfig, l *logger.Logger) (net.Listener, error) {
serverHost := getServerHost(c)
serverPort, err := getDefaultServerPort(c, l)
if err != nil {
return nil, err
}
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", serverHost, serverPort))
if err != nil {
return nil, fmt.Errorf("error while creating listener for default HTTP server: %v", err)
}
return ln, nil
}
func setDebugHandlers(srvMux *http.ServeMux) {
if os.Getenv(DisableHTTPDebugVar) != "" {
return
}
srvMux.HandleFunc("/debug/pprof/", pprof.Index)
srvMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
srvMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
srvMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
srvMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
// InitFromConfig initializes Cloudprober using the provided config.
// Deprecated: This function is kept only for compatibility reasons. It's
// recommended to use Init() or InitWithConfigSource() instead.
func InitFromConfig(configFile string) error {
return InitWithConfigSource(config.ConfigSourceWithFile(configFile))
}
// Init initializes Cloudprober using the default config source.
func Init() error {
return InitWithConfigSource(config.DefaultConfigSource())
}
func InitWithConfigSource(configSrc config.ConfigSource) error {
if err := initWithConfigSource(configSrc); err != nil {
return err
}
return web.InitWithDataFuncs(web.DataFuncs{
GetRawConfig: GetRawConfig,
GetParsedConfig: GetParsedConfig,
GetInfo: GetInfo,
})
}
func initWithConfigSource(configSrc config.ConfigSource) error {
// Return immediately if prober is already initialized.
cloudProber.Lock()
defer cloudProber.Unlock()
if cloudProber.prober != nil {
return nil
}
// Initialize sysvars module
if err := sysvars.Init(logger.NewWithAttrs(slog.String("component", sysvarsModuleName)), nil); err != nil {
return err
}
cfg, err := configSrc.GetConfig()
if err != nil {
return err
}
globalLogger := logger.NewWithAttrs(slog.String("component", "global"))
// Start default HTTP server. It's used for profile handlers and
// prometheus exporter.
ln, err := initDefaultServer(cfg, globalLogger)
if err != nil {
return err
}
srvMux := http.NewServeMux()
setDebugHandlers(srvMux)
runconfig.SetDefaultHTTPServeMux(srvMux)
var grpcLn net.Listener
if cfg.GetGrpcPort() != 0 {
serverHost := getServerHost(cfg)
grpcLn, err = net.Listen("tcp", fmt.Sprintf("%s:%d", serverHost, cfg.GetGrpcPort()))
if err != nil {
return fmt.Errorf("error while creating listener for default gRPC server: %v", err)
}
// Create the default gRPC server now, so that other modules can register
// their services with it in the prober.Init() phase.
var serverOpts []grpc.ServerOption
if cfg.GetGrpcTlsConfig() != nil {
tlsConfig := &tls.Config{}
if err := tlsconfig.UpdateTLSConfig(tlsConfig, cfg.GetGrpcTlsConfig()); err != nil {
return err
}
tlsConfig.ClientCAs = tlsConfig.RootCAs
serverOpts = append(serverOpts, grpc.Creds(credentials.NewTLS(tlsConfig)))
}
s := grpc.NewServer(serverOpts...)
reflection.Register(s)
// register channelz service to the default grpc server port
service.RegisterChannelzServiceToServer(s)
runconfig.SetDefaultGRPCServer(s)
}
pr := &prober.Prober{}
// initCtx is used to clean up in case of partial initialization failures. For
// example, user-configured servers open listeners during initialization and
// if initialization fails at a later stage, say in probers or surfacers,
// pr.Init returns an error and we cancel the initCtx, which makes servers
// close their listeners.
// TODO(manugarg): Plumb init context from cmd/cloudprober.
initCtx, cancelFunc := context.WithCancel(context.TODO())
if err := pr.Init(initCtx, cfg, globalLogger); err != nil {
cancelFunc()
ln.Close()
return err
}
cloudProber.prober = pr
cloudProber.config = cfg
cloudProber.configSource = configSrc
cloudProber.defaultServerLn = ln
cloudProber.defaultGRPCLn = grpcLn
cloudProber.cancelInitCtx = cancelFunc
return nil
}
// Start starts a previously initialized Cloudprober.
func Start(ctx context.Context) {
cloudProber.Lock()
defer cloudProber.Unlock()
// Default servers
srvMux := runconfig.DefaultHTTPServeMux()
httpSrv := &http.Server{Handler: srvMux}
grpcSrv := runconfig.DefaultGRPCServer()
// Set up a goroutine to cleanup if context ends.
go func() {
<-ctx.Done()
httpSrv.Close() // This will close the listener as well.
if grpcSrv != nil {
grpcSrv.Stop()
}
cloudProber.cancelInitCtx()
cloudProber.Lock()
defer cloudProber.Unlock()
cloudProber.defaultServerLn = nil
cloudProber.defaultGRPCLn = nil
cloudProber.config = nil
cloudProber.configSource = nil
cloudProber.prober = nil
}()
go httpSrv.Serve(cloudProber.defaultServerLn)
if grpcSrv != nil && cloudProber.defaultGRPCLn != nil {
go grpcSrv.Serve(cloudProber.defaultGRPCLn)
}
if cloudProber.prober == nil {
panic("Prober is not initialized. Did you call cloudprober.InitFromConfig first?")
}
cloudProber.prober.Start(ctx)
srvMux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
})
}
// GetConfig returns the prober config.
func GetConfig() *configpb.ProberConfig {
cloudProber.RLock()
defer cloudProber.RUnlock()
return cloudProber.config
}
// GetRawConfig returns the prober config in text proto format.
func GetRawConfig() string {
cloudProber.RLock()
defer cloudProber.RUnlock()
return cloudProber.configSource.RawConfig()
}
// GetParsedConfig returns the parsed prober config.
func GetParsedConfig() string {
cloudProber.RLock()
defer cloudProber.RUnlock()
return cloudProber.configSource.ParsedConfig()
}
// GetInfo returns information on all the probes, servers and surfacers.
func GetInfo() (map[string]*probes.ProbeInfo, []*surfacers.SurfacerInfo, []*servers.ServerInfo) {
cloudProber.RLock()
defer cloudProber.RUnlock()
return cloudProber.prober.Probes, cloudProber.prober.Surfacers, cloudProber.prober.Servers
}
func GetProber() *prober.Prober {
cloudProber.RLock()
defer cloudProber.RUnlock()
return cloudProber.prober
}