-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
511 lines (428 loc) · 12.8 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright 2024 kharf
//
// 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 kube
import (
"context"
"errors"
"slices"
"strings"
"time"
"helm.sh/helm/v3/pkg/action"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
)
type InMemoryRESTClientGetter struct {
Cfg *rest.Config
RestMapper meta.RESTMapper
}
var _ action.RESTClientGetter = (*InMemoryRESTClientGetter)(nil)
func (c *InMemoryRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
return c.Cfg, nil
}
func (c *InMemoryRESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
client, err := discovery.NewDiscoveryClientForConfig(c.Cfg)
return memory.NewMemCacheClient(client), err
}
func (c *InMemoryRESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
return c.RestMapper, nil
}
func (c *InMemoryRESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
}
type applyOptions struct {
dryRun bool
force bool
}
// ApplyOption is a specific configuration used for applying changes to an object.
type ApplyOption interface {
Apply(opts *applyOptions)
}
// DryRun indicates that modifications should not be persisted.
type DryRun bool
func (dr DryRun) Apply(opts *applyOptions) {
opts.dryRun = bool(dr)
}
// Force indicates that conflicts should not error.
type Force bool
func (f Force) Apply(opts *applyOptions) {
opts.force = bool(f)
}
// Client connects to a Kubernetes cluster
// to create, read, update and delete manifests/objects.
type Client[T any, R any] interface {
// Apply applies changes to an object through a Server-Side Apply
// and takes the ownership of this object.
// The object is created when it does not exist.
// It errors on conflicts if force is set to false.
Apply(ctx context.Context, obj *T, fieldManager string, opts ...ApplyOption) error
// Get retrieves the unstructured object from a Kubernetes cluster.
Get(ctx context.Context, obj *T) (*R, error)
// Delete removes the object from the Kubernetes cluster.
Delete(ctx context.Context, obj *T) error
// Returns the [meta.RESTMapper] associated with this client.
RESTMapper() meta.RESTMapper
}
// DynamicClient connects to a Kubernetes cluster
// to create, read, update and delete unstructured manifests/objects.
type DynamicClient struct {
dynamicClient *dynamic.DynamicClient
restMapper meta.RESTMapper
invalidate func()
}
var _ Client[unstructured.Unstructured, unstructured.Unstructured] = (*DynamicClient)(nil)
// NewDynamicClient constructs a new DynamicClient,
// which connects to a Kubernetes cluster to create, read, update and delete unstructured manifests/objects.
func NewDynamicClient(config *rest.Config) (*DynamicClient, error) {
dynClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
cacheClient := memory.NewMemCacheClient(discoveryClient)
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cacheClient)
return &DynamicClient{
dynamicClient: dynClient,
restMapper: restMapper,
invalidate: restMapper.Reset,
}, nil
}
// Invalidate resets the internally cached Discovery information and will
// cause the next mapping request to re-discover.
func (client *DynamicClient) Invalidate() error {
client.invalidate()
return nil
}
// Apply applies changes to an object through a Server-Side Apply
// and takes the ownership of this object.
// The object is created when it does not exist.
// It errors on conflicts if force is set to false.
func (client *DynamicClient) Apply(
ctx context.Context,
obj *unstructured.Unstructured,
fieldManager string,
opts ...ApplyOption,
) error {
applyOptions := new(applyOptions)
for _, opt := range opts {
opt.Apply(applyOptions)
}
return client.apply(
ctx,
obj,
fieldManager,
applyOptions,
)
}
func (client *DynamicClient) apply(
ctx context.Context,
obj *unstructured.Unstructured,
fieldManager string,
options *applyOptions,
) error {
resourceInterface, err := client.resourceInterface(obj.GroupVersionKind(), obj.GetNamespace())
if err != nil {
return err
}
createOptions := v1.ApplyOptions{
FieldManager: fieldManager,
Force: options.force,
}
if options.dryRun {
createOptions.DryRun = []string{"All"}
}
_, err = resourceInterface.Apply(ctx, obj.GetName(), obj, createOptions)
if err != nil {
return err
}
if !options.dryRun {
timeoutCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
_, err = client.wait(
timeoutCtx,
obj.GetName(),
v1.TypeMeta{
Kind: obj.GetKind(),
APIVersion: obj.GetAPIVersion(),
},
resourceInterface,
)
if err != nil {
return err
}
}
if obj.GetKind() == "CustomResourceDefinition" {
// clear cache because we just introduced a new crd
if err := client.Invalidate(); err != nil {
return err
}
}
return nil
}
func (client *DynamicClient) wait(
ctx context.Context,
name string,
typeMeta v1.TypeMeta,
resourceInterface dynamic.ResourceInterface,
) (bool, error) {
if typeMeta.Kind != "CustomResourceDefinition" {
return true, nil
}
select {
case <-ctx.Done():
return false, ctx.Err()
default:
}
obj, err := resourceInterface.Get(ctx, name, v1.GetOptions{
TypeMeta: typeMeta,
})
if err != nil {
return false, err
}
conditions := getConditions(obj)
ok := slices.ContainsFunc(conditions, func(cond condition) bool {
return cond.cType == string(apiextensionsv1.Established) &&
cond.status == string(apiextensionsv1.ConditionTrue)
})
if ok {
return true, nil
}
time.Sleep(1 * time.Second)
return client.wait(ctx, name, typeMeta, resourceInterface)
}
type condition struct {
cType string
status string
}
func getConditions(obj *unstructured.Unstructured) []condition {
conditions := make([]condition, 0, 2)
status, ok := obj.Object["status"]
if !ok {
return conditions
}
statusMap, ok := status.(map[string]interface{})
if !ok {
return conditions
}
conditionsArr, ok := statusMap["conditions"].([]interface{})
if !ok {
return conditions
}
for _, c := range conditionsArr {
cond, ok := c.(map[string]interface{})
if !ok {
return conditions
}
t, ok := cond["type"].(string)
if !ok {
return conditions
}
status, ok := cond["status"].(string)
if !ok {
return conditions
}
conditions = append(conditions, condition{cType: t, status: status})
}
return conditions
}
// Delete removes the unstructured object from a Kubernetes cluster.
// Following fields have to be set on obj:
// - GVK, Namespace, Name
func (client *DynamicClient) Delete(ctx context.Context, obj *unstructured.Unstructured) error {
resourceInterface, err := client.resourceInterface(obj.GroupVersionKind(), obj.GetNamespace())
if err != nil {
return err
}
if err := resourceInterface.Delete(ctx, obj.GetName(), v1.DeleteOptions{
TypeMeta: v1.TypeMeta{
Kind: obj.GetKind(),
APIVersion: obj.GetAPIVersion(),
},
}); err != nil {
return err
}
return nil
}
var (
ErrManifestNoMetadata = errors.New("Helm chart manifest has no metadata")
)
// Get retrieves the unstructured object from a Kubernetes cluster.
// Following fields have to be set on obj:
// - GVK, Name
func (client *DynamicClient) Get(
ctx context.Context,
obj *unstructured.Unstructured,
) (*unstructured.Unstructured, error) {
namespace := ""
metadata, ok := obj.Object["metadata"].(map[string]interface{})
if !ok {
return nil, ErrManifestNoMetadata
}
namespace, ok = metadata["namespace"].(string)
if !ok {
namespace = ""
}
resourceInterface, err := client.resourceInterface(obj.GroupVersionKind(), namespace)
if err != nil {
return nil, err
}
name := metadata["name"].(string)
foundObj, err := resourceInterface.Get(ctx, name, v1.GetOptions{
TypeMeta: v1.TypeMeta{
Kind: obj.GetKind(),
APIVersion: obj.GetAPIVersion(),
},
})
if err != nil {
return nil, err
}
return foundObj, nil
}
func (client *DynamicClient) RESTMapper() meta.RESTMapper {
return client.restMapper
}
func (client *DynamicClient) resourceInterface(
gvk schema.GroupVersionKind,
namespace string,
) (dynamic.ResourceInterface, error) {
restMapper := client.restMapper
dynamicClient := client.dynamicClient
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, err
}
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
return dynamicClient.Resource(mapping.Resource).Namespace(namespace), nil
}
return dynamicClient.Resource(mapping.Resource), nil
}
// DynamicClient connects to a Kubernetes cluster
// to create, read, update and delete extended unstructured manifests/objects.
type ExtendedDynamicClient struct {
dynamicClient *DynamicClient
}
var _ Client[ExtendedUnstructured, unstructured.Unstructured] = (*ExtendedDynamicClient)(nil)
// NewExtendedDynamicClient constructs a new DynamicClient,
// which connects to a Kubernetes cluster to create, read, update and delete unstructured manifests/objects.
func NewExtendedDynamicClient(config *rest.Config) (*ExtendedDynamicClient, error) {
dynClient, err := NewDynamicClient(config)
if err != nil {
return nil, err
}
return &ExtendedDynamicClient{
dynamicClient: dynClient,
}, nil
}
func (e *ExtendedDynamicClient) DynamicClient() *DynamicClient {
return e.dynamicClient
}
func (e *ExtendedDynamicClient) Apply(
ctx context.Context,
obj *ExtendedUnstructured,
fieldManager string,
opts ...ApplyOption,
) error {
applyOptions := new(applyOptions)
for _, opt := range opts {
opt.Apply(applyOptions)
}
originalForce := applyOptions.force
// First try always applies and errors on conflict.
// That is done to avoid ownership push around because there might be other managers specifically managing fields of manifests.
// For example, HPAs managing replicas fields.
applyOptions.force = false
if err := e.dynamicClient.apply(ctx, obj.Unstructured, fieldManager, applyOptions); err != nil {
statusErr, ok := err.(*k8sErrors.StatusError)
if ok && statusErr.Status().Reason == v1.StatusReasonConflict {
if !obj.AttributeInfo.HasIgnoreConflictAttributes && !originalForce {
return err
}
unstr := obj.Unstructured
if obj.AttributeInfo.HasIgnoreConflictAttributes {
causes := statusErr.Status().Details.Causes
unstr = obj.DeepCopy()
for _, cause := range causes {
if err := deleteIgnoredFields(cause.Field, unstr.Object, obj.Metadata); err != nil {
return err
}
}
}
// Retry with original force option.
applyOptions.force = originalForce
if err := e.dynamicClient.apply(ctx, unstr, fieldManager, applyOptions); err != nil {
return err
}
} else {
return err
}
}
return nil
}
func deleteIgnoredFields(
jsonPath string,
unstrMap map[string]any,
metadata ManifestMetadata,
) error {
mtn, ok := metadata.(*ManifestMetadataNode)
if !ok {
return nil
}
metadataNode := *mtn
keys := strings.Split(jsonPath, ".")
for i, key := range keys {
if i == 0 {
continue
}
if i == len(keys)-1 {
fieldMetadata, ok := metadataNode[key].(*ManifestFieldMetadata)
if ok && fieldMetadata.IgnoreAttr == OnConflict {
delete(unstrMap, key)
}
return nil
}
metadata, found := metadataNode[key]
if !found {
break
}
unstrMap = unstrMap[key].(map[string]any)
metadataNode = *(metadata.(*ManifestMetadataNode))
}
return nil
}
func (e *ExtendedDynamicClient) Delete(ctx context.Context, obj *ExtendedUnstructured) error {
return e.dynamicClient.Delete(ctx, obj.Unstructured)
}
func (e *ExtendedDynamicClient) Get(
ctx context.Context,
obj *ExtendedUnstructured,
) (*unstructured.Unstructured, error) {
return e.dynamicClient.Get(ctx, obj.Unstructured)
}
func (e *ExtendedDynamicClient) RESTMapper() meta.RESTMapper {
return e.dynamicClient.RESTMapper()
}