-
Notifications
You must be signed in to change notification settings - Fork 17
/
kubernetes.go
419 lines (351 loc) · 11 KB
/
kubernetes.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
/*
Copyright 2021 The OpenEBS 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 deploy
import (
"context"
"encoding/json"
"strings"
client "github.com/openebs/lib-csi/pkg/common/kubernetes/client"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)
// getClientsetFn is a typed function
// that abstracts fetching of kubernetes clientset
type getClientsetFn func() (*kubernetes.Clientset, error)
// getClientsetFromPathFn is a typed function that
// abstracts fetching of clientset from kubeconfig path
type getClientsetForPathFn func(path string) (*kubernetes.Clientset, error)
// getFn is a typed function that abstracts fetching a
// deployment instance from kubernetes cluster
type getFn func(
cli *kubernetes.Clientset,
name string,
namespace string,
opts *metav1.GetOptions,
) (*appsv1.Deployment, error)
// listFn is a typed function that abstracts listing
// deployment instances from kubernetes cluster
type listFn func(
cli *kubernetes.Clientset,
namespace string,
opts *metav1.ListOptions,
) (*appsv1.DeploymentList, error)
// createFn is a typed function that abstracts
// creating a deployment instance in kubernetes cluster
type createFn func(
cli *kubernetes.Clientset,
namespace string,
deploy *appsv1.Deployment,
) (*appsv1.Deployment, error)
// deleteFn is a typed function that abstracts
// deleting a deployment from kubernetes cluster
type deleteFn func(
cli *kubernetes.Clientset,
namespace string,
name string,
opts *metav1.DeleteOptions,
) error
// patchFn is a typed function that abstracts
// patching a deployment from kubernetes cluster
type patchFn func(
cli *kubernetes.Clientset,
name, namespace string,
pt types.PatchType,
data []byte,
subresources ...string,
) (*appsv1.Deployment, error)
// rolloutStatusFn is a typed function that abstracts
// fetching rollout status of a deployment instance from
// kubernetes cluster
type rolloutStatusFn func(d *appsv1.Deployment) (*RolloutOutput, error)
// rolloutStatusfFn is a typed function that abstracts
// fetching rollout status of a deployment instance from
// kubernetes cluster
type rolloutStatusfFn func(d *appsv1.Deployment) ([]byte, error)
// defaultGetClientset is the default implementation to
// get kubernetes clientset instance
func defaultGetClientset() (*kubernetes.Clientset, error) {
return client.Instance().Clientset()
}
// defaultGetClientsetForPath is the default implementation to
// get kubernetes clientset instance based on the given
// kubeconfig path
func defaultGetClientsetForPath(path string) (*kubernetes.Clientset, error) {
return client.New(client.WithKubeConfigPath(path)).Clientset()
}
// defaultGet is the default implementation to get a
// deployment instance from kubernetes cluster
func defaultGet(
cli *kubernetes.Clientset,
name string,
namespace string,
opts *metav1.GetOptions,
) (*appsv1.Deployment, error) {
return cli.AppsV1().Deployments(namespace).Get(context.TODO(), name, *opts)
}
// defaultList is the default implementation to list
// deployment instances from kubernetes cluster
func defaultList(
cli *kubernetes.Clientset,
namespace string,
opts *metav1.ListOptions,
) (*appsv1.DeploymentList, error) {
return cli.AppsV1().Deployments(namespace).List(context.TODO(), *opts)
}
// defaultCreate is the default implementation to create
// a deployment instance in kubernetes cluster
func defaultCreate(
cli *kubernetes.Clientset,
namespace string,
deploy *appsv1.Deployment,
) (*appsv1.Deployment, error) {
return cli.AppsV1().Deployments(namespace).Create(context.TODO(), deploy, metav1.CreateOptions{})
}
// defaultDel is the default implementation to delete a
// deployment instance in kubernetes cluster
func defaultDel(
cli *kubernetes.Clientset,
namespace string,
name string,
opts *metav1.DeleteOptions,
) error {
return cli.AppsV1().Deployments(namespace).Delete(context.TODO(), name, *opts)
}
func defaultPatch(
cli *kubernetes.Clientset,
name, namespace string,
pt types.PatchType,
data []byte,
subresources ...string,
) (*appsv1.Deployment, error) {
return cli.AppsV1().Deployments(namespace).Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
}
// defaultRolloutStatus is the default implementation to
// fetch rollout status of a deployment instance from kubernetes
// cluster
func defaultRolloutStatus(d *appsv1.Deployment) (*RolloutOutput, error) {
b := NewForAPIObject(d)
return b.RolloutStatus()
}
// defaultRolloutStatusf is the default implementation to fetch
// rollout status of a deployment instance from kubernetes cluster
func defaultRolloutStatusf(d *appsv1.Deployment) ([]byte, error) {
b := NewForAPIObject(d)
return b.RolloutStatusRaw()
}
// Kubeclient enables kubernetes API operations on deployment instance
type Kubeclient struct {
// clientset refers to kubernetes clientset
//
// It enables CRUD operations of a deployment instance
// against a kubernetes cluster
clientset *kubernetes.Clientset
namespace string
// kubeconfig path to get kubernetes clientset
kubeConfigPath string
// functions useful during mocking
getClientset getClientsetFn
getClientsetForPath getClientsetForPathFn
get getFn
list listFn
create createFn
del deleteFn
patch patchFn
rolloutStatus rolloutStatusFn
rolloutStatusf rolloutStatusfFn
}
// KubeclientBuildOption defines the abstraction to build a
// kubeclient instance
type KubeclientBuildOption func(*Kubeclient)
// withDefaults sets kubeclient instance's fields with defaults
// if these fields are not set
func (k *Kubeclient) withDefaults() {
if k.getClientset == nil {
k.getClientset = defaultGetClientset
}
if k.getClientsetForPath == nil {
k.getClientsetForPath = defaultGetClientsetForPath
}
if k.get == nil {
k.get = defaultGet
}
if k.list == nil {
k.list = defaultList
}
if k.create == nil {
k.create = defaultCreate
}
if k.del == nil {
k.del = defaultDel
}
if k.patch == nil {
k.patch = defaultPatch
}
if k.rolloutStatus == nil {
k.rolloutStatus = defaultRolloutStatus
}
if k.rolloutStatusf == nil {
k.rolloutStatusf = defaultRolloutStatusf
}
}
// WithClientset sets the kubernetes client against the kubeclient instance
func WithClientset(c *kubernetes.Clientset) KubeclientBuildOption {
return func(k *Kubeclient) {
k.clientset = c
}
}
// WithNamespace sets the kubernetes client against
// the provided namespace
func (k *Kubeclient) WithNamespace(namespace string) *Kubeclient {
k.namespace = namespace
return k
}
// WithKubeConfigPath sets the kubeConfig path
// against client instance
func WithKubeConfigPath(path string) KubeclientBuildOption {
return func(k *Kubeclient) {
k.kubeConfigPath = path
}
}
// WithNamespace set namespace in kubeclient object
func WithNamespace(namespace string) KubeclientBuildOption {
return func(k *Kubeclient) {
k.namespace = namespace
}
}
// NewKubeClient returns a new instance of kubeclient meant for deployment.
// caller can configure it with different kubeclientBuildOption
func NewKubeClient(opts ...KubeclientBuildOption) *Kubeclient {
k := &Kubeclient{}
for _, o := range opts {
o(k)
}
k.withDefaults()
return k
}
func (k *Kubeclient) getClientsetForPathOrDirect() (*kubernetes.Clientset, error) {
if k.kubeConfigPath != "" {
return k.getClientsetForPath(k.kubeConfigPath)
}
return k.getClientset()
}
// getClientOrCached returns either a new instance
// of kubernetes client or its cached copy
func (k *Kubeclient) getClientOrCached() (*kubernetes.Clientset, error) {
if k.clientset != nil {
return k.clientset, nil
}
c, err := k.getClientsetForPathOrDirect()
if err != nil {
return nil, err
}
k.clientset = c
return k.clientset, nil
}
// Get returns deployment object for given name
func (k *Kubeclient) Get(name string) (*appsv1.Deployment, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
return k.get(cli, name, k.namespace, &metav1.GetOptions{})
}
// List returns deployment object for given name
func (k *Kubeclient) List(opts *metav1.ListOptions) (*appsv1.DeploymentList, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
return k.list(cli, k.namespace, opts)
}
// Patch patches deployment object for given name
func (k *Kubeclient) Patch(
name string,
pt types.PatchType,
data []byte,
subresources ...string,
) (*appsv1.Deployment, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
return k.patch(cli, name, k.namespace, pt, data, subresources...)
}
// GetRaw returns deployment object for given name
func (k *Kubeclient) GetRaw(name string) ([]byte, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
d, err := k.get(cli, name, k.namespace, &metav1.GetOptions{})
if err != nil {
return nil, err
}
return json.Marshal(d)
}
// Delete deletes a deployment instance from the
// kubernetes cluster
func (k *Kubeclient) Delete(name string, opts *metav1.DeleteOptions) error {
if strings.TrimSpace(name) == "" {
return errors.New("failed to delete deployment: missing deployment name")
}
cli, err := k.getClientOrCached()
if err != nil {
return errors.Wrapf(err, "failed to delete deployment {%s}", name)
}
return k.del(cli, k.namespace, name, opts)
}
// Create creates a deployment in specified namespace in kubernetes cluster
func (k *Kubeclient) Create(deployment *appsv1.Deployment) (*appsv1.Deployment, error) {
if deployment == nil {
return nil, errors.New("failed to create deployment: nil deployment object")
}
cli, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to create deployment {%s} in namespace {%s}",
deployment.Name,
deployment.Namespace,
)
}
return k.create(cli, k.namespace, deployment)
}
// RolloutStatusf returns deployment's rollout status for given name
// in raw bytes
func (k *Kubeclient) RolloutStatusf(name string) (op []byte, err error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
d, err := k.get(cli, name, k.namespace, &metav1.GetOptions{})
if err != nil {
return nil, err
}
return k.rolloutStatusf(d)
}
// RolloutStatus returns deployment's rollout status for given name
func (k *Kubeclient) RolloutStatus(name string) (*RolloutOutput, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
d, err := k.get(cli, name, k.namespace, &metav1.GetOptions{})
if err != nil {
return nil, err
}
return k.rolloutStatus(d)
}