Skip to content

Commit

Permalink
add default frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmoraisjr committed Jan 29, 2019
1 parent 696c06f commit a2c24d9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
59 changes: 36 additions & 23 deletions pkg/converters/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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})
Expand All @@ -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 (
Expand Down
31 changes: 22 additions & 9 deletions pkg/haproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit a2c24d9

Please sign in to comment.