Factorize labels

* refactor(accesslog): factorize file name.
* traefik.frontend.rule
* traefik.frontend.value
* traefik.backend.circuitbreaker.expression
* traefik.enable
* traefik.backend.loadbalancer.method
* traefik.backend.loadbalancer.sticky
* traefik.backend.maxconn.amount
* traefik.backend.maxconn.extractorfunc
* traefik.port
* traefik.tags
* traefik.backend
* traefik.weight
* traefik.domain
* traefik.protocol
* traefik.frontend.passHostHeader
* traefik.frontend.whitelistSourceRange
* traefik.frontend.priority
* traefik.frontend.entryPoints
* traefik.frontend.auth.basic
* traefik.backend.id
* traefik.backend.circuitbreaker
* traefik.frontend.rule.type
* traefik.portIndex
* refactor(docker): specific labels
* refactor(rancher): specific labels
* traefik.backend.healthcheck.*
* refactor(providers): factorize labels.
This commit is contained in:
Ludovic Fernandez 2017-07-10 16:58:12 +02:00 committed by SALLEYRON Julien
parent 2e84b1e556
commit d653a348b1
20 changed files with 390 additions and 330 deletions

View file

@ -16,13 +16,18 @@ import (
checker "github.com/vdemeester/shakers"
)
const (
traefikTestLogFile = "traefik.log"
traefikTestAccessLogFile = "access.log"
)
// AccessLogSuite
type AccessLogSuite struct{ BaseSuite }
func (s *AccessLogSuite) TestAccessLog(c *check.C) {
// Ensure working directory is clean
os.Remove("access.log")
os.Remove("traefik.log")
os.Remove(traefikTestAccessLogFile)
os.Remove(traefikTestLogFile)
// Start Traefik
cmd, _ := s.cmdTraefik(withConfigFile("fixtures/access_log_config.toml"))
@ -30,11 +35,11 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) {
c.Assert(err, checker.IsNil)
defer cmd.Process.Kill()
defer os.Remove("access.log")
defer os.Remove("traefik.log")
defer os.Remove(traefikTestAccessLogFile)
defer os.Remove(traefikTestLogFile)
err = try.Do(1*time.Second, func() error {
if _, err := os.Stat("traefik.log"); err != nil {
if _, err := os.Stat(traefikTestLogFile); err != nil {
return fmt.Errorf("could not get stats for log file: %s", err)
}
return nil
@ -42,7 +47,7 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) {
c.Assert(err, checker.IsNil)
// Verify Traefik started OK
traefikLog, err := ioutil.ReadFile("traefik.log")
traefikLog, err := ioutil.ReadFile(traefikTestLogFile)
c.Assert(err, checker.IsNil)
if len(traefikLog) > 0 {
fmt.Printf("%s\n", string(traefikLog))
@ -66,7 +71,7 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) {
c.Assert(err, checker.IsNil)
// Verify access.log output as expected
accessLog, err := ioutil.ReadFile("access.log")
accessLog, err := ioutil.ReadFile(traefikTestAccessLogFile)
c.Assert(err, checker.IsNil)
lines := strings.Split(string(accessLog), "\n")
count := 0
@ -86,7 +91,7 @@ func (s *AccessLogSuite) TestAccessLog(c *check.C) {
c.Assert(count, checker.GreaterOrEqualThan, 3)
// Verify no other Traefik problems
traefikLog, err = ioutil.ReadFile("traefik.log")
traefikLog, err = ioutil.ReadFile(traefikTestLogFile)
c.Assert(err, checker.IsNil)
if len(traefikLog) > 0 {
fmt.Printf("%s\n", string(traefikLog))

View file

@ -10,9 +10,9 @@ import (
"time"
"github.com/containous/traefik/integration/try"
"github.com/containous/traefik/types"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/go-check/check"
d "github.com/libkermit/docker"
docker "github.com/libkermit/docker-check"
checker "github.com/vdemeester/shakers"
@ -123,7 +123,7 @@ func (s *DockerSuite) TestDockerContainersWithLabels(c *check.C) {
defer os.Remove(file)
// Start a container with some labels
labels := map[string]string{
"traefik.frontend.rule": "Host:my.super.host",
types.LabelFrontendRule: "Host:my.super.host",
}
s.startContainerWithLabels(c, "swarm:1.0.0", labels, "manage", "token://blabla")
@ -155,7 +155,7 @@ func (s *DockerSuite) TestDockerContainersWithOneMissingLabels(c *check.C) {
defer os.Remove(file)
// Start a container with some labels
labels := map[string]string{
"traefik.frontend.value": "my.super.host",
types.LabelTraefikFrontendValue: "my.super.host",
}
s.startContainerWithLabels(c, "swarm:1.0.0", labels, "manage", "token://blabla")

View file

@ -35,6 +35,11 @@ const (
SwarmAPIVersion string = "1.24"
// SwarmDefaultWatchTime is the duration of the interval when polling docker
SwarmDefaultWatchTime = 15 * time.Second
labelDockerNetwork = "traefik.docker.network"
labelBackendLoadbalancerSwarm = "traefik.backend.loadbalancer.swarm"
labelDockerComposeProject = "com.docker.compose.project"
labelDockerComposeService = "com.docker.compose.service"
)
var _ provider.Provider = (*Provider)(nil)
@ -319,7 +324,7 @@ func (p *Provider) loadDockerConfig(containersInspected []dockerData) *types.Con
}
func (p *Provider) hasCircuitBreakerLabel(container dockerData) bool {
if _, err := getLabel(container, "traefik.backend.circuitbreaker.expression"); err != nil {
if _, err := getLabel(container, types.LabelBackendCircuitbreakerExpression); err != nil {
return false
}
return true
@ -455,8 +460,8 @@ func (p *Provider) getServiceProtocol(container dockerData, serviceName string)
}
func (p *Provider) hasLoadBalancerLabel(container dockerData) bool {
_, errMethod := getLabel(container, "traefik.backend.loadbalancer.method")
_, errSticky := getLabel(container, "traefik.backend.loadbalancer.sticky")
_, errMethod := getLabel(container, types.LabelBackendLoadbalancerMethod)
_, errSticky := getLabel(container, types.LabelBackendLoadbalancerSticky)
if errMethod != nil && errSticky != nil {
return false
}
@ -464,31 +469,31 @@ func (p *Provider) hasLoadBalancerLabel(container dockerData) bool {
}
func (p *Provider) hasMaxConnLabels(container dockerData) bool {
if _, err := getLabel(container, "traefik.backend.maxconn.amount"); err != nil {
if _, err := getLabel(container, types.LabelBackendMaxconnAmount); err != nil {
return false
}
if _, err := getLabel(container, "traefik.backend.maxconn.extractorfunc"); err != nil {
if _, err := getLabel(container, types.LabelBackendMaxconnExtractorfunc); err != nil {
return false
}
return true
}
func (p *Provider) getCircuitBreakerExpression(container dockerData) string {
if label, err := getLabel(container, "traefik.backend.circuitbreaker.expression"); err == nil {
if label, err := getLabel(container, types.LabelBackendCircuitbreakerExpression); err == nil {
return label
}
return "NetworkErrorRatio() > 1"
}
func (p *Provider) getLoadBalancerMethod(container dockerData) string {
if label, err := getLabel(container, "traefik.backend.loadbalancer.method"); err == nil {
if label, err := getLabel(container, types.LabelBackendLoadbalancerMethod); err == nil {
return label
}
return "wrr"
}
func (p *Provider) getMaxConnAmount(container dockerData) int64 {
if label, err := getLabel(container, "traefik.backend.maxconn.amount"); err == nil {
if label, err := getLabel(container, types.LabelBackendMaxconnAmount); err == nil {
i, errConv := strconv.ParseInt(label, 10, 64)
if errConv != nil {
log.Errorf("Unable to parse traefik.backend.maxconn.amount %s", label)
@ -500,14 +505,14 @@ func (p *Provider) getMaxConnAmount(container dockerData) int64 {
}
func (p *Provider) getMaxConnExtractorFunc(container dockerData) string {
if label, err := getLabel(container, "traefik.backend.maxconn.extractorfunc"); err == nil {
if label, err := getLabel(container, types.LabelBackendMaxconnExtractorfunc); err == nil {
return label
}
return "request.host"
}
func (p *Provider) containerFilter(container dockerData) bool {
_, err := strconv.Atoi(container.Labels["traefik.port"])
_, err := strconv.Atoi(container.Labels[types.LabelPort])
if len(container.NetworkSettings.Ports) == 0 && err != nil {
log.Debugf("Filtering container without port and no traefik.port label %s", container.Name)
return false
@ -518,7 +523,7 @@ func (p *Provider) containerFilter(container dockerData) bool {
return false
}
constraintTags := strings.Split(container.Labels["traefik.tags"], ",")
constraintTags := strings.Split(container.Labels[types.LabelTags], ",")
if ok, failingConstraint := p.MatchConstraints(constraintTags); !ok {
if failingConstraint != nil {
log.Debugf("Container %v pruned by '%v' constraint", container.Name, failingConstraint.String())
@ -547,11 +552,11 @@ func (p *Provider) getFrontendName(container dockerData) string {
// GetFrontendRule returns the frontend rule for the specified container, using
// it's label. It returns a default one (Host) if the label is not present.
func (p *Provider) getFrontendRule(container dockerData) string {
if label, err := getLabel(container, "traefik.frontend.rule"); err == nil {
if label, err := getLabel(container, types.LabelFrontendRule); err == nil {
return label
}
if labels, err := getLabels(container, []string{"com.docker.compose.project", "com.docker.compose.service"}); err == nil {
return "Host:" + p.getSubDomain(labels["com.docker.compose.service"]+"."+labels["com.docker.compose.project"]) + "." + p.Domain
if labels, err := getLabels(container, []string{labelDockerComposeProject, labelDockerComposeService}); err == nil {
return "Host:" + p.getSubDomain(labels[labelDockerComposeService]+"."+labels[labelDockerComposeProject]) + "." + p.Domain
}
if len(p.Domain) > 0 {
return "Host:" + p.getSubDomain(container.ServiceName) + "." + p.Domain
@ -560,17 +565,17 @@ func (p *Provider) getFrontendRule(container dockerData) string {
}
func (p *Provider) getBackend(container dockerData) string {
if label, err := getLabel(container, "traefik.backend"); err == nil {
if label, err := getLabel(container, types.LabelBackend); err == nil {
return provider.Normalize(label)
}
if labels, err := getLabels(container, []string{"com.docker.compose.project", "com.docker.compose.service"}); err == nil {
return provider.Normalize(labels["com.docker.compose.service"] + "_" + labels["com.docker.compose.project"])
if labels, err := getLabels(container, []string{labelDockerComposeProject, labelDockerComposeService}); err == nil {
return provider.Normalize(labels[labelDockerComposeService] + "_" + labels[labelDockerComposeProject])
}
return provider.Normalize(container.ServiceName)
}
func (p *Provider) getIPAddress(container dockerData) string {
if label, err := getLabel(container, "traefik.docker.network"); err == nil && label != "" {
if label, err := getLabel(container, labelDockerNetwork); err == nil && label != "" {
networkSettings := container.NetworkSettings
if networkSettings.Networks != nil {
network := networkSettings.Networks[label]
@ -606,7 +611,7 @@ func (p *Provider) getIPAddress(container dockerData) string {
}
func (p *Provider) getPort(container dockerData) string {
if label, err := getLabel(container, "traefik.port"); err == nil {
if label, err := getLabel(container, types.LabelPort); err == nil {
return label
}
@ -630,42 +635,42 @@ func (p *Provider) getPort(container dockerData) string {
}
func (p *Provider) getWeight(container dockerData) string {
if label, err := getLabel(container, "traefik.weight"); err == nil {
if label, err := getLabel(container, types.LabelWeight); err == nil {
return label
}
return "0"
}
func (p *Provider) getSticky(container dockerData) string {
if label, err := getLabel(container, "traefik.backend.loadbalancer.sticky"); err == nil {
if label, err := getLabel(container, types.LabelBackendLoadbalancerSticky); err == nil {
return label
}
return "false"
}
func (p *Provider) getIsBackendLBSwarm(container dockerData) string {
if label, err := getLabel(container, "traefik.backend.loadbalancer.swarm"); err == nil {
if label, err := getLabel(container, labelBackendLoadbalancerSwarm); err == nil {
return label
}
return "false"
}
func (p *Provider) getDomain(container dockerData) string {
if label, err := getLabel(container, "traefik.domain"); err == nil {
if label, err := getLabel(container, types.LabelDomain); err == nil {
return label
}
return p.Domain
}
func (p *Provider) getProtocol(container dockerData) string {
if label, err := getLabel(container, "traefik.protocol"); err == nil {
if label, err := getLabel(container, types.LabelProtocol); err == nil {
return label
}
return "http"
}
func (p *Provider) getPassHostHeader(container dockerData) string {
if passHostHeader, err := getLabel(container, "traefik.frontend.passHostHeader"); err == nil {
if passHostHeader, err := getLabel(container, types.LabelFrontendPassHostHeader); err == nil {
return passHostHeader
}
return "true"
@ -674,28 +679,28 @@ func (p *Provider) getPassHostHeader(container dockerData) string {
func (p *Provider) getWhitelistSourceRange(container dockerData) []string {
var whitelistSourceRange []string
if whitelistSourceRangeLabel, err := getLabel(container, "traefik.frontend.whitelistSourceRange"); err == nil {
if whitelistSourceRangeLabel, err := getLabel(container, types.LabelTraefikFrontendWhitelistSourceRange); err == nil {
whitelistSourceRange = provider.SplitAndTrimString(whitelistSourceRangeLabel)
}
return whitelistSourceRange
}
func (p *Provider) getPriority(container dockerData) string {
if priority, err := getLabel(container, "traefik.frontend.priority"); err == nil {
if priority, err := getLabel(container, types.LabelFrontendPriority); err == nil {
return priority
}
return "0"
}
func (p *Provider) getEntryPoints(container dockerData) []string {
if entryPoints, err := getLabel(container, "traefik.frontend.entryPoints"); err == nil {
if entryPoints, err := getLabel(container, types.LabelFrontendEntryPoints); err == nil {
return strings.Split(entryPoints, ",")
}
return []string{}
}
func (p *Provider) getBasicAuth(container dockerData) []string {
if basicAuth, err := getLabel(container, "traefik.frontend.auth.basic"); err == nil {
if basicAuth, err := getLabel(container, types.LabelFrontendAuthBasic); err == nil {
return strings.Split(basicAuth, ",")
}
@ -703,7 +708,7 @@ func (p *Provider) getBasicAuth(container dockerData) []string {
}
func isContainerEnabled(container dockerData, exposedByDefault bool) bool {
return exposedByDefault && container.Labels["traefik.enable"] != "false" || container.Labels["traefik.enable"] == "true"
return exposedByDefault && container.Labels[types.LabelEnable] != "false" || container.Labels[types.LabelEnable] == "true"
}
func getLabel(container dockerData, label string) (string, error) {

View file

@ -23,7 +23,7 @@ func TestDockerGetFrontendName(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.rule": "Headers:User-Agent,bat/0.1.0",
types.LabelFrontendRule: "Headers:User-Agent,bat/0.1.0",
})),
expected: "Headers-User-Agent-bat-0-1-0",
},
@ -36,19 +36,19 @@ func TestDockerGetFrontendName(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
})),
expected: "Host-foo-bar",
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.rule": "Path:/test",
types.LabelFrontendRule: "Path:/test",
})),
expected: "Path-test",
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.rule": "PathPrefix:/test2",
types.LabelFrontendRule: "PathPrefix:/test2",
})),
expected: "PathPrefix-test2",
},
@ -85,7 +85,7 @@ func TestDockerGetFrontendRule(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
})),
expected: "Host:foo.bar",
}, {
@ -97,7 +97,7 @@ func TestDockerGetFrontendRule(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.rule": "Path:/test",
types.LabelFrontendRule: "Path:/test",
})),
expected: "Path:/test",
},
@ -134,7 +134,7 @@ func TestDockerGetBackend(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.backend": "foobar",
types.LabelBackend: "foobar",
})),
expected: "foobar",
},
@ -173,7 +173,7 @@ func TestDockerGetIPAddress(t *testing.T) {
{
container: containerJSON(
labels(map[string]string{
"traefik.docker.network": "testnet",
labelDockerNetwork: "testnet",
}),
withNetwork("testnet", ipv4("10.11.12.13")),
),
@ -182,7 +182,7 @@ func TestDockerGetIPAddress(t *testing.T) {
{
container: containerJSON(
labels(map[string]string{
"traefik.docker.network": "testnet2",
labelDockerNetwork: "testnet2",
}),
withNetwork("testnet", ipv4("10.11.12.13")),
withNetwork("testnet2", ipv4("10.11.12.14")),
@ -237,13 +237,13 @@ func TestDockerGetPort(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.port": "8080",
types.LabelPort: "8080",
})),
expected: "8080",
},
{
container: containerJSON(labels(map[string]string{
"traefik.port": "8080",
types.LabelPort: "8080",
}), ports(nat.PortMap{
"80/tcp": {},
})),
@ -251,7 +251,7 @@ func TestDockerGetPort(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.port": "8080",
types.LabelPort: "8080",
}), ports(nat.PortMap{
"8080/tcp": {},
"80/tcp": {},
@ -285,7 +285,7 @@ func TestDockerGetWeight(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.weight": "10",
types.LabelWeight: "10",
})),
expected: "10",
},
@ -316,7 +316,7 @@ func TestDockerGetDomain(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.domain": "foo.bar",
types.LabelDomain: "foo.bar",
})),
expected: "foo.bar",
},
@ -349,7 +349,7 @@ func TestDockerGetProtocol(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.protocol": "https",
types.LabelProtocol: "https",
})),
expected: "https",
},
@ -380,7 +380,7 @@ func TestDockerGetPassHostHeader(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.passHostHeader": "false",
types.LabelFrontendPassHostHeader: "false",
})),
expected: "false",
},
@ -414,14 +414,14 @@ func TestDockerGetWhitelistSourceRange(t *testing.T) {
{
desc: "whitelist-label with empty string",
container: containerJSON(labels(map[string]string{
"traefik.frontend.whitelistSourceRange": "",
types.LabelTraefikFrontendWhitelistSourceRange: "",
})),
expected: nil,
},
{
desc: "whitelist-label with IPv4 mask",
container: containerJSON(labels(map[string]string{
"traefik.frontend.whitelistSourceRange": "1.2.3.4/16",
types.LabelTraefikFrontendWhitelistSourceRange: "1.2.3.4/16",
})),
expected: []string{
"1.2.3.4/16",
@ -430,7 +430,7 @@ func TestDockerGetWhitelistSourceRange(t *testing.T) {
{
desc: "whitelist-label with IPv6 mask",
container: containerJSON(labels(map[string]string{
"traefik.frontend.whitelistSourceRange": "fe80::/16",
types.LabelTraefikFrontendWhitelistSourceRange: "fe80::/16",
})),
expected: []string{
"fe80::/16",
@ -439,7 +439,7 @@ func TestDockerGetWhitelistSourceRange(t *testing.T) {
{
desc: "whitelist-label with multiple masks",
container: containerJSON(labels(map[string]string{
"traefik.frontend.whitelistSourceRange": "1.1.1.1/24, 1234:abcd::42/32",
types.LabelTraefikFrontendWhitelistSourceRange: "1.1.1.1/24, 1234:abcd::42/32",
})),
expected: []string{
"1.1.1.1/24",
@ -576,7 +576,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.enable": "false",
types.LabelEnable: "false",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -600,7 +600,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -665,7 +665,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.port": "80",
types.LabelPort: "80",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -690,7 +690,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.enable": "true",
types.LabelEnable: "true",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -714,7 +714,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.enable": "anything",
types.LabelEnable: "anything",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -738,7 +738,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -782,7 +782,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.enable": "true",
types.LabelEnable: "true",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -806,7 +806,7 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.enable": "true",
types.LabelEnable: "true",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -829,8 +829,8 @@ func TestDockerTraefikFilter(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.enable": "true",
"traefik.frontend.rule": "Host:i.love.this.host",
types.LabelEnable: "true",
types.LabelFrontendRule: "Host:i.love.this.host",
},
},
NetworkSettings: &docker.NetworkSettings{
@ -912,9 +912,9 @@ func TestDockerLoadDockerConfig(t *testing.T) {
containerJSON(
name("test1"),
labels(map[string]string{
"traefik.backend": "foobar",
"traefik.frontend.entryPoints": "http,https",
"traefik.frontend.auth.basic": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
types.LabelBackend: "foobar",
types.LabelFrontendEntryPoints: "http,https",
types.LabelFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
}),
ports(nat.PortMap{
"80/tcp": {},
@ -924,7 +924,7 @@ func TestDockerLoadDockerConfig(t *testing.T) {
containerJSON(
name("test2"),
labels(map[string]string{
"traefik.backend": "foobar",
types.LabelBackend: "foobar",
}),
ports(nat.PortMap{
"80/tcp": {},
@ -977,12 +977,12 @@ func TestDockerLoadDockerConfig(t *testing.T) {
containerJSON(
name("test1"),
labels(map[string]string{
"traefik.backend": "foobar",
"traefik.frontend.entryPoints": "http,https",
"traefik.backend.maxconn.amount": "1000",
"traefik.backend.maxconn.extractorfunc": "somethingelse",
"traefik.backend.loadbalancer.method": "drr",
"traefik.backend.circuitbreaker.expression": "NetworkErrorRatio() > 0.5",
types.LabelBackend: "foobar",
types.LabelFrontendEntryPoints: "http,https",
types.LabelBackendMaxconnAmount: "1000",
types.LabelBackendMaxconnExtractorfunc: "somethingelse",
types.LabelBackendLoadbalancerMethod: "drr",
types.LabelBackendCircuitbreakerExpression: "NetworkErrorRatio() > 0.5",
}),
ports(nat.PortMap{
"80/tcp": {},

View file

@ -23,7 +23,7 @@ func TestDockerGetServiceProtocol(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.protocol": "https",
types.LabelProtocol: "https",
})),
expected: "https",
},
@ -61,7 +61,7 @@ func TestDockerGetServiceWeight(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.weight": "200",
types.LabelWeight: "200",
})),
expected: "200",
},
@ -99,7 +99,7 @@ func TestDockerGetServicePort(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.port": "2500",
types.LabelPort: "2500",
})),
expected: "2500",
},
@ -137,7 +137,7 @@ func TestDockerGetServiceFrontendRule(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.rule": "Path:/helloworld",
types.LabelFrontendRule: "Path:/helloworld",
})),
expected: "Path:/helloworld",
},
@ -175,7 +175,7 @@ func TestDockerGetServiceBackend(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.backend": "another-backend",
types.LabelBackend: "another-backend",
})),
expected: "another-backend-myservice",
},
@ -213,7 +213,7 @@ func TestDockerGetServicePriority(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.priority": "33",
types.LabelFrontendPriority: "33",
})),
expected: "33",
},
@ -251,7 +251,7 @@ func TestDockerGetServicePassHostHeader(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.passHostHeader": "false",
types.LabelFrontendPassHostHeader: "false",
})),
expected: "false",
},
@ -289,7 +289,7 @@ func TestDockerGetServiceEntryPoints(t *testing.T) {
},
{
container: containerJSON(labels(map[string]string{
"traefik.frontend.entryPoints": "http,https",
types.LabelFrontendEntryPoints: "http,https",
})),
expected: []string{"http", "https"},
},

View file

@ -28,21 +28,21 @@ func TestSwarmGetFrontendName(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.rule": "Headers:User-Agent,bat/0.1.0",
types.LabelFrontendRule: "Headers:User-Agent,bat/0.1.0",
})),
expected: "Headers-User-Agent-bat-0-1-0",
networks: map[string]*docker.NetworkResource{},
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
})),
expected: "Host-foo-bar",
networks: map[string]*docker.NetworkResource{},
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.rule": "Path:/test",
types.LabelFrontendRule: "Path:/test",
})),
expected: "Path-test",
networks: map[string]*docker.NetworkResource{},
@ -51,7 +51,7 @@ func TestSwarmGetFrontendName(t *testing.T) {
service: swarmService(
serviceName("test"),
serviceLabels(map[string]string{
"traefik.frontend.rule": "PathPrefix:/test2",
types.LabelFrontendRule: "PathPrefix:/test2",
}),
),
expected: "PathPrefix-test2",
@ -94,14 +94,14 @@ func TestSwarmGetFrontendRule(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
})),
expected: "Host:foo.bar",
networks: map[string]*docker.NetworkResource{},
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.rule": "Path:/test",
types.LabelFrontendRule: "Path:/test",
})),
expected: "Path:/test",
networks: map[string]*docker.NetworkResource{},
@ -143,7 +143,7 @@ func TestSwarmGetBackend(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.backend": "foobar",
types.LabelBackend: "foobar",
})),
expected: "foobar",
networks: map[string]*docker.NetworkResource{},
@ -192,7 +192,7 @@ func TestSwarmGetIPAddress(t *testing.T) {
{
service: swarmService(
serviceLabels(map[string]string{
"traefik.docker.network": "barnet",
labelDockerNetwork: "barnet",
}),
withEndpointSpec(modeVIP),
withEndpoint(
@ -237,7 +237,7 @@ func TestSwarmGetPort(t *testing.T) {
{
service: swarmService(
serviceLabels(map[string]string{
"traefik.port": "8080",
types.LabelPort: "8080",
}),
withEndpointSpec(modeDNSSR),
),
@ -275,7 +275,7 @@ func TestSwarmGetWeight(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.weight": "10",
types.LabelWeight: "10",
})),
expected: "10",
networks: map[string]*docker.NetworkResource{},
@ -311,7 +311,7 @@ func TestSwarmGetDomain(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.domain": "foo.bar",
types.LabelDomain: "foo.bar",
})),
expected: "foo.bar",
networks: map[string]*docker.NetworkResource{},
@ -348,7 +348,7 @@ func TestSwarmGetProtocol(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.protocol": "https",
types.LabelProtocol: "https",
})),
expected: "https",
networks: map[string]*docker.NetworkResource{},
@ -384,7 +384,7 @@ func TestSwarmGetPassHostHeader(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.passHostHeader": "false",
types.LabelFrontendPassHostHeader: "false",
})),
expected: "false",
networks: map[string]*docker.NetworkResource{},
@ -520,8 +520,8 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.enable": "false",
"traefik.port": "80",
types.LabelEnable: "false",
types.LabelPort: "80",
})),
expected: false,
networks: map[string]*docker.NetworkResource{},
@ -533,8 +533,8 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
"traefik.port": "80",
types.LabelFrontendRule: "Host:foo.bar",
types.LabelPort: "80",
})),
expected: true,
networks: map[string]*docker.NetworkResource{},
@ -546,7 +546,7 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.port": "80",
types.LabelPort: "80",
})),
expected: true,
networks: map[string]*docker.NetworkResource{},
@ -558,8 +558,8 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.enable": "true",
"traefik.port": "80",
types.LabelEnable: "true",
types.LabelPort: "80",
})),
expected: true,
networks: map[string]*docker.NetworkResource{},
@ -571,8 +571,8 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.enable": "anything",
"traefik.port": "80",
types.LabelEnable: "anything",
types.LabelPort: "80",
})),
expected: true,
networks: map[string]*docker.NetworkResource{},
@ -584,8 +584,8 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
"traefik.port": "80",
types.LabelFrontendRule: "Host:foo.bar",
types.LabelPort: "80",
})),
expected: true,
networks: map[string]*docker.NetworkResource{},
@ -597,7 +597,7 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.port": "80",
types.LabelPort: "80",
})),
expected: false,
networks: map[string]*docker.NetworkResource{},
@ -609,8 +609,8 @@ func TestSwarmTraefikFilter(t *testing.T) {
},
{
service: swarmService(serviceLabels(map[string]string{
"traefik.enable": "true",
"traefik.port": "80",
types.LabelEnable: "true",
types.LabelPort: "80",
})),
expected: true,
networks: map[string]*docker.NetworkResource{},
@ -653,7 +653,7 @@ func TestSwarmLoadDockerConfig(t *testing.T) {
swarmService(
serviceName("test"),
serviceLabels(map[string]string{
"traefik.port": "80",
types.LabelPort: "80",
}),
withEndpointSpec(modeVIP),
withEndpoint(virtualIP("1", "127.0.0.1/24")),
@ -695,10 +695,10 @@ func TestSwarmLoadDockerConfig(t *testing.T) {
swarmService(
serviceName("test1"),
serviceLabels(map[string]string{
"traefik.port": "80",
"traefik.backend": "foobar",
"traefik.frontend.entryPoints": "http,https",
"traefik.frontend.auth.basic": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
types.LabelPort: "80",
types.LabelBackend: "foobar",
types.LabelFrontendEntryPoints: "http,https",
types.LabelFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
}),
withEndpointSpec(modeVIP),
withEndpoint(virtualIP("1", "127.0.0.1/24")),
@ -706,8 +706,8 @@ func TestSwarmLoadDockerConfig(t *testing.T) {
swarmService(
serviceName("test2"),
serviceLabels(map[string]string{
"traefik.port": "80",
"traefik.backend": "foobar",
types.LabelPort: "80",
types.LabelBackend: "foobar",
}),
withEndpointSpec(modeVIP),
withEndpoint(virtualIP("1", "127.0.0.1/24")),

View file

@ -390,7 +390,7 @@ func (p *Provider) filterInstance(i ecsInstance) bool {
return false
}
label := i.label("traefik.enable")
label := i.label(types.LabelEnable)
enabled := p.ExposedByDefault && label != "false" || label == "true"
if !enabled {
log.Debugf("Filtering disabled ecs instance %s (%s) (traefik.enabled = '%s')", i.Name, i.ID, label)
@ -414,7 +414,7 @@ func (p *Provider) filterFrontends(instances []ecsInstance) []ecsInstance {
}
func (p *Provider) getFrontendRule(i ecsInstance) string {
if label := i.label("traefik.frontend.rule"); label != "" {
if label := i.label(types.LabelFrontendRule); label != "" {
return label
}
return "Host:" + strings.ToLower(strings.Replace(i.Name, "_", "-", -1)) + "." + p.Domain
@ -437,7 +437,7 @@ func (p *Provider) chunkedTaskArns(tasks []*string) [][]*string {
}
func (i ecsInstance) Protocol() string {
if label := i.label("traefik.protocol"); label != "" {
if label := i.label(types.LabelProtocol); label != "" {
return label
}
return "http"
@ -452,28 +452,28 @@ func (i ecsInstance) Port() string {
}
func (i ecsInstance) Weight() string {
if label := i.label("traefik.weight"); label != "" {
if label := i.label(types.LabelWeight); label != "" {
return label
}
return "0"
}
func (i ecsInstance) PassHostHeader() string {
if label := i.label("traefik.frontend.passHostHeader"); label != "" {
if label := i.label(types.LabelFrontendPassHostHeader); label != "" {
return label
}
return "true"
}
func (i ecsInstance) Priority() string {
if label := i.label("traefik.frontend.priority"); label != "" {
if label := i.label(types.LabelFrontendPriority); label != "" {
return label
}
return "0"
}
func (i ecsInstance) EntryPoints() []string {
if label := i.label("traefik.frontend.entryPoints"); label != "" {
if label := i.label(types.LabelFrontendEntryPoints); label != "" {
return strings.Split(label, ",")
}
return []string{}

View file

@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/containous/traefik/types"
)
func makeEcsInstance(containerDef *ecs.ContainerDefinition) ecsInstance {
@ -68,7 +69,7 @@ func TestEcsProtocol(t *testing.T) {
{
expected: "https",
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.protocol": aws.String("https"),
types.LabelProtocol: aws.String("https"),
}),
},
}
@ -131,7 +132,7 @@ func TestEcsWeight(t *testing.T) {
{
expected: "10",
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.weight": aws.String("10"),
types.LabelWeight: aws.String("10"),
}),
},
}
@ -156,7 +157,7 @@ func TestEcsPassHostHeader(t *testing.T) {
{
expected: "false",
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.frontend.passHostHeader": aws.String("false"),
types.LabelFrontendPassHostHeader: aws.String("false"),
}),
},
}
@ -181,7 +182,7 @@ func TestEcsPriority(t *testing.T) {
{
expected: "10",
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.frontend.priority": aws.String("10"),
types.LabelFrontendPriority: aws.String("10"),
}),
},
}
@ -206,13 +207,13 @@ func TestEcsEntryPoints(t *testing.T) {
{
expected: []string{"http"},
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.frontend.entryPoints": aws.String("http"),
types.LabelFrontendEntryPoints: aws.String("http"),
}),
},
{
expected: []string{"http", "https"},
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.frontend.entryPoints": aws.String("http,https"),
types.LabelFrontendEntryPoints: aws.String("http,https"),
}),
},
}
@ -261,14 +262,14 @@ func TestFilterInstance(t *testing.T) {
expected: false,
exposedByDefault: true,
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.enable": aws.String("false"),
types.LabelEnable: aws.String("false"),
}),
},
{
expected: true,
exposedByDefault: false,
instanceInfo: simpleEcsInstance(map[string]*string{
"traefik.enable": aws.String("true"),
types.LabelEnable: aws.String("true"),
}),
},
{

View file

@ -131,14 +131,14 @@ func (p *Provider) getProtocol(instance eureka.InstanceInfo) string {
}
func (p *Provider) getWeight(instance eureka.InstanceInfo) string {
if val, ok := instance.Metadata.Map["traefik.weight"]; ok {
if val, ok := instance.Metadata.Map[types.LabelWeight]; ok {
return val
}
return "0"
}
func (p *Provider) getInstanceID(instance eureka.InstanceInfo) string {
if val, ok := instance.Metadata.Map["traefik.backend.id"]; ok {
if val, ok := instance.Metadata.Map[types.LabelBackendID]; ok {
return val
}
return strings.Replace(instance.IpAddr, ".", "-", -1) + "-" + p.getPort(instance)

View file

@ -4,6 +4,7 @@ import (
"testing"
"github.com/ArthurHlt/go-eureka-client/eureka"
"github.com/containous/traefik/types"
)
func TestEurekaGetPort(t *testing.T) {
@ -106,7 +107,7 @@ func TestEurekaGetWeight(t *testing.T) {
},
Metadata: &eureka.MetaData{
Map: map[string]string{
"traefik.weight": "10",
types.LabelWeight: "10",
},
},
},
@ -139,7 +140,7 @@ func TestEurekaGetInstanceId(t *testing.T) {
},
Metadata: &eureka.MetaData{
Map: map[string]string{
"traefik.backend.id": "MyInstanceId",
types.LabelBackendID: "MyInstanceId",
},
},
},

View file

@ -27,9 +27,8 @@ import (
var _ provider.Provider = (*Provider)(nil)
const (
annotationFrontendRuleType = "traefik.frontend.rule.type"
ruleTypePathPrefix = "PathPrefix"
ruleTypeReplacePath = "ReplacePath"
ruleTypePathPrefix = "PathPrefix"
ruleTypeReplacePath = "ReplacePath"
annotationKubernetesIngressClass = "kubernetes.io/ingress.class"
annotationKubernetesAuthRealm = "ingress.kubernetes.io/auth-realm"
@ -169,7 +168,7 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
PassHostHeader := p.getPassHostHeader()
passHostHeaderAnnotation, ok := i.Annotations["traefik.frontend.passHostHeader"]
passHostHeaderAnnotation, ok := i.Annotations[types.LabelFrontendPassHostHeader]
switch {
case !ok:
// No op.
@ -178,7 +177,7 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
case passHostHeaderAnnotation == "true":
PassHostHeader = true
default:
log.Warnf("Unknown value '%s' for traefik.frontend.passHostHeader, falling back to %s", passHostHeaderAnnotation, PassHostHeader)
log.Warnf("Unknown value '%s' for %s, falling back to %s", passHostHeaderAnnotation, types.LabelFrontendPassHostHeader, PassHostHeader)
}
if realm := i.Annotations[annotationKubernetesAuthRealm]; realm != "" && realm != traefikDefaultRealm {
return nil, errors.New("no realm customization supported")
@ -235,17 +234,17 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
continue
}
if expression := service.Annotations["traefik.backend.circuitbreaker"]; expression != "" {
if expression := service.Annotations[types.LabelTraefikBackendCircuitbreaker]; expression != "" {
templateObjects.Backends[r.Host+pa.Path].CircuitBreaker = &types.CircuitBreaker{
Expression: expression,
}
}
if service.Annotations["traefik.backend.loadbalancer.method"] == "drr" {
if service.Annotations[types.LabelBackendLoadbalancerMethod] == "drr" {
templateObjects.Backends[r.Host+pa.Path].LoadBalancer.Method = "drr"
}
if service.Annotations["traefik.backend.loadbalancer.sticky"] == "true" {
if service.Annotations[types.LabelBackendLoadbalancerSticky] == "true" {
templateObjects.Backends[r.Host+pa.Path].LoadBalancer.Sticky = true
}
@ -309,7 +308,7 @@ func getRuleForPath(pa v1beta1.HTTPIngressPath, i *v1beta1.Ingress) string {
return ""
}
ruleType := i.Annotations[annotationFrontendRuleType]
ruleType := i.Annotations[types.LabelFrontendRuleType]
if ruleType == "" {
ruleType = ruleTypePathPrefix
}

View file

@ -383,7 +383,7 @@ func TestRuleType(t *testing.T) {
if test.ingressRuleType != "" {
ingress.ObjectMeta.Annotations = map[string]string{
annotationFrontendRuleType: test.ingressRuleType,
types.LabelFrontendRuleType: test.ingressRuleType,
}
}
@ -1224,8 +1224,8 @@ func TestServiceAnnotations(t *testing.T) {
UID: "1",
Namespace: "testing",
Annotations: map[string]string{
"traefik.backend.circuitbreaker": "NetworkErrorRatio() > 0.5",
"traefik.backend.loadbalancer.method": "drr",
types.LabelTraefikBackendCircuitbreaker: "NetworkErrorRatio() > 0.5",
types.LabelBackendLoadbalancerMethod: "drr",
},
},
Spec: v1.ServiceSpec{
@ -1243,8 +1243,8 @@ func TestServiceAnnotations(t *testing.T) {
UID: "2",
Namespace: "testing",
Annotations: map[string]string{
"traefik.backend.circuitbreaker": "",
"traefik.backend.loadbalancer.sticky": "true",
types.LabelTraefikBackendCircuitbreaker: "",
types.LabelBackendLoadbalancerSticky: "true",
},
},
Spec: v1.ServiceSpec{
@ -1418,7 +1418,7 @@ func TestIngressAnnotations(t *testing.T) {
ObjectMeta: v1.ObjectMeta{
Namespace: "testing",
Annotations: map[string]string{
"traefik.frontend.passHostHeader": "false",
types.LabelFrontendPassHostHeader: "false",
},
},
Spec: v1beta1.IngressSpec{
@ -1447,7 +1447,7 @@ func TestIngressAnnotations(t *testing.T) {
Namespace: "testing",
Annotations: map[string]string{
"kubernetes.io/ingress.class": "traefik",
"traefik.frontend.passHostHeader": "true",
types.LabelFrontendPassHostHeader: "true",
},
},
Spec: v1beta1.IngressSpec{
@ -1784,7 +1784,7 @@ func TestInvalidPassHostHeaderValue(t *testing.T) {
ObjectMeta: v1.ObjectMeta{
Namespace: "testing",
Annotations: map[string]string{
"traefik.frontend.passHostHeader": "herpderp",
types.LabelFrontendPassHostHeader: "herpderp",
},
},
Spec: v1beta1.IngressSpec{

View file

@ -25,11 +25,7 @@ import (
)
const (
labelPort = "traefik.port"
labelPortIndex = "traefik.portIndex"
labelBackendHealthCheckPath = "traefik.backend.healthcheck.path"
labelBackendHealthCheckInterval = "traefik.backend.healthcheck.interval"
traceMaxScanTokenSize = 1024 * 1024
traceMaxScanTokenSize = 1024 * 1024
)
var _ provider.Provider = (*Provider)(nil)
@ -222,15 +218,15 @@ func (p *Provider) taskFilter(task marathon.Task, applications *marathon.Applica
}
// Filter illegal port label specification.
_, hasPortIndexLabel := p.getLabel(application, labelPortIndex)
_, hasPortLabel := p.getLabel(application, labelPort)
_, hasPortIndexLabel := p.getLabel(application, types.LabelPortIndex)
_, hasPortLabel := p.getLabel(application, types.LabelPort)
if hasPortIndexLabel && hasPortLabel {
log.Debugf("Filtering Marathon task %s from application %s specifying both traefik.portIndex and traefik.port labels", task.ID, application.ID)
return false
}
// Filter by constraints.
label, _ := p.getLabel(application, "traefik.tags")
label, _ := p.getLabel(application, types.LabelTags)
constraintTags := strings.Split(label, ",")
if p.MarathonLBCompatibility {
if label, ok := p.getLabel(application, "HAPROXY_GROUP"); ok {
@ -266,7 +262,7 @@ func (p *Provider) taskFilter(task marathon.Task, applications *marathon.Applica
}
func (p *Provider) applicationFilter(app marathon.Application, filteredTasks []marathon.Task) bool {
label, _ := p.getLabel(app, "traefik.tags")
label, _ := p.getLabel(app, types.LabelTags)
constraintTags := strings.Split(label, ",")
if p.MarathonLBCompatibility {
if label, ok := p.getLabel(app, "HAPROXY_GROUP"); ok {
@ -295,7 +291,7 @@ func getApplication(task marathon.Task, apps []marathon.Application) (marathon.A
}
func isApplicationEnabled(application marathon.Application, exposedByDefault bool) bool {
return exposedByDefault && (*application.Labels)["traefik.enable"] != "false" || (*application.Labels)["traefik.enable"] == "true"
return exposedByDefault && (*application.Labels)[types.LabelEnable] != "false" || (*application.Labels)[types.LabelEnable] == "true"
}
func (p *Provider) getLabel(application marathon.Application, label string) (string, bool) {
@ -328,14 +324,14 @@ func (p *Provider) getWeight(task marathon.Task, applications []marathon.Applica
log.Errorf("Unable to get marathon application from task %s", task.AppID)
return "0"
}
if label, ok := p.getLabel(application, "traefik.weight"); ok {
if label, ok := p.getLabel(application, types.LabelWeight); ok {
return label
}
return "0"
}
func (p *Provider) getDomain(application marathon.Application) string {
if label, ok := p.getLabel(application, "traefik.domain"); ok {
if label, ok := p.getLabel(application, types.LabelDomain); ok {
return label
}
return p.Domain
@ -347,35 +343,35 @@ func (p *Provider) getProtocol(task marathon.Task, applications []marathon.Appli
log.Errorf("Unable to get marathon application from task %s", task.AppID)
return "http"
}
if label, ok := p.getLabel(application, "traefik.protocol"); ok {
if label, ok := p.getLabel(application, types.LabelProtocol); ok {
return label
}
return "http"
}
func (p *Provider) getSticky(application marathon.Application) string {
if sticky, ok := p.getLabel(application, "traefik.backend.loadbalancer.sticky"); ok {
if sticky, ok := p.getLabel(application, types.LabelBackendLoadbalancerSticky); ok {
return sticky
}
return "false"
}
func (p *Provider) getPassHostHeader(application marathon.Application) string {
if passHostHeader, ok := p.getLabel(application, "traefik.frontend.passHostHeader"); ok {
if passHostHeader, ok := p.getLabel(application, types.LabelFrontendPassHostHeader); ok {
return passHostHeader
}
return "true"
}
func (p *Provider) getPriority(application marathon.Application) string {
if priority, ok := p.getLabel(application, "traefik.frontend.priority"); ok {
if priority, ok := p.getLabel(application, types.LabelFrontendPriority); ok {
return priority
}
return "0"
}
func (p *Provider) getEntryPoints(application marathon.Application) []string {
if entryPoints, ok := p.getLabel(application, "traefik.frontend.entryPoints"); ok {
if entryPoints, ok := p.getLabel(application, types.LabelFrontendEntryPoints); ok {
return strings.Split(entryPoints, ",")
}
return []string{}
@ -384,7 +380,7 @@ func (p *Provider) getEntryPoints(application marathon.Application) []string {
// getFrontendRule returns the frontend rule for the specified application, using
// it's label. It returns a default one (Host) if the label is not present.
func (p *Provider) getFrontendRule(application marathon.Application) string {
if label, ok := p.getLabel(application, "traefik.frontend.rule"); ok {
if label, ok := p.getLabel(application, types.LabelFrontendRule); ok {
return label
}
if p.MarathonLBCompatibility {
@ -405,7 +401,7 @@ func (p *Provider) getBackend(task marathon.Task, applications []marathon.Applic
}
func (p *Provider) getFrontendBackend(application marathon.Application) string {
if label, ok := p.getLabel(application, "traefik.backend"); ok {
if label, ok := p.getLabel(application, types.LabelBackend); ok {
return label
}
return provider.Replace("/", "-", application.ID)
@ -422,26 +418,26 @@ func (p *Provider) getSubDomain(name string) string {
}
func (p *Provider) hasCircuitBreakerLabels(application marathon.Application) bool {
_, ok := p.getLabel(application, "traefik.backend.circuitbreaker.expression")
_, ok := p.getLabel(application, types.LabelBackendCircuitbreakerExpression)
return ok
}
func (p *Provider) hasLoadBalancerLabels(application marathon.Application) bool {
_, errMethod := p.getLabel(application, "traefik.backend.loadbalancer.method")
_, errSticky := p.getLabel(application, "traefik.backend.loadbalancer.sticky")
_, errMethod := p.getLabel(application, types.LabelBackendLoadbalancerMethod)
_, errSticky := p.getLabel(application, types.LabelBackendLoadbalancerSticky)
return errMethod || errSticky
}
func (p *Provider) hasMaxConnLabels(application marathon.Application) bool {
if _, ok := p.getLabel(application, "traefik.backend.maxconn.amount"); !ok {
if _, ok := p.getLabel(application, types.LabelBackendMaxconnAmount); !ok {
return false
}
_, ok := p.getLabel(application, "traefik.backend.maxconn.extractorfunc")
_, ok := p.getLabel(application, types.LabelBackendMaxconnExtractorfunc)
return ok
}
func (p *Provider) getMaxConnAmount(application marathon.Application) int64 {
if label, ok := p.getLabel(application, "traefik.backend.maxconn.amount"); ok {
if label, ok := p.getLabel(application, types.LabelBackendMaxconnAmount); ok {
i, errConv := strconv.ParseInt(label, 10, 64)
if errConv != nil {
log.Errorf("Unable to parse traefik.backend.maxconn.amount %s", label)
@ -453,21 +449,21 @@ func (p *Provider) getMaxConnAmount(application marathon.Application) int64 {
}
func (p *Provider) getMaxConnExtractorFunc(application marathon.Application) string {
if label, ok := p.getLabel(application, "traefik.backend.maxconn.extractorfunc"); ok {
if label, ok := p.getLabel(application, types.LabelBackendMaxconnExtractorfunc); ok {
return label
}
return "request.host"
}
func (p *Provider) getLoadBalancerMethod(application marathon.Application) string {
if label, ok := p.getLabel(application, "traefik.backend.loadbalancer.method"); ok {
if label, ok := p.getLabel(application, types.LabelBackendLoadbalancerMethod); ok {
return label
}
return "wrr"
}
func (p *Provider) getCircuitBreakerExpression(application marathon.Application) string {
if label, ok := p.getLabel(application, "traefik.backend.circuitbreaker.expression"); ok {
if label, ok := p.getLabel(application, types.LabelBackendCircuitbreakerExpression); ok {
return label
}
return "NetworkErrorRatio() > 1"
@ -478,21 +474,21 @@ func (p *Provider) hasHealthCheckLabels(application marathon.Application) bool {
}
func (p *Provider) getHealthCheckPath(application marathon.Application) string {
if label, ok := p.getLabel(application, labelBackendHealthCheckPath); ok {
if label, ok := p.getLabel(application, types.LabelBackendHealthcheckPath); ok {
return label
}
return ""
}
func (p *Provider) getHealthCheckInterval(application marathon.Application) string {
if label, ok := p.getLabel(application, labelBackendHealthCheckInterval); ok {
if label, ok := p.getLabel(application, types.LabelBackendHealthcheckInterval); ok {
return label
}
return ""
}
func (p *Provider) getBasicAuth(application marathon.Application) []string {
if basicAuth, ok := p.getLabel(application, "traefik.frontend.auth.basic"); ok {
if basicAuth, ok := p.getLabel(application, types.LabelFrontendAuthBasic); ok {
return strings.Split(basicAuth, ",")
}
@ -500,7 +496,7 @@ func (p *Provider) getBasicAuth(application marathon.Application) []string {
}
func processPorts(application marathon.Application, task marathon.Task) (int, error) {
if portLabel, ok := (*application.Labels)[labelPort]; ok {
if portLabel, ok := (*application.Labels)[types.LabelPort]; ok {
port, err := strconv.Atoi(portLabel)
switch {
case err != nil:
@ -517,7 +513,7 @@ func processPorts(application marathon.Application, task marathon.Task) (int, er
}
portIndex := 0
portIndexLabel, ok := (*application.Labels)[labelPortIndex]
portIndexLabel, ok := (*application.Labels)[types.LabelPortIndex]
if ok {
var err error
portIndex, err = parseIndex(portIndexLabel, len(ports))

View file

@ -126,8 +126,8 @@ func TestMarathonLoadConfig(t *testing.T) {
ID: "/testLoadBalancerAndCircuitBreaker.dot",
Ports: []int{80},
Labels: &map[string]string{
"traefik.backend.loadbalancer.method": "drr",
"traefik.backend.circuitbreaker.expression": "NetworkErrorRatio() > 0.5",
types.LabelBackendLoadbalancerMethod: "drr",
types.LabelBackendCircuitbreakerExpression: "NetworkErrorRatio() > 0.5",
},
},
},
@ -185,8 +185,8 @@ func TestMarathonLoadConfig(t *testing.T) {
ID: "/testMaxConn",
Ports: []int{80},
Labels: &map[string]string{
"traefik.backend.maxconn.amount": "1000",
"traefik.backend.maxconn.extractorfunc": "client.ip",
types.LabelBackendMaxconnAmount: "1000",
types.LabelBackendMaxconnExtractorfunc: "client.ip",
},
},
},
@ -242,7 +242,7 @@ func TestMarathonLoadConfig(t *testing.T) {
ID: "/testMaxConnOnlySpecifyAmount",
Ports: []int{80},
Labels: &map[string]string{
"traefik.backend.maxconn.amount": "1000",
types.LabelBackendMaxconnAmount: "1000",
},
},
},
@ -295,7 +295,7 @@ func TestMarathonLoadConfig(t *testing.T) {
ID: "/testMaxConnOnlyExtractorFunc",
Ports: []int{80},
Labels: &map[string]string{
"traefik.backend.maxconn.extractorfunc": "client.ip",
types.LabelBackendMaxconnExtractorfunc: "client.ip",
},
},
},
@ -348,8 +348,8 @@ func TestMarathonLoadConfig(t *testing.T) {
ID: "/testHealthCheck",
Ports: []int{80},
Labels: &map[string]string{
labelBackendHealthCheckPath: "/path",
labelBackendHealthCheckInterval: "5m",
types.LabelBackendHealthcheckPath: "/path",
types.LabelBackendHealthcheckInterval: "5m",
},
},
},
@ -513,7 +513,7 @@ func TestMarathonTaskFilter(t *testing.T) {
ID: "disable",
Ports: []int{80},
Labels: &map[string]string{
"traefik.enable": "false",
types.LabelEnable: "false",
},
},
},
@ -532,8 +532,8 @@ func TestMarathonTaskFilter(t *testing.T) {
ID: "specify-both-port-index-and-number",
Ports: []int{80, 443},
Labels: &map[string]string{
"traefik.port": "443",
"traefik.portIndex": "1",
types.LabelPort: "443",
types.LabelPortIndex: "1",
},
},
},
@ -667,7 +667,7 @@ func TestMarathonTaskFilter(t *testing.T) {
ID: "disable-default-expose-disable-in-label",
Ports: []int{80},
Labels: &map[string]string{
"traefik.enable": "false",
types.LabelEnable: "false",
},
},
},
@ -686,7 +686,7 @@ func TestMarathonTaskFilter(t *testing.T) {
ID: "disable-default-expose-enable-in-label",
Ports: []int{80},
Labels: &map[string]string{
"traefik.enable": "true",
types.LabelEnable: "true",
},
},
},
@ -729,7 +729,7 @@ func TestMarathonAppConstraints(t *testing.T) {
application: marathon.Application{
ID: "foo2",
Labels: &map[string]string{
"traefik.tags": "valid",
types.LabelTags: "valid",
},
},
filteredTasks: []marathon.Task{
@ -745,7 +745,7 @@ func TestMarathonAppConstraints(t *testing.T) {
ID: "foo3",
Labels: &map[string]string{
"HAPROXY_GROUP": "valid",
"traefik.tags": "notvalid",
types.LabelTags: "notvalid",
},
},
filteredTasks: []marathon.Task{
@ -786,7 +786,7 @@ func TestMarathonTaskConstraints(t *testing.T) {
}, {
ID: "foo1",
Labels: &map[string]string{
"traefik.tags": "other",
types.LabelTags: "other",
},
},
},
@ -802,7 +802,7 @@ func TestMarathonTaskConstraints(t *testing.T) {
{
ID: "foo2",
Labels: &map[string]string{
"traefik.tags": "valid",
types.LabelTags: "valid",
},
},
},
@ -819,13 +819,13 @@ func TestMarathonTaskConstraints(t *testing.T) {
ID: "foo3",
Labels: &map[string]string{
"HAPROXY_GROUP": "valid",
"traefik.tags": "notvalid",
types.LabelTags: "notvalid",
},
}, {
ID: "foo4",
Labels: &map[string]string{
"HAPROXY_GROUP": "notvalid",
"traefik.tags": "valid",
types.LabelTags: "valid",
},
},
},
@ -961,7 +961,7 @@ func TestMarathonGetPort(t *testing.T) {
{
ID: "app",
Labels: &map[string]string{
"traefik.port": "80",
types.LabelPort: "80",
},
},
},
@ -977,7 +977,7 @@ func TestMarathonGetPort(t *testing.T) {
{
ID: "app",
Labels: &map[string]string{
"traefik.port": "foobar",
types.LabelPort: "foobar",
},
},
},
@ -993,7 +993,7 @@ func TestMarathonGetPort(t *testing.T) {
{
ID: "app",
Labels: &map[string]string{
"traefik.port": "-1",
types.LabelPort: "-1",
},
},
},
@ -1102,7 +1102,7 @@ func TestMarathonGetPort(t *testing.T) {
{
ID: "app",
Labels: &map[string]string{
"traefik.portIndex": "1",
types.LabelPortIndex: "1",
},
},
},
@ -1118,7 +1118,7 @@ func TestMarathonGetPort(t *testing.T) {
{
ID: "app",
Labels: &map[string]string{
"traefik.portIndex": "foobar",
types.LabelPortIndex: "foobar",
},
},
},
@ -1175,7 +1175,7 @@ func TestMarathonGetWeight(t *testing.T) {
{
ID: "test1",
Labels: &map[string]string{
"traefik.weight": "10",
types.LabelWeight: "10",
},
},
},
@ -1203,7 +1203,7 @@ func TestMarathonGetWeight(t *testing.T) {
{
ID: "test",
Labels: &map[string]string{
"traefik.weight": "10",
types.LabelWeight: "10",
},
},
},
@ -1239,7 +1239,7 @@ func TestMarathonGetDomain(t *testing.T) {
{
application: marathon.Application{
Labels: &map[string]string{
"traefik.domain": "foo.bar",
types.LabelDomain: "foo.bar",
},
},
expected: "foo.bar",
@ -1272,7 +1272,7 @@ func TestMarathonGetProtocol(t *testing.T) {
{
ID: "test1",
Labels: &map[string]string{
"traefik.protocol": "https",
types.LabelProtocol: "https",
},
},
},
@ -1300,7 +1300,7 @@ func TestMarathonGetProtocol(t *testing.T) {
{
ID: "test",
Labels: &map[string]string{
"traefik.protocol": "https",
types.LabelProtocol: "https",
},
},
},
@ -1334,7 +1334,7 @@ func TestMarathonGetPassHostHeader(t *testing.T) {
{
application: marathon.Application{
Labels: &map[string]string{
"traefik.frontend.passHostHeader": "false",
types.LabelFrontendPassHostHeader: "false",
},
},
expected: "false",
@ -1364,7 +1364,7 @@ func TestMarathonGetEntryPoints(t *testing.T) {
{
application: marathon.Application{
Labels: &map[string]string{
"traefik.frontend.entryPoints": "http,https",
types.LabelFrontendEntryPoints: "http,https",
},
},
expected: []string{"http", "https"},
@ -1405,7 +1405,7 @@ func TestMarathonGetFrontendRule(t *testing.T) {
{
application: marathon.Application{
Labels: &map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
"HAPROXY_0_VHOST": "notvalid",
},
},
@ -1446,7 +1446,7 @@ func TestMarathonGetBackend(t *testing.T) {
application: marathon.Application{
ID: "foo",
Labels: &map[string]string{
"traefik.backend": "bar",
types.LabelBackend: "bar",
},
},
expected: "bar",
@ -1522,7 +1522,7 @@ func TestMarathonHasHealthCheckLabels(t *testing.T) {
Labels: &map[string]string{},
}
if test.value != nil {
app.AddLabel(labelBackendHealthCheckPath, *test.value)
app.AddLabel(types.LabelBackendHealthcheckPath, *test.value)
}
prov := &Provider{}
got := prov.hasHealthCheckLabels(app)
@ -1558,7 +1558,7 @@ func TestMarathonGetHealthCheckPath(t *testing.T) {
app := marathon.Application{}
app.EmptyLabels()
if test.value != nil {
app.AddLabel(labelBackendHealthCheckPath, *test.value)
app.AddLabel(types.LabelBackendHealthcheckPath, *test.value)
}
prov := &Provider{}
got := prov.getHealthCheckPath(app)
@ -1595,7 +1595,7 @@ func TestMarathonGetHealthCheckInterval(t *testing.T) {
Labels: &map[string]string{},
}
if test.value != nil {
app.AddLabel(labelBackendHealthCheckInterval, *test.value)
app.AddLabel(types.LabelBackendHealthcheckInterval, *test.value)
}
prov := &Provider{}
got := prov.getHealthCheckInterval(app)
@ -1825,7 +1825,7 @@ func TestMarathonGetBasicAuth(t *testing.T) {
desc: "basic auth label is set with user:password",
application: marathon.Application{
Labels: &map[string]string{
"traefik.frontend.auth.basic": "user:password",
types.LabelFrontendAuthBasic: "user:password",
},
},
expected: []string{"user:password"},

View file

@ -206,21 +206,21 @@ func mesosTaskFilter(task state.Task, exposedByDefaultFlag bool) bool {
}
//filter indeterminable task port
portIndexLabel := labels(task, "traefik.portIndex")
portValueLabel := labels(task, "traefik.port")
portIndexLabel := labels(task, types.LabelPortIndex)
portValueLabel := labels(task, types.LabelPort)
if portIndexLabel != "" && portValueLabel != "" {
log.Debugf("Filtering Mesos task %s specifying both traefik.portIndex and traefik.port labels", task.Name)
return false
}
if portIndexLabel != "" {
index, err := strconv.Atoi(labels(task, "traefik.portIndex"))
index, err := strconv.Atoi(labels(task, types.LabelPortIndex))
if err != nil || index < 0 || index > len(task.DiscoveryInfo.Ports.DiscoveryPorts)-1 {
log.Debugf("Filtering Mesos task %s with unexpected value for traefik.portIndex label", task.Name)
return false
}
}
if portValueLabel != "" {
port, err := strconv.Atoi(labels(task, "traefik.port"))
port, err := strconv.Atoi(labels(task, types.LabelPort))
if err != nil {
log.Debugf("Filtering Mesos task %s with unexpected value for traefik.port label", task.Name)
return false
@ -259,7 +259,7 @@ func getMesos(task state.Task, apps []state.Task) (state.Task, error) {
}
func isMesosApplicationEnabled(task state.Task, exposedByDefault bool) bool {
return exposedByDefault && labels(task, "traefik.enable") != "false" || labels(task, "traefik.enable") == "true"
return exposedByDefault && labels(task, types.LabelEnable) != "false" || labels(task, types.LabelEnable) == "true"
}
func (p *Provider) getLabel(task state.Task, label string) (string, error) {
@ -278,12 +278,12 @@ func (p *Provider) getPort(task state.Task, applications []state.Task) string {
return ""
}
if portIndexLabel, err := p.getLabel(application, "traefik.portIndex"); err == nil {
if portIndexLabel, err := p.getLabel(application, types.LabelPortIndex); err == nil {
if index, err := strconv.Atoi(portIndexLabel); err == nil {
return strconv.Itoa(task.DiscoveryInfo.Ports.DiscoveryPorts[index].Number)
}
}
if portValueLabel, err := p.getLabel(application, "traefik.port"); err == nil {
if portValueLabel, err := p.getLabel(application, types.LabelPort); err == nil {
return portValueLabel
}
@ -300,14 +300,14 @@ func (p *Provider) getWeight(task state.Task, applications []state.Task) string
return "0"
}
if label, err := p.getLabel(application, "traefik.weight"); err == nil {
if label, err := p.getLabel(application, types.LabelWeight); err == nil {
return label
}
return "0"
}
func (p *Provider) getDomain(task state.Task) string {
if label, err := p.getLabel(task, "traefik.domain"); err == nil {
if label, err := p.getLabel(task, types.LabelDomain); err == nil {
return label
}
return p.Domain
@ -319,28 +319,28 @@ func (p *Provider) getProtocol(task state.Task, applications []state.Task) strin
log.Errorf("Unable to get Mesos application from task %s", task.DiscoveryInfo.Name)
return "http"
}
if label, err := p.getLabel(application, "traefik.protocol"); err == nil {
if label, err := p.getLabel(application, types.LabelProtocol); err == nil {
return label
}
return "http"
}
func (p *Provider) getPassHostHeader(task state.Task) string {
if passHostHeader, err := p.getLabel(task, "traefik.frontend.passHostHeader"); err == nil {
if passHostHeader, err := p.getLabel(task, types.LabelFrontendPassHostHeader); err == nil {
return passHostHeader
}
return "false"
}
func (p *Provider) getPriority(task state.Task) string {
if priority, err := p.getLabel(task, "traefik.frontend.priority"); err == nil {
if priority, err := p.getLabel(task, types.LabelFrontendPriority); err == nil {
return priority
}
return "0"
}
func (p *Provider) getEntryPoints(task state.Task) []string {
if entryPoints, err := p.getLabel(task, "traefik.frontend.entryPoints"); err == nil {
if entryPoints, err := p.getLabel(task, types.LabelFrontendEntryPoints); err == nil {
return strings.Split(entryPoints, ",")
}
return []string{}
@ -349,7 +349,7 @@ func (p *Provider) getEntryPoints(task state.Task) []string {
// getFrontendRule returns the frontend rule for the specified application, using
// it's label. It returns a default one (Host) if the label is not present.
func (p *Provider) getFrontendRule(task state.Task) string {
if label, err := p.getLabel(task, "traefik.frontend.rule"); err == nil {
if label, err := p.getLabel(task, types.LabelFrontendRule); err == nil {
return label
}
return "Host:" + strings.ToLower(strings.Replace(p.getSubDomain(task.DiscoveryInfo.Name), "_", "-", -1)) + "." + p.Domain
@ -365,7 +365,7 @@ func (p *Provider) getBackend(task state.Task, applications []state.Task) string
}
func (p *Provider) getFrontendBackend(task state.Task) string {
if label, err := p.getLabel(task, "traefik.backend"); err == nil {
if label, err := p.getLabel(task, types.LabelBackend); err == nil {
return label
}
return "-" + cleanupSpecialChars(task.DiscoveryInfo.Name)

View file

@ -30,7 +30,7 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "false"),
setLabels(types.LabelEnable, "false"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: false, // because label traefik.enable = false
@ -40,7 +40,7 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true"),
setLabels(types.LabelEnable, "true"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: true,
@ -50,7 +50,7 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true"),
setLabels(types.LabelEnable, "true"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: true,
@ -60,7 +60,7 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "false"),
setLabels(types.LabelEnable, "false"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: false, // because label traefik.enable = false (even wherek exposedByDefault = true)
@ -70,9 +70,9 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true",
"traefik.portIndex", "1",
"traefik.port", "80"),
setLabels(types.LabelEnable, "true",
types.LabelPortIndex, "1",
types.LabelPort, "80"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: false, // traefik.portIndex & traefik.port cannot be set both
@ -82,8 +82,8 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true",
"traefik.portIndex", "1"),
setLabels(types.LabelEnable, "true",
types.LabelPortIndex, "1"),
discovery(setDiscoveryPorts("TCP", 80, "WEB HTTP", "TCP", 443, "WEB HTTPS")),
),
expected: true,
@ -93,7 +93,7 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true"),
setLabels(types.LabelEnable, "true"),
discovery(setDiscoveryPorts("TCP", 80, "WEB HTTP", "TCP", 443, "WEB HTTPS")),
),
expected: true, // Default to first index
@ -103,8 +103,8 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true",
"traefik.portIndex", "1"),
setLabels(types.LabelEnable, "true",
types.LabelPortIndex, "1"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: false, // traefik.portIndex and discoveryPorts don't correspond
@ -113,8 +113,8 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true",
"traefik.portIndex", "0"),
setLabels(types.LabelEnable, "true",
types.LabelPortIndex, "0"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: true, // traefik.portIndex and discoveryPorts correspond
@ -123,8 +123,8 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true",
"traefik.port", "TRAEFIK"),
setLabels(types.LabelEnable, "true",
types.LabelPort, "TRAEFIK"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: false, // traefik.port is not an integer
@ -133,8 +133,8 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true",
"traefik.port", "443"),
setLabels(types.LabelEnable, "true",
types.LabelPort, "443"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: false, // traefik.port is not the same as discovery.port
@ -143,8 +143,8 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(true))),
setLabels("traefik.enable", "true",
"traefik.port", "80"),
setLabels(types.LabelEnable, "true",
types.LabelPort, "80"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: true, // traefik.port is the same as discovery.port
@ -152,8 +152,8 @@ func TestMesosTaskFilter(t *testing.T) {
}, {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"))),
setLabels("traefik.enable", "true",
"traefik.port", "80"),
setLabels(types.LabelEnable, "true",
types.LabelPort, "80"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: true, // No healthCheck
@ -162,8 +162,8 @@ func TestMesosTaskFilter(t *testing.T) {
mesosTask: task(statuses(status(
setState("TASK_RUNNING"),
setHealthy(false))),
setLabels("traefik.enable", "true",
"traefik.port", "80"),
setLabels(types.LabelEnable, "true",
types.LabelPort, "80"),
discovery(setDiscoveryPort("TCP", 80, "WEB")),
),
expected: false, // HealthCheck at false

View file

@ -10,13 +10,12 @@ import (
"github.com/containous/traefik/log"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
rancher "github.com/rancher/go-rancher/client"
)
var (
withoutPagination *rancher.ListOpts
)
const labelRancheStackServiceName = "io.rancher.stack_service.name"
var withoutPagination *rancher.ListOpts
// APIConfiguration contains configuration properties specific to the Rancher
// API provider.
@ -221,7 +220,7 @@ func parseAPISourcedRancherData(environments []*rancher.Project, services []*ran
}
for _, container := range containers {
if container.Labels["io.rancher.stack_service.name"] == rancherData.Name &&
if container.Labels[labelRancheStackServiceName] == rancherData.Name &&
containerFilter(container.Name, container.HealthState, container.State) {
rancherData.Containers = append(rancherData.Containers, container.PrimaryIpAddress)
}

View file

@ -42,35 +42,35 @@ func (r rancherData) String() string {
// Frontend Labels
func (p *Provider) getPassHostHeader(service rancherData) string {
if passHostHeader, err := getServiceLabel(service, "traefik.frontend.passHostHeader"); err == nil {
if passHostHeader, err := getServiceLabel(service, types.LabelFrontendPassHostHeader); err == nil {
return passHostHeader
}
return "true"
}
func (p *Provider) getPriority(service rancherData) string {
if priority, err := getServiceLabel(service, "traefik.frontend.priority"); err == nil {
if priority, err := getServiceLabel(service, types.LabelFrontendPriority); err == nil {
return priority
}
return "0"
}
func (p *Provider) getEntryPoints(service rancherData) []string {
if entryPoints, err := getServiceLabel(service, "traefik.frontend.entryPoints"); err == nil {
if entryPoints, err := getServiceLabel(service, types.LabelFrontendEntryPoints); err == nil {
return strings.Split(entryPoints, ",")
}
return []string{}
}
func (p *Provider) getFrontendRule(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.frontend.rule"); err == nil {
if label, err := getServiceLabel(service, types.LabelFrontendRule); err == nil {
return label
}
return "Host:" + strings.ToLower(strings.Replace(service.Name, "/", ".", -1)) + "." + p.Domain
}
func (p *Provider) getBasicAuth(service rancherData) []string {
if basicAuth, err := getServiceLabel(service, "traefik.frontend.auth.basic"); err == nil {
if basicAuth, err := getServiceLabel(service, types.LabelFrontendAuthBasic); err == nil {
return strings.Split(basicAuth, ",")
}
return []string{}
@ -83,15 +83,15 @@ func (p *Provider) getFrontendName(service rancherData) string {
// Backend Labels
func (p *Provider) getLoadBalancerMethod(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.backend.loadbalancer.method"); err == nil {
if label, err := getServiceLabel(service, types.LabelBackendLoadbalancerMethod); err == nil {
return label
}
return "wrr"
}
func (p *Provider) hasLoadBalancerLabel(service rancherData) bool {
_, errMethod := getServiceLabel(service, "traefik.backend.loadbalancer.method")
_, errSticky := getServiceLabel(service, "traefik.backend.loadbalancer.sticky")
_, errMethod := getServiceLabel(service, types.LabelBackendLoadbalancerMethod)
_, errSticky := getServiceLabel(service, types.LabelBackendLoadbalancerSticky)
if errMethod != nil && errSticky != nil {
return false
}
@ -99,28 +99,28 @@ func (p *Provider) hasLoadBalancerLabel(service rancherData) bool {
}
func (p *Provider) hasCircuitBreakerLabel(service rancherData) bool {
if _, err := getServiceLabel(service, "traefik.backend.circuitbreaker.expression"); err != nil {
if _, err := getServiceLabel(service, types.LabelBackendCircuitbreakerExpression); err != nil {
return false
}
return true
}
func (p *Provider) getCircuitBreakerExpression(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.backend.circuitbreaker.expression"); err == nil {
if label, err := getServiceLabel(service, types.LabelBackendCircuitbreakerExpression); err == nil {
return label
}
return "NetworkErrorRatio() > 1"
}
func (p *Provider) getSticky(service rancherData) string {
if _, err := getServiceLabel(service, "traefik.backend.loadbalancer.sticky"); err == nil {
if _, err := getServiceLabel(service, types.LabelBackendLoadbalancerSticky); err == nil {
return "true"
}
return "false"
}
func (p *Provider) getBackend(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.backend"); err == nil {
if label, err := getServiceLabel(service, types.LabelBackend); err == nil {
return provider.Normalize(label)
}
return provider.Normalize(service.Name)
@ -128,48 +128,48 @@ func (p *Provider) getBackend(service rancherData) string {
// General Application Stuff
func (p *Provider) getPort(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.port"); err == nil {
if label, err := getServiceLabel(service, types.LabelPort); err == nil {
return label
}
return ""
}
func (p *Provider) getProtocol(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.protocol"); err == nil {
if label, err := getServiceLabel(service, types.LabelProtocol); err == nil {
return label
}
return "http"
}
func (p *Provider) getWeight(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.weight"); err == nil {
if label, err := getServiceLabel(service, types.LabelWeight); err == nil {
return label
}
return "0"
}
func (p *Provider) getDomain(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.domain"); err == nil {
if label, err := getServiceLabel(service, types.LabelDomain); err == nil {
return label
}
return p.Domain
}
func (p *Provider) hasMaxConnLabels(service rancherData) bool {
if _, err := getServiceLabel(service, "traefik.backend.maxconn.amount"); err != nil {
if _, err := getServiceLabel(service, types.LabelBackendMaxconnAmount); err != nil {
return false
}
if _, err := getServiceLabel(service, "traefik.backend.maxconn.extractorfunc"); err != nil {
if _, err := getServiceLabel(service, types.LabelBackendMaxconnExtractorfunc); err != nil {
return false
}
return true
}
func (p *Provider) getMaxConnAmount(service rancherData) int64 {
if label, err := getServiceLabel(service, "traefik.backend.maxconn.amount"); err == nil {
if label, err := getServiceLabel(service, types.LabelBackendMaxconnAmount); err == nil {
i, errConv := strconv.ParseInt(label, 10, 64)
if errConv != nil {
log.Errorf("Unable to parse traefik.backend.maxconn.amount %s", label)
log.Errorf("Unable to parse %s %s", types.LabelBackendMaxconnAmount, label)
return math.MaxInt64
}
return i
@ -178,7 +178,7 @@ func (p *Provider) getMaxConnAmount(service rancherData) int64 {
}
func (p *Provider) getMaxConnExtractorFunc(service rancherData) string {
if label, err := getServiceLabel(service, "traefik.backend.maxconn.extractorfunc"); err == nil {
if label, err := getServiceLabel(service, types.LabelBackendMaxconnExtractorfunc); err == nil {
return label
}
return "request.host"
@ -274,7 +274,7 @@ func containerFilter(name, healthState, state string) bool {
func (p *Provider) serviceFilter(service rancherData) bool {
if service.Labels["traefik.port"] == "" {
if service.Labels[types.LabelPort] == "" {
log.Debugf("Filtering service %s without traefik.port label", service.Name)
return false
}
@ -284,7 +284,7 @@ func (p *Provider) serviceFilter(service rancherData) bool {
return false
}
constraintTags := strings.Split(service.Labels["traefik.tags"], ",")
constraintTags := strings.Split(service.Labels[types.LabelTags], ",")
if ok, failingConstraint := p.MatchConstraints(constraintTags); !ok {
if failingConstraint != nil {
log.Debugf("Filtering service %s with constraint %s", service.Name, failingConstraint.String())
@ -311,8 +311,8 @@ func (p *Provider) serviceFilter(service rancherData) bool {
func isServiceEnabled(service rancherData, exposedByDefault bool) bool {
if service.Labels["traefik.enable"] != "" {
var v = service.Labels["traefik.enable"]
if service.Labels[types.LabelEnable] != "" {
var v = service.Labels[types.LabelEnable]
return exposedByDefault && v != "false" || v == "true"
}
return exposedByDefault

View file

@ -24,7 +24,7 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.enable": "true",
types.LabelEnable: "true",
},
Health: "healthy",
State: "active",
@ -34,8 +34,8 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.port": "80",
"traefik.enable": "false",
types.LabelPort: "80",
types.LabelEnable: "false",
},
Health: "healthy",
State: "active",
@ -45,8 +45,8 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.port": "80",
"traefik.enable": "true",
types.LabelPort: "80",
types.LabelEnable: "true",
},
Health: "unhealthy",
State: "active",
@ -56,9 +56,9 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.tags": "not-cheesy",
"traefik.port": "80",
"traefik.enable": "true",
types.LabelTags: "not-cheesy",
types.LabelPort: "80",
types.LabelEnable: "true",
},
Health: "healthy",
State: "inactive",
@ -68,9 +68,9 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.tags": "cheese",
"traefik.port": "80",
"traefik.enable": "true",
types.LabelTags: "cheese",
types.LabelPort: "80",
types.LabelEnable: "true",
},
Health: "healthy",
State: "active",
@ -80,9 +80,9 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.tags": "cheeeeese",
"traefik.port": "80",
"traefik.enable": "true",
types.LabelTags: "cheeeeese",
types.LabelPort: "80",
types.LabelEnable: "true",
},
Health: "healthy",
State: "upgraded",
@ -92,9 +92,9 @@ func TestRancherServiceFilter(t *testing.T) {
{
service: rancherData{
Labels: map[string]string{
"traefik.tags": "chose",
"traefik.port": "80",
"traefik.enable": "true",
types.LabelTags: "chose",
types.LabelPort: "80",
types.LabelEnable: "true",
},
Health: "healthy",
State: "active",
@ -171,7 +171,7 @@ func TestRancherGetFrontendName(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.rule": "Headers:User-Agent,bat/0.1.0",
types.LabelFrontendRule: "Headers:User-Agent,bat/0.1.0",
},
},
@ -181,7 +181,7 @@ func TestRancherGetFrontendName(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.rule": "Host:foo.bar",
types.LabelFrontendRule: "Host:foo.bar",
},
},
@ -191,7 +191,7 @@ func TestRancherGetFrontendName(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.rule": "Path:/test",
types.LabelFrontendRule: "Path:/test",
},
},
@ -201,7 +201,7 @@ func TestRancherGetFrontendName(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.rule": "PathPrefix:/test2",
types.LabelFrontendRule: "PathPrefix:/test2",
},
},
@ -242,7 +242,7 @@ func TestRancherGetFrontendRule(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.rule": "Host:foo.bar.com",
types.LabelFrontendRule: "Host:foo.bar.com",
},
},
@ -252,7 +252,7 @@ func TestRancherGetFrontendRule(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.rule": "Path:/test",
types.LabelFrontendRule: "Path:/test",
},
},
@ -262,7 +262,7 @@ func TestRancherGetFrontendRule(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.rule": "PathPrefix:/test2",
types.LabelFrontendRule: "PathPrefix:/test2",
},
},
@ -297,7 +297,7 @@ func TestRancherGetBackend(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.backend": "foobar",
types.LabelBackend: "foobar",
},
},
@ -332,7 +332,7 @@ func TestRancherGetWeight(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.weight": "5",
types.LabelWeight: "5",
},
},
@ -367,7 +367,7 @@ func TestRancherGetPort(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.port": "1337",
types.LabelPort: "1337",
},
},
@ -402,7 +402,7 @@ func TestRancherGetDomain(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.domain": "foo.bar",
types.LabelDomain: "foo.bar",
},
},
@ -437,7 +437,7 @@ func TestRancherGetProtocol(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.protocol": "https",
types.LabelProtocol: "https",
},
},
@ -472,7 +472,7 @@ func TestRancherGetPassHostHeader(t *testing.T) {
service: rancherData{
Name: "test-service",
Labels: map[string]string{
"traefik.frontend.passHostHeader": "false",
types.LabelFrontendPassHostHeader: "false",
},
},
@ -541,8 +541,8 @@ func TestRancherLoadRancherConfig(t *testing.T) {
{
Name: "test/service",
Labels: map[string]string{
"traefik.port": "80",
"traefik.frontend.auth.basic": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
types.LabelPort: "80",
types.LabelFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
},
Health: "healthy",
Containers: []string{"127.0.0.1"},

54
types/common_label.go Normal file
View file

@ -0,0 +1,54 @@
package types
const (
// LabelDomain Traefik label
LabelDomain = "traefik.domain"
// LabelEnable Traefik label
LabelEnable = "traefik.enable"
// LabelPort Traefik label
LabelPort = "traefik.port"
// LabelPortIndex Traefik label
LabelPortIndex = "traefik.portIndex"
// LabelProtocol Traefik label
LabelProtocol = "traefik.protocol"
// LabelTags Traefik label
LabelTags = "traefik.tags"
// LabelWeight Traefik label
LabelWeight = "traefik.weight"
// LabelFrontendAuthBasic Traefik label
LabelFrontendAuthBasic = "traefik.frontend.auth.basic"
// LabelFrontendEntryPoints Traefik label
LabelFrontendEntryPoints = "traefik.frontend.entryPoints"
// LabelFrontendPassHostHeader Traefik label
LabelFrontendPassHostHeader = "traefik.frontend.passHostHeader"
// LabelFrontendPriority Traefik label
LabelFrontendPriority = "traefik.frontend.priority"
// LabelFrontendRule Traefik label
LabelFrontendRule = "traefik.frontend.rule"
// LabelFrontendRuleType Traefik label
LabelFrontendRuleType = "traefik.frontend.rule.type"
// LabelTraefikFrontendValue Traefik label
LabelTraefikFrontendValue = "traefik.frontend.value"
// LabelTraefikFrontendWhitelistSourceRange Traefik label
LabelTraefikFrontendWhitelistSourceRange = "traefik.frontend.whitelistSourceRange"
// LabelBackend Traefik label
LabelBackend = "traefik.backend"
// LabelBackendID Traefik label
LabelBackendID = "traefik.backend.id"
// LabelTraefikBackendCircuitbreaker Traefik label
LabelTraefikBackendCircuitbreaker = "traefik.backend.circuitbreaker"
// LabelBackendCircuitbreakerExpression Traefik label
LabelBackendCircuitbreakerExpression = "traefik.backend.circuitbreaker.expression"
// LabelBackendHealthcheckPath Traefik label
LabelBackendHealthcheckPath = "traefik.backend.healthcheck.path"
// LabelBackendHealthcheckInterval Traefik label
LabelBackendHealthcheckInterval = "traefik.backend.healthcheck.interval"
// LabelBackendLoadbalancerMethod Traefik label
LabelBackendLoadbalancerMethod = "traefik.backend.loadbalancer.method"
// LabelBackendLoadbalancerSticky Traefik label
LabelBackendLoadbalancerSticky = "traefik.backend.loadbalancer.sticky"
// LabelBackendMaxconnAmount Traefik label
LabelBackendMaxconnAmount = "traefik.backend.maxconn.amount"
// LabelBackendMaxconnExtractorfunc Traefik label
LabelBackendMaxconnExtractorfunc = "traefik.backend.maxconn.extractorfunc"
)