forked from cloudfoundry/loggregator-agent-release
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyslog_binding_cache.go
More file actions
127 lines (111 loc) · 3.38 KB
/
syslog_binding_cache.go
File metadata and controls
127 lines (111 loc) · 3.38 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
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
package app
import (
"crypto/tls"
"fmt"
"log"
"net/http"
_ "net/http/pprof" //nolint:gosec
"sync"
"time"
metrics "code.cloudfoundry.org/go-metric-registry"
"code.cloudfoundry.org/tlsconfig"
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/binding"
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/cache"
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/ingress/api"
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/plumbing"
"github.com/go-chi/chi/v5"
)
type SyslogBindingCache struct {
config Config
pprofServer *http.Server
server *http.Server
log *log.Logger
metrics Metrics
mu sync.Mutex
}
type Metrics interface {
NewCounter(name, helpText string, options ...metrics.MetricOption) metrics.Counter
NewGauge(name, helpText string, o ...metrics.MetricOption) metrics.Gauge
RegisterDebugMetrics()
}
func NewSyslogBindingCache(config Config, metrics Metrics, log *log.Logger) *SyslogBindingCache {
return &SyslogBindingCache{
config: config,
log: log,
metrics: metrics,
}
}
func (sbc *SyslogBindingCache) Run() {
if sbc.config.MetricsServer.DebugMetrics {
sbc.metrics.RegisterDebugMetrics()
sbc.pprofServer = &http.Server{
Addr: fmt.Sprintf("127.0.0.1:%d", sbc.config.MetricsServer.PprofPort),
Handler: http.DefaultServeMux,
ReadHeaderTimeout: 2 * time.Second,
}
go func() { sbc.log.Println("PPROF SERVER STOPPED " + sbc.pprofServer.ListenAndServe().Error()) }()
}
store := binding.NewStore(sbc.metrics)
aggregateStore := binding.NewAggregateStore(sbc.config.AggregateDrainsFile)
poller := binding.NewPoller(sbc.apiClient(), sbc.config.APIPollingInterval, store, sbc.metrics, sbc.log)
go poller.Poll()
router := chi.NewRouter()
router.Get("/v2/bindings", cache.Handler(store))
router.Get("/v2/aggregate", cache.AggregateHandler(aggregateStore))
sbc.startServer(router)
}
func (sbc *SyslogBindingCache) Stop() {
if sbc.pprofServer != nil {
sbc.pprofServer.Close()
}
sbc.mu.Lock()
defer sbc.mu.Unlock()
if sbc.server != nil {
sbc.server.Close()
}
}
func (sbc *SyslogBindingCache) apiClient() api.Client {
httpClient := plumbing.NewTLSHTTPClient(
sbc.config.APICertFile,
sbc.config.APIKeyFile,
sbc.config.APICAFile,
sbc.config.APICommonName,
sbc.config.APIDisableKeepAlives,
)
return api.Client{
Addr: sbc.config.APIURL,
Client: httpClient,
BatchSize: sbc.config.APIBatchSize,
}
}
func (sbc *SyslogBindingCache) startServer(router chi.Router) {
listenAddr := fmt.Sprintf(":%d", sbc.config.CachePort)
sbc.mu.Lock()
sbc.server = &http.Server{
Addr: listenAddr,
Handler: router,
TLSConfig: sbc.tlsConfig(),
ReadHeaderTimeout: 2 * time.Second,
}
sbc.mu.Unlock()
err := sbc.server.ListenAndServeTLS("", "")
if err != http.ErrServerClosed {
sbc.log.Panicf("error creating listener: %s", err)
}
}
func (sbc *SyslogBindingCache) tlsConfig() *tls.Config {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(sbc.config.CacheCertFile, sbc.config.CacheKeyFile),
).Server(
tlsconfig.WithClientAuthenticationFromFile(sbc.config.CacheCAFile),
)
if err != nil {
sbc.log.Panicf("failed to load server TLS config: %s", err)
}
if len(sbc.config.CipherSuites) > 0 {
opt := plumbing.WithCipherSuites(sbc.config.CipherSuites)
opt(tlsConfig)
}
return tlsConfig
}