-
Notifications
You must be signed in to change notification settings - Fork 35
/
client.go
296 lines (248 loc) · 8.27 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
/*
* Copyright contributors to the Hyperledger Fabric Operator project
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 controllerclient
import (
"context"
"time"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
//go:generate counterfeiter -o ../../controller/mocks/client.go -fake-name Client . Client
type Client interface {
Get(ctx context.Context, key k8sclient.ObjectKey, obj k8sclient.Object) error
List(ctx context.Context, list k8sclient.ObjectList, opts ...k8sclient.ListOption) error
Create(ctx context.Context, obj k8sclient.Object, opts ...CreateOption) error
CreateOrUpdate(ctx context.Context, obj k8sclient.Object, opts ...CreateOrUpdateOption) error
Delete(ctx context.Context, obj k8sclient.Object, opts ...k8sclient.DeleteOption) error
Patch(ctx context.Context, obj k8sclient.Object, patch k8sclient.Patch, opts ...PatchOption) error
PatchStatus(ctx context.Context, obj k8sclient.Object, patch k8sclient.Patch, opts ...PatchOption) error
Update(ctx context.Context, obj k8sclient.Object, opts ...UpdateOption) error
UpdateStatus(ctx context.Context, obj k8sclient.Object, opts ...k8sclient.UpdateOption) error
}
// GlobalConfig applies the global configuration defined in operator's config to appropriate
// kubernetes resources
type GlobalConfig interface {
Apply(runtime.Object)
}
type ClientImpl struct {
k8sClient k8sclient.Client
GlobalConfig GlobalConfig
}
func New(c k8sclient.Client, gc GlobalConfig) *ClientImpl {
return &ClientImpl{
k8sClient: c,
GlobalConfig: gc,
}
}
func (c *ClientImpl) Get(ctx context.Context, key k8sclient.ObjectKey, obj k8sclient.Object) error {
err := c.k8sClient.Get(ctx, key, obj)
if err != nil {
return err
}
return nil
}
func (c *ClientImpl) List(ctx context.Context, list k8sclient.ObjectList, opts ...k8sclient.ListOption) error {
err := c.k8sClient.List(ctx, list, opts...)
if err != nil {
return err
}
return nil
}
func (c *ClientImpl) Create(ctx context.Context, obj k8sclient.Object, opts ...CreateOption) error {
var createOpts []k8sclient.CreateOption
c.GlobalConfig.Apply(obj)
if opts != nil && len(opts) > 0 {
if err := setControllerReference(opts[0].Owner, obj, opts[0].Scheme); err != nil {
return err
}
createOpts = opts[0].Opts
}
err := c.k8sClient.Create(ctx, obj, createOpts...)
if err != nil {
return util.IgnoreAlreadyExistError(err)
}
return nil
}
func (c *ClientImpl) Patch(ctx context.Context, obj k8sclient.Object, patch k8sclient.Patch, opts ...PatchOption) error {
var patchOpts []k8sclient.PatchOption
c.GlobalConfig.Apply(obj)
if opts != nil && len(opts) > 0 {
if opts[0].Resilient != nil {
return c.ResilientPatch(ctx, obj, opts[0].Resilient, opts[0].Opts...)
}
patchOpts = opts[0].Opts
}
err := c.k8sClient.Patch(ctx, obj, patch, patchOpts...)
if err != nil {
return err
}
return nil
}
func (c *ClientImpl) ResilientPatch(ctx context.Context, obj k8sclient.Object, resilient *ResilientPatch, opts ...k8sclient.PatchOption) error {
retry := resilient.Retry
into := resilient.Into
strategy := resilient.Strategy
c.GlobalConfig.Apply(obj)
for i := 0; i < retry; i++ {
err := c.resilientPatch(ctx, obj, strategy, into, opts...)
if err != nil {
if i == retry {
return err
}
if k8serrors.IsConflict(err) {
time.Sleep(2 * time.Second)
continue
}
return err
}
}
return nil
}
func (c *ClientImpl) resilientPatch(ctx context.Context, obj k8sclient.Object, strategy func(k8sclient.Object) k8sclient.Patch, into k8sclient.Object, opts ...k8sclient.PatchOption) error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
err := c.Get(ctx, key, into)
if err != nil {
return err
}
err = c.k8sClient.Patch(ctx, obj, strategy(into), opts...)
if err != nil {
return err
}
return nil
}
// If utilizing resilient option, nil can be passed for patch parameter
func (c *ClientImpl) PatchStatus(ctx context.Context, obj k8sclient.Object, patch k8sclient.Patch, opts ...PatchOption) error {
var patchOpts []k8sclient.PatchOption
if opts != nil && len(opts) > 0 {
if opts[0].Resilient != nil {
return c.ResilientPatchStatus(ctx, obj, opts[0].Resilient, opts[0].Opts...)
}
patchOpts = opts[0].Opts
}
err := c.k8sClient.Status().Patch(ctx, obj, patch, patchOpts...)
if err != nil {
return err
}
return nil
}
func (c *ClientImpl) ResilientPatchStatus(ctx context.Context, obj k8sclient.Object, resilient *ResilientPatch, opts ...k8sclient.PatchOption) error {
retry := resilient.Retry
into := resilient.Into
strategy := resilient.Strategy
for i := 0; i < retry; i++ {
err := c.resilientPatchStatus(ctx, obj, strategy, into, opts...)
if err != nil {
if i == retry {
return err
}
if k8serrors.IsConflict(err) {
time.Sleep(2 * time.Second)
continue
}
return err
}
}
return nil
}
func (c *ClientImpl) resilientPatchStatus(ctx context.Context, obj k8sclient.Object, strategy func(k8sclient.Object) k8sclient.Patch, into k8sclient.Object, opts ...k8sclient.PatchOption) error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
err := c.Get(ctx, key, into)
if err != nil {
return err
}
err = c.k8sClient.Status().Patch(ctx, obj, strategy(into), opts...)
if err != nil {
return err
}
return nil
}
// NOTE: Currently, Resilient Update is not supported as it requires more specific
// implementation based on scenario. When possible, should utilize resilient Patch.
func (c *ClientImpl) Update(ctx context.Context, obj k8sclient.Object, opts ...UpdateOption) error {
var updateOpts []k8sclient.UpdateOption
c.GlobalConfig.Apply(obj)
if opts != nil && len(opts) > 0 {
if err := setControllerReference(opts[0].Owner, obj, opts[0].Scheme); err != nil {
return err
}
updateOpts = opts[0].Opts
}
err := c.k8sClient.Update(ctx, obj, updateOpts...)
if err != nil {
return err
}
return nil
}
// NOTE: Currently, Resilient UpdateStatus is not supported as it requires more specific
// implementation based on scenario. When possible, should utilize resilient PatchStatus.
func (c *ClientImpl) UpdateStatus(ctx context.Context, obj k8sclient.Object, opts ...k8sclient.UpdateOption) error {
err := c.k8sClient.Status().Update(ctx, obj, opts...)
if err != nil {
return err
}
return nil
}
func (c *ClientImpl) Delete(ctx context.Context, obj k8sclient.Object, opts ...k8sclient.DeleteOption) error {
err := c.k8sClient.Delete(ctx, obj, opts...)
if err != nil {
return err
}
return nil
}
// CreateOrUpdate does not support k8sclient.CreateOption or k8sclient.UpdateOption being passed as variadic parameters,
// if want to use opts use Create or Update methods
func (c *ClientImpl) CreateOrUpdate(ctx context.Context, obj k8sclient.Object, opts ...CreateOrUpdateOption) error {
if opts != nil && len(opts) > 0 {
if err := setControllerReference(opts[0].Owner, obj, opts[0].Scheme); err != nil {
return err
}
}
c.GlobalConfig.Apply(obj)
err := c.k8sClient.Create(ctx, obj)
if err != nil {
if k8serrors.IsAlreadyExists(err) {
return c.k8sClient.Update(ctx, obj)
}
return err
}
return nil
}
func setControllerReference(owner v1.Object, obj v1.Object, scheme *runtime.Scheme) error {
if owner != nil && obj != nil && scheme != nil {
err := controllerutil.SetControllerReference(owner, obj, scheme)
if err != nil {
if _, ok := err.(*controllerutil.AlreadyOwnedError); ok {
return nil
}
return errors.Wrap(err, "controller reference error")
}
}
return nil
}