Skip to content

Commit

Permalink
feat: separate debug server with admin (envoyproxy#1967)
Browse files Browse the repository at this point in the history
Signed-off-by: bitliu <[email protected]>
  • Loading branch information
Xunzhuo authored Oct 16, 2023
1 parent d4a4c42 commit 8e8b072
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 107 deletions.
54 changes: 53 additions & 1 deletion api/v1alpha1/envoygateway_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package v1alpha1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -21,6 +23,7 @@ func DefaultEnvoyGateway() *EnvoyGateway {
Provider: DefaultEnvoyGatewayProvider(),
Logging: DefaultEnvoyGatewayLogging(),
Admin: DefaultEnvoyGatewayAdmin(),
Debug: DefaultEnvoyGatewayDebug(),
},
}
}
Expand All @@ -45,6 +48,9 @@ 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 @@ -60,6 +66,36 @@ func (e *EnvoyGateway) GetEnvoyGatewayAdmin() *EnvoyGatewayAdmin {
return e.Admin
}

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

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 @@ -103,7 +139,6 @@ func DefaultEnvoyGatewayKubeProvider() *EnvoyGatewayKubernetesProvider {
// DefaultEnvoyGatewayAdmin returns a new EnvoyGatewayAdmin with default configuration parameters.
func DefaultEnvoyGatewayAdmin() *EnvoyGatewayAdmin {
return &EnvoyGatewayAdmin{
Debug: false,
Address: DefaultEnvoyGatewayAdminAddress(),
}
}
Expand All @@ -116,6 +151,23 @@ 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: 43 additions & 2 deletions api/v1alpha1/envoygateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ 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 @@ -51,13 +55,20 @@ type EnvoyGatewaySpec struct {
// +optional
// +kubebuilder:default={default: info}
Logging *EnvoyGatewayLogging `json:"logging,omitempty"`

// Admin defines the desired admin related abilities.
// If unspecified, the Admin is used with default configuration
// parameters.
//
// +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 @@ -424,11 +435,26 @@ 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.
//
// +optional
EnableDumpConfig bool `json:"enableDumpConfig,omitempty"`

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

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

// EnvoyGatewayAdminAddress defines the Envoy Gateway Admin Address configuration.
Expand All @@ -446,6 +472,21 @@ 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{})
}
26 changes: 13 additions & 13 deletions api/v1alpha1/envoyproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,44 +129,44 @@ type ProxyLogging struct {
// and the log level is the value. If unspecified, defaults to "default: warn".
//
// +kubebuilder:default={default: warn}
Level map[LogComponent]LogLevel `json:"level,omitempty"`
Level map[ProxyLogComponent]LogLevel `json:"level,omitempty"`
}

// LogComponent defines a component that supports a configured logging level.
// ProxyLogComponent defines a component that supports a configured logging level.
// +kubebuilder:validation:Enum=system;upstream;http;connection;admin;client;filter;main;router;runtime
type LogComponent string
type ProxyLogComponent string

const (
// LogComponentDefault defines the default logging component.
// See more details: https://www.envoyproxy.io/docs/envoy/latest/operations/cli#cmdoption-l
LogComponentDefault LogComponent = "default"
LogComponentDefault ProxyLogComponent = "default"

// LogComponentUpstream defines the "upstream" logging component.
LogComponentUpstream LogComponent = "upstream"
LogComponentUpstream ProxyLogComponent = "upstream"

// LogComponentHTTP defines the "http" logging component.
LogComponentHTTP LogComponent = "http"
LogComponentHTTP ProxyLogComponent = "http"

// LogComponentConnection defines the "connection" logging component.
LogComponentConnection LogComponent = "connection"
LogComponentConnection ProxyLogComponent = "connection"

// LogComponentAdmin defines the "admin" logging component.
LogComponentAdmin LogComponent = "admin"
LogComponentAdmin ProxyLogComponent = "admin"

// LogComponentClient defines the "client" logging component.
LogComponentClient LogComponent = "client"
LogComponentClient ProxyLogComponent = "client"

// LogComponentFilter defines the "filter" logging component.
LogComponentFilter LogComponent = "filter"
LogComponentFilter ProxyLogComponent = "filter"

// LogComponentMain defines the "main" logging component.
LogComponentMain LogComponent = "main"
LogComponentMain ProxyLogComponent = "main"

// LogComponentRouter defines the "router" logging component.
LogComponentRouter LogComponent = "router"
LogComponentRouter ProxyLogComponent = "router"

// LogComponentRuntime defines the "runtime" logging component.
LogComponentRuntime LogComponent = "runtime"
LogComponentRuntime ProxyLogComponent = "runtime"
)

