fix all containers - no matter of pagination

fmt & lint
This commit is contained in:
Manuel Laufenberg 2017-02-05 22:54:24 +01:00
parent 38bd49b97e
commit 48524a58ff

View file

@ -1,26 +1,26 @@
package provider package provider
import ( import (
rancher "github.com/rancher/go-rancher/client" "github.com/BurntSushi/ty/fun"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
"github.com/containous/traefik/log"
"github.com/cenk/backoff" "github.com/cenk/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/log"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
rancher "github.com/rancher/go-rancher/client"
"time" "time"
"github.com/BurntSushi/ty/fun"
//"context" //"context"
"errors" "errors"
"strings"
"strconv"
"math"
"fmt" "fmt"
"math"
"strconv"
"strings"
"text/template" "text/template"
) )
var _ Provider = (*Rancher)(nil) var _ Provider = (*Rancher)(nil)
// Rancher holds configurations of the Docker provider. // Rancher holds configurations of the Rancher provider.
type Rancher struct { type Rancher struct {
BaseProvider `mapstructure:",squash"` BaseProvider `mapstructure:",squash"`
Endpoint string `description:"Rancher server HTTP(S) endpoint."` Endpoint string `description:"Rancher server HTTP(S) endpoint."`
@ -37,11 +37,10 @@ type rancherData struct {
Health string Health string
} }
func (r rancherData)String() string{ func (r rancherData) String() string {
return fmt.Sprintf("{name:%s, labels:%v, containers: %v, health: %s}", r.Name, r.Labels, r.Containers, r.Health) return fmt.Sprintf("{name:%s, labels:%v, containers: %v, health: %s}", r.Name, r.Labels, r.Containers, r.Health)
} }
// Frontend Labels // Frontend Labels
func (provider *Rancher) getPassHostHeader(service rancherData) string { func (provider *Rancher) getPassHostHeader(service rancherData) string {
if passHostHeader, err := getServiceLabel(service, "traefik.frontend.passHostHeader"); err == nil { if passHostHeader, err := getServiceLabel(service, "traefik.frontend.passHostHeader"); err == nil {
@ -71,7 +70,6 @@ func (provider *Rancher) getFrontendRule(service rancherData) string {
return "Host:" + strings.ToLower(strings.Replace(service.Name, "/", "_", -1)) + "." + provider.Domain return "Host:" + strings.ToLower(strings.Replace(service.Name, "/", "_", -1)) + "." + provider.Domain
} }
func (provider *Rancher) getFrontendName(service rancherData) string { func (provider *Rancher) getFrontendName(service rancherData) string {
// Replace '.' with '-' in quoted keys because of this issue https://github.com/BurntSushi/toml/issues/78 // Replace '.' with '-' in quoted keys because of this issue https://github.com/BurntSushi/toml/issues/78
return normalize(provider.getFrontendRule(service)) return normalize(provider.getFrontendRule(service))
@ -85,7 +83,6 @@ func (provider *Rancher) getLoadBalancerMethod(service rancherData) string {
return "wrr" return "wrr"
} }
func (provider *Rancher) hasLoadBalancerLabel(service rancherData) bool { func (provider *Rancher) hasLoadBalancerLabel(service rancherData) bool {
_, errMethod := getServiceLabel(service, "traefik.backend.loadbalancer.method") _, errMethod := getServiceLabel(service, "traefik.backend.loadbalancer.method")
_, errSticky := getServiceLabel(service, "traefik.backend.loadbalancer.sticky") _, errSticky := getServiceLabel(service, "traefik.backend.loadbalancer.sticky")
@ -95,7 +92,6 @@ func (provider *Rancher) hasLoadBalancerLabel(service rancherData) bool {
return true return true
} }
func (provider *Rancher) hasCircuitBreakerLabel(service rancherData) bool { func (provider *Rancher) hasCircuitBreakerLabel(service rancherData) bool {
if _, err := getServiceLabel(service, "traefik.backend.circuitbreaker.expression"); err != nil { if _, err := getServiceLabel(service, "traefik.backend.circuitbreaker.expression"); err != nil {
return false return false
@ -182,19 +178,16 @@ func (provider *Rancher) getMaxConnExtractorFunc(service rancherData) string {
return "request.host" return "request.host"
} }
// Container Stuff // Container Stuff
func (provider *Rancher) getIPAddress(container *rancher.Container) string { func (provider *Rancher) getIPAddress(container *rancher.Container) string {
ipAdress := container.PrimaryIpAddress; ipAdress := container.PrimaryIpAddress
if ipAdress != ""{ if ipAdress != "" {
return ipAdress return ipAdress
} }
return "" return ""
} }
func getServiceLabel(service rancherData, label string) (string, error) { func getServiceLabel(service rancherData, label string) (string, error) {
for key, value := range service.Labels { for key, value := range service.Labels {
if key == label { if key == label {
@ -212,6 +205,8 @@ func (provider *Rancher) createClient() (*rancher.RancherClient, error) {
}) })
} }
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error { func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
safe.Go(func() { safe.Go(func() {
@ -224,10 +219,8 @@ func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, p
var rancherData = parseRancherData(environments, services, container) var rancherData = parseRancherData(environments, services, container)
fmt.Printf("Rancher Data #2 %s", &rancherData)
if err != nil { if err != nil {
log.Errorf("Failed to create a client for docker, error: %s", err) log.Errorf("Failed to create a client for rancher, error: %s", err)
return err return err
} }
@ -251,7 +244,7 @@ func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, p
return nil return nil
} }
func listRancherEnvironments(client *rancher.RancherClient)([]*rancher.Environment){ func listRancherEnvironments(client *rancher.RancherClient) []*rancher.Environment {
var environmentList = []*rancher.Environment{} var environmentList = []*rancher.Environment{}
@ -271,8 +264,8 @@ func listRancherEnvironments(client *rancher.RancherClient)([]*rancher.Environme
/* /*
"io.rancher.stack.name" "io.rancher.stack.name"
*/ */
func listRancherServices(client *rancher.RancherClient)([]*rancher.Service){ func listRancherServices(client *rancher.RancherClient) []*rancher.Service {
var servicesList = []*rancher.Service{} var servicesList = []*rancher.Service{}
@ -290,40 +283,53 @@ func listRancherServices(client *rancher.RancherClient)([]*rancher.Service){
return servicesList return servicesList
} }
func listRancherContainer(client *rancher.RancherClient)([]*rancher.Container){ func listRancherContainer(client *rancher.RancherClient) []*rancher.Container {
var containerList = []*rancher.Container{} containerList := []*rancher.Container{}
container, err := client.Container.List(nil) container, err := client.Container.List(nil)
log.Debugf("first container len: %i", len(container.Data))
if err != nil { if err != nil {
log.Errorf("Cannot get Rancher Services %+v", err) log.Errorf("Cannot get Rancher Services %+v", err)
} }
valid := true
for valid {
for k, singleContainer := range container.Data { for k, singleContainer := range container.Data {
log.Debugf("Adding container with id %s", singleContainer.Id) log.Debugf("Adding container with id %s", singleContainer.Id)
containerList = append(containerList, &container.Data[k]) containerList = append(containerList, &container.Data[k])
} }
log.Debugf("calling container.Next()")
container, err = container.Next()
if err != nil {
log.Debugf("Error - Break it babe")
break
}
if container == nil || len(container.Data) == 0 {
log.Debugf("No more containers - valid false")
valid = false
} else {
log.Debugf("Next length %i", len(container.Data))
}
}
return containerList return containerList
} }
func parseRancherData(environments []*rancher.Environment, services []*rancher.Service, containers []*rancher.Container) []rancherData { func parseRancherData(environments []*rancher.Environment, services []*rancher.Service, containers []*rancher.Container) []rancherData {
log.Debugf("Starting to parse Rancher Data")
var rancherDataList []rancherData var rancherDataList []rancherData
for _, environment := range environments { for _, environment := range environments {
log.Debugf("Iterating trough environment %s", environment.Name)
for _, service := range services { for _, service := range services {
log.Debugf("Iterating trough service %s with id %s for environment %s", service.Name, service.AccountId, environment.Id)
if service.EnvironmentId != environment.Id { if service.EnvironmentId != environment.Id {
log.Debugf("NO MATCH")
continue continue
} }
@ -339,7 +345,6 @@ func parseRancherData(environments []*rancher.Environment, services []*rancher.S
} }
for _, container := range containers { for _, container := range containers {
for key, value := range container.Labels { for key, value := range container.Labels {
if key == "io.rancher.stack_service.name" && value == rancherData.Name { if key == "io.rancher.stack_service.name" && value == rancherData.Name {
@ -377,7 +382,6 @@ func (provider *Rancher) loadRancherConfig(services []rancherData) *types.Config
"getSticky": provider.getSticky, "getSticky": provider.getSticky,
} }
// filter services // filter services
filteredServices := fun.Filter(func(service rancherData) bool { filteredServices := fun.Filter(func(service rancherData) bool {
return provider.serviceFilter(service) return provider.serviceFilter(service)
@ -393,9 +397,6 @@ func (provider *Rancher) loadRancherConfig(services []rancherData) *types.Config
backends[backendName] = service backends[backendName] = service
} }
fmt.Printf("Frontends %v", frontends)
fmt.Printf("Backends %v", backends)
templateObjects := struct { templateObjects := struct {
Frontends map[string]rancherData Frontends map[string]rancherData
Backends map[string]rancherData Backends map[string]rancherData
@ -418,7 +419,7 @@ func (provider *Rancher) serviceFilter(service rancherData) bool {
if service.Labels["traefik.port"] == "" { if service.Labels["traefik.port"] == "" {
log.Debugf("Filtering service %s without traefik.port label", service.Name) log.Debugf("Filtering service %s without traefik.port label", service.Name)
return false; return false
} }
if !isServiceEnabled(service, provider.ExposedByDefault) { if !isServiceEnabled(service, provider.ExposedByDefault) {
@ -446,21 +447,6 @@ func (provider *Rancher) serviceFilter(service rancherData) bool {
return true return true
} }
func (provider *Rancher) containerFilter(container *rancher.Container, instanceIds []string) bool {
//log.Debugf("Filtering Containers for InstanceIds %v ", instanceIds)
for _, instanceId := range instanceIds {
//log.Debugf("Looking for instanceId %s on on container %s", instanceId, container.Id)
if container.Id == instanceId {
//log.Debugf("Found container with id %s", instanceId)
return true
}
}
return false
}
func isServiceEnabled(service rancherData, exposedByDefault bool) bool { func isServiceEnabled(service rancherData, exposedByDefault bool) bool {
if service.Labels["traefik.enable"] != "" { if service.Labels["traefik.enable"] != "" {