Custom headers by service labels for docker backends
This commit is contained in:
parent
260ee980e0
commit
c66d9de759
7 changed files with 169 additions and 26 deletions
|
@ -81,16 +81,20 @@ func (p *Provider) buildConfiguration(containersInspected []dockerData) *types.C
|
|||
"hasIsDevelopmentHeaders": hasFunc(label.TraefikFrontendIsDevelopment),
|
||||
"getIsDevelopmentHeaders": getFuncBoolLabel(label.TraefikFrontendIsDevelopment, false),
|
||||
|
||||
"hasServices": hasServices,
|
||||
"getServiceNames": getServiceNames,
|
||||
"getServicePort": getServicePort,
|
||||
"getServiceWeight": getFuncServiceStringLabel(label.SuffixWeight, label.DefaultWeight),
|
||||
"getServiceProtocol": getFuncServiceStringLabel(label.SuffixProtocol, label.DefaultProtocol),
|
||||
"getServiceEntryPoints": getFuncServiceSliceStringLabel(label.SuffixFrontendEntryPoints),
|
||||
"getServiceBasicAuth": getFuncServiceSliceStringLabel(label.SuffixFrontendAuthBasic),
|
||||
"getServiceFrontendRule": p.getServiceFrontendRule,
|
||||
"getServicePassHostHeader": getFuncServiceStringLabel(label.SuffixFrontendPassHostHeader, label.DefaultPassHostHeader),
|
||||
"getServicePriority": getFuncServiceStringLabel(label.SuffixFrontendPriority, label.DefaultFrontendPriority),
|
||||
"hasServices": hasServices,
|
||||
"getServiceNames": getServiceNames,
|
||||
"getServicePort": getServicePort,
|
||||
"hasServiceRequestHeaders": hasFuncServiceLabel(label.SuffixFrontendRequestHeaders),
|
||||
"getServiceRequestHeaders": getFuncServiceMapLabel(label.SuffixFrontendRequestHeaders),
|
||||
"hasServiceResponseHeaders": hasFuncServiceLabel(label.SuffixFrontendResponseHeaders),
|
||||
"getServiceResponseHeaders": getFuncServiceMapLabel(label.SuffixFrontendResponseHeaders),
|
||||
"getServiceWeight": getFuncServiceStringLabel(label.SuffixWeight, label.DefaultWeight),
|
||||
"getServiceProtocol": getFuncServiceStringLabel(label.SuffixProtocol, label.DefaultProtocol),
|
||||
"getServiceEntryPoints": getFuncServiceSliceStringLabel(label.SuffixFrontendEntryPoints),
|
||||
"getServiceBasicAuth": getFuncServiceSliceStringLabel(label.SuffixFrontendAuthBasic),
|
||||
"getServiceFrontendRule": p.getServiceFrontendRule,
|
||||
"getServicePassHostHeader": getFuncServiceStringLabel(label.SuffixFrontendPassHostHeader, label.DefaultPassHostHeader),
|
||||
"getServicePriority": getFuncServiceStringLabel(label.SuffixFrontendPriority, label.DefaultFrontendPriority),
|
||||
}
|
||||
// filter containers
|
||||
filteredContainers := fun.Filter(func(container dockerData) bool {
|
||||
|
|
|
@ -88,6 +88,12 @@ func getServicePort(container dockerData, serviceName string) string {
|
|||
|
||||
// Service label functions
|
||||
|
||||
func getFuncServiceMapLabel(labelSuffix string) func(container dockerData, serviceName string) map[string]string {
|
||||
return func(container dockerData, serviceName string) map[string]string {
|
||||
return getServiceMapLabel(container, serviceName, labelSuffix)
|
||||
}
|
||||
}
|
||||
|
||||
func getFuncServiceSliceStringLabel(labelSuffix string) func(container dockerData, serviceName string) []string {
|
||||
return func(container dockerData, serviceName string) []string {
|
||||
return getServiceSliceStringLabel(container, serviceName, labelSuffix)
|
||||
|
@ -114,6 +120,14 @@ func hasServiceLabel(container dockerData, serviceName string, labelSuffix strin
|
|||
return label.Has(container.Labels, label.Prefix+labelSuffix)
|
||||
}
|
||||
|
||||
func getServiceMapLabel(container dockerData, serviceName string, labelSuffix string) map[string]string {
|
||||
if value, ok := getServiceLabels(container, serviceName)[labelSuffix]; ok {
|
||||
lblName := label.GetServiceLabel(labelSuffix, serviceName)
|
||||
return label.ParseMapValue(lblName, value)
|
||||
}
|
||||
return label.GetMapValue(container.Labels, label.Prefix+labelSuffix)
|
||||
}
|
||||
|
||||
func getServiceSliceStringLabel(container dockerData, serviceName string, labelSuffix string) []string {
|
||||
if value, ok := getServiceLabels(container, serviceName)[labelSuffix]; ok {
|
||||
return label.SplitAndTrimString(value, ",")
|
||||
|
|
|
@ -7,8 +7,80 @@ import (
|
|||
|
||||
"github.com/containous/traefik/provider/label"
|
||||
docker "github.com/docker/docker/api/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDockerGetFuncMapLabel(t *testing.T) {
|
||||
serviceName := "myservice"
|
||||
fakeSuffix := "frontend.foo"
|
||||
fakeLabel := label.Prefix + fakeSuffix
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
container docker.ContainerJSON
|
||||
suffixLabel string
|
||||
expectedKey string
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
desc: "fallback to container label value",
|
||||
container: containerJSON(labels(map[string]string{
|
||||
fakeLabel: "X-Custom-Header: ContainerRequestHeader",
|
||||
})),
|
||||
suffixLabel: fakeSuffix,
|
||||
expected: map[string]string{
|
||||
"X-Custom-Header": "ContainerRequestHeader",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "use service label instead of container label",
|
||||
container: containerJSON(labels(map[string]string{
|
||||
fakeLabel: "X-Custom-Header: ContainerRequestHeader",
|
||||
label.GetServiceLabel(fakeLabel, serviceName): "X-Custom-Header: ServiceRequestHeader",
|
||||
})),
|
||||
suffixLabel: fakeSuffix,
|
||||
expected: map[string]string{
|
||||
"X-Custom-Header": "ServiceRequestHeader",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "use service label with an empty value instead of container label",
|
||||
container: containerJSON(labels(map[string]string{
|
||||
fakeLabel: "X-Custom-Header: ContainerRequestHeader",
|
||||
label.GetServiceLabel(fakeLabel, serviceName): "X-Custom-Header: ",
|
||||
})),
|
||||
suffixLabel: fakeSuffix,
|
||||
expected: map[string]string{
|
||||
"X-Custom-Header": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "multiple values",
|
||||
container: containerJSON(labels(map[string]string{
|
||||
fakeLabel: "X-Custom-Header: MultiHeaders || Authorization: Basic YWRtaW46YWRtaW4=",
|
||||
})),
|
||||
suffixLabel: fakeSuffix,
|
||||
expected: map[string]string{
|
||||
"X-Custom-Header": "MultiHeaders",
|
||||
"Authorization": "Basic YWRtaW46YWRtaW4=",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dData := parseContainer(test.container)
|
||||
|
||||
values := getFuncServiceMapLabel(test.suffixLabel)(dData, serviceName)
|
||||
|
||||
assert.EqualValues(t, test.expected, values)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDockerGetFuncServiceStringLabel(t *testing.T) {
|
||||
testCases := []struct {
|
||||
container docker.ContainerJSON
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue