Skip to content

Commit

Permalink
feat: support enbale nginx debug_connection (kubernetes#8637)
Browse files Browse the repository at this point in the history
  • Loading branch information
phantooom authored and rchshld committed May 17, 2023
1 parent 0a77b62 commit bf730ee
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/user-guide/nginx-configuration/configmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ The following table shows a configuration option's name, type, and the default v
|[global-rate-limit-status-code](#global-rate-limit)|int|429|
|[service-upstream](#service-upstream)|bool|"false"|
|[ssl-reject-handshake](#ssl-reject-handshake)|bool|"false"|
|[debug-connections](#debug-connections)|[]string|"127.0.0.1,1.1.1.1/24"|

## add-headers

Expand Down Expand Up @@ -1300,3 +1301,10 @@ _**default:**_ "false"

_References:_
[https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_reject_handshake](https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_reject_handshake)

## debug-connections
Enables debugging log for selected client connections.
_**default:**_ ""

_References:_
[http://nginx.org/en/docs/ngx_core_module.html#debug_connection](http://nginx.org/en/docs/ngx_core_module.html#debug_connection)
6 changes: 6 additions & 0 deletions internal/ingress/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,11 @@ type Configuration struct {
// GlobalRateLimitStatucCode determines the HTTP status code to return
// when limit is exceeding during global rate limiting.
GlobalRateLimitStatucCode int `json:"global-rate-limit-status-code"`

// DebugConnections Enables debugging log for selected client connections
// http://nginx.org/en/docs/ngx_core_module.html#debug_connection
// Default: ""
DebugConnections []string `json:"debug-connections"`
}

// NewDefault returns the default nginx configuration
Expand Down Expand Up @@ -932,6 +937,7 @@ func NewDefault() Configuration {
GlobalRateLimitMemcachedMaxIdleTimeout: 10000,
GlobalRateLimitMemcachedPoolSize: 50,
GlobalRateLimitStatucCode: 429,
DebugConnections: []string{},
}

if klog.V(5).Enabled() {
Expand Down
20 changes: 20 additions & 0 deletions internal/ingress/controller/template/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
globalAuthAlwaysSetCookie = "global-auth-always-set-cookie"
luaSharedDictsKey = "lua-shared-dicts"
plugins = "plugins"
debugConnections = "debug-connections"
)

var (
Expand Down Expand Up @@ -111,6 +112,7 @@ func ReadConfig(src map[string]string) config.Configuration {
blockRefererList := make([]string, 0)
responseHeaders := make([]string, 0)
luaSharedDicts := make(map[string]int)
debugConnectionsList := make([]string, 0)

//parse lua shared dict values
if val, ok := conf[luaSharedDictsKey]; ok {
Expand Down Expand Up @@ -373,6 +375,24 @@ func ReadConfig(src map[string]string) config.Configuration {
delete(conf, plugins)
}

if val, ok := conf[debugConnections]; ok {
delete(conf, debugConnections)
for _, i := range splitAndTrimSpace(val, ",") {
validIp := net.ParseIP(i)
if validIp != nil {
debugConnectionsList = append(debugConnectionsList, i)
} else {
_, _, err := net.ParseCIDR(i)
if err == nil {
debugConnectionsList = append(debugConnectionsList, i)
} else {
klog.Warningf("%v is not a valid IP or CIDR address", i)
}
}
}
to.DebugConnections = debugConnectionsList
}

to.CustomHTTPErrors = filterErrors(errors)
to.SkipAccessLogURLs = skipUrls
to.WhitelistSourceRange = whiteList
Expand Down
2 changes: 2 additions & 0 deletions internal/ingress/controller/template/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestMergeConfigMapToStruct(t *testing.T) {
"proxy-add-original-uri-header": "false",
"disable-ipv6-dns": "true",
"default-type": "text/plain",
"debug-connections": "127.0.0.1,1.1.1.1/24,::1",
}
def := config.NewDefault()
def.CustomHTTPErrors = []int{300, 400}
Expand All @@ -99,6 +100,7 @@ func TestMergeConfigMapToStruct(t *testing.T) {
def.LuaSharedDicts = defaultLuaSharedDicts
def.DisableIpv6DNS = true
def.DefaultType = "text/plain"
def.DebugConnections = []string{"127.0.0.1", "1.1.1.1/24", "::1"}

hash, err := hashstructure.Hash(def, &hashstructure.HashOptions{
TagName: "json",
Expand Down
3 changes: 3 additions & 0 deletions rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ events {
multi_accept {{ if $cfg.EnableMultiAccept }}on{{ else }}off{{ end }};
worker_connections {{ $cfg.MaxWorkerConnections }};
use epoll;
{{ range $index , $v := $cfg.DebugConnections }}
debug_connection {{ $v }};
{{ end }}
}

http {
Expand Down

0 comments on commit bf730ee

Please sign in to comment.