Skip to content

Commit 210f1a9

Browse files
committed
Move client config to a separate package
1 parent bd239d4 commit 210f1a9

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = [
6+
"azure_client_config.go",
7+
"doc.go",
8+
],
9+
importmap = "k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/azure/clients",
10+
importpath = "k8s.io/legacy-cloud-providers/azure/clients",
11+
visibility = ["//visibility:public"],
12+
deps = [
13+
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
14+
"//staging/src/k8s.io/legacy-cloud-providers/azure/retry:go_default_library",
15+
"//vendor/github.com/Azure/go-autorest/autorest/adal:go_default_library",
16+
],
17+
)
18+
19+
filegroup(
20+
name = "package-srcs",
21+
srcs = glob(["**"]),
22+
tags = ["automanaged"],
23+
visibility = ["//visibility:private"],
24+
)
25+
26+
filegroup(
27+
name = "all-srcs",
28+
srcs = [":package-srcs"],
29+
tags = ["automanaged"],
30+
visibility = ["//visibility:public"],
31+
)
32+
33+
go_test(
34+
name = "go_default_test",
35+
srcs = ["azure_client_config_test.go"],
36+
embed = [":go_default_library"],
37+
deps = [
38+
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
39+
"//vendor/github.com/stretchr/testify/assert:go_default_library",
40+
],
41+
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
"k8s.io/client-go/util/flowcontrol"
26+
)
27+
28+
func TestWithRateLimiter(t *testing.T) {
29+
config := &ClientConfig{}
30+
assert.Nil(t, config.RateLimitConfig)
31+
config.WithRateLimiter(&RateLimitConfig{CloudProviderRateLimit: true})
32+
assert.Equal(t, &RateLimitConfig{CloudProviderRateLimit: true}, config.RateLimitConfig)
33+
config.WithRateLimiter(nil)
34+
assert.Nil(t, config.RateLimitConfig)
35+
}
36+
37+
func TestRateLimitEnabled(t *testing.T) {
38+
assert.Equal(t, false, RateLimitEnabled(nil))
39+
config := &RateLimitConfig{}
40+
assert.Equal(t, false, RateLimitEnabled(config))
41+
config.CloudProviderRateLimit = true
42+
assert.Equal(t, true, RateLimitEnabled(config))
43+
}
44+
45+
func TestNewRateLimiter(t *testing.T) {
46+
fakeRateLimiter := flowcontrol.NewFakeAlwaysRateLimiter()
47+
readLimiter, writeLimiter := NewRateLimiter(nil)
48+
assert.Equal(t, readLimiter, fakeRateLimiter)
49+
assert.Equal(t, writeLimiter, fakeRateLimiter)
50+
51+
rateLimitConfig := &RateLimitConfig{
52+
CloudProviderRateLimit: false,
53+
}
54+
readLimiter, writeLimiter = NewRateLimiter(rateLimitConfig)
55+
assert.Equal(t, readLimiter, fakeRateLimiter)
56+
assert.Equal(t, writeLimiter, fakeRateLimiter)
57+
58+
rateLimitConfig = &RateLimitConfig{
59+
CloudProviderRateLimit: true,
60+
CloudProviderRateLimitQPS: 3,
61+
CloudProviderRateLimitBucket: 10,
62+
CloudProviderRateLimitQPSWrite: 1,
63+
CloudProviderRateLimitBucketWrite: 3,
64+
}
65+
readLimiter, writeLimiter = NewRateLimiter(rateLimitConfig)
66+
assert.Equal(t, flowcontrol.NewTokenBucketRateLimiter(3, 10), readLimiter)
67+
assert.Equal(t, flowcontrol.NewTokenBucketRateLimiter(1, 3), writeLimiter)
68+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 contains a set of Azure ARM clients.
20+
package clients // import "k8s.io/legacy-cloud-providers/azure/clients"

0 commit comments

Comments
 (0)