Skip to content

Commit

Permalink
api: merge debug and admin (envoyproxy#1978)
Browse files Browse the repository at this point in the history
Signed-off-by: bitliu <[email protected]>
  • Loading branch information
Xunzhuo authored Oct 17, 2023
1 parent 0ffd485 commit fa55804
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 236 deletions.
45 changes: 3 additions & 42 deletions api/v1alpha1/envoygateway_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func DefaultEnvoyGateway() *EnvoyGateway {
Provider: DefaultEnvoyGatewayProvider(),
Logging: DefaultEnvoyGatewayLogging(),
Admin: DefaultEnvoyGatewayAdmin(),
Debug: DefaultEnvoyGatewayDebug(),
},
}
}
Expand All @@ -48,9 +47,6 @@ func (e *EnvoyGateway) SetEnvoyGatewayDefaults() {
if e.Admin == nil {
e.Admin = DefaultEnvoyGatewayAdmin()
}
if e.Debug == nil {
e.Debug = DefaultEnvoyGatewayDebug()
}
}

// GetEnvoyGatewayAdmin returns the EnvoyGatewayAdmin of EnvoyGateway or a default EnvoyGatewayAdmin if unspecified.
Expand All @@ -76,26 +72,6 @@ func (e *EnvoyGateway) GetEnvoyGatewayAdminAddress() string {
return ""
}

// GetEnvoyGatewayDebug returns the EnvoyGatewayDebug of EnvoyGateway or a default EnvoyGatewayDebug if unspecified.
func (e *EnvoyGateway) GetEnvoyGatewayDebug() *EnvoyGatewayDebug {
if e.Debug != nil {
return e.Debug
}
e.Debug = DefaultEnvoyGatewayDebug()

return e.Debug
}

// GetEnvoyGatewayDebugAddress returns the EnvoyGateway Debug Address.
func (e *EnvoyGateway) GetEnvoyGatewayDebugAddress() string {
address := e.GetEnvoyGatewayDebug().Address
if address != nil {
return fmt.Sprintf("%s:%d", address.Host, address.Port)
}

return ""
}

