2017-04-17 10:50:02 +00:00
|
|
|
package rancher
|
2017-01-28 23:01:56 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-06 15:28:12 +00:00
|
|
|
"fmt"
|
2017-02-20 19:41:28 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/containous/traefik/old/log"
|
|
|
|
"github.com/containous/traefik/old/provider"
|
|
|
|
"github.com/containous/traefik/old/types"
|
2017-02-05 21:54:24 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2017-04-18 02:01:11 +00:00
|
|
|
)
|
|
|
|
|
2017-12-02 18:29:09 +00:00
|
|
|
const (
|
|
|
|
// Health
|
|
|
|
healthy = "healthy"
|
|
|
|
updatingHealthy = "updating-healthy"
|
|
|
|
|
|
|
|
// State
|
|
|
|
active = "active"
|
|
|
|
running = "running"
|
|
|
|
upgraded = "upgraded"
|
2018-03-15 21:22:03 +00:00
|
|
|
upgrading = "upgrading"
|
2017-12-02 18:29:09 +00:00
|
|
|
updatingActive = "updating-active"
|
|
|
|
updatingRunning = "updating-running"
|
|
|
|
)
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
var _ provider.Provider = (*Provider)(nil)
|
2017-01-28 23:01:56 +00:00
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provider holds configurations of the provider.
|
|
|
|
type Provider struct {
|
2017-10-02 08:32:02 +00:00
|
|
|
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
|
|
|
APIConfiguration `mapstructure:",squash" export:"true"` // Provide backwards compatibility
|
|
|
|
API *APIConfiguration `description:"Enable the Rancher API provider" export:"true"`
|
|
|
|
Metadata *MetadataConfiguration `description:"Enable the Rancher metadata service provider" export:"true"`
|
|
|
|
Domain string `description:"Default domain used"`
|
|
|
|
RefreshSeconds int `description:"Polling interval (in seconds)" export:"true"`
|
|
|
|
ExposedByDefault bool `description:"Expose services by default" export:"true"`
|
|
|
|
EnableServiceHealthFilter bool `description:"Filter services with unhealthy states and inactive states" export:"true"`
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rancherData struct {
|
2018-03-26 13:32:04 +00:00
|
|
|
Name string
|
|
|
|
Labels map[string]string // List of labels set to container or service
|
|
|
|
Containers []string
|
|
|
|
Health string
|
|
|
|
State string
|
|
|
|
SegmentLabels map[string]string
|
|
|
|
SegmentName string
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:54:24 +00:00
|
|
|
func (r rancherData) String() string {
|
2017-04-29 19:37:54 +00:00
|
|
|
return fmt.Sprintf("{name:%s, labels:%v, containers: %v, health: %s, state: %s}", r.Name, r.Labels, r.Containers, r.Health, r.State)
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 07:08:03 +00:00
|
|
|
// Init the provider
|
|
|
|
func (p *Provider) Init(constraints types.Constraints) error {
|
|
|
|
return p.BaseProvider.Init(constraints)
|
|
|
|
}
|
|
|
|
|
2017-05-08 01:20:38 +00:00
|
|
|
// Provide allows either the Rancher API or metadata service provider to
|
|
|
|
// seed configuration into Traefik using the given configuration channel.
|
2018-07-11 07:08:03 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error {
|
2017-05-08 01:20:38 +00:00
|
|
|
if p.Metadata == nil {
|
2018-07-11 07:08:03 +00:00
|
|
|
return p.apiProvide(configurationChan, pool)
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
2018-07-11 07:08:03 +00:00
|
|
|
return p.metadataProvide(configurationChan, pool)
|
2017-01-28 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 01:20:38 +00:00
|
|
|
func containerFilter(name, healthState, state string) bool {
|
2017-12-02 18:29:09 +00:00
|
|
|
if healthState != "" && healthState != healthy && healthState != updatingHealthy {
|
2017-05-08 01:20:38 +00:00
|
|
|
log.Debugf("Filtering container %s with healthState of %s", name, healthState)
|
2017-04-29 19:37:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:22:03 +00:00
|
|
|
if state != "" && state != running && state != updatingRunning && state != upgraded {
|
2017-05-08 01:20:38 +00:00
|
|
|
log.Debugf("Filtering container %s with state of %s", name, state)
|
2017-04-29 19:37:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|