Support Nomad canary deployment
Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com> Co-authored-by: Mathieu Lonjaret <mathieu.lonjaret@gmail.com>
This commit is contained in:
parent
ab94bbaece
commit
2a2ea759d1
10 changed files with 593 additions and 51 deletions
|
@ -4,8 +4,11 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
||||
"github.com/traefik/traefik/v2/pkg/config/label"
|
||||
|
@ -76,7 +79,7 @@ func (p *Provider) buildConfig(ctx context.Context, items []item) *dynamic.Confi
|
|||
Labels: labels,
|
||||
}
|
||||
|
||||
provider.BuildRouterConfiguration(ctx, config.HTTP, provider.Normalize(i.Name), p.defaultRuleTpl, model)
|
||||
provider.BuildRouterConfiguration(ctx, config.HTTP, getName(i), p.defaultRuleTpl, model)
|
||||
configurations[svcName] = config
|
||||
}
|
||||
|
||||
|
@ -90,7 +93,7 @@ func (p *Provider) buildTCPConfig(i item, configuration *dynamic.TCPConfiguratio
|
|||
lb := new(dynamic.TCPServersLoadBalancer)
|
||||
lb.SetDefaults()
|
||||
|
||||
configuration.Services[provider.Normalize(i.Name)] = &dynamic.TCPService{
|
||||
configuration.Services[getName(i)] = &dynamic.TCPService{
|
||||
LoadBalancer: lb,
|
||||
}
|
||||
}
|
||||
|
@ -108,7 +111,7 @@ func (p *Provider) buildUDPConfig(i item, configuration *dynamic.UDPConfiguratio
|
|||
if len(configuration.Services) == 0 {
|
||||
configuration.Services = make(map[string]*dynamic.UDPService)
|
||||
|
||||
configuration.Services[provider.Normalize(i.Name)] = &dynamic.UDPService{
|
||||
configuration.Services[getName(i)] = &dynamic.UDPService{
|
||||
LoadBalancer: new(dynamic.UDPServersLoadBalancer),
|
||||
}
|
||||
}
|
||||
|
@ -129,7 +132,7 @@ func (p *Provider) buildServiceConfig(i item, configuration *dynamic.HTTPConfigu
|
|||
lb := new(dynamic.ServersLoadBalancer)
|
||||
lb.SetDefaults()
|
||||
|
||||
configuration.Services[provider.Normalize(i.Name)] = &dynamic.Service{
|
||||
configuration.Services[getName(i)] = &dynamic.Service{
|
||||
LoadBalancer: lb,
|
||||
}
|
||||
}
|
||||
|
@ -265,3 +268,18 @@ func (p *Provider) addServer(i item, lb *dynamic.ServersLoadBalancer) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getName(i item) string {
|
||||
if !i.ExtraConf.Canary {
|
||||
return provider.Normalize(i.Name)
|
||||
}
|
||||
|
||||
tags := make([]string, len(i.Tags))
|
||||
copy(tags, i.Tags)
|
||||
|
||||
sort.Strings(tags)
|
||||
|
||||
hasher := fnv.New64()
|
||||
hasher.Write([]byte(strings.Join(tags, "")))
|
||||
return provider.Normalize(fmt.Sprintf("%s-%d", i.Name, hasher.Sum64()))
|
||||
}
|
||||
|
|
|
@ -2209,6 +2209,239 @@ func Test_buildConfig(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "two HTTP service instances with one canary",
|
||||
items: []item{
|
||||
{
|
||||
ID: "1",
|
||||
Node: "Node1",
|
||||
Datacenter: "dc1",
|
||||
Name: "Test",
|
||||
Namespace: "ns",
|
||||
Tags: []string{},
|
||||
Address: "127.0.0.1",
|
||||
Port: 80,
|
||||
ExtraConf: configuration{Enable: true},
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Node: "Node1",
|
||||
Datacenter: "dc1",
|
||||
Name: "Test",
|
||||
Namespace: "ns",
|
||||
Tags: []string{
|
||||
"traefik.nomad.canary = true",
|
||||
},
|
||||
Address: "127.0.0.2",
|
||||
Port: 80,
|
||||
ExtraConf: configuration{
|
||||
Enable: true,
|
||||
Canary: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &dynamic.Configuration{
|
||||
TCP: &dynamic.TCPConfiguration{
|
||||
Routers: map[string]*dynamic.TCPRouter{},
|
||||
Middlewares: map[string]*dynamic.TCPMiddleware{},
|
||||
Services: map[string]*dynamic.TCPService{},
|
||||
},
|
||||
UDP: &dynamic.UDPConfiguration{
|
||||
Routers: map[string]*dynamic.UDPRouter{},
|
||||
Services: map[string]*dynamic.UDPService{},
|
||||
},
|
||||
HTTP: &dynamic.HTTPConfiguration{
|
||||
Routers: map[string]*dynamic.Router{
|
||||
"Test": {
|
||||
Service: "Test",
|
||||
Rule: "Host(`Test.traefik.test`)",
|
||||
},
|
||||
"Test-1234154071633021619": {
|
||||
Service: "Test-1234154071633021619",
|
||||
Rule: "Host(`Test.traefik.test`)",
|
||||
},
|
||||
},
|
||||
Middlewares: map[string]*dynamic.Middleware{},
|
||||
Services: map[string]*dynamic.Service{
|
||||
"Test": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.1:80",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
},
|
||||
},
|
||||
"Test-1234154071633021619": {
|
||||
LoadBalancer: &dynamic.ServersLoadBalancer{
|
||||
Servers: []dynamic.Server{
|
||||
{
|
||||
URL: "http://127.0.0.2:80",
|
||||
},
|
||||
},
|
||||
PassHostHeader: Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
ServersTransports: map[string]*dynamic.ServersTransport{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "two TCP service instances with one canary",
|
||||
items: []item{
|
||||
{
|
||||
ID: "1",
|
||||
Node: "Node1",
|
||||
Datacenter: "dc1",
|
||||
Name: "Test",
|
||||
Namespace: "ns",
|
||||
Tags: []string{
|
||||
"traefik.tcp.routers.test.rule = HostSNI(`foobar`)",
|
||||
},
|
||||
Address: "127.0.0.1",
|
||||
Port: 80,
|
||||
ExtraConf: configuration{Enable: true},
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Node: "Node1",
|
||||
Datacenter: "dc1",
|
||||
Name: "Test",
|
||||
Namespace: "ns",
|
||||
Tags: []string{
|
||||
"traefik.nomad.canary = true",
|
||||
"traefik.tcp.routers.test-canary.rule = HostSNI(`canary.foobar`)",
|
||||
},
|
||||
Address: "127.0.0.2",
|
||||
Port: 80,
|
||||
ExtraConf: configuration{
|
||||
Enable: true,
|
||||
Canary: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &dynamic.Configuration{
|
||||
TCP: &dynamic.TCPConfiguration{
|
||||
Routers: map[string]*dynamic.TCPRouter{
|
||||
"test": {
|
||||
Service: "Test",
|
||||
Rule: "HostSNI(`foobar`)",
|
||||
},
|
||||
"test-canary": {
|
||||
Service: "Test-8769860286750522282",
|
||||
Rule: "HostSNI(`canary.foobar`)",
|
||||
},
|
||||
},
|
||||
Middlewares: map[string]*dynamic.TCPMiddleware{},
|
||||
Services: map[string]*dynamic.TCPService{
|
||||
"Test": {
|
||||
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
||||
Servers: []dynamic.TCPServer{
|
||||
{Address: "127.0.0.1:80"},
|
||||
},
|
||||
TerminationDelay: Int(100),
|
||||
},
|
||||
},
|
||||
"Test-8769860286750522282": {
|
||||
LoadBalancer: &dynamic.TCPServersLoadBalancer{
|
||||
Servers: []dynamic.TCPServer{
|
||||
{Address: "127.0.0.2:80"},
|
||||
},
|
||||
TerminationDelay: Int(100),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
UDP: &dynamic.UDPConfiguration{
|
||||
Routers: map[string]*dynamic.UDPRouter{},
|
||||
Services: map[string]*dynamic.UDPService{},
|
||||
},
|
||||
HTTP: &dynamic.HTTPConfiguration{
|
||||
Routers: map[string]*dynamic.Router{},
|
||||
Middlewares: map[string]*dynamic.Middleware{},
|
||||
Services: map[string]*dynamic.Service{},
|
||||
ServersTransports: map[string]*dynamic.ServersTransport{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "two UDP service instances with one canary",
|
||||
items: []item{
|
||||
{
|
||||
ID: "1",
|
||||
Node: "Node1",
|
||||
Datacenter: "dc1",
|
||||
Name: "Test",
|
||||
Namespace: "ns",
|
||||
Tags: []string{
|
||||
"traefik.udp.routers.test.entrypoints = udp",
|
||||
},
|
||||
Address: "127.0.0.1",
|
||||
Port: 80,
|
||||
ExtraConf: configuration{Enable: true},
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Node: "Node1",
|
||||
Datacenter: "dc1",
|
||||
Name: "Test",
|
||||
Namespace: "ns",
|
||||
Tags: []string{
|
||||
"traefik.nomad.canary = true",
|
||||
"traefik.udp.routers.test-canary.entrypoints = udp",
|
||||
},
|
||||
Address: "127.0.0.2",
|
||||
Port: 80,
|
||||
ExtraConf: configuration{
|
||||
Enable: true,
|
||||
Canary: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: &dynamic.Configuration{
|
||||
TCP: &dynamic.TCPConfiguration{
|
||||
Routers: map[string]*dynamic.TCPRouter{},
|
||||
Middlewares: map[string]*dynamic.TCPMiddleware{},
|
||||
Services: map[string]*dynamic.TCPService{},
|
||||
},
|
||||
UDP: &dynamic.UDPConfiguration{
|
||||
Routers: map[string]*dynamic.UDPRouter{
|
||||
"test": {
|
||||
EntryPoints: []string{"udp"},
|
||||
Service: "Test",
|
||||
},
|
||||
"test-canary": {
|
||||
EntryPoints: []string{"udp"},
|
||||
Service: "Test-1611429260986126224",
|
||||
},
|
||||
},
|
||||
Services: map[string]*dynamic.UDPService{
|
||||
"Test": {
|
||||
LoadBalancer: &dynamic.UDPServersLoadBalancer{
|
||||
Servers: []dynamic.UDPServer{
|
||||
{Address: "127.0.0.1:80"},
|
||||
},
|
||||
},
|
||||
},
|
||||
"Test-1611429260986126224": {
|
||||
LoadBalancer: &dynamic.UDPServersLoadBalancer{
|
||||
Servers: []dynamic.UDPServer{
|
||||
{Address: "127.0.0.2:80"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
HTTP: &dynamic.HTTPConfiguration{
|
||||
Routers: map[string]*dynamic.Router{},
|
||||
Middlewares: map[string]*dynamic.Middleware{},
|
||||
Services: map[string]*dynamic.Service{},
|
||||
ServersTransports: map[string]*dynamic.ServersTransport{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
|
|
@ -185,19 +185,25 @@ func createClient(namespace string, endpoint *EndpointConfig) (*api.Client, erro
|
|||
// configuration contains information from the service's tags that are globals
|
||||
// (not specific to the dynamic configuration).
|
||||
type configuration struct {
|
||||
Enable bool // <prefix>.enable
|
||||
Enable bool // <prefix>.enable is the corresponding label.
|
||||
Canary bool // <prefix>.nomad.canary is the corresponding label.
|
||||
}
|
||||
|
||||
// globalConfig returns a configuration with settings not specific to the dynamic configuration (i.e. "<prefix>.enable").
|
||||
func (p *Provider) globalConfig(tags []string) configuration {
|
||||
enabled := p.ExposedByDefault
|
||||
// getExtraConf returns a configuration with settings which are not part of the dynamic configuration (e.g. "<prefix>.enable").
|
||||
func (p *Provider) getExtraConf(tags []string) configuration {
|
||||
labels := tagsToLabels(tags, p.Prefix)
|
||||
|
||||
enabled := p.ExposedByDefault
|
||||
if v, exists := labels["traefik.enable"]; exists {
|
||||
enabled = strings.EqualFold(v, "true")
|
||||
}
|
||||
|
||||
return configuration{Enable: enabled}
|
||||
var canary bool
|
||||
if v, exists := labels["traefik.nomad.canary"]; exists {
|
||||
canary = strings.EqualFold(v, "true")
|
||||
}
|
||||
|
||||
return configuration{Enable: enabled, Canary: canary}
|
||||
}
|
||||
|
||||
func (p *Provider) getNomadServiceData(ctx context.Context) ([]item, error) {
|
||||
|
@ -216,8 +222,8 @@ func (p *Provider) getNomadServiceData(ctx context.Context) ([]item, error) {
|
|||
for _, service := range stub.Services {
|
||||
logger := log.FromContext(log.With(ctx, log.Str("serviceName", service.ServiceName)))
|
||||
|
||||
globalCfg := p.globalConfig(service.Tags)
|
||||
if !globalCfg.Enable {
|
||||
extraConf := p.getExtraConf(service.Tags)
|
||||
if !extraConf.Enable {
|
||||
logger.Debug("Filter Nomad service that is not enabled")
|
||||
continue
|
||||
}
|
||||
|
@ -248,7 +254,7 @@ func (p *Provider) getNomadServiceData(ctx context.Context) ([]item, error) {
|
|||
Address: i.Address,
|
||||
Port: i.Port,
|
||||
Tags: i.Tags,
|
||||
ExtraConf: p.globalConfig(i.Tags),
|
||||
ExtraConf: p.getExtraConf(i.Tags),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ func Test_globalConfig(t *testing.T) {
|
|||
for _, test := range cases {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
p := Provider{ExposedByDefault: test.ExposedByDefault, Prefix: test.Prefix}
|
||||
result := p.globalConfig(test.Tags)
|
||||
result := p.getExtraConf(test.Tags)
|
||||
require.Equal(t, test.exp, result)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue