|
| 1 | +// +build !providerless |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2019 The Kubernetes Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package clients |
| 20 | + |
| 21 | +import ( |
| 22 | + "github.com/Azure/go-autorest/autorest/adal" |
| 23 | + "k8s.io/client-go/util/flowcontrol" |
| 24 | + "k8s.io/legacy-cloud-providers/azure/retry" |
| 25 | +) |
| 26 | + |
| 27 | +// ClientConfig contains all essential information to create an Azure client. |
| 28 | +type ClientConfig struct { |
| 29 | + Location string |
| 30 | + SubscriptionID string |
| 31 | + ResourceManagerEndpoint string |
| 32 | + ServicePrincipalToken *adal.ServicePrincipalToken |
| 33 | + RateLimitConfig *RateLimitConfig |
| 34 | + Backoff *retry.Backoff |
| 35 | + |
| 36 | + // Depracated configures (retry.Backoff is preferred). |
| 37 | + // Those configurations would be removed after all Azure clients are moved to new implementations. |
| 38 | + CloudProviderBackoffRetries int |
| 39 | + CloudProviderBackoffDuration int |
| 40 | + ShouldOmitCloudProviderBackoff bool |
| 41 | +} |
| 42 | + |
| 43 | +// WithRateLimiter returns ClientConfig with rateLimitConfig set. |
| 44 | +func (cfg *ClientConfig) WithRateLimiter(rl *RateLimitConfig) *ClientConfig { |
| 45 | + cfg.RateLimitConfig = rl |
| 46 | + return cfg |
| 47 | +} |
| 48 | + |
| 49 | +// RateLimitConfig indicates the rate limit config options. |
| 50 | +type RateLimitConfig struct { |
| 51 | + // Enable rate limiting |
| 52 | + CloudProviderRateLimit bool `json:"cloudProviderRateLimit,omitempty" yaml:"cloudProviderRateLimit,omitempty"` |
| 53 | + // Rate limit QPS (Read) |
| 54 | + CloudProviderRateLimitQPS float32 `json:"cloudProviderRateLimitQPS,omitempty" yaml:"cloudProviderRateLimitQPS,omitempty"` |
| 55 | + // Rate limit Bucket Size |
| 56 | + CloudProviderRateLimitBucket int `json:"cloudProviderRateLimitBucket,omitempty" yaml:"cloudProviderRateLimitBucket,omitempty"` |
| 57 | + // Rate limit QPS (Write) |
| 58 | + CloudProviderRateLimitQPSWrite float32 `json:"cloudProviderRateLimitQPSWrite,omitempty" yaml:"cloudProviderRateLimitQPSWrite,omitempty"` |
| 59 | + // Rate limit Bucket Size |
| 60 | + CloudProviderRateLimitBucketWrite int `json:"cloudProviderRateLimitBucketWrite,omitempty" yaml:"cloudProviderRateLimitBucketWrite,omitempty"` |
| 61 | +} |
| 62 | + |
| 63 | +// RateLimitEnabled returns true if CloudProviderRateLimit is set to true. |
| 64 | +func RateLimitEnabled(config *RateLimitConfig) bool { |
| 65 | + return config != nil && config.CloudProviderRateLimit |
| 66 | +} |
| 67 | + |
| 68 | +// NewRateLimiter creates new read and write flowcontrol.RateLimiter from RateLimitConfig. |
| 69 | +func NewRateLimiter(config *RateLimitConfig) (flowcontrol.RateLimiter, flowcontrol.RateLimiter) { |
| 70 | + readLimiter := flowcontrol.NewFakeAlwaysRateLimiter() |
| 71 | + writeLimiter := flowcontrol.NewFakeAlwaysRateLimiter() |
| 72 | + |
| 73 | + if config != nil && config.CloudProviderRateLimit { |
| 74 | + readLimiter = flowcontrol.NewTokenBucketRateLimiter( |
| 75 | + config.CloudProviderRateLimitQPS, |
| 76 | + config.CloudProviderRateLimitBucket) |
| 77 | + |
| 78 | + writeLimiter = flowcontrol.NewTokenBucketRateLimiter( |
| 79 | + config.CloudProviderRateLimitQPSWrite, |
| 80 | + config.CloudProviderRateLimitBucketWrite) |
| 81 | + } |
| 82 | + |
| 83 | + return readLimiter, writeLimiter |
| 84 | +} |
0 commit comments