mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Move client config to a separate package
This commit is contained in:
parent
bd239d42e4
commit
210f1a904d
@ -0,0 +1,41 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"azure_client_config.go",
|
||||
"doc.go",
|
||||
],
|
||||
importmap = "k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/azure/clients",
|
||||
importpath = "k8s.io/legacy-cloud-providers/azure/clients",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
|
||||
"//staging/src/k8s.io/legacy-cloud-providers/azure/retry:go_default_library",
|
||||
"//vendor/github.com/Azure/go-autorest/autorest/adal:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["azure_client_config_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
],
|
||||
)
|
@ -0,0 +1,84 @@
|
||||
// +build !providerless
|
||||
|
||||
/*
|
||||
Copyright 2019 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 clients
|
||||
|
||||
import (
|
||||
"github.com/Azure/go-autorest/autorest/adal"
|
||||
"k8s.io/client-go/util/flowcontrol"
|
||||
"k8s.io/legacy-cloud-providers/azure/retry"
|
||||
)
|
||||
|
||||
// ClientConfig contains all essential information to create an Azure client.
|
||||
type ClientConfig struct {
|
||||
Location string
|
||||
SubscriptionID string
|
||||
ResourceManagerEndpoint string
|
||||
ServicePrincipalToken *adal.ServicePrincipalToken
|
||||
RateLimitConfig *RateLimitConfig
|
||||
Backoff *retry.Backoff
|
||||
|
||||
// Depracated configures (retry.Backoff is preferred).
|
||||
// Those configurations would be removed after all Azure clients are moved to new implementations.
|
||||
CloudProviderBackoffRetries int
|
||||
CloudProviderBackoffDuration int
|
||||
ShouldOmitCloudProviderBackoff bool
|
||||
}
|
||||
|
||||
// WithRateLimiter returns ClientConfig with rateLimitConfig set.
|
||||
func (cfg *ClientConfig) WithRateLimiter(rl *RateLimitConfig) *ClientConfig {
|
||||
cfg.RateLimitConfig = rl
|
||||
return cfg
|
||||
}
|
||||
|
||||
// RateLimitConfig indicates the rate limit config options.
|
||||
type RateLimitConfig struct {
|
||||
// Enable rate limiting
|
||||
CloudProviderRateLimit bool `json:"cloudProviderRateLimit,omitempty" yaml:"cloudProviderRateLimit,omitempty"`
|
||||
// Rate limit QPS (Read)
|
||||
CloudProviderRateLimitQPS float32 `json:"cloudProviderRateLimitQPS,omitempty" yaml:"cloudProviderRateLimitQPS,omitempty"`
|
||||
// Rate limit Bucket Size
|
||||
CloudProviderRateLimitBucket int `json:"cloudProviderRateLimitBucket,omitempty" yaml:"cloudProviderRateLimitBucket,omitempty"`
|
||||
// Rate limit QPS (Write)
|
||||
CloudProviderRateLimitQPSWrite float32 `json:"cloudProviderRateLimitQPSWrite,omitempty" yaml:"cloudProviderRateLimitQPSWrite,omitempty"`
|
||||
// Rate limit Bucket Size
|
||||
CloudProviderRateLimitBucketWrite int `json:"cloudProviderRateLimitBucketWrite,omitempty" yaml:"cloudProviderRateLimitBucketWrite,omitempty"`
|
||||
}
|
||||
|
||||
// RateLimitEnabled returns true if CloudProviderRateLimit is set to true.
|
||||
func RateLimitEnabled(config *RateLimitConfig) bool {
|
||||
return config != nil && config.CloudProviderRateLimit
|
||||
}
|
||||
|
||||
// NewRateLimiter creates new read and write flowcontrol.RateLimiter from RateLimitConfig.
|
||||
func NewRateLimiter(config *RateLimitConfig) (flowcontrol.RateLimiter, flowcontrol.RateLimiter) {
|
||||
readLimiter := flowcontrol.NewFakeAlwaysRateLimiter()
|
||||
writeLimiter := flowcontrol.NewFakeAlwaysRateLimiter()
|
||||
|
||||
if config != nil && config.CloudProviderRateLimit {
|
||||
readLimiter = flowcontrol.NewTokenBucketRateLimiter(
|
||||
config.CloudProviderRateLimitQPS,
|
||||
config.CloudProviderRateLimitBucket)
|
||||
|
||||
writeLimiter = flowcontrol.NewTokenBucketRateLimiter(
|
||||
config.CloudProviderRateLimitQPSWrite,
|
||||
config.CloudProviderRateLimitBucketWrite)
|
||||
}
|
||||
|
||||
return readLimiter, writeLimiter
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
// +build !providerless
|
||||
|
||||
/*
|
||||
Copyright 2019 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 clients
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"k8s.io/client-go/util/flowcontrol"
|
||||
)
|
||||
|
||||
func TestWithRateLimiter(t *testing.T) {
|
||||
config := &ClientConfig{}
|
||||
assert.Nil(t, config.RateLimitConfig)
|
||||
config.WithRateLimiter(&RateLimitConfig{CloudProviderRateLimit: true})
|
||||
assert.Equal(t, &RateLimitConfig{CloudProviderRateLimit: true}, config.RateLimitConfig)
|
||||
config.WithRateLimiter(nil)
|
||||
assert.Nil(t, config.RateLimitConfig)
|
||||
}
|
||||
|
||||
func TestRateLimitEnabled(t *testing.T) {
|
||||
assert.Equal(t, false, RateLimitEnabled(nil))
|
||||
config := &RateLimitConfig{}
|
||||
assert.Equal(t, false, RateLimitEnabled(config))
|
||||
config.CloudProviderRateLimit = true
|
||||
assert.Equal(t, true, RateLimitEnabled(config))
|
||||
}
|
||||
|
||||
func TestNewRateLimiter(t *testing.T) {
|
||||
fakeRateLimiter := flowcontrol.NewFakeAlwaysRateLimiter()
|
||||
readLimiter, writeLimiter := NewRateLimiter(nil)
|
||||
assert.Equal(t, readLimiter, fakeRateLimiter)
|
||||
assert.Equal(t, writeLimiter, fakeRateLimiter)
|
||||
|
||||
rateLimitConfig := &RateLimitConfig{
|
||||
CloudProviderRateLimit: false,
|
||||
}
|
||||
readLimiter, writeLimiter = NewRateLimiter(rateLimitConfig)
|
||||
assert.Equal(t, readLimiter, fakeRateLimiter)
|
||||
assert.Equal(t, writeLimiter, fakeRateLimiter)
|
||||
|
||||
rateLimitConfig = &RateLimitConfig{
|
||||
CloudProviderRateLimit: true,
|
||||
CloudProviderRateLimitQPS: 3,
|
||||
CloudProviderRateLimitBucket: 10,
|
||||
CloudProviderRateLimitQPSWrite: 1,
|
||||
CloudProviderRateLimitBucketWrite: 3,
|
||||
}
|
||||
readLimiter, writeLimiter = NewRateLimiter(rateLimitConfig)
|
||||
assert.Equal(t, flowcontrol.NewTokenBucketRateLimiter(3, 10), readLimiter)
|
||||
assert.Equal(t, flowcontrol.NewTokenBucketRateLimiter(1, 3), writeLimiter)
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
// +build !providerless
|
||||
|
||||
/*
|
||||
Copyright 2019 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 clients contains a set of Azure ARM clients.
|
||||
package clients // import "k8s.io/legacy-cloud-providers/azure/clients"
|
Loading…
Reference in New Issue
Block a user