Skip to content

Commit

Permalink
Merge pull request #916 from traPtitech/traefik-priority-offset
Browse files Browse the repository at this point in the history
performance(backend): Add traefik rule priority offset
  • Loading branch information
motoki317 authored May 31, 2024
2 parents b7deef4 + e16b12a commit aedfb1a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .local-dev/config/ns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ components:
ss:
url: http://static-server:80/

routing:
type: traefik
traefik:
priorityOffset: 0
tls:
certResolver: nsresolver
wildcard:
Expand Down Expand Up @@ -118,6 +122,10 @@ components:
port: 80
scheme: http

routing:
type: traefik
traefik:
priorityOffset: 0
tls:
type: traefik
traefik:
Expand Down
4 changes: 4 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func init() {
viper.SetDefault("components.controller.docker.ports", nil)

viper.SetDefault("components.controller.docker.ss.url", "")
viper.SetDefault("components.controller.docker.routing.type", "traefik")
viper.SetDefault("components.controller.docker.routing.traefik.priorityOffset", 0)
viper.SetDefault("components.controller.docker.tls.certResolver", "nsresolver")
viper.SetDefault("components.controller.docker.tls.wildcard.domains", nil)

Expand All @@ -183,6 +185,8 @@ func init() {
viper.SetDefault("components.controller.k8s.ss.port", 80)
viper.SetDefault("components.controller.k8s.ss.scheme", "http")

viper.SetDefault("components.controller.k8s.routing.type", "traefik")
viper.SetDefault("components.controller.k8s.routing.traefik.priorityOffset", 0)
viper.SetDefault("components.controller.k8s.tls.type", "traefik")
viper.SetDefault("components.controller.k8s.tls.traefik.certResolver", "nsresolver")
viper.SetDefault("components.controller.k8s.tls.traefik.wildcard.domains", nil)
Expand Down
9 changes: 5 additions & 4 deletions pkg/infrastructure/backend/dockerimpl/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ func prepareManager(t *testing.T) (*Backend, *client.Client) {
t.Fatal(err)
}

m, err := NewDockerBackend(c, Config{
ConfDir: "../../../../.local-dev/traefik",
Network: "neoshowcase_apps",
}, builder.ImageConfig{})
config := Config{}
config.ConfDir = "../../../../.local-dev/traefik"
config.Network = "neoshowcase_apps"
config.Routing.Type = routingTypeTraefik
m, err := NewDockerBackend(c, config, builder.ImageConfig{})
require.NoError(t, err)
err = m.Start(context.Background())
require.NoError(t, err)
Expand Down
23 changes: 23 additions & 0 deletions pkg/infrastructure/backend/dockerimpl/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dockerimpl

import (
"fmt"
"math"

"github.com/friendsofgo/errors"
Expand All @@ -10,6 +11,10 @@ import (
"github.com/traPtitech/neoshowcase/pkg/util/mapper"
)

const (
routingTypeTraefik = "traefik"
)

type domainAuthConf = struct {
Available bool `mapstructure:"available" yaml:"available"`
Soft []string `mapstructure:"soft" yaml:"soft"`
Expand Down Expand Up @@ -67,6 +72,18 @@ type Config struct {
URL string `mapstructure:"url" yaml:"url"`
} `mapstructure:"ss" yaml:"ss"`

// Routing section defines ingress settings.
Routing struct {
// Type defines which ingress controller to use.
// Possible values:
// "traefik: Uses traefik ingress controller.
Type string `mapstructure:"type" yaml:"type"`
Traefik struct {
// PriorityOffset defines HTTP routes' priority offset for user apps.
// This is optionally used to optimize routing performance.
PriorityOffset int `mapstructure:"priorityOffset" yaml:"priorityOffset"`
} `mapstructure:"traefik" yaml:"traefik"`
} `mapstructure:"routing" yaml:"routing"`
// TLS section defines tls setting for user app ingress.
TLS struct {
CertResolver string `mapstructure:"certResolver" yaml:"certResolver"`
Expand Down Expand Up @@ -107,6 +124,12 @@ func (c *Config) Validate() error {
}
}

switch c.Routing.Type {
case routingTypeTraefik:
// Nothing to validate as of now
default:
return errors.New(fmt.Sprintf("docker.routing.type is invalid: %s", c.Routing.Type))
}
if err := c.TLS.Wildcard.Domains.Validate(); err != nil {
return errors.Wrap(err, "docker.tls.wildcard.domains is invalid")
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/infrastructure/backend/dockerimpl/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ func (b *Backend) routerBase(website *domain.Website, svcName string) (router m,
}
}

priorityOffset := b.config.Routing.Traefik.PriorityOffset
priority := len(rule) + priorityOffset

router = m{
"entrypoints": entrypoints,
"middlewares": middlewareNames,
"rule": rule,
"priority": priority,
"service": svcName,
}

Expand Down
1 change: 1 addition & 0 deletions pkg/infrastructure/backend/k8simpl/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func prepareManager(t *testing.T) (*Backend, *kubernetes.Clientset, *traefikv1al

var config Config
config.Namespace = appsNamespace
config.Routing.Type = routingTypeTraefik
config.TLS.Type = tlsTypeTraefik
b, err := NewK8SBackend(kubeconf, client, traefikClient, certManagerClient, config)
require.NoError(t, err)
Expand Down
21 changes: 21 additions & 0 deletions pkg/infrastructure/backend/k8simpl/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k8simpl

import (
"fmt"
"strconv"

"github.com/friendsofgo/errors"
Expand All @@ -17,6 +18,8 @@ import (
)

const (
routingTypeTraefik = "traefik"

tlsTypeTraefik = "traefik"
tlsTypeCertManager = "cert-manager"

Expand Down Expand Up @@ -166,6 +169,18 @@ type Config struct {
Scheme string `mapstructure:"scheme" yaml:"scheme"`
} `mapstructure:"ss" yaml:"ss"`

// Routing section defines ingress controller settings.
Routing struct {
// Type defines which ingress controller to use.
// Possible values:
// "traefik: Uses traefik ingress controller.
Type string `mapstructure:"type" yaml:"type"`
Traefik struct {
// PriorityOffset defines HTTP routes' priority offset for user apps.
// This is optionally used to optimize routing performance.
PriorityOffset int `mapstructure:"priorityOffset" yaml:"priorityOffset"`
} `mapstructure:"traefik" yaml:"traefik"`
} `mapstructure:"routing" yaml:"routing"`
// TLS section defines tls setting for user app ingress.
TLS struct {
// Type defines which provider is responsible for obtaining http certificates.
Expand Down Expand Up @@ -326,6 +341,12 @@ func (c *Config) Validate() error {
}
}

switch c.Routing.Type {
case routingTypeTraefik:
// Nothing to validate as of now
default:
return errors.New(fmt.Sprintf("k8s.routing.type is invalid: %s", c.Routing.Type))
}
switch c.TLS.Type {
case tlsTypeTraefik:
if err := c.TLS.Traefik.Wildcard.Domains.Validate(); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/infrastructure/backend/k8simpl/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func (b *Backend) ingressRoute(
middlewareRefs = append(middlewareRefs, traefikv1alpha1.MiddlewareRef{Name: middleware.Name})
}
}
var rulePriority int
{
priorityOffset := b.config.Routing.Traefik.PriorityOffset
rulePriority = len(rule) + priorityOffset
}

var tls *traefikv1alpha1.TLS
var certs []*certmanagerv1.Certificate
Expand Down Expand Up @@ -141,6 +146,7 @@ func (b *Backend) ingressRoute(
EntryPoints: entrypoints,
Routes: []traefikv1alpha1.Route{{
Match: rule,
Priority: rulePriority,
Kind: "Rule",
Services: serviceRefs,
Middlewares: middlewareRefs,
Expand Down

0 comments on commit aedfb1a

Please sign in to comment.