Skip to content

Commit 22c2d42

Browse files
authored
fix rules panic (#864)
* persist backends from dry run to real run * relocate pprof routes to own pkg --------- Signed-off-by: James Ranson <james@ranson.org>
1 parent f777fcc commit 22c2d42

File tree

8 files changed

+196
-166
lines changed

8 files changed

+196
-166
lines changed

pkg/config/validate/validate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package validate
1818

1919
import (
20+
"github.com/trickstercache/trickster/v2/pkg/backends"
2021
"github.com/trickstercache/trickster/v2/pkg/backends/alb"
2122
"github.com/trickstercache/trickster/v2/pkg/backends/rule"
2223
"github.com/trickstercache/trickster/v2/pkg/cache"
@@ -143,7 +144,7 @@ func Backends(c *config.Config) error {
143144
return c.Backends.Validate()
144145
}
145146

146-
func RoutesRulesAndPools(c *config.Config) error {
147+
func RoutesRulesAndPools(c *config.Config, clients backends.Backends) error {
147148
var caches = make(cache.Lookup)
148149
for k := range c.Caches {
149150
caches[k] = nil
@@ -155,7 +156,7 @@ func RoutesRulesAndPools(c *config.Config) error {
155156
if err != nil {
156157
return err
157158
}
158-
clients, err := routing.RegisterProxyRoutes(c, r, mr, caches, tracers, true)
159+
err = routing.RegisterProxyRoutes(c, clients, r, mr, caches, tracers, true)
159160
if err != nil {
160161
return err
161162
}

pkg/daemon/daemon.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/trickstercache/trickster/v2/pkg/appinfo"
2828
"github.com/trickstercache/trickster/v2/pkg/appinfo/usage"
29+
"github.com/trickstercache/trickster/v2/pkg/backends"
2930
"github.com/trickstercache/trickster/v2/pkg/config/reload"
3031
"github.com/trickstercache/trickster/v2/pkg/config/validate"
3132
"github.com/trickstercache/trickster/v2/pkg/daemon/instance"
@@ -79,8 +80,9 @@ func Start() error {
7980
return err
8081
}
8182

83+
clients := make(backends.Backends, len(conf.Backends))
8284
// these can't be done until the config is processed
83-
err = validate.RoutesRulesAndPools(conf)
85+
err = validate.RoutesRulesAndPools(conf, clients)
8486
if err != nil {
8587
return err
8688
}
@@ -90,7 +92,7 @@ func Start() error {
9092
return Hup(si, source)
9193
}
9294
// Serve with Config
93-
err = setup.ApplyConfig(si, conf, hupFunc, func() { os.Exit(1) })
95+
err = setup.ApplyConfig(si, conf, clients, hupFunc, func() { os.Exit(1) })
9496
if err != nil {
9597
return err
9698
}
@@ -118,15 +120,16 @@ func Hup(si *instance.ServerInstance, source string) (bool, error) {
118120
if err != nil {
119121
return false, err
120122
}
123+
clients := make(backends.Backends, len(conf.Backends))
121124
// these can't be done until the config is processed
122-
err = validate.RoutesRulesAndPools(conf)
125+
err = validate.RoutesRulesAndPools(conf, clients)
123126
if err != nil {
124127
return false, err
125128
}
126129
var hupFunc reload.Reloader = func(source string) (bool, error) {
127130
return Hup(si, source)
128131
}
129-
err = setup.ApplyConfig(si, conf, hupFunc, nil)
132+
err = setup.ApplyConfig(si, conf, clients, hupFunc, nil)
130133
if err != nil {
131134
logger.Warn(reload.ConfigNotReloadedText,
132135
logging.Pairs{"error": err.Error(), "source": source})

pkg/daemon/setup/listeners.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import (
2626
"github.com/trickstercache/trickster/v2/pkg/observability/logging"
2727
"github.com/trickstercache/trickster/v2/pkg/observability/logging/logger"
2828
"github.com/trickstercache/trickster/v2/pkg/observability/metrics"
29+
"github.com/trickstercache/trickster/v2/pkg/observability/pprof"
2930
"github.com/trickstercache/trickster/v2/pkg/observability/tracing"
3031
ch "github.com/trickstercache/trickster/v2/pkg/proxy/handlers/trickster/config"
3132
ph "github.com/trickstercache/trickster/v2/pkg/proxy/handlers/trickster/purge"
3233
"github.com/trickstercache/trickster/v2/pkg/proxy/listener"
33-
ttls "github.com/trickstercache/trickster/v2/pkg/proxy/tls"
3434
"github.com/trickstercache/trickster/v2/pkg/proxy/router"
3535
"github.com/trickstercache/trickster/v2/pkg/proxy/router/lm"
36-
"github.com/trickstercache/trickster/v2/pkg/routing"
36+
ttls "github.com/trickstercache/trickster/v2/pkg/proxy/tls"
3737
)
3838

3939
var lg = listener.NewGroup()
@@ -140,7 +140,7 @@ func applyListenerConfigs(conf, oldConf *config.Config,
140140
metricsRouter.RegisterRoute(conf.MgmtConfig.ConfigHandlerPath, nil, nil,
141141
false, http.HandlerFunc(ch.HandlerFunc(conf)))
142142
if conf.MgmtConfig.PprofServer == "both" || conf.MgmtConfig.PprofServer == "metrics" {
143-
routing.RegisterPprofRoutes("metrics", metricsRouter)
143+
pprof.RegisterRoutes("metrics", metricsRouter)
144144
}
145145
go lg.StartListener("metricsListener",
146146
conf.Metrics.ListenAddress, conf.Metrics.ListenPort,
@@ -166,7 +166,7 @@ func applyListenerConfigs(conf, oldConf *config.Config,
166166
mr.RegisterRoute(conf.MgmtConfig.PurgeByPathHandlerPath, nil, nil,
167167
true, http.HandlerFunc(ph.PathHandler(conf.MgmtConfig.PurgeByPathHandlerPath, &o)))
168168
if conf.MgmtConfig.PprofServer == "both" || conf.MgmtConfig.PprofServer == "mgmt" {
169-
routing.RegisterPprofRoutes("mgmt", mr)
169+
pprof.RegisterRoutes("mgmt", mr)
170170
}
171171
go lg.StartListener("mgmtListener",
172172
conf.MgmtConfig.ListenAddress, conf.MgmtConfig.ListenPort,

pkg/daemon/setup/setup.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/trickstercache/trickster/v2/pkg/appinfo"
2828
"github.com/trickstercache/trickster/v2/pkg/appinfo/usage"
29+
"github.com/trickstercache/trickster/v2/pkg/backends"
2930
"github.com/trickstercache/trickster/v2/pkg/backends/alb"
3031
"github.com/trickstercache/trickster/v2/pkg/cache"
3132
"github.com/trickstercache/trickster/v2/pkg/cache/index"
@@ -85,7 +86,7 @@ func LoadAndValidate() (*config.Config, error) {
8586
}
8687

8788
func ApplyConfig(si *instance.ServerInstance, newConf *config.Config,
88-
hupFunc dr.Reloader, errorFunc func()) error {
89+
clients backends.Backends, hupFunc dr.Reloader, errorFunc func()) error {
8990
if si == nil || newConf == nil {
9091
return nil
9192
}
@@ -125,7 +126,7 @@ func ApplyConfig(si *instance.ServerInstance, newConf *config.Config,
125126

126127
var caches = applyCachingConfig(si, newConf)
127128
rh := reload.HandlerFunc(hupFunc)
128-
backends, err := routing.RegisterProxyRoutes(newConf, r, mr, caches, tracers, false)
129+
err = routing.RegisterProxyRoutes(newConf, clients, r, mr, caches, tracers, false)
129130
if err != nil {
130131
handleStartupIssue("route registration failed",
131132
logging.Pairs{"detail": err.Error()}, errorFunc)
@@ -137,28 +138,28 @@ func ApplyConfig(si *instance.ServerInstance, newConf *config.Config,
137138
}
138139
r.RegisterRoute(newConf.MgmtConfig.PurgeByKeyHandlerPath, nil,
139140
[]string{http.MethodDelete}, true,
140-
http.HandlerFunc(ph.KeyHandler(newConf.MgmtConfig.PurgeByKeyHandlerPath, backends)))
141+
http.HandlerFunc(ph.KeyHandler(newConf.MgmtConfig.PurgeByKeyHandlerPath, clients)))
141142

142143
if si.Backends != nil {
143144
alb.StopPools(si.Backends)
144145
}
145146
if si.HealthChecker != nil {
146147
si.HealthChecker.Shutdown()
147148
}
148-
si.HealthChecker, err = backends.StartHealthChecks()
149+
si.HealthChecker, err = clients.StartHealthChecks()
149150
if err != nil {
150151
return err
151152
}
152-
alb.StartALBPools(backends, si.HealthChecker.Statuses())
153-
routing.RegisterDefaultBackendRoutes(r, newConf, backends, tracers)
153+
alb.StartALBPools(clients, si.HealthChecker.Statuses())
154+
routing.RegisterDefaultBackendRoutes(r, newConf, clients, tracers)
154155
routing.RegisterHealthHandler(mr, newConf.MgmtConfig.HealthHandlerPath, si.HealthChecker)
155-
applyListenerConfigs(newConf, si.Config, r, rh, mr, tracers, backends, errorFunc)
156+
applyListenerConfigs(newConf, si.Config, r, rh, mr, tracers, clients, errorFunc)
156157

157158
metrics.LastReloadSuccessfulTimestamp.Set(float64(time.Now().Unix()))
158159
metrics.LastReloadSuccessful.Set(1)
159160
si.Config = newConf
160161
si.Caches = caches
161-
si.Backends = backends
162+
si.Backends = clients
162163
return nil
163164
}
164165

pkg/observability/pprof/pprof.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2018 The Trickster Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pprof
18+
19+
import (
20+
"net/http"
21+
"net/http/pprof"
22+
23+
"github.com/trickstercache/trickster/v2/pkg/observability/logging"
24+
"github.com/trickstercache/trickster/v2/pkg/observability/logging/logger"
25+
"github.com/trickstercache/trickster/v2/pkg/proxy/router"
26+
)
27+
28+
// RegisterRoutes will register the Pprof Debugging endpoints to the provided router
29+
func RegisterRoutes(routerName string, r router.Router) {
30+
logger.Info("registering pprof /debug routes", logging.Pairs{"routerName": routerName})
31+
r.RegisterRoute("/debug/pprof/", nil, nil,
32+
false, http.HandlerFunc(pprof.Index))
33+
r.RegisterRoute("/debug/pprof/cmdline", nil, nil,
34+
false, http.HandlerFunc(pprof.Cmdline))
35+
r.RegisterRoute("/debug/pprof/profile", nil, nil,
36+
false, http.HandlerFunc(pprof.Profile))
37+
r.RegisterRoute("/debug/pprof/symbol", nil, nil,
38+
false, http.HandlerFunc(pprof.Symbol))
39+
r.RegisterRoute("/debug/pprof/trace", nil, nil,
40+
false, http.HandlerFunc(pprof.Trace))
41+
}

pkg/observability/pprof/pprof_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2018 The Trickster Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pprof
18+
19+
import (
20+
"net/http"
21+
"testing"
22+
23+
"github.com/trickstercache/trickster/v2/pkg/observability/logging"
24+
"github.com/trickstercache/trickster/v2/pkg/observability/logging/level"
25+
"github.com/trickstercache/trickster/v2/pkg/observability/logging/logger"
26+
"github.com/trickstercache/trickster/v2/pkg/proxy/router/lm"
27+
)
28+
29+
func TestRegisterRoutes(t *testing.T) {
30+
logger.SetLogger(logging.ConsoleLogger(level.Info))
31+
router := lm.NewRouter()
32+
RegisterRoutes("test", router)
33+
r, _ := http.NewRequest("GET", "http://0/debug/pprof", nil)
34+
h := router.Handler(r)
35+
if h == nil {
36+
t.Error("expected non-nil handler")
37+
}
38+
}

0 commit comments

Comments
 (0)