-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding full sync with external LB provider, changed LB model and added
provider healthcheck
- Loading branch information
Prachi Damle
authored and
Prachi Damle
committed
Apr 15, 2016
1 parent
55603bf
commit 4d1a065
Showing
10 changed files
with
451 additions
and
127 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Sirupsen/logrus" | ||
"github.com/rancher/external-lb/model" | ||
"strings" | ||
) | ||
|
||
func UpdateProviderLBConfigs(metadataConfigs map[string]model.LBConfig) error { | ||
providerConfigs, err := getProviderLBConfigs() | ||
if err != nil { | ||
return fmt.Errorf("Provider error reading lb configs: %v", err) | ||
} | ||
logrus.Debugf("Rancher LB configs from provider: %v", providerConfigs) | ||
|
||
removeExtraConfigs(metadataConfigs, providerConfigs) | ||
|
||
addMissingConfigs(metadataConfigs, providerConfigs) | ||
|
||
updateExistingConfigs(metadataConfigs, providerConfigs) | ||
|
||
return nil | ||
} | ||
|
||
func getProviderLBConfigs() (map[string]model.LBConfig, error) { | ||
logrus.Debugf("Getting Rancher LB configs from provider: %v", provider.GetName()) | ||
allConfigs, err := provider.GetLBConfigs() | ||
if err != nil { | ||
logrus.Debugf("Error Getting Rancher LB configs from provider: %v", err) | ||
return nil, err | ||
} | ||
rancherConfigs := make(map[string]model.LBConfig, len(allConfigs)) | ||
suffix := "_rancher.internal" | ||
for _, value := range allConfigs { | ||
if strings.HasSuffix(value.LBTargetName, suffix) { | ||
rancherConfigs[value.LBEndpoint] = value | ||
} | ||
} | ||
return rancherConfigs, nil | ||
} | ||
|
||
func removeExtraConfigs(metadataConfigs map[string]model.LBConfig, providerConfigs map[string]model.LBConfig) []model.LBConfig { | ||
var toRemove []model.LBConfig | ||
for key := range providerConfigs { | ||
if _, ok := metadataConfigs[key]; !ok { | ||
toRemove = append(toRemove, providerConfigs[key]) | ||
} | ||
} | ||
|
||
if len(toRemove) == 0 { | ||
logrus.Debug("No LB configs to remove") | ||
} else { | ||
logrus.Infof("LB configs to remove: %v", toRemove) | ||
} | ||
return updateProvider(toRemove, &Remove) | ||
} | ||
|
||
func addMissingConfigs(metadataConfigs map[string]model.LBConfig, providerConfigs map[string]model.LBConfig) []model.LBConfig { | ||
var toAdd []model.LBConfig | ||
for key := range metadataConfigs { | ||
if _, ok := providerConfigs[key]; !ok { | ||
toAdd = append(toAdd, metadataConfigs[key]) | ||
} | ||
} | ||
if len(toAdd) == 0 { | ||
logrus.Debug("No LB configs to add") | ||
} else { | ||
logrus.Infof("LB configs to add: %v", toAdd) | ||
} | ||
return updateProvider(toAdd, &Add) | ||
} | ||
|
||
func updateExistingConfigs(metadataConfigs map[string]model.LBConfig, providerConfigs map[string]model.LBConfig) []model.LBConfig { | ||
var toUpdate []model.LBConfig | ||
for key := range metadataConfigs { | ||
if _, ok := providerConfigs[key]; ok { | ||
mLBConfig := metadataConfigs[key] | ||
pLBConfig := providerConfigs[key] | ||
var update bool | ||
//check that the targetName and targets match | ||
if mLBConfig.LBTargetName == pLBConfig.LBTargetName { | ||
if len(mLBConfig.LBTargets) != len(pLBConfig.LBTargets) { | ||
update = true | ||
} | ||
//check if any target have changed | ||
for _, mTarget := range mLBConfig.LBTargets { | ||
targetExists := false | ||
for _, pTarget := range pLBConfig.LBTargets { | ||
if pTarget.HostIP == mTarget.HostIP && pTarget.Port == mTarget.Port { | ||
targetExists = true | ||
break | ||
} | ||
} | ||
if !targetExists { | ||
//lb target changed, update the config on provider | ||
update = true | ||
break | ||
} | ||
} | ||
} | ||
|
||
if update { | ||
toUpdate = append(toUpdate, metadataConfigs[key]) | ||
} | ||
} | ||
} | ||
|
||
if len(toUpdate) == 0 { | ||
logrus.Debug("No LB configs to update") | ||
} else { | ||
logrus.Infof("LB configs to update: %v", toUpdate) | ||
} | ||
|
||
return updateProvider(toUpdate, &Update) | ||
} | ||
|
||
func updateProvider(toChange []model.LBConfig, op *Op) []model.LBConfig { | ||
var changed []model.LBConfig | ||
for _, value := range toChange { | ||
switch *op { | ||
case Add: | ||
logrus.Infof("Adding LB config: %v", value) | ||
if err := provider.AddLBConfig(value); err != nil { | ||
logrus.Errorf("Failed to add LB config to provider %v: %v", value, err) | ||
} else { | ||
changed = append(changed, value) | ||
} | ||
case Remove: | ||
logrus.Infof("Removing LB config: %v", value) | ||
if err := provider.RemoveLBConfig(value); err != nil { | ||
logrus.Errorf("Failed to remove LB config from provider %v: %v", value, err) | ||
} | ||
case Update: | ||
logrus.Infof("Updating LB config: %v", value) | ||
if err := provider.UpdateLBConfig(value); err != nil { | ||
logrus.Errorf("Failed to update LB config to provider %v: %v", value, err) | ||
} else { | ||
changed = append(changed, value) | ||
} | ||
} | ||
} | ||
return changed | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
package model | ||
|
||
import ( | ||
) | ||
|
||
|
||
type LBRecord struct { | ||
Vip string | ||
ServiceName string | ||
Nodes []LBNode | ||
type LBConfig struct { | ||
LBEndpoint string | ||
LBTargetName string | ||
LBTargets []LBTarget | ||
} | ||
|
||
type LBNode struct { | ||
HostIP string | ||
Port string | ||
} | ||
type LBTarget struct { | ||
HostIP string | ||
Port string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.