handle errors when working with rancher

This commit is contained in:
apsifly 2019-01-30 19:10:03 +03:00 committed by Traefiker Bot
parent 6ea9c4dd3f
commit 06905cb14a

View file

@ -71,9 +71,18 @@ func (p *Provider) apiProvide(configurationChan chan<- types.ConfigMessage, pool
} }
ctx := context.Background() ctx := context.Background()
var stacks = listRancherStacks(rancherClient) stacks, err := listRancherStacks(rancherClient)
var services = listRancherServices(rancherClient) if err != nil {
var container = listRancherContainer(rancherClient) return err
}
services, err := listRancherServices(rancherClient)
if err != nil {
return err
}
container, err := listRancherContainer(rancherClient)
if err != nil {
return err
}
var rancherData = parseAPISourcedRancherData(stacks, services, container) var rancherData = parseAPISourcedRancherData(stacks, services, container)
@ -94,20 +103,29 @@ func (p *Provider) apiProvide(configurationChan chan<- types.ConfigMessage, pool
if errAPI != nil { if errAPI != nil {
log.Errorf("Cannot establish connection: %+v, Rancher API return: %+v; Skipping refresh Data from Rancher API.", errAPI, checkAPI) log.Errorf("Cannot establish connection: %+v, Rancher API return: %+v; Skipping refresh Data from Rancher API.", errAPI, checkAPI)
} else { continue
log.Debugf("Refreshing new Data from Rancher API") }
stacks := listRancherStacks(rancherClient) log.Debugf("Refreshing new Data from Rancher API")
services := listRancherServices(rancherClient) stacks, err = listRancherStacks(rancherClient)
container := listRancherContainer(rancherClient) if err != nil {
continue
}
services, err = listRancherServices(rancherClient)
if err != nil {
continue
}
container, err = listRancherContainer(rancherClient)
if err != nil {
continue
}
rancherData := parseAPISourcedRancherData(stacks, services, container) rancherData := parseAPISourcedRancherData(stacks, services, container)
configuration := p.buildConfiguration(rancherData) configuration := p.buildConfiguration(rancherData)
if configuration != nil { if configuration != nil {
configurationChan <- types.ConfigMessage{ configurationChan <- types.ConfigMessage{
ProviderName: "rancher", ProviderName: "rancher",
Configuration: configuration, Configuration: configuration,
}
} }
} }
case <-stop: case <-stop:
@ -133,7 +151,7 @@ func (p *Provider) apiProvide(configurationChan chan<- types.ConfigMessage, pool
return nil return nil
} }
func listRancherStacks(client *rancher.RancherClient) []*rancher.Stack { func listRancherStacks(client *rancher.RancherClient) ([]*rancher.Stack, error) {
var stackList []*rancher.Stack var stackList []*rancher.Stack
@ -147,10 +165,10 @@ func listRancherStacks(client *rancher.RancherClient) []*rancher.Stack {
stackList = append(stackList, &stacks.Data[k]) stackList = append(stackList, &stacks.Data[k])
} }
return stackList return stackList, err
} }
func listRancherServices(client *rancher.RancherClient) []*rancher.Service { func listRancherServices(client *rancher.RancherClient) ([]*rancher.Service, error) {
var servicesList []*rancher.Service var servicesList []*rancher.Service
@ -164,10 +182,10 @@ func listRancherServices(client *rancher.RancherClient) []*rancher.Service {
servicesList = append(servicesList, &services.Data[k]) servicesList = append(servicesList, &services.Data[k])
} }
return servicesList return servicesList, err
} }
func listRancherContainer(client *rancher.RancherClient) []*rancher.Container { func listRancherContainer(client *rancher.RancherClient) ([]*rancher.Container, error) {
var containerList []*rancher.Container var containerList []*rancher.Container
@ -175,6 +193,7 @@ func listRancherContainer(client *rancher.RancherClient) []*rancher.Container {
if err != nil { if err != nil {
log.Errorf("Cannot get Provider Services %+v", err) log.Errorf("Cannot get Provider Services %+v", err)
return containerList, err
} }
valid := true valid := true
@ -195,7 +214,7 @@ func listRancherContainer(client *rancher.RancherClient) []*rancher.Container {
} }
} }
return containerList return containerList, err
} }
func parseAPISourcedRancherData(stacks []*rancher.Stack, services []*rancher.Service, containers []*rancher.Container) []rancherData { func parseAPISourcedRancherData(stacks []*rancher.Stack, services []*rancher.Service, containers []*rancher.Container) []rancherData {