// DefaultGateway returns a new Gateway with default configuration parameters.
func DefaultGateway() *Gateway {
return &Gateway{
Expand Down Expand Up @@ -139,7 +115,9 @@ func DefaultEnvoyGatewayKubeProvider() *EnvoyGatewayKubernetesProvider {
// DefaultEnvoyGatewayAdmin returns a new EnvoyGatewayAdmin with default configuration parameters.
func DefaultEnvoyGatewayAdmin() *EnvoyGatewayAdmin {
return &EnvoyGatewayAdmin{
Address: DefaultEnvoyGatewayAdminAddress(),
Address: DefaultEnvoyGatewayAdminAddress(),
EnableDumpConfig: false,
EnablePprof: false,
}
}

Expand All @@ -151,23 +129,6 @@ func DefaultEnvoyGatewayAdminAddress() *EnvoyGatewayAdminAddress {
}
}

// DefaultEnvoyGatewayDebug returns a new EnvoyGatewayDebug with default configuration parameters.
func DefaultEnvoyGatewayDebug() *EnvoyGatewayDebug {
return &EnvoyGatewayDebug{
EnableDumpConfig: false,
EnablePprof: false,
Address: DefaultEnvoyGatewayDebugAddress(),
}
}

// DefaultEnvoyGatewayDebugAddress returns a new EnvoyGatewayDebugAddress with default configuration parameters.
func DefaultEnvoyGatewayDebugAddress() *EnvoyGatewayDebugAddress {
return &EnvoyGatewayDebugAddress{
Port: GatewayDebugPort,
Host: GatewayDebugHost,
}
}

// GetEnvoyGatewayKubeProvider returns the EnvoyGatewayKubernetesProvider of Provider or
// a default EnvoyGatewayKubernetesProvider if unspecified. If EnvoyGatewayProvider is not of
// type "Kubernetes", a nil EnvoyGatewayKubernetesProvider is returned.
Expand Down
45 changes: 4 additions & 41 deletions api/v1alpha1/envoygateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const (
GatewayAdminPort = 19000
// GatewayAdminHost is the host of envoy gateway admin server.
GatewayAdminHost = "127.0.0.1"
// GatewayDebugPort is the port which envoy gateway debug server is listening on.
GatewayDebugPort = 19010
// GatewayDebugHost is the host of envoy gateway debug server.
GatewayDebugHost = "127.0.0.1"
)

// +kubebuilder:object:root=true
Expand Down Expand Up @@ -63,12 +59,6 @@ type EnvoyGatewaySpec struct {
// +optional
Admin *EnvoyGatewayAdmin `json:"admin,omitempty"`

// Debug defines the desired debug related abilities.
// If unspecified, the debug will not be running, including pprof, dump config etc.
//
// +optional
Debug *EnvoyGatewayDebug `json:"debug,omitempty"`

// RateLimit defines the configuration associated with the Rate Limit service
// deployed by Envoy Gateway required to implement the Global Rate limiting
// functionality. The specific rate limit service used here is the reference
Expand Down Expand Up @@ -435,26 +425,14 @@ type EnvoyGatewayAdmin struct {
//
// +optional
Address *EnvoyGatewayAdminAddress `json:"address,omitempty"`
}

// EnvoyGatewayDebug defines the Envoy Gateway Debug configuration.
type EnvoyGatewayDebug struct {

// EnableDumpConfig defines if enables dump the Envoy Gateway config in logs.
// EnableDumpConfig defines if enable dump config in Envoy Gateway logs.
//
// +optional
EnableDumpConfig bool `json:"enableDumpConfig,omitempty"`

// EnablePprof defines if enables pprof in Envoy Gateway debug server.
//
// +optional
EnablePprof bool `json:"enablePprof,omitempty"`

// Address defines the address of Envoy Gateway Debug Server.
// Pprof will use the debug address, if you set it to non-nil.
EnableDumpConfig bool
// EnablePprof defines if enable pprof in Envoy Gateway Admin Server.
//
// +optional
Address *EnvoyGatewayDebugAddress `json:"address,omitempty"`
EnablePprof bool
}

// EnvoyGatewayAdminAddress defines the Envoy Gateway Admin Address configuration.
Expand All @@ -472,21 +450,6 @@ type EnvoyGatewayAdminAddress struct {
Host string `json:"host,omitempty"`
}

// EnvoyGatewayDebugAddress defines the Envoy Gateway Debug Address configuration.
type EnvoyGatewayDebugAddress struct {
// Port defines the port the debug server is exposed on.
//
// +optional
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=19010
Port int `json:"port,omitempty"`
// Host defines the debug server hostname.
//
// +optional
// +kubebuilder:default="127.0.0.1"
Host string `json:"host,omitempty"`
}

func init() {
SchemeBuilder.Register(&EnvoyGateway{})
}
55 changes: 17 additions & 38 deletions api/v1alpha1/validation/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,24 @@ func TestEnvoyGatewayAdmin(t *testing.T) {
assert.True(t, egAdmin != nil)
assert.True(t, egAdmin.Address.Port == egv1a1.GatewayAdminPort)
assert.True(t, egAdmin.Address.Host == egv1a1.GatewayAdminHost)
assert.True(t, egAdmin.EnableDumpConfig == false)
assert.True(t, egAdmin.EnablePprof == false)

// override the admin config
// values should be updated
eg.Admin.Address = nil
assert.True(t, eg.GetEnvoyGatewayAdmin().Address.Port == egv1a1.GatewayAdminPort)
assert.True(t, eg.GetEnvoyGatewayAdmin().Address.Host == egv1a1.GatewayAdminHost)
eg.Admin = &egv1a1.EnvoyGatewayAdmin{
Address: &egv1a1.EnvoyGatewayAdminAddress{
Host: "0.0.0.0",
Port: 19010,
},
EnableDumpConfig: true,
EnablePprof: true,
}

assert.True(t, eg.GetEnvoyGatewayAdmin().Address.Port == 19010)
assert.True(t, eg.GetEnvoyGatewayAdmin().Address.Host == "0.0.0.0")
assert.True(t, eg.GetEnvoyGatewayAdmin().EnableDumpConfig == true)
assert.True(t, eg.GetEnvoyGatewayAdmin().EnablePprof == true)

// set eg defaults when admin is nil
// the admin should not be nil
Expand All @@ -551,41 +563,8 @@ func TestEnvoyGatewayAdmin(t *testing.T) {
assert.True(t, eg.Admin != nil)
assert.True(t, eg.Admin.Address.Port == egv1a1.GatewayAdminPort)
assert.True(t, eg.Admin.Address.Host == egv1a1.GatewayAdminHost)
}

func TestEnvoyGatewayDebug(t *testing.T) {
// default envoygateway config debug should not be nil
eg := egv1a1.DefaultEnvoyGateway()
assert.True(t, eg.Debug != nil)

// get default debug config from envoygateway
// values should be set in default
egDebug := eg.GetEnvoyGatewayDebug()
assert.True(t, egDebug != nil)
assert.True(t, eg.Debug.Address.Host == egv1a1.GatewayDebugHost)
assert.True(t, eg.Debug.Address.Port == egv1a1.GatewayDebugPort)
assert.True(t, egDebug.EnableDumpConfig == false)
assert.True(t, egDebug.EnablePprof == false)

// override the debug config
// values should be updated
eg.Debug.Address = &egv1a1.EnvoyGatewayDebugAddress{
Host: "0.0.0.0",
Port: 8899,
}
eg.Debug.EnableDumpConfig = true
eg.Debug.EnablePprof = true
assert.True(t, eg.GetEnvoyGatewayDebug().Address.Port == 8899)
assert.True(t, eg.GetEnvoyGatewayDebug().Address.Host == "0.0.0.0")
assert.True(t, egDebug.EnableDumpConfig == true)
assert.True(t, egDebug.EnablePprof == true)

// set eg defaults when debug is nil
// the debug should not be nil
eg.Debug = nil
eg.SetEnvoyGatewayDefaults()
assert.True(t, eg.Debug != nil)
assert.True(t, eg.Debug.Address != nil)
assert.True(t, eg.Admin.EnableDumpConfig == false)
assert.True(t, eg.Admin.EnablePprof == false)
}

func TestGetEnvoyProxyDefaultComponentLevel(t *testing.T) {
Expand Down
40 changes: 0 additions & 40 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions internal/debug/server.go → internal/admin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package debug
package admin

import (
"net/http"
Expand All @@ -18,11 +18,11 @@ import (
)

var (
debugLogger = logging.DefaultLogger(v1alpha1.LogLevelInfo).WithName("debug")
debugLogger = logging.DefaultLogger(v1alpha1.LogLevelInfo).WithName("admin")
)

func Init(cfg *config.Server) error {
if cfg.EnvoyGateway.GetEnvoyGatewayDebug().EnableDumpConfig {
if cfg.EnvoyGateway.GetEnvoyGatewayAdmin().EnableDumpConfig {
spewConfig := spew.NewDefaultConfig()
spewConfig.DisableMethods = true
spewConfig.Dump(cfg)
Expand All @@ -33,10 +33,10 @@ func Init(cfg *config.Server) error {

func start(cfg *config.Server) error {
handlers := http.NewServeMux()
address := cfg.EnvoyGateway.GetEnvoyGatewayDebugAddress()
enablePprof := cfg.EnvoyGateway.GetEnvoyGatewayDebug().EnablePprof
address := cfg.EnvoyGateway.GetEnvoyGatewayAdminAddress()
enablePprof := cfg.EnvoyGateway.GetEnvoyGatewayAdmin().EnablePprof

debugLogger.Info("starting debug server", "address", address, "enablePprof", enablePprof)
debugLogger.Info("starting admin server", "address", address, "enablePprof", enablePprof)

if enablePprof {
// Serve pprof endpoints to aid in live debugging.
Expand All @@ -56,10 +56,10 @@ func start(cfg *config.Server) error {
IdleTimeout: 15 * time.Second,
}

// Listen And Serve Debug Server.
// Listen And Serve Admin Server.
go func() {
if err := debugServer.ListenAndServe(); err != nil {
cfg.Logger.Error(err, "start debug server failed")
cfg.Logger.Error(err, "start admin server failed")
}
}()

Expand Down
15 changes: 3 additions & 12 deletions internal/debug/server_test.go → internal/admin/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package debug
package admin

import (
"testing"
Expand All @@ -14,19 +14,10 @@ import (
"github.com/envoyproxy/gateway/internal/envoygateway/config"
)

func TestInitDebugServer(t *testing.T) {
func TestInitAdminServer(t *testing.T) {
svrConfig := &config.Server{
EnvoyGateway: &v1alpha1.EnvoyGateway{
EnvoyGatewaySpec: v1alpha1.EnvoyGatewaySpec{
Debug: &v1alpha1.EnvoyGatewayDebug{
EnableDumpConfig: true,
EnablePprof: true,
Address: &v1alpha1.EnvoyGatewayDebugAddress{
Host: "127.0.0.1",
Port: 19010,
},
},
},
EnvoyGatewaySpec: v1alpha1.EnvoyGatewaySpec{},
},
}
err := Init(svrConfig)
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/envoyproxy/gateway/internal/debug"
"github.com/envoyproxy/gateway/internal/admin"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
extensionregistry "github.com/envoyproxy/gateway/internal/extension/registry"
gatewayapirunner "github.com/envoyproxy/gateway/internal/gatewayapi/runner"
Expand Down Expand Up @@ -50,8 +50,8 @@ func server() error {
return err
}

// Init eg debug servers.
if err := debug.Init(cfg); err != nil {
// Init eg admin servers.
if err := admin.Init(cfg); err != nil {
return err
}
// init eg runners.
Expand Down
Loading

0 comments on commit fa55804

Please sign in to comment.