add watch function

This commit is contained in:
Manuel Laufenberg 2017-02-06 16:28:12 +01:00
parent 9a5dc54f85
commit bdb63ac785

View file

@ -1,6 +1,9 @@
package provider package provider
import ( import (
"context"
"errors"
"fmt"
"github.com/BurntSushi/ty/fun" "github.com/BurntSushi/ty/fun"
"github.com/cenk/backoff" "github.com/cenk/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
@ -8,14 +11,15 @@ import (
"github.com/containous/traefik/safe" "github.com/containous/traefik/safe"
"github.com/containous/traefik/types" "github.com/containous/traefik/types"
rancher "github.com/rancher/go-rancher/client" rancher "github.com/rancher/go-rancher/client"
"time"
//"context"
"errors"
"fmt"
"math" "math"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
"time"
)
const (
RancherDefaultWatchTime = 15 * time.Second
) )
var _ Provider = (*Rancher)(nil) var _ Provider = (*Rancher)(nil)
@ -202,7 +206,7 @@ func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, p
safe.Go(func() { safe.Go(func() {
operation := func() error { operation := func() error {
rancherClient, err := provider.createClient() rancherClient, err := provider.createClient()
//ctx := context.Background() ctx := context.Background()
var environments = listRancherEnvironments(rancherClient) var environments = listRancherEnvironments(rancherClient)
var services = listRancherServices(rancherClient) var services = listRancherServices(rancherClient)
var container = listRancherContainer(rancherClient) var container = listRancherContainer(rancherClient)
@ -220,6 +224,37 @@ func (provider *Rancher) Provide(configurationChan chan<- types.ConfigMessage, p
Configuration: configuration, Configuration: configuration,
} }
if provider.Watch {
_, cancel := context.WithCancel(ctx)
ticker := time.NewTicker(RancherDefaultWatchTime)
pool.Go(func(stop chan bool) {
for {
select {
case <-ticker.C:
log.Debugf("Refreshing new Data from Rancher API")
var environments = listRancherEnvironments(rancherClient)
var services = listRancherServices(rancherClient)
var container = listRancherContainer(rancherClient)
rancherData := parseRancherData(environments, services, container)
configuration := provider.loadRancherConfig(rancherData)
if configuration != nil {
configurationChan <- types.ConfigMessage{
ProviderName: "rancher",
Configuration: configuration,
}
}
case <-stop:
ticker.Stop()
cancel()
return
}
}
})
}
return nil return nil
} }
notify := func(err error, time time.Duration) { notify := func(err error, time time.Duration) {
@ -244,17 +279,13 @@ func listRancherEnvironments(client *rancher.RancherClient) []*rancher.Environme
log.Errorf("Cannot get Rancher Environments %+v", err) log.Errorf("Cannot get Rancher Environments %+v", err)
} }
for k, environment := range environments.Data { for k, _ := range environments.Data {
log.Debugf("Adding environment with id %s", environment.Id)
environmentList = append(environmentList, &environments.Data[k]) environmentList = append(environmentList, &environments.Data[k])
} }
return environmentList return environmentList
} }
/*
"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{}
@ -265,8 +296,7 @@ func listRancherServices(client *rancher.RancherClient) []*rancher.Service {
log.Errorf("Cannot get Rancher Services %+v", err) log.Errorf("Cannot get Rancher Services %+v", err)
} }
for k, service := range services.Data { for k, _ := range services.Data {
log.Debugf("Adding service with id %s", service.Id)
servicesList = append(servicesList, &services.Data[k]) servicesList = append(servicesList, &services.Data[k])
} }
@ -288,25 +318,18 @@ func listRancherContainer(client *rancher.RancherClient) []*rancher.Container {
valid := true valid := true
for valid { for valid {
for k, singleContainer := range container.Data { for k := range container.Data {
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() container, err = container.Next()
if err != nil { if err != nil {
log.Debugf("Error - Break it babe")
break break
} }
if container == nil || len(container.Data) == 0 { if container == nil || len(container.Data) == 0 {
log.Debugf("No more containers - valid false")
valid = false valid = false
} else {
log.Debugf("Next length %i", len(container.Data))
} }
} }