From eafb1890d6876866aef82f1f947f60ac06ad9877 Mon Sep 17 00:00:00 2001 From: Dmitry Stolyarov Date: Thu, 10 May 2018 00:50:55 +0300 Subject: [PATCH 1/3] Move vars to the very beginning of the location To make it more clear, that you could use $namespace, $ingress_name and $service_name variables anywhere in location (especialy in lua), move their definition to the very begining of the location. --- rootfs/etc/nginx/template/nginx.tmpl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index c258ab2e3..86c8ed5a0 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -833,6 +833,11 @@ stream { {{ end }} location {{ $path }} { + {{ $ing := (getIngressInformation $location.Ingress $location.Path) }} + set $namespace "{{ $ing.Namespace }}"; + set $ingress_name "{{ $ing.Rule }}"; + set $service_name "{{ $ing.Service }}"; + {{ if not $all.DisableLua }} rewrite_by_lua_block { {{ if $all.DynamicConfigurationEnabled}} @@ -912,12 +917,6 @@ stream { set $proxy_upstream_name "{{ buildUpstreamName $server.Hostname $all.Backends $location $all.DynamicConfigurationEnabled }}"; - {{ $ing := (getIngressInformation $location.Ingress $location.Path) }} - {{/* $ing.Metadata contains the Ingress metadata */}} - set $namespace "{{ $ing.Namespace }}"; - set $ingress_name "{{ $ing.Rule }}"; - set $service_name "{{ $ing.Service }}"; - {{/* redirect to HTTPS can be achieved forcing the redirect or having a SSL Certificate configured for the server */}} {{ if (or $location.Rewrite.ForceSSLRedirect (and (not (empty $server.SSLCertificate)) $location.Rewrite.SSLRedirect)) }} {{ if not (isLocationInLocationList $location $all.Cfg.NoTLSRedirectLocations) }} From 59aac73785911444c1fe1eda295af2497b5f3382 Mon Sep 17 00:00:00 2001 From: Dmitry Stolyarov Date: Thu, 10 May 2018 00:59:58 +0300 Subject: [PATCH 2/3] Add $service_port variable According to TCP/IP (and common sense), $service_name is not enough to uniquely identify service, we need $service_port for that. --- rootfs/etc/nginx/template/nginx.tmpl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index 86c8ed5a0..3c2b58e49 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -182,6 +182,7 @@ http { # $namespace # $ingress_name # $service_name + # $service_port log_format upstreaminfo {{ if $cfg.LogFormatEscapeJSON }}escape=json {{ end }}'{{ buildLogFormatUpstream $cfg }}'; {{/* map urls that should not appear in access.log */}} @@ -663,6 +664,7 @@ stream { proxy_set_header X-Namespace $namespace; proxy_set_header X-Ingress-Name $ingress_name; proxy_set_header X-Service-Name $service_name; + proxy_set_header X-Service-Port $service_port; rewrite (.*) / break; @@ -837,6 +839,7 @@ stream { set $namespace "{{ $ing.Namespace }}"; set $ingress_name "{{ $ing.Rule }}"; set $service_name "{{ $ing.Service }}"; + set $service_port "{{ $location.Port }}"; {{ if not $all.DisableLua }} rewrite_by_lua_block { @@ -1090,6 +1093,7 @@ stream { proxy_set_header X-Namespace $namespace; proxy_set_header X-Ingress-Name $ingress_name; proxy_set_header X-Service-Name $service_name; + proxy_set_header X-Service-Port $service_port; {{ end }} {{ if not (empty $location.Backend) }} From 02ff8244a27d3130855fa2af460ccd20cc28ef2c Mon Sep 17 00:00:00 2001 From: Dmitry Stolyarov Date: Thu, 10 May 2018 01:07:42 +0300 Subject: [PATCH 3/3] Add $location_path variable When you define rules in ingress resource, you use path. So it would be very useful to be able to use the same path in logs. --- rootfs/etc/nginx/template/nginx.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index 3c2b58e49..d2a38e147 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -840,6 +840,7 @@ stream { set $ingress_name "{{ $ing.Rule }}"; set $service_name "{{ $ing.Service }}"; set $service_port "{{ $location.Port }}"; + set $location_path "{{ $location.Path }}"; {{ if not $all.DisableLua }} rewrite_by_lua_block {