/
client.go
412 lines (352 loc) · 14.2 KB
/
client.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 kubernetes
import (
"context"
"errors"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
kubernetesclientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
componentbaseconfig "k8s.io/component-base/config"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
gardencoreinstall "github.com/gardener/gardener/pkg/apis/core/install"
seedmanagementinstall "github.com/gardener/gardener/pkg/apis/seedmanagement/install"
settingsinstall "github.com/gardener/gardener/pkg/apis/settings/install"
kubernetescache "github.com/gardener/gardener/pkg/client/kubernetes/cache"
"github.com/gardener/gardener/pkg/utils"
versionutils "github.com/gardener/gardener/pkg/utils/version"
)
const (
// KubeConfig is the key to the kubeconfig
KubeConfig = "kubeconfig"
// AuthClientCertificate references the AuthInfo.ClientCertificate field of a kubeconfig
AuthClientCertificate = "client-certificate"
// AuthClientKey references the AuthInfo.ClientKey field of a kubeconfig
AuthClientKey = "client-key"
// AuthTokenFile references the AuthInfo.Tokenfile field of a kubeconfig
AuthTokenFile = "tokenFile"
// AuthImpersonate references the AuthInfo.Impersonate field of a kubeconfig
AuthImpersonate = "act-as"
// AuthProvider references the AuthInfo.AuthProvider field of a kubeconfig
AuthProvider = "auth-provider"
// AuthExec references the AuthInfo.Exec field of a kubeconfig
AuthExec = "exec"
)
func init() {
// enable protobuf for Gardener API for controller-runtime clients
protobufSchemeBuilder := runtime.NewSchemeBuilder(
gardencoreinstall.AddToScheme,
seedmanagementinstall.AddToScheme,
settingsinstall.AddToScheme,
)
utilruntime.Must(apiutil.AddToProtobufScheme(protobufSchemeBuilder.AddToScheme))
}
// NewClientFromFile creates a new Client struct for a given kubeconfig. The kubeconfig will be
// read from the filesystem at location <kubeconfigPath>. If given, <masterURL> overrides the
// master URL in the kubeconfig.
// If no filepath is given, the in-cluster configuration will be taken into account.
func NewClientFromFile(masterURL, kubeconfigPath string, fns ...ConfigFunc) (Interface, error) {
if kubeconfigPath == "" && masterURL == "" {
kubeconfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
opts := append([]ConfigFunc{WithRESTConfig(kubeconfig)}, fns...)
return NewWithConfig(opts...)
}
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterURL}},
)
config, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
}
opts := append([]ConfigFunc{WithRESTConfig(config), WithClientConfig(clientConfig)}, fns...)
return NewWithConfig(opts...)
}
// NewClientFromBytes creates a new Client struct for a given kubeconfig byte slice.
func NewClientFromBytes(kubeconfig []byte, fns ...ConfigFunc) (Interface, error) {
config, err := RESTConfigFromClientConnectionConfiguration(nil, kubeconfig)
if err != nil {
return nil, err
}
opts := append([]ConfigFunc{WithRESTConfig(config)}, fns...)
return NewWithConfig(opts...)
}
// NewClientFromSecret creates a new Client struct for a given kubeconfig stored as a
// Secret in an existing Kubernetes cluster. This cluster will be accessed by the <k8sClient>. It will
// read the Secret <secretName> in <namespace>. The Secret must contain a field "kubeconfig" which will
// be used.
func NewClientFromSecret(ctx context.Context, c client.Client, namespace, secretName string, fns ...ConfigFunc) (Interface, error) {
secret := &corev1.Secret{}
if err := c.Get(ctx, client.ObjectKey{Namespace: namespace, Name: secretName}, secret); err != nil {
return nil, err
}
return NewClientFromSecretObject(secret, fns...)
}
// NewClientFromSecretObject creates a new Client struct for a given Kubernetes Secret object. The Secret must
// contain a field "kubeconfig" which will be used.
func NewClientFromSecretObject(secret *corev1.Secret, fns ...ConfigFunc) (Interface, error) {
if kubeconfig, ok := secret.Data[KubeConfig]; ok {
if len(kubeconfig) == 0 {
return nil, errors.New("the secret's field 'kubeconfig' is empty")
}
return NewClientFromBytes(kubeconfig, fns...)
}
return nil, errors.New("the secret does not contain a field with name 'kubeconfig'")
}
// RESTConfigFromClientConnectionConfiguration creates a *rest.Config from a componentbaseconfig.ClientConnectionConfiguration and the configured kubeconfig.
// Allowed fields are not considered unsupported if used in the kubeconfig.
func RESTConfigFromClientConnectionConfiguration(cfg *componentbaseconfig.ClientConnectionConfiguration, kubeconfig []byte, allowedFields ...string) (*rest.Config, error) {
var (
restConfig *rest.Config
err error
)
if kubeconfig == nil {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: cfg.Kubeconfig},
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: ""}},
)
if err := validateClientConfig(clientConfig, allowedFields); err != nil {
return nil, err
}
restConfig, err = clientConfig.ClientConfig()
if err != nil {
return nil, err
}
} else {
restConfig, err = RESTConfigFromKubeconfig(kubeconfig, allowedFields...)
if err != nil {
return restConfig, err
}
}
if cfg != nil {
restConfig.Burst = int(cfg.Burst)
restConfig.QPS = cfg.QPS
restConfig.AcceptContentTypes = cfg.AcceptContentTypes
restConfig.ContentType = cfg.ContentType
}
return restConfig, nil
}
// RESTConfigFromKubeconfig returns a rest.Config from the bytes of a kubeconfig.
// Allowed fields are not considered unsupported if used in the kubeconfig.
func RESTConfigFromKubeconfig(kubeconfig []byte, allowedFields ...string) (*rest.Config, error) {
clientConfig, err := clientcmd.NewClientConfigFromBytes(kubeconfig)
if err != nil {
return nil, err
}
if err := validateClientConfig(clientConfig, allowedFields); err != nil {
return nil, err
}
restConfig, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
}
return restConfig, nil
}
func validateClientConfig(clientConfig clientcmd.ClientConfig, allowedFields []string) error {
if clientConfig == nil {
return nil
}
rawConfig, err := clientConfig.RawConfig()
if err != nil {
return err
}
return ValidateConfigWithAllowList(rawConfig, allowedFields)
}
// ValidateConfig validates that the auth info of a given kubeconfig doesn't have unsupported fields.
func ValidateConfig(config clientcmdapi.Config) error {
return ValidateConfigWithAllowList(config, nil)
}
// ValidateConfigWithAllowList validates that the auth info of a given kubeconfig doesn't have unsupported fields. It takes an additional list of allowed fields.
func ValidateConfigWithAllowList(config clientcmdapi.Config, allowedFields []string) error {
validFields := []string{"client-certificate-data", "client-key-data", "token", "username", "password"}
validFields = append(validFields, allowedFields...)
for user, authInfo := range config.AuthInfos {
switch {
case authInfo.ClientCertificate != "" && !utils.ValueExists(AuthClientCertificate, validFields):
return fmt.Errorf("client certificate files are not supported (user %q), these are the valid fields: %+v", user, validFields)
case authInfo.ClientKey != "" && !utils.ValueExists(AuthClientKey, validFields):
return fmt.Errorf("client key files are not supported (user %q), these are the valid fields: %+v", user, validFields)
case authInfo.TokenFile != "" && !utils.ValueExists(AuthTokenFile, validFields):
return fmt.Errorf("token files are not supported (user %q), these are the valid fields: %+v", user, validFields)
case (authInfo.Impersonate != "" || len(authInfo.ImpersonateGroups) > 0) && !utils.ValueExists(AuthImpersonate, validFields):
return fmt.Errorf("impersonation is not supported, these are the valid fields: %+v", validFields)
case (authInfo.AuthProvider != nil && len(authInfo.AuthProvider.Config) > 0) && !utils.ValueExists(AuthProvider, validFields):
return fmt.Errorf("auth provider configurations are not supported (user %q), these are the valid fields: %+v", user, validFields)
case authInfo.Exec != nil && !utils.ValueExists(AuthExec, validFields):
return fmt.Errorf("exec configurations are not supported (user %q), these are the valid fields: %+v", user, validFields)
}
}
return nil
}
var supportedKubernetesVersions = []string{
"1.20",
"1.21",
"1.22",
"1.23",
"1.24",
"1.25",
"1.26",
}
func checkIfSupportedKubernetesVersion(gitVersion string) error {
for _, supportedVersion := range supportedKubernetesVersions {
ok, err := versionutils.CompareVersions(gitVersion, "~", supportedVersion)
if err != nil {
return err
}
if ok {
return nil
}
}
return fmt.Errorf("unsupported kubernetes version %q", gitVersion)
}
// NewWithConfig returns a new Kubernetes base client.
func NewWithConfig(fns ...ConfigFunc) (Interface, error) {
conf := &Config{}
for _, f := range fns {
if err := f(conf); err != nil {
return nil, err
}
}
return newClientSet(conf)
}
func newClientSet(conf *Config) (Interface, error) {
if err := validateClientConfig(conf.clientConfig, conf.allowedUserFields); err != nil {
return nil, err
}
if err := setConfigDefaults(conf); err != nil {
return nil, err
}
var (
runtimeAPIReader = conf.runtimeAPIReader
runtimeClient = conf.runtimeClient
runtimeCache = conf.runtimeCache
err error
)
if runtimeCache == nil {
runtimeCache, err = conf.newRuntimeCache(conf.restConfig, cache.Options{
Scheme: conf.clientOptions.Scheme,
Mapper: conf.clientOptions.Mapper,
Resync: conf.cacheResync,
})
if err != nil {
return nil, err
}
}
var uncachedClient client.Client
if runtimeAPIReader == nil || runtimeClient == nil {
uncachedClient, err = client.New(conf.restConfig, conf.clientOptions)
if err != nil {
return nil, err
}
}
if runtimeAPIReader == nil {
runtimeAPIReader = uncachedClient
}
if runtimeClient == nil {
if conf.disableCache {
runtimeClient = uncachedClient
} else {
delegatingClient, err := client.NewDelegatingClient(client.NewDelegatingClientInput{
CacheReader: runtimeCache,
Client: uncachedClient,
UncachedObjects: conf.uncachedObjects,
})
if err != nil {
return nil, err
}
runtimeClient = &FallbackClient{
Client: delegatingClient,
Reader: runtimeAPIReader,
}
}
}
// prepare rest config with contentType defaulted to protobuf for client-go style clients that either talk to
// kubernetes or aggregated APIs that support protobuf.
cfg := defaultContentTypeProtobuf(conf.restConfig)
kubernetes, err := kubernetesclientset.NewForConfig(cfg)
if err != nil {
return nil, err
}
cs := &clientSet{
config: conf.restConfig,
restClient: kubernetes.Discovery().RESTClient(),
applier: NewApplier(runtimeClient, conf.clientOptions.Mapper),
client: runtimeClient,
apiReader: runtimeAPIReader,
cache: runtimeCache,
kubernetes: kubernetes,
}
if _, err := cs.DiscoverVersion(); err != nil {
return nil, fmt.Errorf("error discovering kubernetes version: %w", err)
}
return cs, nil
}
func setConfigDefaults(conf *Config) error {
// we can't default to protobuf ContentType here, otherwise controller-runtime clients will also try to talk to
// CRD resources (e.g. extension CRs in the Seed) using protobuf, but CRDs don't support protobuf
// see https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/#advanced-features-and-flexibility
if err := setClientOptionsDefaults(conf.restConfig, &conf.clientOptions); err != nil {
return err
}
if conf.newRuntimeCache == nil {
conf.newRuntimeCache = NewRuntimeCache
}
return nil
}
func defaultContentTypeProtobuf(c *rest.Config) *rest.Config {
config := *c
if config.ContentType == "" {
config.ContentType = runtime.ContentTypeProtobuf
}
return &config
}
var _ client.Client = &FallbackClient{}
// FallbackClient holds a `client.Client` and a `client.Reader` which is meant as a fallback
// in case Get/List requests with the ordinary `client.Client` fail (e.g. because of cache errors).
type FallbackClient struct {
client.Client
Reader client.Reader
}
var cacheError = &kubernetescache.CacheError{}
// Get retrieves an obj for a given object key from the Kubernetes Cluster.
// In case of a cache error, the underlying API reader is used to execute the request again.
func (d *FallbackClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
err := d.Client.Get(ctx, key, obj)
if err != nil && errors.As(err, &cacheError) {
logf.Log.V(1).Info("Falling back to API reader because a cache error occurred", "error", err)
return d.Reader.Get(ctx, key, obj)
}
return err
}
// List retrieves list of objects for a given namespace and list options.
// In case of a cache error, the underlying API reader is used to execute the request again.
func (d *FallbackClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
err := d.Client.List(ctx, list, opts...)
if err != nil && errors.As(err, &cacheError) {
logf.Log.V(1).Info("Falling back to API reader because a cache error occurred", "error", err)
return d.Reader.List(ctx, list, opts...)
}
return err
}