Skip to content

Commit

Permalink
De-duplicate load balancer health probes
Browse files Browse the repository at this point in the history
De-duplicate load balancer health probes by collecting them in an index
keyed by their functional characteristics (protocol, port, interval,
count, and path). Two probes with the same functional characteristics
will generate the same key, and only one of the two will ultimately be added.
  • Loading branch information
rainest committed Oct 6, 2022
1 parent a4f7fe9 commit 7f80566
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/provider/azure_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,25 @@ func (az *Cloud) buildHealthProbeRulesForPort(annotations map[string]string, por
return probe, nil
}

// probeKey generates a string key containing all functional probe configuration, to allow de-duplication of like probes.
func probeKey(probe network.Probe) string {
var port, interval, number int32
var path string
if probe.Port != nil {
port = *probe.Port
}
if probe.IntervalInSeconds != nil {
interval = *probe.IntervalInSeconds
}
if probe.NumberOfProbes != nil {
number = *probe.NumberOfProbes
}
if probe.RequestPath != nil {
path = *probe.RequestPath
}
return fmt.Sprintf("%s-%d-%d-%d-%s", probe.Protocol, port, interval, number, path)
}

// buildLBRules
// for following sku: basic loadbalancer vs standard load balancer
// for following scenario: internal vs external
Expand All @@ -2201,6 +2220,7 @@ func (az *Cloud) getExpectedLBRules(

var expectedRules []network.LoadBalancingRule
var expectedProbes []network.Probe
uniqueProbes := make(map[string]network.Probe)

// support podPresence health check when External Traffic Policy is local
// take precedence over user defined probe configuration
Expand All @@ -2221,7 +2241,7 @@ func (az *Cloud) getExpectedLBRules(
NumberOfProbes: to.Int32Ptr(consts.HealthProbeDefaultNumOfProbe),
},
}
expectedProbes = append(expectedProbes, *nodeEndpointHealthprobe)
uniqueProbes[probeKey(*nodeEndpointHealthprobe)] = *nodeEndpointHealthprobe
}

// In HA mode, lb forward traffic of all port to backend
Expand All @@ -2248,7 +2268,7 @@ func (az *Cloud) getExpectedLBRules(
//ignore error because we only need one correct rule
}
if portprobe != nil {
expectedProbes = append(expectedProbes, *portprobe)
uniqueProbes[probeKey(*portprobe)] = *portprobe
props.Probe = &network.SubResource{
ID: to.StringPtr(az.getLoadBalancerProbeID(lbName, az.getLoadBalancerResourceGroup(), *portprobe.Name)),
}
Expand Down Expand Up @@ -2294,7 +2314,7 @@ func (az *Cloud) getExpectedLBRules(
return expectedProbes, expectedRules, err
}
if portprobe != nil {
expectedProbes = append(expectedProbes, *portprobe)
uniqueProbes[probeKey(*portprobe)] = *portprobe
props.Probe = &network.SubResource{
ID: to.StringPtr(az.getLoadBalancerProbeID(lbName, az.getLoadBalancerResourceGroup(), *portprobe.Name)),
}
Expand All @@ -2316,6 +2336,10 @@ func (az *Cloud) getExpectedLBRules(
}
}

for _, probe := range uniqueProbes {
expectedProbes = append(expectedProbes, probe)
}

return expectedProbes, expectedRules, nil
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/provider/azure_loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2436,6 +2436,14 @@ func TestReconcileLoadBalancerRule(t *testing.T) {
expectedRules: getDefaultTestRules(false),
expectedProbes: getTestProbes("Http", "/", to.Int32Ptr(5), to.Int32Ptr(80), to.Int32Ptr(15012), to.Int32Ptr(2)),
},
{
desc: "getExpectedLBRules should not include duplicate probes when overrides would create them",
service: getTestService("test1", v1.ProtocolTCP, map[string]string{
"service.beta.kubernetes.io/port_8000_health-probe_port": "10080",
}, false, 80, 8000),
expectedRules: []network.LoadBalancingRule{getTestRule(false, 80), getTestRule(false, 8000)},
expectedProbes: getTestProbes("Tcp", "/", to.Int32Ptr(5), to.Int32Ptr(8000), to.Int32Ptr(10080), to.Int32Ptr(2)),
},
}
rules := getDefaultTestRules(true)
rules[0].IdleTimeoutInMinutes = to.Int32Ptr(5)
Expand Down

0 comments on commit 7f80566

Please sign in to comment.