-
Notifications
You must be signed in to change notification settings - Fork 87
/
config.go
129 lines (102 loc) · 3.67 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright 2023 The Radius 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 kubeutil
import (
"errors"
"fmt"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
const (
// DefaultServerBurst is the default number of queries per second for server.
DefaultServerQPS float32 = 200.0
// DefaultServerBurst is the default number of queries k8sclient handles concurrently for server.
DefaultServerBurst int = 200
// DefaultCLIQPS is the default number of queries per second for CLI.
DefaultCLIQPS float32 = 50.0
// DefaultCLIBurst is the default number of queries k8sclient handles concurrently for CLI.
DefaultCLIBurst int = 100
)
// ConfigOptions is custom options to configure kubernetes client config.
type ConfigOptions struct {
ConfigFilePath string
QPS float32
Burst int
ContextName string
}
func buildConfigOptions(options *ConfigOptions) *ConfigOptions {
if options == nil {
options = &ConfigOptions{}
}
if options.ConfigFilePath == "" {
options.ConfigFilePath = clientcmd.RecommendedHomeFile
}
return options
}
// LoadConfigFile loads the Kubernetes config file from the given path, or will load the default
// config from the home directory if no path is provided.
func LoadConfigFile(configFilePath string) (*api.Config, error) {
if configFilePath == "" {
configFilePath = clientcmd.RecommendedHomeFile
}
return clientcmd.LoadFromFile(configFilePath)
}
// NewClientConfig builds a Kubernetes client config from either in-cluster or local configuration, and sets the QPS and
// Burst values if provided. It returns an error if the config cannot be built.
func NewClientConfig(options *ConfigOptions) (*rest.Config, error) {
options = buildConfigOptions(options)
var config *rest.Config
var err error
config, err = rest.InClusterConfig()
if errors.Is(err, rest.ErrNotInCluster) {
config, err = NewClientConfigFromLocal(options)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("failed to initialize Kubernetes client config: %w", err)
}
if options.QPS > 0.0 {
config.QPS = options.QPS
}
if options.Burst > 0 {
config.Burst = options.Burst
}
return config, nil
}
// NewClientConfigFromLocal builds a Kubernetes client config from a ConfigOptions instance, loading the config file from the
// ConfigFilePath and setting the QPS and Burst values if specified. It returns an error if the config file cannot be loaded
// or the client config cannot be initialized.
func NewClientConfigFromLocal(options *ConfigOptions) (*rest.Config, error) {
options = buildConfigOptions(options)
cfg, err := LoadConfigFile(options.ConfigFilePath)
if err != nil {
return nil, fmt.Errorf("failed to initialize Kubernetes client config: %w", err)
}
contextName := options.ContextName
if contextName == "" {
contextName = cfg.CurrentContext
}
merged, err := clientcmd.NewNonInteractiveClientConfig(*cfg, contextName, nil, nil).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to initialize Kubernetes client config: %w", err)
}
if options.QPS > 0.0 {
merged.QPS = options.QPS
}
if options.Burst > 0 {
merged.Burst = options.Burst
}
return merged, nil
}