Skip to content

Commit d0bea69

Browse files
authored
Add nginx debug flag (#401)
* Add nginx-debug flag * Add nginx-debug flag documentation * Add nginxDebug support to helm chart * Add unit test for getNginxCommand
1 parent 814c10f commit d0bea69

File tree

10 files changed

+59
-12
lines changed

10 files changed

+59
-12
lines changed

cmd/nginx-ingress/main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"time"
1414

1515
"github.com/golang/glog"
16-
1716
"github.com/nginxinc/kubernetes-ingress/internal/controller"
1817
"github.com/nginxinc/kubernetes-ingress/internal/handlers"
1918
"github.com/nginxinc/kubernetes-ingress/internal/nginx"
@@ -91,6 +90,9 @@ The external address of the service is used when reporting the status of Ingress
9190

9291
nginxStatus = flag.Bool("nginx-status", true,
9392
"Enable the NGINX stub_status, or the NGINX Plus API.")
93+
94+
nginxDebug = flag.Bool("nginx-debug", false,
95+
"Enable debugging for NGINX. Uses the nginx-debug binary. Requires 'error-log-level: debug' in the ConfigMap.")
9496
)
9597

9698
func main() {
@@ -154,11 +156,16 @@ func main() {
154156
nginxIngressTemplatePath = *ingressTemplatePath
155157
}
156158

159+
nginxBinaryPath := "/usr/sbin/nginx"
160+
if *nginxDebug {
161+
nginxBinaryPath = "/usr/sbin/nginx-debug"
162+
}
163+
157164
templateExecutor, err := nginx.NewTemplateExecutor(nginxConfTemplatePath, nginxIngressTemplatePath, *healthStatus, *nginxStatus, allowedCIDRs, *nginxStatusPort)
158165
if err != nil {
159166
glog.Fatalf("Error creating TemplateExecutor: %v", err)
160167
}
161-
ngxc := nginx.NewNginxController("/etc/nginx/", local)
168+
ngxc := nginx.NewNginxController("/etc/nginx/", nginxBinaryPath, local)
162169

163170
if *defaultServerSecret != "" {
164171
ns, name, err := utils.ParseNamespaceName(*defaultServerSecret)

deployments/helm-chart/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Parameter | Description | Default
6161
`controller.kind` | The kind of the Ingress controller installation - deployment or daemonset. | deployment
6262
`controller.nginxplus` | Deploys the Ingress controller for NGINX Plus. | false
6363
`controller.hostNetwork` | Enables the Ingress controller pods to use the host's network namespace. | false
64+
`controller.nginxDebug` | Enables debugging for NGINX. Uses the `nginx-debug` binary. Requires `error-log-level: debug` in the ConfigMap via `controller.config.entries`. | false
6465
`controller.image.repository` | The image repository of the Ingress controller. | nginx/nginx-ingress
6566
`controller.image.tag` | The tag of the Ingress controller image. | edge
6667
`controller.image.pullPolicy` | The pull policy for the Ingress controller image. | IfNotPresent

deployments/helm-chart/templates/controller-daemonset.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ spec:
7171
- -watch-namespace={{ .Values.controller.watchNamespace }}
7272
{{- end }}
7373
- -health-status={{ .Values.controller.healthStatus }}
74+
- -nginx-debug={{ .Values.controller.nginxDebug }}
7475
{{- if .Values.controller.nginxStatus.enable }}
7576
- -nginx-status
7677
- -nginx-status-port={{ .Values.controller.nginxStatus.port }}

deployments/helm-chart/templates/controller-deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ spec:
5757
- -watch-namespace={{ .Values.controller.watchNamespace }}
5858
{{- end }}
5959
- -health-status={{ .Values.controller.healthStatus }}
60+
- -nginx-debug={{ .Values.controller.nginxDebug }}
6061
{{- if .Values.controller.nginxStatus.enable }}
6162
- -nginx-status
6263
- -nginx-status-port={{ .Values.controller.nginxStatus.port }}

deployments/helm-chart/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ controller:
33
kind: deployment
44
nginxplus: false
55
hostNetwork: false
6+
nginxDebug: false
67
image:
78
repository: nginx/nginx-ingress
89
tag: "edge"

docs/cli-arguments.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Usage of ./nginx-ingress:
3737
A ConfigMap resource for customizing NGINX configuration. If a ConfigMap is set,
3838
but the Ingress controller is not able to fetch it from Kubernetes API, the Ingress controller will fail to start.
3939
Format: <namespace>/<name>
40+
-nginx-debug
41+
Enable debugging for NGINX. Uses the nginx-debug binary. Requires 'error-log-level: debug' in the ConfigMap.
4042
-nginx-plus
4143
Enable support for NGINX Plus
4244
-nginx-status

internal/controller/controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ func TestFindIngressesForSecret(t *testing.T) {
930930
if err != nil {
931931
t.Fatalf("templateExecuter could not start: %v", err)
932932
}
933-
ngxc := nginx.NewNginxController("/etc/nginx", true)
933+
ngxc := nginx.NewNginxController("/etc/nginx", "nginx", true)
934934
apiCtrl, err := plus.NewNginxAPIController(&http.Client{}, "", true)
935935
if err != nil {
936936
t.Fatalf("NGINX API Controller could not start: %v", err)
@@ -1111,7 +1111,7 @@ func TestFindIngressesForSecretWithMinions(t *testing.T) {
11111111
if err != nil {
11121112
t.Fatalf("templateExecuter could not start: %v", err)
11131113
}
1114-
ngxc := nginx.NewNginxController("/etc/nginx", true)
1114+
ngxc := nginx.NewNginxController("/etc/nginx", "nginx", true)
11151115
apiCtrl, err := plus.NewNginxAPIController(&http.Client{}, "", true)
11161116
if err != nil {
11171117
t.Fatalf("NGINX API Controller could not start: %v", err)

internal/nginx/configurator_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88

99
"github.com/nginxinc/kubernetes-ingress/internal/nginx/plus"
10-
1110
api_v1 "k8s.io/api/core/v1"
1211
extensions "k8s.io/api/extensions/v1beta1"
1312
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -528,7 +527,7 @@ func createTestConfigurator() (*Configurator, error) {
528527
if err != nil {
529528
return nil, err
530529
}
531-
ngxc := NewNginxController("/etc/nginx", true)
530+
ngxc := NewNginxController("/etc/nginx", "nginx", true)
532531
apiCtrl, err := plus.NewNginxAPIController(&http.Client{}, "", true)
533532
if err != nil {
534533
return nil, err
@@ -545,7 +544,7 @@ func createTestConfiguratorInvalidIngressTemplate() (*Configurator, error) {
545544
if err := templateExecutor.UpdateIngressTemplate(&invalidIngressTemplate); err != nil {
546545
return nil, err
547546
}
548-
ngxc := NewNginxController("/etc/nginx", true)
547+
ngxc := NewNginxController("/etc/nginx", "nginx", true)
549548
apiCtrl, _ := plus.NewNginxAPIController(&http.Client{}, "", true)
550549
return NewConfigurator(ngxc, NewDefaultConfig(), apiCtrl, templateExecutor), nil
551550
}

internal/nginx/nginx.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Controller struct {
2323
nginxConfdPath string
2424
nginxSecretsPath string
2525
local bool
26+
nginxBinaryPath string
2627
verifyConfigGenerator *verify.ConfigGenerator
2728
verifyClient *verify.Client
2829
configVersion int
@@ -187,12 +188,13 @@ func NewUpstreamWithDefaultServer(name string) Upstream {
187188
Port: "8181",
188189
MaxFails: 1,
189190
FailTimeout: "10s",
190-
}},
191+
},
192+
},
191193
}
192194
}
193195

194196
// NewNginxController creates a NGINX controller
195-
func NewNginxController(nginxConfPath string, local bool) *Controller {
197+
func NewNginxController(nginxConfPath string, nginxBinaryPath string, local bool) *Controller {
196198
verifyConfigGenerator, err := verify.NewConfigGenerator()
197199
if err != nil {
198200
glog.Fatalf("error instantiating a verify.ConfigGenerator: %v", err)
@@ -202,6 +204,7 @@ func NewNginxController(nginxConfPath string, local bool) *Controller {
202204
nginxConfdPath: path.Join(nginxConfPath, "conf.d"),
203205
nginxSecretsPath: path.Join(nginxConfPath, "secrets"),
204206
local: local,
207+
nginxBinaryPath: nginxBinaryPath,
205208
verifyConfigGenerator: verifyConfigGenerator,
206209
configVersion: 0,
207210
verifyClient: verify.NewClient(),
@@ -295,6 +298,10 @@ func (nginx *Controller) getSecretFileName(name string) string {
295298
return path.Join(nginx.nginxSecretsPath, name)
296299
}
297300

301+
func (nginx *Controller) getNginxCommand(cmd string) string {
302+
return fmt.Sprint(nginx.nginxBinaryPath, " -s ", cmd)
303+
}
304+
298305
// Reload reloads NGINX
299306
func (nginx *Controller) Reload() error {
300307
if nginx.local {
@@ -306,7 +313,9 @@ func (nginx *Controller) Reload() error {
306313
nginx.UpdateConfigVersionFile()
307314

308315
glog.V(3).Infof("Reloading nginx. configVersion: %v", nginx.configVersion)
309-
if err := shellOut("nginx -s reload"); err != nil {
316+
317+
reloadCmd := nginx.getNginxCommand("reload")
318+
if err := shellOut(reloadCmd); err != nil {
310319
return fmt.Errorf("nginx reload failed: %v", err)
311320
}
312321
err := nginx.verifyClient.WaitForCorrectVersion(nginx.configVersion)
@@ -325,7 +334,7 @@ func (nginx *Controller) Start(done chan error) {
325334
return
326335
}
327336

328-
cmd := exec.Command("nginx")
337+
cmd := exec.Command(nginx.nginxBinaryPath)
329338
cmd.Stdout = os.Stdout
330339
cmd.Stderr = os.Stderr
331340
if err := cmd.Start(); err != nil {
@@ -345,7 +354,8 @@ func (nginx *Controller) Start(done chan error) {
345354
// Quit shutdowns NGINX gracefully
346355
func (nginx *Controller) Quit() {
347356
if !nginx.local {
348-
if err := shellOut("nginx -s quit"); err != nil {
357+
quitCmd := nginx.getNginxCommand("quit")
358+
if err := shellOut(quitCmd); err != nil {
349359
glog.Fatalf("Failed to quit nginx: %v", err)
350360
}
351361
} else {

internal/nginx/nginx_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package nginx
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetNginxCommand(t *testing.T) {
8+
tests := []struct {
9+
cmd string
10+
nginxBinaryPath string
11+
expected string
12+
}{
13+
{"reload", "/usr/sbin/nginx", "/usr/sbin/nginx -s reload"},
14+
{"stop", "/usr/sbin/nginx-debug", "/usr/sbin/nginx-debug -s stop"},
15+
}
16+
for _, test := range tests {
17+
t.Run(test.cmd, func(t *testing.T) {
18+
nginx := NewNginxController("/etc/nginx", test.nginxBinaryPath, true)
19+
20+
if got := nginx.getNginxCommand(test.cmd); got != test.expected {
21+
t.Errorf("getNginxCommand returned \n%v, but expected \n%v", got, test.expected)
22+
}
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)