Skip to content

Commit dc00a9e

Browse files
authored
feat: add adc debug server (#270)
1 parent 5bf9ab9 commit dc00a9e

File tree

12 files changed

+451
-5
lines changed

12 files changed

+451
-5
lines changed

config/samples/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ leader_election:
1919
metrics_addr: ":8080" # The address the metrics endpoint binds to.
2020
# The default value is ":8080".
2121

22+
server_addr: "127.0.0.1:9092" # Available endpoints: /debug can be used to debug in-memory state of translated adc configs to be synced with data plane.
23+
2224
enable_http2: false # Whether to enable HTTP/2 for the server.
2325
# The default value is false.
2426

docs/en/latest/reference/configuration-file.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ leader_election:
5151
metrics_addr: ":8080" # The address the metrics endpoint binds to.
5252
# The default value is ":8080".
5353

54+
server_addr: "127.0.0.1:9092" # Available endpoints: /debug can be used to debug in-memory state of translated adc configs to be synced with data plane.
55+
5456
enable_http2: false # Whether to enable HTTP/2 for the server.
5557
# The default value is false.
5658

internal/adc/client/client.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ type Client struct {
4646
executor ADCExecutor
4747
BackendMode string
4848

49-
ConfigManager *common.ConfigManager[types.NamespacedNameKind, adctypes.Config]
49+
ConfigManager *common.ConfigManager[types.NamespacedNameKind, adctypes.Config]
50+
ADCDebugProvider *common.ADCDebugProvider
5051
}
5152

5253
func New(mode string, timeout time.Duration) (*Client, error) {
@@ -55,12 +56,15 @@ func New(mode string, timeout time.Duration) (*Client, error) {
5556
serverURL = defaultHTTPADCExecutorAddr
5657
}
5758

59+
store := cache.NewStore()
60+
configManager := common.NewConfigManager[types.NamespacedNameKind, adctypes.Config]()
5861
log.Infow("using HTTP ADC Executor", zap.String("server_url", serverURL))
5962
return &Client{
60-
Store: cache.NewStore(),
61-
executor: NewHTTPADCExecutor(serverURL, timeout),
62-
BackendMode: mode,
63-
ConfigManager: common.NewConfigManager[types.NamespacedNameKind, adctypes.Config](),
63+
Store: store,
64+
executor: NewHTTPADCExecutor(serverURL, timeout),
65+
BackendMode: mode,
66+
ConfigManager: configManager,
67+
ADCDebugProvider: common.NewADCDebugProvider(store, configManager),
6468
}, nil
6569
}
6670

internal/controller/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func NewDefaultConfig() *Config {
4848
LeaderElectionID: DefaultLeaderElectionID,
4949
ProbeAddr: DefaultProbeAddr,
5050
MetricsAddr: DefaultMetricsAddr,
51+
ServerAddr: DefaultServerAddr,
5152
LeaderElection: NewLeaderElection(),
5253
ExecADCTimeout: types.TimeDuration{Duration: 15 * time.Second},
5354
ProviderConfig: ProviderConfig{

internal/controller/config/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type Config struct {
6060
ControllerName string `json:"controller_name" yaml:"controller_name"`
6161
LeaderElectionID string `json:"leader_election_id" yaml:"leader_election_id"`
6262
MetricsAddr string `json:"metrics_addr" yaml:"metrics_addr"`
63+
ServerAddr string `json:"server_addr" yaml:"server_addr"`
64+
EnableServer bool `json:"enable_server" yaml:"enable_server"`
6365
EnableHTTP2 bool `json:"enable_http2" yaml:"enable_http2"`
6466
ProbeAddr string `json:"probe_addr" yaml:"probe_addr"`
6567
SecureMetrics bool `json:"secure_metrics" yaml:"secure_metrics"`

internal/manager/run.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/apache/apisix-ingress-controller/internal/controller/config"
4444
"github.com/apache/apisix-ingress-controller/internal/controller/status"
4545
"github.com/apache/apisix-ingress-controller/internal/manager/readiness"
46+
"github.com/apache/apisix-ingress-controller/internal/manager/server"
4647
"github.com/apache/apisix-ingress-controller/internal/provider"
4748
_ "github.com/apache/apisix-ingress-controller/internal/provider/init"
4849
_ "github.com/apache/apisix-ingress-controller/pkg/metrics"
@@ -189,6 +190,14 @@ func Run(ctx context.Context, logger logr.Logger) error {
189190
return err
190191
}
191192

193+
if cfg.EnableServer {
194+
srv := server.NewServer(config.ControllerConfig.ServerAddr)
195+
srv.Register("/debug", provider)
196+
if err := mgr.Add(srv); err != nil {
197+
setupLog.Error(err, "unable to add debug server to manager")
198+
return err
199+
}
200+
}
192201
if err := mgr.Add(provider); err != nil {
193202
setupLog.Error(err, "unable to add provider to manager")
194203
return err

internal/manager/server/server.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package server
19+
20+
import (
21+
"context"
22+
"net/http"
23+
"time"
24+
25+
"github.com/apache/apisix-ingress-controller/internal/provider"
26+
)
27+
28+
type Server struct {
29+
server *http.Server
30+
mux *http.ServeMux
31+
}
32+
33+
func (s *Server) Start(ctx context.Context) error {
34+
stop := make(chan error, 1)
35+
go func() {
36+
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
37+
stop <- err
38+
}
39+
close(stop)
40+
}()
41+
select {
42+
case <-ctx.Done():
43+
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
44+
defer cancel()
45+
return s.server.Shutdown(shutdownCtx)
46+
case err := <-stop:
47+
return err
48+
}
49+
}
50+
51+
func (s *Server) Register(pathPrefix string, registrant provider.RegisterHandler) {
52+
subMux := http.NewServeMux()
53+
registrant.Register(pathPrefix, subMux)
54+
s.mux.Handle(pathPrefix+"/", http.StripPrefix(pathPrefix, subMux))
55+
s.mux.HandleFunc(pathPrefix, func(w http.ResponseWriter, r *http.Request) {
56+
http.Redirect(w, r, pathPrefix+"/", http.StatusPermanentRedirect)
57+
})
58+
}
59+
60+
func NewServer(addr string) *Server {
61+
mux := http.NewServeMux()
62+
return &Server{
63+
server: &http.Server{
64+
Addr: addr,
65+
Handler: mux,
66+
},
67+
mux: mux,
68+
}
69+
}

internal/provider/api7ee/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package api7ee
1919

2020
import (
2121
"context"
22+
"net/http"
2223
"sync/atomic"
2324
"time"
2425

@@ -84,6 +85,10 @@ func New(updater status.Updater, readier readiness.ReadinessManager, opts ...pro
8485
}, nil
8586
}
8687

88+
func (d *api7eeProvider) Register(pathPrefix string, mux *http.ServeMux) {
89+
d.client.ADCDebugProvider.SetupHandler(pathPrefix, mux)
90+
}
91+
8792
func (d *api7eeProvider) Update(ctx context.Context, tctx *provider.TranslateContext, obj client.Object) error {
8893
log.Debugw("updating object", zap.Any("object", obj))
8994
var (

internal/provider/apisix/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package apisix
1919

2020
import (
2121
"context"
22+
"net/http"
2223
"sync"
2324
"time"
2425

@@ -91,6 +92,10 @@ func New(updater status.Updater, readier readiness.ReadinessManager, opts ...pro
9192
}, nil
9293
}
9394

95+
func (d *apisixProvider) Register(pathPrefix string, mux *http.ServeMux) {
96+
d.client.ADCDebugProvider.SetupHandler(pathPrefix, mux)
97+
}
98+
9499
func (d *apisixProvider) Update(ctx context.Context, tctx *provider.TranslateContext, obj client.Object) error {
95100
log.Debugw("updating object", zap.Any("object", obj))
96101
var (

0 commit comments

Comments
 (0)