agent: simplify some loops

This commit is contained in:
Frank Schroeder 2017-08-28 14:17:11 +02:00
parent cbaf97bced
commit c00bbdb5e4
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 11 additions and 13 deletions

View File

@ -1620,8 +1620,8 @@ func (a *Agent) RemoveService(serviceID string, persist bool) error {
} }
// Deregister any associated health checks // Deregister any associated health checks
for checkID, health := range a.state.Checks() { for checkID, check := range a.state.Checks() {
if health.ServiceID != serviceID { if check.ServiceID != serviceID {
continue continue
} }
if err := a.RemoveCheck(checkID, persist); err != nil { if err := a.RemoveCheck(checkID, persist); err != nil {
@ -1653,11 +1653,11 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *structs.CheckType,
} }
if check.ServiceID != "" { if check.ServiceID != "" {
svc, ok := a.state.Services()[check.ServiceID] s := a.state.Services()[check.ServiceID]
if !ok { if s == nil {
return fmt.Errorf("ServiceID %q does not exist", check.ServiceID) return fmt.Errorf("ServiceID %q does not exist", check.ServiceID)
} }
check.ServiceName = svc.Service check.ServiceName = s.Service
} }
a.checkLock.Lock() a.checkLock.Lock()
@ -2159,12 +2159,11 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig) error {
// unloadServices will deregister all services other than the 'consul' service // unloadServices will deregister all services other than the 'consul' service
// known to the local agent. // known to the local agent.
func (a *Agent) unloadServices() error { func (a *Agent) unloadServices() error {
for _, service := range a.state.Services() { for id := range a.state.Services() {
if err := a.RemoveService(service.ID, false); err != nil { if err := a.RemoveService(id, false); err != nil {
return fmt.Errorf("Failed deregistering service '%s': %v", service.ID, err) return fmt.Errorf("Failed deregistering service '%s': %v", id, err)
} }
} }
return nil return nil
} }
@ -2247,12 +2246,11 @@ func (a *Agent) loadChecks(conf *config.RuntimeConfig) error {
// unloadChecks will deregister all checks known to the local agent. // unloadChecks will deregister all checks known to the local agent.
func (a *Agent) unloadChecks() error { func (a *Agent) unloadChecks() error {
for _, check := range a.state.Checks() { for id := range a.state.Checks() {
if err := a.RemoveCheck(check.CheckID, false); err != nil { if err := a.RemoveCheck(id, false); err != nil {
return fmt.Errorf("Failed deregistering check '%s': %s", check.CheckID, err) return fmt.Errorf("Failed deregistering check '%s': %s", id, err)
} }
} }
return nil return nil
} }