Frontend rule and segment labels.
This commit is contained in:
parent
118b4eb07a
commit
a731b43b52
11 changed files with 40 additions and 30 deletions
|
@ -631,7 +631,7 @@ var _templatesDockerTmpl = []byte(`{{$backendServers := .Servers}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
[frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"]
|
[frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"]
|
||||||
rule = "{{ getFrontendRule $container }}"
|
rule = "{{ getFrontendRule $container $container.SegmentLabels }}"
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
`)
|
`)
|
||||||
|
@ -2011,8 +2011,8 @@ var _templatesRancherTmpl = []byte(`{{ $backendServers := .Backends }}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
[frontends."frontend-{{$frontendName}}".routes."route-frontend-{{$frontendName}}"]
|
[frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"]
|
||||||
rule = "{{getFrontendRule $service}}"
|
rule = "{{ getFrontendRule $service.Name $service.SegmentLabels }}"
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
`)
|
`)
|
||||||
|
|
|
@ -116,7 +116,7 @@ func (p *Provider) containerFilter(container dockerData) bool {
|
||||||
for segmentName, labels := range segmentProperties {
|
for segmentName, labels := range segmentProperties {
|
||||||
errPort = checkSegmentPort(labels, segmentName)
|
errPort = checkSegmentPort(labels, segmentName)
|
||||||
|
|
||||||
if len(p.getFrontendRule(container)) == 0 {
|
if len(p.getFrontendRule(container, labels)) == 0 {
|
||||||
log.Debugf("Filtering container with empty frontend rule %s %s", container.Name, segmentName)
|
log.Debugf("Filtering container with empty frontend rule %s %s", container.Name, segmentName)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -160,14 +160,14 @@ func (p *Provider) getFrontendName(container dockerData, idx int) string {
|
||||||
if len(container.SegmentName) > 0 {
|
if len(container.SegmentName) > 0 {
|
||||||
name = getBackendName(container)
|
name = getBackendName(container)
|
||||||
} else {
|
} else {
|
||||||
name = p.getFrontendRule(container) + "-" + strconv.Itoa(idx)
|
name = p.getFrontendRule(container, container.SegmentLabels) + "-" + strconv.Itoa(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
return provider.Normalize(name)
|
return provider.Normalize(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) getFrontendRule(container dockerData) string {
|
func (p *Provider) getFrontendRule(container dockerData, segmentLabels map[string]string) string {
|
||||||
if value := label.GetStringValue(container.SegmentLabels, label.TraefikFrontendRule, ""); len(value) != 0 {
|
if value := label.GetStringValue(segmentLabels, label.TraefikFrontendRule, ""); len(value) != 0 {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -634,9 +634,6 @@ func TestDockerTraefikFilter(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
dData := parseContainer(test.container)
|
dData := parseContainer(test.container)
|
||||||
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
|
||||||
dData.SegmentLabels = segmentProperties[""]
|
|
||||||
|
|
||||||
actual := test.provider.containerFilter(dData)
|
actual := test.provider.containerFilter(dData)
|
||||||
if actual != test.expected {
|
if actual != test.expected {
|
||||||
t.Errorf("expected %v for %+v, got %+v", test.expected, test, actual)
|
t.Errorf("expected %v for %+v, got %+v", test.expected, test, actual)
|
||||||
|
@ -746,12 +743,12 @@ func TestDockerGetFrontendRule(t *testing.T) {
|
||||||
|
|
||||||
dData := parseContainer(test.container)
|
dData := parseContainer(test.container)
|
||||||
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
||||||
dData.SegmentLabels = segmentProperties[""]
|
|
||||||
|
|
||||||
provider := &Provider{
|
provider := &Provider{
|
||||||
Domain: "docker.localhost",
|
Domain: "docker.localhost",
|
||||||
}
|
}
|
||||||
actual := provider.getFrontendRule(dData)
|
|
||||||
|
actual := provider.getFrontendRule(dData, segmentProperties[""])
|
||||||
assert.Equal(t, test.expected, actual)
|
assert.Equal(t, test.expected, actual)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -469,8 +469,6 @@ func TestSwarmTraefikFilter(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
dData := parseService(test.service, test.networks)
|
dData := parseService(test.service, test.networks)
|
||||||
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
|
||||||
dData.SegmentLabels = segmentProperties[""]
|
|
||||||
|
|
||||||
actual := test.provider.containerFilter(dData)
|
actual := test.provider.containerFilter(dData)
|
||||||
if actual != test.expected {
|
if actual != test.expected {
|
||||||
|
@ -583,14 +581,13 @@ func TestSwarmGetFrontendRule(t *testing.T) {
|
||||||
|
|
||||||
dData := parseService(test.service, test.networks)
|
dData := parseService(test.service, test.networks)
|
||||||
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
segmentProperties := label.ExtractTraefikLabels(dData.Labels)
|
||||||
dData.SegmentLabels = segmentProperties[""]
|
|
||||||
|
|
||||||
provider := &Provider{
|
provider := &Provider{
|
||||||
Domain: "docker.localhost",
|
Domain: "docker.localhost",
|
||||||
SwarmMode: true,
|
SwarmMode: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
actual := provider.getFrontendRule(dData)
|
actual := provider.getFrontendRule(dData, segmentProperties[""])
|
||||||
assert.Equal(t, test.expected, actual)
|
assert.Equal(t, test.expected, actual)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,7 +128,9 @@ func ExtractTraefikLabels(originLabels map[string]string) SegmentProperties {
|
||||||
allLabels[segmentName][Prefix+propertyName] = value
|
allLabels[segmentName][Prefix+propertyName] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Debug(originLabels, allLabels)
|
|
||||||
|
log.Debug("originLabels", originLabels)
|
||||||
|
log.Debug("allLabels", allLabels)
|
||||||
|
|
||||||
allLabels.mergeDefault()
|
allLabels.mergeDefault()
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ func (p *Provider) serviceFilter(service rancherData) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(p.getFrontendRule(service)) == 0 {
|
if len(p.getFrontendRule(service.Name, labels)) == 0 {
|
||||||
log.Debugf("Filtering container with empty frontend rule %s %s", service.Name, segmentName)
|
log.Debugf("Filtering container with empty frontend rule %s %s", service.Name, segmentName)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -123,9 +123,9 @@ func (p *Provider) serviceFilter(service rancherData) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) getFrontendRule(service rancherData) string {
|
func (p *Provider) getFrontendRule(serviceName string, labels map[string]string) string {
|
||||||
defaultRule := "Host:" + strings.ToLower(strings.Replace(service.Name, "/", ".", -1)) + "." + p.Domain
|
defaultRule := "Host:" + strings.ToLower(strings.Replace(serviceName, "/", ".", -1)) + "." + p.Domain
|
||||||
return label.GetStringValue(service.Labels, label.TraefikFrontendRule, defaultRule)
|
return label.GetStringValue(labels, label.TraefikFrontendRule, defaultRule)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) getFrontendName(service rancherData) string {
|
func (p *Provider) getFrontendName(service rancherData) string {
|
||||||
|
@ -133,7 +133,7 @@ func (p *Provider) getFrontendName(service rancherData) string {
|
||||||
if len(service.SegmentName) > 0 {
|
if len(service.SegmentName) > 0 {
|
||||||
name = getBackendName(service)
|
name = getBackendName(service)
|
||||||
} else {
|
} else {
|
||||||
name = p.getFrontendRule(service)
|
name = p.getFrontendRule(service.Name, service.SegmentLabels)
|
||||||
}
|
}
|
||||||
|
|
||||||
return provider.Normalize(name)
|
return provider.Normalize(name)
|
||||||
|
|
|
@ -260,6 +260,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
label.Prefix + "sauternes." + label.SuffixProtocol: "https",
|
label.Prefix + "sauternes." + label.SuffixProtocol: "https",
|
||||||
label.Prefix + "sauternes." + label.SuffixWeight: "12",
|
label.Prefix + "sauternes." + label.SuffixWeight: "12",
|
||||||
|
|
||||||
|
label.Prefix + "sauternes." + label.SuffixFrontendRule: "Host:traefik.wtf",
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendEntryPoints: "http,https",
|
label.Prefix + "sauternes." + label.SuffixFrontendEntryPoints: "http,https",
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendPassHostHeader: "true",
|
label.Prefix + "sauternes." + label.SuffixFrontendPassHostHeader: "true",
|
||||||
|
@ -319,7 +320,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Backend: "backend-sauternes",
|
Backend: "backend-sauternes",
|
||||||
Routes: map[string]types.Route{
|
Routes: map[string]types.Route{
|
||||||
"route-frontend-sauternes": {
|
"route-frontend-sauternes": {
|
||||||
Rule: "Host:.rancher.localhost",
|
Rule: "Host:traefik.wtf",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
PassHostHeader: true,
|
PassHostHeader: true,
|
||||||
|
@ -697,6 +698,9 @@ func TestProviderGetFrontendName(t *testing.T) {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
segmentProperties := label.ExtractTraefikLabels(test.service.Labels)
|
||||||
|
test.service.SegmentLabels = segmentProperties[""]
|
||||||
|
|
||||||
actual := provider.getFrontendName(test.service)
|
actual := provider.getFrontendName(test.service)
|
||||||
assert.Equal(t, test.expected, actual)
|
assert.Equal(t, test.expected, actual)
|
||||||
})
|
})
|
||||||
|
@ -762,7 +766,10 @@ func TestProviderGetFrontendRule(t *testing.T) {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
actual := provider.getFrontendRule(test.service)
|
segmentProperties := label.ExtractTraefikLabels(test.service.Labels)
|
||||||
|
test.service.SegmentLabels = segmentProperties[""]
|
||||||
|
|
||||||
|
actual := provider.getFrontendRule(test.service.Name, test.service.SegmentLabels)
|
||||||
assert.Equal(t, test.expected, actual)
|
assert.Equal(t, test.expected, actual)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package rancher
|
package rancher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/BurntSushi/ty/fun"
|
"github.com/BurntSushi/ty/fun"
|
||||||
|
@ -31,7 +32,7 @@ func (p *Provider) buildConfigurationV1(services []rancherData) *types.Configura
|
||||||
|
|
||||||
// Frontend functions
|
// Frontend functions
|
||||||
"getBackend": getBackendNameV1,
|
"getBackend": getBackendNameV1,
|
||||||
"getFrontendRule": p.getFrontendRule,
|
"getFrontendRule": p.getFrontendRuleV1,
|
||||||
"getPriority": getFuncIntV1(label.TraefikFrontendPriority, label.DefaultFrontendPriorityInt),
|
"getPriority": getFuncIntV1(label.TraefikFrontendPriority, label.DefaultFrontendPriorityInt),
|
||||||
"getPassHostHeader": getFuncBoolV1(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeaderBool),
|
"getPassHostHeader": getFuncBoolV1(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeaderBool),
|
||||||
"getEntryPoints": getFuncSliceStringV1(label.TraefikFrontendEntryPoints),
|
"getEntryPoints": getFuncSliceStringV1(label.TraefikFrontendEntryPoints),
|
||||||
|
@ -109,9 +110,15 @@ func (p *Provider) serviceFilterV1(service rancherData) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated
|
||||||
|
func (p *Provider) getFrontendRuleV1(service rancherData) string {
|
||||||
|
defaultRule := "Host:" + strings.ToLower(strings.Replace(service.Name, "/", ".", -1)) + "." + p.Domain
|
||||||
|
return label.GetStringValue(service.Labels, label.TraefikFrontendRule, defaultRule)
|
||||||
|
}
|
||||||
|
|
||||||
// Deprecated
|
// Deprecated
|
||||||
func (p *Provider) getFrontendNameV1(service rancherData) string {
|
func (p *Provider) getFrontendNameV1(service rancherData) string {
|
||||||
return provider.Normalize(p.getFrontendRule(service))
|
return provider.Normalize(p.getFrontendRuleV1(service))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated
|
// Deprecated
|
||||||
|
|
|
@ -459,7 +459,7 @@ func TestProviderGetFrontendRuleV1(t *testing.T) {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
actual := provider.getFrontendRule(test.service)
|
actual := provider.getFrontendRule(test.service.Name, test.service.Labels)
|
||||||
assert.Equal(t, test.expected, actual)
|
assert.Equal(t, test.expected, actual)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,6 +171,6 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
[frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"]
|
[frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"]
|
||||||
rule = "{{ getFrontendRule $container }}"
|
rule = "{{ getFrontendRule $container $container.SegmentLabels }}"
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -169,7 +169,7 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
[frontends."frontend-{{$frontendName}}".routes."route-frontend-{{$frontendName}}"]
|
[frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"]
|
||||||
rule = "{{getFrontendRule $service}}"
|
rule = "{{ getFrontendRule $service.Name $service.SegmentLabels }}"
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
Loading…
Reference in a new issue