From b51334d19bb74901b21b2390d4ca156f296550b3 Mon Sep 17 00:00:00 2001 From: Vladimir Zhukov Date: Mon, 19 Jun 2023 15:21:59 +0300 Subject: [PATCH] Added parsing of seconds6 millisecond for nginx timeouts* --- internal/ingress/annotations/parser/main.go | 35 ++++++++++++++++++++ internal/ingress/annotations/proxy/main.go | 10 ++++-- internal/ingress/controller/config/config.go | 6 ++-- internal/ingress/defaults/main.go | 6 ++-- rootfs/etc/nginx/template/nginx.tmpl | 6 ++-- 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/internal/ingress/annotations/parser/main.go b/internal/ingress/annotations/parser/main.go index 554ad9dde..60995820c 100644 --- a/internal/ingress/annotations/parser/main.go +++ b/internal/ingress/annotations/parser/main.go @@ -19,6 +19,7 @@ package parser import ( "fmt" "net/url" + "regexp" "strconv" "strings" @@ -139,6 +140,29 @@ func (a ingAnnotations) parseInt(name string) (int, error) { return 0, errors.ErrMissingAnnotations } +func (a ingAnnotations) parseTimeout(name string) (string, error) { + val, ok := a[name] + if ok { + setUnits, _ := regexp.Compile("(\\d+)(s|ms)$") + + if setUnits.MatchString(name) { + return name, nil + } + + noUnits, _ := regexp.Compile("\\d+$") + + if noUnits.MatchString(name) { + return fmt.Sprintf("%ss", name), nil + } + + if name == "" { + return "0", errors.NewInvalidAnnotationContent(name, val) + } + + } + return "0", errors.ErrMissingAnnotations +} + func (a ingAnnotations) parseFloat32(name string) (float32, error) { val, ok := a[name] if ok { @@ -179,6 +203,17 @@ func GetIntAnnotation(name string, ing *networking.Ingress, fields AnnotationFie return ingAnnotations(ing.GetAnnotations()).parseInt(v) } +// GetTimeoutAnnotation extracts a string from an Ingress annotation, special format 10s +func GetTimeoutAnnotation(name string, ing *networking.Ingress) (string, error) { + v := GetAnnotationWithPrefix(name) + err := checkAnnotation(v, ing) + if err != nil { + return "", err + } + + return ingAnnotations(ing.GetAnnotations()).parseTimeout(v) +} + // GetFloatAnnotation extracts a float32 from an Ingress annotation func GetFloatAnnotation(name string, ing *networking.Ingress, fields AnnotationFields) (float32, error) { v, err := checkAnnotation(name, ing, fields) diff --git a/internal/ingress/annotations/proxy/main.go b/internal/ingress/annotations/proxy/main.go index 9d2646261..daa97eaf6 100644 --- a/internal/ingress/annotations/proxy/main.go +++ b/internal/ingress/annotations/proxy/main.go @@ -162,9 +162,9 @@ var proxyAnnotations = parser.Annotation{ // Config returns the proxy timeout to use in the upstream server/s type Config struct { BodySize string `json:"bodySize"` - ConnectTimeout int `json:"connectTimeout"` - SendTimeout int `json:"sendTimeout"` - ReadTimeout int `json:"readTimeout"` + ConnectTimeout string `json:"connectTimeout"` + SendTimeout string `json:"sendTimeout"` + ReadTimeout string `json:"readTimeout"` BuffersNumber int `json:"buffersNumber"` BufferSize string `json:"bufferSize"` CookieDomain string `json:"cookieDomain"` @@ -266,16 +266,20 @@ func (a proxy) Parse(ing *networking.Ingress) (interface{}, error) { var err error config.ConnectTimeout, err = parser.GetIntAnnotation(proxyConnectTimeoutAnnotation, ing, a.annotationConfig.Annotations) + if err != nil { config.ConnectTimeout = defBackend.ProxyConnectTimeout } + config.SendTimeout, err = parser.GetIntAnnotation(proxySendTimeoutAnnotation, ing, a.annotationConfig.Annotations) + if err != nil { config.SendTimeout = defBackend.ProxySendTimeout } config.ReadTimeout, err = parser.GetIntAnnotation(proxyReadTimeoutAnnotation, ing, a.annotationConfig.Annotations) + if err != nil { config.ReadTimeout = defBackend.ProxyReadTimeout } diff --git a/internal/ingress/controller/config/config.go b/internal/ingress/controller/config/config.go index fadc924be..14f2c0e85 100644 --- a/internal/ingress/controller/config/config.go +++ b/internal/ingress/controller/config/config.go @@ -952,9 +952,9 @@ func NewDefault() Configuration { ProxyStreamNextUpstreamTries: 3, Backend: defaults.Backend{ ProxyBodySize: bodySize, - ProxyConnectTimeout: 5, - ProxyReadTimeout: 60, - ProxySendTimeout: 60, + ProxyConnectTimeout: "5s", + ProxyReadTimeout: "60s", + ProxySendTimeout: "60s", ProxyBuffersNumber: 4, ProxyBufferSize: "4k", ProxyCookieDomain: "off", diff --git a/internal/ingress/defaults/main.go b/internal/ingress/defaults/main.go index 0526d443e..e76f939d5 100644 --- a/internal/ingress/defaults/main.go +++ b/internal/ingress/defaults/main.go @@ -41,17 +41,17 @@ type Backend struct { // Defines a timeout for establishing a connection with a proxied server. // It should be noted that this timeout cannot usually exceed 75 seconds. // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout - ProxyConnectTimeout int `json:"proxy-connect-timeout"` + ProxyConnectTimeout string `json:"proxy-connect-timeout"` // Timeout in seconds for reading a response from the proxied server. The timeout is set only between // two successive read operations, not for the transmission of the whole response // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout - ProxyReadTimeout int `json:"proxy-read-timeout"` + ProxyReadTimeout string `json:"proxy-read-timeout"` // Timeout in seconds for transmitting a request to the proxied server. The timeout is set only between // two successive write operations, not for the transmission of the whole request. // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_send_timeout - ProxySendTimeout int `json:"proxy-send-timeout"` + ProxySendTimeout string `json:"proxy-send-timeout"` // Sets the number of the buffers used for reading a response from the proxied server // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffers diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index a6ebe5d2a..7794ab13b 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -1443,9 +1443,9 @@ stream { {{ $proxySetHeader }} {{ $k }} {{ $v | quote }}; {{ end }} - proxy_connect_timeout {{ $location.Proxy.ConnectTimeout }}s; - proxy_send_timeout {{ $location.Proxy.SendTimeout }}s; - proxy_read_timeout {{ $location.Proxy.ReadTimeout }}s; + proxy_connect_timeout {{ $location.Proxy.ConnectTimeout }}; + proxy_send_timeout {{ $location.Proxy.SendTimeout }}; + proxy_read_timeout {{ $location.Proxy.ReadTimeout }}; proxy_buffering {{ $location.Proxy.ProxyBuffering }}; proxy_buffer_size {{ $location.Proxy.BufferSize }};