traefik/cmd/convert/convert.go

152 lines
3.5 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package main
import (
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/types"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/config"
2018-11-14 09:18:03 +00:00
"github.com/sirupsen/logrus"
)
var oldvalue = `
[backends]
[backends.backend1]
[backends.backend1.servers.server1]
url = "http://127.0.0.1:9010"
weight = 1
[backends.backend2]
[backends.backend2.servers.server1]
url = "http://127.0.0.1:9020"
weight = 1
[frontends]
[frontends.frontend1]
backend = "backend1"
[frontends.frontend1.routes.test_1]
rule = "Host:snitest.com"
[frontends.frontend2]
backend = "backend2"
[frontends.frontend2.routes.test_2]
rule = "Host:snitest.org"
`
// Temporary utility to convert dynamic conf v1 to v2
func main() {
log.SetOutput(os.Stdout)
log.SetLevel(logrus.DebugLevel)
2019-03-04 15:40:05 +00:00
oldConfig := &types.Configuration{}
_, err := toml.Decode(oldvalue, oldConfig)
if err != nil {
log.Fatal(err)
}
2018-11-14 09:18:03 +00:00
newConfig := config.HTTPConfiguration{
2018-11-14 09:18:03 +00:00
Routers: make(map[string]*config.Router),
Middlewares: make(map[string]*config.Middleware),
Services: make(map[string]*config.Service),
}
2019-03-04 15:40:05 +00:00
for frontendName, frontend := range oldConfig.Frontends {
newConfig.Routers[replaceFrontend(frontendName)] = convertFrontend(frontend)
2018-11-14 09:18:03 +00:00
if frontend.PassHostHeader {
log.Warn("ignore PassHostHeader")
}
}
2019-03-04 15:40:05 +00:00
for backendName, backend := range oldConfig.Backends {
newConfig.Services[replaceBackend(backendName)] = convertBackend(backend)
2018-11-14 09:18:03 +00:00
}
encoder := toml.NewEncoder(os.Stdout)
2019-03-04 15:40:05 +00:00
err = encoder.Encode(newConfig)
if err != nil {
log.Fatal(err)
}
2018-11-14 09:18:03 +00:00
}
func replaceBackend(name string) string {
return strings.Replace(name, "backend", "service", -1)
}
func replaceFrontend(name string) string {
return strings.Replace(name, "frontend", "router", -1)
}
func convertFrontend(frontend *types.Frontend) *config.Router {
router := &config.Router{
EntryPoints: frontend.EntryPoints,
Middlewares: nil,
Service: replaceBackend(frontend.Backend),
Priority: frontend.Priority,
}
if len(frontend.Routes) > 1 {
log.Fatal("Multiple routes")
}
for _, route := range frontend.Routes {
router.Rule = route.Rule
}
return router
}
func convertBackend(backend *types.Backend) *config.Service {
service := &config.Service{
LoadBalancer: &config.LoadBalancerService{
Stickiness: nil,
Servers: nil,
Method: "",
HealthCheck: nil,
PassHostHeader: false,
},
}
if backend.Buffering != nil {
log.Warn("Buffering not implemented")
}
if backend.CircuitBreaker != nil {
log.Warn("CircuitBreaker not implemented")
}
if backend.MaxConn != nil {
log.Warn("MaxConn not implemented")
}
for _, oldserver := range backend.Servers {
service.LoadBalancer.Servers = append(service.LoadBalancer.Servers, config.Server{
URL: oldserver.URL,
Weight: oldserver.Weight,
})
}
if backend.LoadBalancer != nil {
service.LoadBalancer.Method = backend.LoadBalancer.Method
if backend.LoadBalancer.Stickiness != nil {
service.LoadBalancer.Stickiness = &config.Stickiness{
CookieName: backend.LoadBalancer.Stickiness.CookieName,
}
}
if backend.HealthCheck != nil {
service.LoadBalancer.HealthCheck = &config.HealthCheck{
Scheme: backend.HealthCheck.Scheme,
Path: backend.HealthCheck.Path,
Port: backend.HealthCheck.Port,
Interval: backend.HealthCheck.Interval,
Timeout: backend.HealthCheck.Timeout,
Hostname: backend.HealthCheck.Hostname,
Headers: backend.HealthCheck.Headers,
}
}
}
return service
}