-
Notifications
You must be signed in to change notification settings - Fork 0
/
external-lb.go
168 lines (146 loc) · 4.48 KB
/
external-lb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/rancher/external-lb/model"
"strings"
)
type Op int
const (
ADD Op = iota
REMOVE
UPDATE
)
func UpdateProviderLBConfigs(metadataConfigs map[string]model.LBConfig) (map[string]model.LBConfig, error) {
providerConfigs, err := getProviderLBConfigs()
if err != nil {
return nil, fmt.Errorf("Failed to get LB configs from provider: %v", err)
}
removeExtraConfigs(metadataConfigs, providerConfigs)
updated := addMissingConfigs(metadataConfigs, providerConfigs)
updated_ := updateExistingConfigs(metadataConfigs, providerConfigs)
for k, v := range updated_ {
if _, ok := updated[k]; !ok {
updated[k] = v
}
}
return updated, nil
}
func getProviderLBConfigs() (map[string]model.LBConfig, error) {
allConfigs, err := provider.GetLBConfigs()
if err != nil {
return nil, err
}
rancherConfigs := make(map[string]model.LBConfig, len(allConfigs))
suffix := "_" + m.EnvironmentUUID + "_" + targetPoolSuffix
for _, value := range allConfigs {
if strings.HasSuffix(value.LBTargetPoolName, suffix) {
rancherConfigs[value.LBEndpoint] = value
}
}
logrus.Debugf("LBConfigs from provider: %v", allConfigs)
return rancherConfigs, nil
}
func removeExtraConfigs(metadataConfigs, providerConfigs map[string]model.LBConfig) map[string]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: %d", len(toRemove))
}
return updateProvider(toRemove, REMOVE)
}
func addMissingConfigs(metadataConfigs, providerConfigs map[string]model.LBConfig) map[string]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: %d", len(toAdd))
}
return updateProvider(toAdd, ADD)
}
func updateExistingConfigs(metadataConfigs, providerConfigs map[string]model.LBConfig) map[string]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 targetPoolName and targets match
if strings.EqualFold(mLBConfig.LBTargetPoolName, pLBConfig.LBTargetPoolName) {
if len(mLBConfig.LBTargets) != len(pLBConfig.LBTargets) {
update = true
} else {
//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
}
}
}
} else {
//targetPool should be changed
logrus.Debugf("The LBEndPoint %s will be updated to map to a new LBTargetPoolName %s", key, mLBConfig.LBTargetPoolName)
update = true
}
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: %d", len(toUpdate))
}
return updateProvider(toUpdate, UPDATE)
}
func updateProvider(toChange []model.LBConfig, op Op) map[string]model.LBConfig {
// map of FQDN -> LBConfig
updateFqdn := make(map[string]model.LBConfig)
for _, value := range toChange {
switch op {
case ADD:
logrus.Infof("Adding LB config: %v", value)
fqdn, err := provider.AddLBConfig(value)
if err != nil {
logrus.Errorf("Failed to add LB config for endpoint %s: %v", value.LBEndpoint, err)
} else if fqdn != "" {
updateFqdn[fqdn] = value
}
case REMOVE:
logrus.Infof("Removing LB config: %v", value)
if err := provider.RemoveLBConfig(value); err != nil {
logrus.Errorf("Failed to remove LB config for endpoint %s: %v", value.LBEndpoint, err)
}
case UPDATE:
logrus.Infof("Updating LB config: %v", value)
fqdn, err := provider.UpdateLBConfig(value)
if err != nil {
logrus.Errorf("Failed to update LB config for endpoint %s: %v", value.LBEndpoint, err)
} else if fqdn != "" {
updateFqdn[fqdn] = value
}
}
}
return updateFqdn
}