This commit is contained in:
barnettZQG 2021-04-27 11:52:02 +08:00
parent 0a7bc39767
commit 26b38edc33
10 changed files with 135 additions and 77 deletions

View File

@ -41,6 +41,7 @@ type Config struct {
CookieDomain string `json:"cookieDomain"` CookieDomain string `json:"cookieDomain"`
CookiePath string `json:"cookiePath"` CookiePath string `json:"cookiePath"`
NextUpstream string `json:"nextUpstream"` NextUpstream string `json:"nextUpstream"`
NextUpstreamTimeout int `json:"nextUpstreamTimeout"`
NextUpstreamTries int `json:"nextUpstreamTries"` NextUpstreamTries int `json:"nextUpstreamTries"`
ProxyRedirectFrom string `json:"proxyRedirectFrom"` ProxyRedirectFrom string `json:"proxyRedirectFrom"`
ProxyRedirectTo string `json:"proxyRedirectTo"` ProxyRedirectTo string `json:"proxyRedirectTo"`
@ -95,6 +96,7 @@ func NewProxyConfig() Config {
CookiePath: defBackend.ProxyCookiePath, CookiePath: defBackend.ProxyCookiePath,
NextUpstream: defBackend.ProxyNextUpstream, NextUpstream: defBackend.ProxyNextUpstream,
NextUpstreamTries: defBackend.ProxyNextUpstreamTries, NextUpstreamTries: defBackend.ProxyNextUpstreamTries,
NextUpstreamTimeout: defBackend.ProxyNextUpstreamTimeout,
RequestBuffering: defBackend.ProxyRequestBuffering, RequestBuffering: defBackend.ProxyRequestBuffering,
ProxyRedirectFrom: defBackend.ProxyRedirectFrom, ProxyRedirectFrom: defBackend.ProxyRedirectFrom,
ProxyRedirectTo: defBackend.ProxyRedirectTo, ProxyRedirectTo: defBackend.ProxyRedirectTo,
@ -233,6 +235,11 @@ func (a proxy) Parse(ing *extensions.Ingress) (interface{}, error) {
config.NextUpstreamTries = defBackend.ProxyNextUpstreamTries config.NextUpstreamTries = defBackend.ProxyNextUpstreamTries
} }
config.NextUpstreamTimeout, err = parser.GetIntAnnotation("proxy-next-upstream-timeout", ing)
if err != nil {
config.NextUpstreamTimeout = defBackend.ProxyNextUpstreamTimeout
}
config.RequestBuffering, err = parser.GetStringAnnotation("proxy-request-buffering", ing) config.RequestBuffering, err = parser.GetStringAnnotation("proxy-request-buffering", ing)
if err != nil { if err != nil {
config.RequestBuffering = defBackend.ProxyRequestBuffering config.RequestBuffering = defBackend.ProxyRequestBuffering

View File

@ -76,8 +76,9 @@ func (m mockBackend) GetDefaultBackend() defaults.Backend {
ProxyBuffersNumber: 4, ProxyBuffersNumber: 4,
ProxyBufferSize: "10k", ProxyBufferSize: "10k",
ProxyBodySize: 3, ProxyBodySize: 3,
ProxyNextUpstream: "error", ProxyNextUpstream: "error timeout",
ProxyNextUpstreamTries: 3, ProxyNextUpstreamTries: 3,
ProxyNextUpstreamTimeout: 0,
ProxyRequestBuffering: "on", ProxyRequestBuffering: "on",
ProxyBuffering: "off", ProxyBuffering: "off",
} }
@ -125,9 +126,6 @@ func TestProxy(t *testing.T) {
if p.BodySize != 2 { if p.BodySize != 2 {
t.Errorf("expected 2 as body-size but returned %v", p.BodySize) t.Errorf("expected 2 as body-size but returned %v", p.BodySize)
} }
if p.NextUpstream != "off" {
t.Errorf("expected off as next-upstream but returned %v", p.NextUpstream)
}
if p.NextUpstreamTries != 3 { if p.NextUpstreamTries != 3 {
t.Errorf("expected 3 as next-upstream-tries but returned %v", p.NextUpstreamTries) t.Errorf("expected 3 as next-upstream-tries but returned %v", p.NextUpstreamTries)
} }
@ -171,9 +169,6 @@ func TestProxyWithNoAnnotation(t *testing.T) {
if p.BodySize != 3 { if p.BodySize != 3 {
t.Errorf("expected 3k as body-size but returned %v", p.BodySize) t.Errorf("expected 3k as body-size but returned %v", p.BodySize)
} }
if p.NextUpstream != "error" {
t.Errorf("expected error as next-upstream but returned %v", p.NextUpstream)
}
if p.NextUpstreamTries != 3 { if p.NextUpstreamTries != 3 {
t.Errorf("expected 3 as next-upstream-tries but returned %v", p.NextUpstreamTries) t.Errorf("expected 3 as next-upstream-tries but returned %v", p.NextUpstreamTries)
} }

View File

@ -81,7 +81,7 @@ func NewDefault() Configuration {
cfg := Configuration{ cfg := Configuration{
Backend: defaults.Backend{ Backend: defaults.Backend{
ProxyBodySize: bodySize, ProxyBodySize: bodySize,
ProxyConnectTimeout: 60, ProxyConnectTimeout: 5,
ProxyReadTimeout: 60, ProxyReadTimeout: 60,
ProxySendTimeout: 60, ProxySendTimeout: 60,
ProxyBuffersNumber: 4, ProxyBuffersNumber: 4,
@ -90,6 +90,7 @@ func NewDefault() Configuration {
ProxyCookiePath: "off", ProxyCookiePath: "off",
ProxyNextUpstream: "error timeout", ProxyNextUpstream: "error timeout",
ProxyNextUpstreamTries: 3, ProxyNextUpstreamTries: 3,
ProxyNextUpstreamTimeout: 0,
ProxyRequestBuffering: "on", ProxyRequestBuffering: "on",
ProxyRedirectFrom: "off", ProxyRedirectFrom: "off",
ProxyRedirectTo: "off", ProxyRedirectTo: "off",

View File

@ -42,6 +42,9 @@ type Server struct {
ProxyStreamResponses int ProxyStreamResponses int
ProxyStreamTimeout string ProxyStreamTimeout string
ProxyStreamNextUpstream bool `json:"proxyStreamNextUpstream"`
ProxyStreamNextUpstreamTimeout string `json:"proxyStreamNextUpstreamTimeout"`
ProxyStreamNextUpstreamTries int `json:"proxyStreamNextUpstreamTries"`
//proxy protocol for tcp real ip //proxy protocol for tcp real ip
ProxyProtocol ProxyProtocol ProxyProtocol ProxyProtocol
} }

View File

@ -211,6 +211,9 @@ func (o *OrService) getNgxServer(conf *v1.Config) (l7srv []*model.Server, l4srv
"tenant_id": vs.Namespace, "tenant_id": vs.Namespace,
"service_id": vs.ServiceID, "service_id": vs.ServiceID,
}, },
ProxyStreamNextUpstream: true,
ProxyStreamNextUpstreamTimeout: "600s",
ProxyStreamNextUpstreamTries: 3,
} }
if vs.SSLCert != nil { if vs.SSLCert != nil {
server.SSLProtocols = vs.SSlProtocols server.SSLProtocols = vs.SSlProtocols
@ -245,6 +248,9 @@ func (o *OrService) getNgxServer(conf *v1.Config) (l7srv []*model.Server, l4srv
"service_id": vs.ServiceID, "service_id": vs.ServiceID,
}, },
UpstreamName: vs.PoolName, UpstreamName: vs.PoolName,
ProxyStreamNextUpstream: true,
ProxyStreamNextUpstreamTimeout: "600s",
ProxyStreamNextUpstreamTries: 3,
} }
server.Listen = strings.Join(vs.Listening, " ") server.Listen = strings.Join(vs.Listening, " ")
l4srv = append(l4srv, server) l4srv = append(l4srv, server)

View File

@ -30,6 +30,11 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const (
slash = "/"
nonIdempotent = "non_idempotent"
)
var ( var (
funcMap = text_template.FuncMap{ funcMap = text_template.FuncMap{
"empty": func(input interface{}) bool { "empty": func(input interface{}) bool {
@ -41,6 +46,7 @@ var (
}, },
"buildLuaHeaderRouter": buildLuaHeaderRouter, "buildLuaHeaderRouter": buildLuaHeaderRouter,
"isValidByteSize": isValidByteSize, "isValidByteSize": isValidByteSize,
"buildNextUpstream": buildNextUpstream,
} }
) )
@ -153,3 +159,32 @@ func isValidByteSize(input interface{}, isOffset bool) bool {
return nginxSizeRegex.MatchString(s) return nginxSizeRegex.MatchString(s)
} }
func buildNextUpstream(i, r interface{}) string {
nextUpstream, ok := i.(string)
if !ok {
logrus.Errorf("expected a 'string' type but %T was returned", i)
return ""
}
retryNonIdempotent := r.(bool)
parts := strings.Split(nextUpstream, " ")
nextUpstreamCodes := make([]string, 0, len(parts))
for _, v := range parts {
if v != "" && v != nonIdempotent {
nextUpstreamCodes = append(nextUpstreamCodes, v)
}
if v == nonIdempotent {
retryNonIdempotent = true
}
}
if retryNonIdempotent {
nextUpstreamCodes = append(nextUpstreamCodes, nonIdempotent)
}
return strings.Join(nextUpstreamCodes, " ")
}

View File

@ -77,6 +77,8 @@ type Backend struct {
// https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream_tries // https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream_tries
ProxyNextUpstreamTries int `json:"proxy-next-upstream-tries"` ProxyNextUpstreamTries int `json:"proxy-next-upstream-tries"`
ProxyNextUpstreamTimeout int `json:"proxy-next-upstream-timeout"`
// Sets the original text that should be changed in the "Location" and "Refresh" header fields of a proxied server response. // Sets the original text that should be changed in the "Location" and "Refresh" header fields of a proxied server response.
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
// Default: off // Default: off

View File

@ -274,7 +274,6 @@ function _M.call()
html = string.gsub(_M.defaultHTML, "POWER", [[<p class="text2" id="power">Power By <a href="https://www.rainbond.com" target="_blank" rel="noopener noreferrer">Rainbond</a></p>]],1) html = string.gsub(_M.defaultHTML, "POWER", [[<p class="text2" id="power">Power By <a href="https://www.rainbond.com" target="_blank" rel="noopener noreferrer">Rainbond</a></p>]],1)
end end
ngx.print(html) ngx.print(html)
ngx.status = ngx.HTTP_OK
end end
return _M return _M

View File

@ -68,6 +68,10 @@ server {
proxy_send_timeout {{ $loc.Proxy.SendTimeout }}s; proxy_send_timeout {{ $loc.Proxy.SendTimeout }}s;
proxy_read_timeout {{ $loc.Proxy.ReadTimeout }}s; proxy_read_timeout {{ $loc.Proxy.ReadTimeout }}s;
proxy_next_upstream {{ buildNextUpstream $loc.Proxy.NextUpstream false }};
proxy_next_upstream_timeout {{ $loc.Proxy.NextUpstreamTimeout }};
proxy_next_upstream_tries {{ $loc.Proxy.NextUpstreamTries }};
proxy_buffering {{ $loc.Proxy.ProxyBuffering }}; proxy_buffering {{ $loc.Proxy.ProxyBuffering }};
proxy_buffer_size {{ $loc.Proxy.BufferSize }}; proxy_buffer_size {{ $loc.Proxy.BufferSize }};
proxy_buffers {{ $loc.Proxy.BuffersNumber }} {{ $loc.Proxy.BufferSize }}; proxy_buffers {{ $loc.Proxy.BuffersNumber }} {{ $loc.Proxy.BufferSize }};

View File

@ -8,6 +8,9 @@ server {
{{ if .Listen }}listen {{.Listen}} {{ if $tcpServer.ProxyProtocol.Decode }} proxy_protocol{{ end }};{{ end }} {{ if .Listen }}listen {{.Listen}} {{ if $tcpServer.ProxyProtocol.Decode }} proxy_protocol{{ end }};{{ end }}
proxy_timeout {{ $tcpServer.ProxyStreamTimeout }}; proxy_timeout {{ $tcpServer.ProxyStreamTimeout }};
proxy_pass upstream_balancer; proxy_pass upstream_balancer;
proxy_next_upstream {{ if $tcpServer.ProxyStreamNextUpstream }}on{{ else }}off{{ end }};
proxy_next_upstream_timeout {{ $tcpServer.ProxyStreamNextUpstreamTimeout }};
proxy_next_upstream_tries {{ $tcpServer.ProxyStreamNextUpstreamTries }};
{{ if $tcpServer.ProxyProtocol.Encode }} {{ if $tcpServer.ProxyProtocol.Encode }}
proxy_protocol on; proxy_protocol on;
{{ end }} {{ end }}
@ -23,6 +26,9 @@ server {
{{ if $udpServer.Listen }}listen {{$udpServer.Listen}} {{ if $udpServer.ProxyProtocol.Decode }} proxy_protocol{{ end }};{{ end }} {{ if $udpServer.Listen }}listen {{$udpServer.Listen}} {{ if $udpServer.ProxyProtocol.Decode }} proxy_protocol{{ end }};{{ end }}
{{ if $udpServer.ProxyStreamResponses }}proxy_responses {{ $udpServer.ProxyStreamResponses }}; {{ end }} {{ if $udpServer.ProxyStreamResponses }}proxy_responses {{ $udpServer.ProxyStreamResponses }}; {{ end }}
proxy_timeout {{ $udpServer.ProxyStreamTimeout }}; proxy_timeout {{ $udpServer.ProxyStreamTimeout }};
proxy_next_upstream {{ if $udpServer.ProxyStreamNextUpstream }}on{{ else }}off{{ end }};
proxy_next_upstream_timeout {{ $udpServer.ProxyStreamNextUpstreamTimeout }};
proxy_next_upstream_tries {{ $udpServer.ProxyStreamNextUpstreamTries }};
proxy_pass upstream_balancer; proxy_pass upstream_balancer;
} }
{{ end }} {{ end }}