Skip to content

Commit

Permalink
Use only endpoint.Weight on dynamic config
Browse files Browse the repository at this point in the history
* endpoint.Draining configures endpoint.Weight = 0
* HAProxy’s server state is changed to drain whenever endpoint.Weight == 0
  • Loading branch information
jcmoraisjr committed Apr 22, 2018
1 parent 90e858a commit 466fb68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
5 changes: 5 additions & 0 deletions pkg/common/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,11 @@ func (ic *GenericController) getBackendServers(ingresses []*extensions.Ingress)
ep.Weight = -1
continue
}
if ep.Draining {
// draining state always set Weight to 0
ep.Weight = 0
continue
}
podName := ep.Target.Name
weight := -1
if pod, err := ic.listers.Pod.GetPod(podNamespace, podName); err == nil {
Expand Down
6 changes: 0 additions & 6 deletions pkg/common/ingress/types_equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ func (b1 *Backend) Equal(b2 *Backend) bool {
if !(&b1.SessionAffinity).Equal(&b2.SessionAffinity) {
return false
}
if !(&b1.BlueGreen).Equal(&b2.BlueGreen) {
return false
}
if b1.UpstreamHashBy != b2.UpstreamHashBy {
return false
}
Expand Down Expand Up @@ -259,9 +256,6 @@ func (e1 *Endpoint) Equal(e2 *Endpoint) bool {
if e1.FailTimeout != e2.FailTimeout {
return false
}
if e1.Draining != e2.Draining {
return false
}
if e1.Weight != e2.Weight {
return false
}
Expand Down
30 changes: 17 additions & 13 deletions pkg/controller/stats_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ func removeEndpoint(statsSocket, backendName, backendServerName string) bool {
glog.Warningln("failed socket command srv remove")
return false
}
glog.V(2).Infof("removed endpoint %v from backend %v", backendServerName, backendName)
return true
}

// add Ingress Endpoint to a backend in a specific server slot
func addEndpoint(statsSocket, backendName, backendServerName, address, port string, weight int, draining bool) bool {
func addEndpoint(statsSocket, backendName, backendServerName, address, port string, weight int) bool {
var state string
if draining {
if weight == 0 {
state = "drain"
} else {
state = "ready"
Expand All @@ -89,12 +90,13 @@ func addEndpoint(statsSocket, backendName, backendServerName, address, port stri
glog.Warningln("failed socket command srv add")
return false
}
glog.V(2).Infof("added endpoint %v:%v to server %v/%v", address, port, backendName, backendServerName)
return true
}

func setEndpointDrainState(statsSocket, backendName, backendServerName string, weight int, draining bool) bool {
func setEndpointWeight(statsSocket, backendName, backendServerName string, weight int) bool {
var state string
if draining {
if weight == 0 {
state = "drain"
} else {
state = "ready"
Expand All @@ -105,6 +107,7 @@ func setEndpointDrainState(statsSocket, backendName, backendServerName string, w
glog.Warningln("failed socket command srv weight")
return false
}
glog.V(2).Infof("updated weight of %v/%v to %v", backendName, backendServerName, weight)
return true
}

Expand All @@ -127,21 +130,22 @@ func reconfigureBackends(currentConfig, updatedConfig *types.ControllerConfig) b
if !reflect.DeepEqual(curKeys, updKeys) {
// backend names or number of backends is different
reconfigureEmptySlots = true
} else if onlyDrainStateChanged(curBackendsMap, updBackendsMap) {
} else if onlyWeightChanged(curBackendsMap, updBackendsMap) {
// Everything is the same except for the server's weight, so we can use the stats socket to update all of the server weights.
reloadRequired = false
updatedConfig.BackendSlots = currentConfig.BackendSlots
for _, backendName := range curKeys {
backendSlots := updatedConfig.BackendSlots[backendName]
updEndpoints := ingressEndpointsRemap(updBackendsMap[backendName].Endpoints)
for name, endpoint := range updEndpoints {
if backendSlots.FullSlots[name].BackendEndpoint.Draining != endpoint.Draining {
for name, updEndpoint := range updEndpoints {
curEndpoint := backendSlots.FullSlots[name].BackendEndpoint
if curEndpoint.Weight != updEndpoint.Weight {
backendServerName := backendSlots.FullSlots[name].BackendServerName
backendSlots.FullSlots[name] = types.HAProxyBackendSlot{
BackendServerName: backendServerName,
BackendEndpoint: endpoint,
BackendEndpoint: updEndpoint,
}
reloadRequired = reloadRequired || !setEndpointDrainState(currentConfig.Cfg.StatsSocket, backendName, backendServerName, endpoint.Weight, endpoint.Draining)
reloadRequired = reloadRequired || !setEndpointWeight(currentConfig.Cfg.StatsSocket, backendName, backendServerName, updEndpoint.Weight)
}
}
}
Expand Down Expand Up @@ -181,7 +185,7 @@ func reconfigureBackends(currentConfig, updatedConfig *types.ControllerConfig) b
BackendEndpoint: endpoint,
}
backendSlots.EmptySlots = backendSlots.EmptySlots[1:]
reloadRequired = reloadRequired || !addEndpoint(currentConfig.Cfg.StatsSocket, backendName, backendSlots.FullSlots[k].BackendServerName, endpoint.Address, endpoint.Port, endpoint.Weight, endpoint.Draining)
reloadRequired = reloadRequired || !addEndpoint(currentConfig.Cfg.StatsSocket, backendName, backendSlots.FullSlots[k].BackendServerName, endpoint.Address, endpoint.Port, endpoint.Weight)
}
updatedConfig.BackendSlots[backendName] = backendSlots
}
Expand All @@ -200,9 +204,10 @@ func reconfigureBackends(currentConfig, updatedConfig *types.ControllerConfig) b
return reloadRequired
}

// Determines whether or not the endpoints lists in curBackendsMap and updBackendsMap differ only in state of Weight and Draining.
// Determines whether or not the endpoints lists in curBackendsMap and updBackendsMap
// differ only in state of Weight.
// NOTE: This function assumes that the keys in the incoming maps are identical
func onlyDrainStateChanged(curBackendsMap, updBackendsMap map[string]*ingress.Backend) bool {
func onlyWeightChanged(curBackendsMap, updBackendsMap map[string]*ingress.Backend) bool {
for name, curBackend := range curBackendsMap {
updBackend := updBackendsMap[name]
// If the endpoints length changed, then we can abort early
Expand All @@ -221,7 +226,6 @@ func onlyDrainStateChanged(curBackendsMap, updBackendsMap map[string]*ingress.Ba
if ue, ok := updEndpoints[key]; ok {
ce := curBackend.Endpoints[i]
ueCopy := *ue
ueCopy.Draining = ce.Draining
ueCopy.Weight = ce.Weight
if !ce.Equal(&ueCopy) {
return false
Expand Down

0 comments on commit 466fb68

Please sign in to comment.