diff --git a/apis/v1alpha2/nginxproxy_types.go b/apis/v1alpha2/nginxproxy_types.go index 8f8a2671e8..bfa21aca59 100644 --- a/apis/v1alpha2/nginxproxy_types.go +++ b/apis/v1alpha2/nginxproxy_types.go @@ -435,6 +435,11 @@ type PodSpec struct { // ContainerSpec defines container fields for the NGINX container. type ContainerSpec struct { + // Debug enables debugging for NGINX by using the nginx-debug binary. + // + // +optional + Debug *bool `json:"debug,omitempty"` + // Image is the NGINX image to use. // // +optional diff --git a/apis/v1alpha2/zz_generated.deepcopy.go b/apis/v1alpha2/zz_generated.deepcopy.go index c6420fc2f2..54e5c760b2 100644 --- a/apis/v1alpha2/zz_generated.deepcopy.go +++ b/apis/v1alpha2/zz_generated.deepcopy.go @@ -14,6 +14,11 @@ import ( // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ContainerSpec) DeepCopyInto(out *ContainerSpec) { *out = *in + if in.Debug != nil { + in, out := &in.Debug, &out.Debug + *out = new(bool) + **out = **in + } if in.Image != nil { in, out := &in.Image, &out.Image *out = new(Image) diff --git a/build/entrypoint.sh b/build/entrypoint.sh index 1095831c57..4ccd6be3a2 100755 --- a/build/entrypoint.sh +++ b/build/entrypoint.sh @@ -16,7 +16,13 @@ rm -rf /var/run/nginx/*.sock # Launch nginx echo "starting nginx ..." -/usr/sbin/nginx -g "daemon off;" & + +# if we want to use the nginx-debug binary, we will call this script with an argument "debug" +if [ "${1:-false}" = "debug" ]; then + /usr/sbin/nginx-debug -g "daemon off;" & +else + /usr/sbin/nginx -g "daemon off;" & +fi nginx_pid=$! @@ -31,7 +37,7 @@ done # start nginx-agent, pass args echo "starting nginx-agent ..." -nginx-agent "$@" & +nginx-agent & agent_pid=$! diff --git a/charts/nginx-gateway-fabric/templates/nginxproxy.yaml b/charts/nginx-gateway-fabric/templates/nginxproxy.yaml index 1dd6f44155..f77630fe95 100644 --- a/charts/nginx-gateway-fabric/templates/nginxproxy.yaml +++ b/charts/nginx-gateway-fabric/templates/nginxproxy.yaml @@ -23,6 +23,9 @@ spec: {{- end }} image: {{- toYaml .Values.nginx.image | nindent 10 }} + {{- if .Values.nginx.debug }} + debug: {{ .Values.nginx.debug }} + {{- end }} {{- end }} {{- if .Values.nginx.service }} service: diff --git a/config/crd/bases/gateway.nginx.org_nginxproxies.yaml b/config/crd/bases/gateway.nginx.org_nginxproxies.yaml index a86fcf723e..6593274424 100644 --- a/config/crd/bases/gateway.nginx.org_nginxproxies.yaml +++ b/config/crd/bases/gateway.nginx.org_nginxproxies.yaml @@ -79,6 +79,10 @@ spec: description: Container defines container fields for the NGINX container. properties: + debug: + description: Debug enables debugging for NGINX by using + the nginx-debug binary. + type: boolean image: description: Image is the NGINX image to use. properties: diff --git a/deploy/crds.yaml b/deploy/crds.yaml index cc4ad633b3..08d58ad387 100644 --- a/deploy/crds.yaml +++ b/deploy/crds.yaml @@ -664,6 +664,10 @@ spec: description: Container defines container fields for the NGINX container. properties: + debug: + description: Debug enables debugging for NGINX by using + the nginx-debug binary. + type: boolean image: description: Image is the NGINX image to use. properties: diff --git a/internal/mode/static/provisioner/objects.go b/internal/mode/static/provisioner/objects.go index 19a24cb832..6c3c6b5c01 100644 --- a/internal/mode/static/provisioner/objects.go +++ b/internal/mode/static/provisioner/objects.go @@ -447,6 +447,11 @@ func (p *NginxProvisioner) buildNginxPodTemplateSpec( } container.Lifecycle = containerSpec.Lifecycle container.VolumeMounts = append(container.VolumeMounts, containerSpec.VolumeMounts...) + + if containerSpec.Debug != nil && *containerSpec.Debug { + container.Command = append(container.Command, "/agent/entrypoint.sh") + container.Args = append(container.Args, "debug") + } spec.Spec.Containers[0] = container } } diff --git a/internal/mode/static/state/graph/nginxproxy_test.go b/internal/mode/static/state/graph/nginxproxy_test.go index 2289bb7dab..bf074ab562 100644 --- a/internal/mode/static/state/graph/nginxproxy_test.go +++ b/internal/mode/static/state/graph/nginxproxy_test.go @@ -55,6 +55,7 @@ func TestBuildEffectiveNginxProxy(t *testing.T) { logLevel ngfAPIv1alpha2.NginxErrorLogLevel, setIP bool, disableHTTP bool, + nginxDebug bool, ) *ngfAPIv1alpha2.NginxProxy { return &ngfAPIv1alpha2.NginxProxy{ Spec: ngfAPIv1alpha2.NginxProxySpec{ @@ -79,6 +80,13 @@ func TestBuildEffectiveNginxProxy(t *testing.T) { ErrorLevel: &logLevel, }, DisableHTTP2: &disableHTTP, + Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{ + Deployment: &ngfAPIv1alpha2.DeploymentSpec{ + Container: ngfAPIv1alpha2.ContainerSpec{ + Debug: &nginxDebug, + }, + }, + }, }, } } @@ -100,6 +108,7 @@ func TestBuildEffectiveNginxProxy(t *testing.T) { ngfAPIv1alpha2.NginxLogLevelAlert, true, false, + false, ) } @@ -120,6 +129,7 @@ func TestBuildEffectiveNginxProxy(t *testing.T) { ngfAPIv1alpha2.NginxLogLevelError, false, true, + true, ) }