-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: add load balancer backend pool batch updater #4391
Merged
k8s-ci-robot
merged 2 commits into
kubernetes-sigs:master
from
nilo19:feat/multi-slb/local-svc/batch
Aug 1, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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,220 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/klog/v2" | ||
"k8s.io/utils/pointer" | ||
|
||
"sigs.k8s.io/cloud-provider-azure/pkg/consts" | ||
"sigs.k8s.io/cloud-provider-azure/pkg/retry" | ||
) | ||
|
||
// batchProcessor collects operations in a certain interval and then processes them in batches. | ||
type batchProcessor interface { | ||
// run starts the batchProcessor, and stops if the context exits. | ||
run(ctx context.Context) | ||
|
||
// addOperation adds an operation to the batchProcessor. | ||
addOperation(operation batchOperation) batchOperation | ||
} | ||
|
||
// batchOperation is an operation that can be added to a batchProcessor. | ||
type batchOperation interface { | ||
wait() batchOperationResult | ||
} | ||
|
||
// loadBalancerBackendPoolUpdateOperation is an operation that updates the backend pool of a load balancer. | ||
type loadBalancerBackendPoolUpdateOperation struct { | ||
loadBalancerName string | ||
backendPoolName string | ||
kind consts.LoadBalancerBackendPoolUpdateOperation | ||
nodeIPs []string | ||
result chan batchOperationResult | ||
} | ||
|
||
func (op *loadBalancerBackendPoolUpdateOperation) wait() batchOperationResult { | ||
return <-op.result | ||
} | ||
|
||
// loadBalancerBackendPoolUpdater is a batchProcessor that updates the backend pool of a load balancer. | ||
type loadBalancerBackendPoolUpdater struct { | ||
az *Cloud | ||
interval time.Duration | ||
lock sync.Mutex | ||
operations []batchOperation | ||
} | ||
|
||
// newLoadBalancerBackendPoolUpdater creates a new loadBalancerBackendPoolUpdater. | ||
func newLoadBalancerBackendPoolUpdater(az *Cloud, interval time.Duration) *loadBalancerBackendPoolUpdater { | ||
return &loadBalancerBackendPoolUpdater{ | ||
az: az, | ||
interval: interval, | ||
operations: make([]batchOperation, 0), | ||
} | ||
} | ||
|
||
// run starts the loadBalancerBackendPoolUpdater, and stops if the context exits. | ||
func (updater *loadBalancerBackendPoolUpdater) run(ctx context.Context) { | ||
err := wait.PollUntilContextCancel(ctx, updater.interval, false, func(ctx context.Context) (bool, error) { | ||
updater.process() | ||
return false, nil | ||
}) | ||
if err != nil { | ||
klog.Infof("loadBalancerBackendPoolUpdater.run: stopped due to %s", err.Error()) | ||
} | ||
} | ||
|
||
// getAddIPsToBackendPoolOperation creates a new loadBalancerBackendPoolUpdateOperation | ||
// that adds nodeIPs to the backend pool. | ||
func getAddIPsToBackendPoolOperation(loadBalancerName, backendPoolName string, nodeIPs []string) *loadBalancerBackendPoolUpdateOperation { | ||
return &loadBalancerBackendPoolUpdateOperation{ | ||
loadBalancerName: loadBalancerName, | ||
backendPoolName: backendPoolName, | ||
kind: consts.LoadBalancerBackendPoolUpdateOperationAdd, | ||
nodeIPs: nodeIPs, | ||
result: make(chan batchOperationResult), | ||
} | ||
} | ||
|
||
// getRemoveIPsFromBackendPoolOperation creates a new loadBalancerBackendPoolUpdateOperation | ||
// that removes nodeIPs from the backend pool. | ||
func getRemoveIPsFromBackendPoolOperation(loadBalancerName, backendPoolName string, nodeIPs []string) *loadBalancerBackendPoolUpdateOperation { | ||
return &loadBalancerBackendPoolUpdateOperation{ | ||
loadBalancerName: loadBalancerName, | ||
backendPoolName: backendPoolName, | ||
kind: consts.LoadBalancerBackendPoolUpdateOperationRemove, | ||
nodeIPs: nodeIPs, | ||
result: make(chan batchOperationResult), | ||
} | ||
} | ||
|
||
// addOperation adds an operation to the loadBalancerBackendPoolUpdater. | ||
func (updater *loadBalancerBackendPoolUpdater) addOperation(operation batchOperation) batchOperation { | ||
updater.lock.Lock() | ||
defer updater.lock.Unlock() | ||
|
||
updater.operations = append(updater.operations, operation) | ||
return operation | ||
} | ||
|
||
// process processes all operations in the loadBalancerBackendPoolUpdater. | ||
// It merges operations that have the same loadBalancerName and backendPoolName, | ||
// and then processes them in batches. If an operation fails, it will be retried | ||
// if it is retriable, otherwise all operations in the batch targeting to | ||
// this backend pool will fail. | ||
func (updater *loadBalancerBackendPoolUpdater) process() { | ||
updater.lock.Lock() | ||
defer updater.lock.Unlock() | ||
|
||
if len(updater.operations) == 0 { | ||
klog.V(4).Infof("loadBalancerBackendPoolUpdater.process: no operations to process") | ||
return | ||
} | ||
|
||
// Group operations by loadBalancerName:backendPoolName | ||
groups := make(map[string][]batchOperation) | ||
for _, op := range updater.operations { | ||
lbOp := op.(*loadBalancerBackendPoolUpdateOperation) | ||
key := fmt.Sprintf("%s:%s", lbOp.loadBalancerName, lbOp.backendPoolName) | ||
groups[key] = append(groups[key], op) | ||
} | ||
|
||
// Clear all jobs. | ||
updater.operations = make([]batchOperation, 0) | ||
|
||
for key, ops := range groups { | ||
parts := strings.Split(key, ":") | ||
lbName, poolName := parts[0], parts[1] | ||
operationName := fmt.Sprintf("%s/%s", lbName, poolName) | ||
bp, rerr := updater.az.LoadBalancerClient.GetLBBackendPool(context.Background(), updater.az.ResourceGroup, lbName, poolName, "") | ||
if rerr != nil { | ||
updater.processError(rerr, operationName, ops...) | ||
continue | ||
} | ||
|
||
var changed bool | ||
for _, op := range ops { | ||
lbOp := op.(*loadBalancerBackendPoolUpdateOperation) | ||
switch lbOp.kind { | ||
case consts.LoadBalancerBackendPoolUpdateOperationRemove: | ||
changed = removeNodeIPAddressesFromBackendPool(bp, lbOp.nodeIPs, false, true) | ||
case consts.LoadBalancerBackendPoolUpdateOperationAdd: | ||
changed = updater.az.addNodeIPAddressesToBackendPool(&bp, lbOp.nodeIPs) | ||
default: | ||
panic("loadBalancerBackendPoolUpdater.process: unknown operation type") | ||
} | ||
} | ||
// To keep the code clean, ignore the case when `changed` is true | ||
// but the backend pool object is not changed after multiple times of removal and re-adding. | ||
if changed { | ||
rerr = updater.az.LoadBalancerClient.CreateOrUpdateBackendPools(context.Background(), updater.az.ResourceGroup, lbName, poolName, bp, pointer.StringDeref(bp.Etag, "")) | ||
if rerr != nil { | ||
updater.processError(rerr, operationName, ops...) | ||
continue | ||
} | ||
} | ||
notify(newBatchOperationResult(operationName, true, nil), ops...) | ||
} | ||
} | ||
|
||
// processError mark the operations as retriable if the error is retriable, | ||
// and fail all operations if the error is not retriable. | ||
func (updater *loadBalancerBackendPoolUpdater) processError( | ||
rerr *retry.Error, | ||
operationName string, | ||
operations ...batchOperation, | ||
) { | ||
if rerr.Retriable { | ||
// Retry if retriable. | ||
updater.operations = append(updater.operations, operations...) | ||
} else { | ||
// Fail all operations if not retriable. | ||
notify(newBatchOperationResult(operationName, false, rerr.Error()), operations...) | ||
} | ||
} | ||
|
||
// notify notifies the operations with the result. | ||
func notify(res batchOperationResult, operations ...batchOperation) { | ||
for _, op := range operations { | ||
lbOp := op.(*loadBalancerBackendPoolUpdateOperation) | ||
lbOp.result <- res | ||
} | ||
} | ||
|
||
// batchOperationResult is the result of a batch operation. | ||
type batchOperationResult struct { | ||
name string | ||
success bool | ||
err error | ||
} | ||
|
||
// newBatchOperationResult creates a new batchOperationResult. | ||
func newBatchOperationResult(name string, success bool, err error) batchOperationResult { | ||
return batchOperationResult{ | ||
name: name, | ||
success: success, | ||
err: err, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add two new configurations for the batch updater intervals (LB and route)? we may need to tune the intervals from time to time (this could be done in a following PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.