// ProxyBootstrap defines Envoy Bootstrap configuration.
Expand Down
53 changes: 42 additions & 11 deletions api/v1alpha1/validation/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,12 @@ func TestEnvoyGatewayAdmin(t *testing.T) {
// values should be set in default
egAdmin := eg.GetEnvoyGatewayAdmin()
assert.True(t, egAdmin != nil)
assert.True(t, egAdmin.Debug == false)
assert.True(t, egAdmin.Address.Port == egv1a1.GatewayAdminPort)
assert.True(t, egAdmin.Address.Host == egv1a1.GatewayAdminHost)

// override the admin config
// values should be updated
eg.Admin.Debug = true
eg.Admin.Address = nil
assert.True(t, eg.Admin.Debug == true)
assert.True(t, eg.GetEnvoyGatewayAdmin().Address.Port == egv1a1.GatewayAdminPort)
assert.True(t, eg.GetEnvoyGatewayAdmin().Address.Host == egv1a1.GatewayAdminHost)

Expand All @@ -552,28 +549,62 @@ func TestEnvoyGatewayAdmin(t *testing.T) {
eg.Admin = nil
eg.SetEnvoyGatewayDefaults()
assert.True(t, eg.Admin != nil)
assert.True(t, eg.Admin.Debug == false)
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)
}

func TestGetEnvoyProxyDefaultComponentLevel(t *testing.T) {
cases := []struct {
logging egv1a1.ProxyLogging
component egv1a1.LogComponent
component egv1a1.ProxyLogComponent
expected egv1a1.LogLevel
}{
{
logging: egv1a1.ProxyLogging{
Level: map[egv1a1.LogComponent]egv1a1.LogLevel{
Level: map[egv1a1.ProxyLogComponent]egv1a1.LogLevel{
egv1a1.LogComponentDefault: egv1a1.LogLevelInfo,
},
},
expected: egv1a1.LogLevelInfo,
},
{
logging: egv1a1.ProxyLogging{
Level: map[egv1a1.LogComponent]egv1a1.LogLevel{
Level: map[egv1a1.ProxyLogComponent]egv1a1.LogLevel{
egv1a1.LogComponentDefault: egv1a1.LogLevelInfo,
},
},
Expand All @@ -600,15 +631,15 @@ func TestGetEnvoyProxyComponentLevelArgs(t *testing.T) {
},
{
logging: egv1a1.ProxyLogging{
Level: map[egv1a1.LogComponent]egv1a1.LogLevel{
Level: map[egv1a1.ProxyLogComponent]egv1a1.LogLevel{
egv1a1.LogComponentDefault: egv1a1.LogLevelInfo,
},
},
expected: "",
},
{
logging: egv1a1.ProxyLogging{
Level: map[egv1a1.LogComponent]egv1a1.LogLevel{
Level: map[egv1a1.ProxyLogComponent]egv1a1.LogLevel{
egv1a1.LogComponentDefault: egv1a1.LogLevelInfo,
egv1a1.LogComponentAdmin: egv1a1.LogLevelWarn,
},
Expand All @@ -617,7 +648,7 @@ func TestGetEnvoyProxyComponentLevelArgs(t *testing.T) {
},
{
logging: egv1a1.ProxyLogging{
Level: map[egv1a1.LogComponent]egv1a1.LogLevel{
Level: map[egv1a1.ProxyLogComponent]egv1a1.LogLevel{
egv1a1.LogComponentDefault: egv1a1.LogLevelInfo,
egv1a1.LogComponentAdmin: egv1a1.LogLevelWarn,
egv1a1.LogComponentFilter: egv1a1.LogLevelDebug,
Expand All @@ -627,7 +658,7 @@ func TestGetEnvoyProxyComponentLevelArgs(t *testing.T) {
},
{
logging: egv1a1.ProxyLogging{
Level: map[egv1a1.LogComponent]egv1a1.LogLevel{
Level: map[egv1a1.ProxyLogComponent]egv1a1.LogLevel{
egv1a1.LogComponentDefault: egv1a1.LogLevelInfo,
egv1a1.LogComponentAdmin: egv1a1.LogLevelWarn,
egv1a1.LogComponentFilter: egv1a1.LogLevelDebug,
Expand Down
Loading

0 comments on commit 8e8b072

Please sign in to comment.