-
Notifications
You must be signed in to change notification settings - Fork 302
/
client_deferred.go
120 lines (102 loc) · 2.75 KB
/
client_deferred.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
package controllers
import (
"context"
"errors"
"fmt"
"sync/atomic"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)
var ErrClientNotInitialized = errors.New("controller client not initialized")
type DeferredClient struct {
impl atomic.Value
}
var _ ctrlclient.Client = &DeferredClient{}
func ProvideDeferredClient() *DeferredClient {
return &DeferredClient{}
}
func (d *DeferredClient) initialize(client ctrlclient.Client) {
d.impl.Store(client)
}
func (d *DeferredClient) client() ctrlclient.Client {
v := d.impl.Load()
if v == nil {
return nil
}
cli, ok := v.(ctrlclient.Client)
if !ok {
panic(fmt.Errorf("deferred client initialized with bad type: %T", v))
}
return cli
}
func (d *DeferredClient) Get(ctx context.Context, key ctrlclient.ObjectKey, obj ctrlclient.Object) error {
cli := d.client()
if cli == nil {
return ErrClientNotInitialized
}
return cli.Get(ctx, key, obj)
}
func (d *DeferredClient) List(ctx context.Context, list ctrlclient.ObjectList, opts ...ctrlclient.ListOption) error {
cli := d.client()
if cli == nil {
return ErrClientNotInitialized
}
return cli.List(ctx, list, opts...)
}
func (d *DeferredClient) Create(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.CreateOption) error {
cli := d.client()
if cli == nil {
return ErrClientNotInitialized
}
return cli.Create(ctx, obj, opts...)
}
func (d *DeferredClient) Delete(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.DeleteOption) error {
cli := d.client()
if cli == nil {
return ErrClientNotInitialized
}
return cli.Delete(ctx, obj, opts...)
}
func (d *DeferredClient) Update(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.UpdateOption) error {
cli := d.client()
if cli == nil {
return ErrClientNotInitialized
}
return cli.Update(ctx, obj, opts...)
}
func (d *DeferredClient) Patch(ctx context.Context, obj ctrlclient.Object, patch ctrlclient.Patch, opts ...ctrlclient.PatchOption) error {
cli := d.client()
if cli == nil {
return ErrClientNotInitialized
}
return cli.Patch(ctx, obj, patch, opts...)
}
func (d *DeferredClient) DeleteAllOf(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.DeleteAllOfOption) error {
cli := d.client()
if cli == nil {
return ErrClientNotInitialized
}
return cli.DeleteAllOf(ctx, obj, opts...)
}
func (d *DeferredClient) Status() ctrlclient.StatusWriter {
cli := d.client()
if cli == nil {
return nil
}
return cli.Status()
}
func (d *DeferredClient) Scheme() *runtime.Scheme {
cli := d.client()
if cli == nil {
return nil
}
return cli.Scheme()
}
func (d *DeferredClient) RESTMapper() meta.RESTMapper {
cli := d.client()
if cli == nil {
return nil
}
return cli.RESTMapper()
}