From a2c24d96d423f5c7fa84c78d1f0cb1ad888babb2 Mon Sep 17 00:00:00 2001 From: Joao Morais Date: Mon, 28 Jan 2019 22:24:35 -0200 Subject: [PATCH] add default frontend --- pkg/converters/ingress/ingress_test.go | 59 ++++++++++++++++---------- pkg/haproxy/config.go | 31 ++++++++++---- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/pkg/converters/ingress/ingress_test.go b/pkg/converters/ingress/ingress_test.go index 1fdbdc218..1032a817f 100644 --- a/pkg/converters/ingress/ingress_test.go +++ b/pkg/converters/ingress/ingress_test.go @@ -504,11 +504,11 @@ func TestSyncBackendDefault(t *testing.T) { c.createSvc1Auto() c.Sync(c.createIng2("default/echo", "echo:8080")) - c.compareConfigFront(` -- hostname: '*' - paths: - - path: / - backend: default_echo_8080`) + c.compareConfigDefaultFront(` +hostname: '*' +paths: +- path: / + backend: default_echo_8080`) c.compareConfigBack(` - id: default_echo_8080 @@ -563,11 +563,11 @@ func TestSyncDefaultBackendReusedPath1(t *testing.T) { c.createIng2("default/echo2", "echo2:8080"), ) - c.compareConfigFront(` -- hostname: '*' - paths: - - path: / - backend: default_echo1_8080`) + c.compareConfigDefaultFront(` +hostname: '*' +paths: +- path: / + backend: default_echo1_8080`) c.compareConfigBack(` - id: default_echo1_8080 @@ -590,11 +590,11 @@ func TestSyncDefaultBackendReusedPath2(t *testing.T) { c.createIng1("default/echo2", "'*'", "/", "echo2:8080"), ) - c.compareConfigFront(` -- hostname: '*' - paths: - - path: / - backend: default_echo1_8080`) + c.compareConfigDefaultFront(` +hostname: '*' +paths: +- path: / + backend: default_echo1_8080`) c.compareConfigBack(` - id: default_echo1_8080 @@ -621,11 +621,11 @@ func TestSyncEmptyHost(t *testing.T) { c.createSvc1Auto() c.Sync(c.createIng1("default/echo", "", "/", "echo:8080")) - c.compareConfigFront(` -- hostname: '*' - paths: - - path: / - backend: default_echo_8080`) + c.compareConfigDefaultFront(` +hostname: '*' +paths: +- path: / + backend: default_echo_8080`) } func TestSyncMultiNamespace(t *testing.T) { @@ -1179,9 +1179,9 @@ type ( } ) -func (c *testConfig) compareConfigFront(expected string) { +func convertFrontend(hafronts ...*hatypes.Frontend) []frontendMock { frontends := []frontendMock{} - for _, f := range c.hconfig.Frontends() { + for _, f := range hafronts { paths := []pathMock{} for _, p := range f.Paths { paths = append(paths, pathMock{Path: p.Path, BackendID: p.BackendID}) @@ -1194,7 +1194,20 @@ func (c *testConfig) compareConfigFront(expected string) { TLS: tlsMock{TLSFilename: f.TLS.TLSFilename}, }) } - c.compareText(_yamlMarshal(frontends), expected) + return frontends +} + +func (c *testConfig) compareConfigFront(expected string) { + c.compareText(_yamlMarshal(convertFrontend(c.hconfig.Frontends()...)), expected) +} + +func (c *testConfig) compareConfigDefaultFront(expected string) { + frontend := c.hconfig.DefaultFrontend() + if frontend != nil { + c.compareText(_yamlMarshal(convertFrontend(frontend)[0]), expected) + } else { + c.compareText("[]", expected) + } } type ( diff --git a/pkg/haproxy/config.go b/pkg/haproxy/config.go index 10461be64..6d69d9bd5 100644 --- a/pkg/haproxy/config.go +++ b/pkg/haproxy/config.go @@ -33,6 +33,7 @@ type Config interface { ConfigDefaultBackend(defaultBackend *hatypes.Backend) AddUserlist(name string, users []hatypes.User) *hatypes.Userlist FindUserlist(name string) *hatypes.Userlist + DefaultFrontend() *hatypes.Frontend DefaultBackend() *hatypes.Backend Global() *hatypes.Global Frontends() []*hatypes.Frontend @@ -42,11 +43,12 @@ type Config interface { } type config struct { - global *hatypes.Global - frontends []*hatypes.Frontend - backends []*hatypes.Backend - userlists []*hatypes.Userlist - defaultBackend *hatypes.Backend + global *hatypes.Global + frontends []*hatypes.Frontend + backends []*hatypes.Backend + userlists []*hatypes.Userlist + defaultFrontend *hatypes.Frontend + defaultBackend *hatypes.Backend } func createConfig() Config { @@ -60,14 +62,21 @@ func (c *config) AcquireFrontend(hostname string) *hatypes.Frontend { return frontend } frontend := createFrontend(hostname) - c.frontends = append(c.frontends, frontend) - sort.Slice(c.frontends, func(i, j int) bool { - return c.frontends[i].Hostname < c.frontends[j].Hostname - }) + if frontend.Hostname != "*" { + c.frontends = append(c.frontends, frontend) + sort.Slice(c.frontends, func(i, j int) bool { + return c.frontends[i].Hostname < c.frontends[j].Hostname + }) + } else { + c.defaultFrontend = frontend + } return frontend } func (c *config) FindFrontend(hostname string) *hatypes.Frontend { + if hostname == "*" && c.defaultFrontend != nil { + return c.defaultFrontend + } for _, f := range c.frontends { if f.Hostname == hostname { return f @@ -148,6 +157,10 @@ func (c *config) FindUserlist(name string) *hatypes.Userlist { return nil } +func (c *config) DefaultFrontend() *hatypes.Frontend { + return c.defaultFrontend +} + func (c *config) DefaultBackend() *hatypes.Backend { return c.defaultBackend }