-
Notifications
You must be signed in to change notification settings - Fork 201
/
kubernetes.go
392 lines (348 loc) · 10.1 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
/*
Copyright 2019 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 v1alpha1
import (
"encoding/json"
client "github.com/openebs/maya/pkg/kubernetes/client/v1alpha1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/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 internal clientset
type getClientsetFn func() (clientset *kubernetes.Clientset, err error)
// getClientsetFromPathFn is a typed function that
// abstracts fetching of clientset from kubeConfigPath
type getClientsetForPathFn func(
kubeConfigPath string,
) (*kubernetes.Clientset, error)
// getFn is a typed function that abstracts get of service instances
type getFn func(
cli *kubernetes.Clientset,
name, namespace string,
opts metav1.GetOptions,
) (*corev1.Service, error)
// listFn is a typed function that abstracts list of service instances
type listFn func(
cli *kubernetes.Clientset,
namespace string,
opts metav1.ListOptions,
) (*corev1.ServiceList, error)
// delFn is a typed function that abstracts delete of service instances
type delFn func(
cli *kubernetes.Clientset,
name, namespace string,
opts *metav1.DeleteOptions,
) error
// createFn is a typed function that abstracts delete of service instances
type createFn func(
cli *kubernetes.Clientset,
service *corev1.Service,
namespace string,
) (*corev1.Service, error)
// updateFn is a typed function that abstracts delete of service instances
type updateFn func(
cli *kubernetes.Clientset,
service *corev1.Service,
namespace string,
) (*corev1.Service, error)
// patchFn is a typed function that abstracts patch of service instances
type patchFn func(
cli *kubernetes.Clientset,
name, namespace string,
pt types.PatchType,
data []byte,
subresources ...string,
) (*corev1.Service, error)
// Kubeclient enables kubernetes API operations on service instance
type Kubeclient struct {
// clientset refers to kubernetes clientset. It is responsible to
// make kubernetes API calls for crud op
clientset *kubernetes.Clientset
namespace string
// kubeconfig path to get kubernetes clientset
kubeConfigPath string
// functions useful during mocking
getClientset getClientsetFn
getClientsetForPath getClientsetForPathFn
list listFn
get getFn
del delFn
create createFn
patch patchFn
update updateFn
}
// KubeclientBuildOption defines the abstraction to build a kubeclient instance
type KubeclientBuildOption func(*Kubeclient)
// defaultGetClientset is the default implementation to
// get kubernetes clientset instance
func defaultGetClientset() (clients *kubernetes.Clientset, err error) {
config, err := client.New().Config()
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
// defaultGetClientsetForPath is the default implementation to
// get kubernetes clientset instance based on the given
// kubeconfig path
func defaultGetClientsetForPath(
kubeConfigPath string,
) (clients *kubernetes.Clientset, err error) {
return client.New(client.WithKubeConfigPath(kubeConfigPath)).
Clientset()
}
// defaultCreate is the default implementation to get
// a service instance in kubernetes cluster
func defaultGet(
cli *kubernetes.Clientset,
name, namespace string,
opts metav1.GetOptions,
) (r *corev1.Service, err error) {
r, err = cli.CoreV1().
Services(namespace).
Get(name, opts)
return
}
// defaultCreate is the default implementation to list
// service instances in kubernetes cluster
func defaultList(
cli *kubernetes.Clientset,
namespace string,
opts metav1.ListOptions,
) (rl *corev1.ServiceList, err error) {
rl, err = cli.CoreV1().
Services(namespace).
List(opts)
return
}
// defaultCreate is the default implementation to delete
// a service instance in kubernetes cluster
func defaultDel(
cli *kubernetes.Clientset,
name, namespace string,
opts *metav1.DeleteOptions,
) (err error) {
deletePropagation := metav1.DeletePropagationForeground
opts.PropagationPolicy = &deletePropagation
err = cli.CoreV1().
Services(namespace).
Delete(name, opts)
return
}
// defaultCreate is the default implementation to create
// a service instance in kubernetes cluster
func defaultCreate(
cli *kubernetes.Clientset,
service *corev1.Service,
namespace string,
) (*corev1.Service, error) {
return cli.CoreV1().
Services(namespace).
Create(service)
}
// defaultUpdate is the default implementation to update
// a service instance in kubernetes cluster
func defaultUpdate(
cli *kubernetes.Clientset,
service *corev1.Service,
namespace string,
) (*corev1.Service, error) {
return cli.CoreV1().
Services(namespace).
Update(service)
}
// defaultPatch is the default implementation to patch
// a service instance in kubernetes cluster
func defaultPatch(
cli *kubernetes.Clientset,
name, namespace string,
pt types.PatchType,
data []byte,
subresources ...string,
) (*corev1.Service, error) {
return cli.CoreV1().
Services(namespace).
Patch(name, pt, data, subresources...)
}
// withDefaults sets the default options of kubeclient instance
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.del == nil {
k.del = defaultDel
}
if k.create == nil {
k.create = defaultCreate
}
if k.patch == nil {
k.patch = defaultPatch
}
if k.update == nil {
k.update = defaultUpdate
}
}
// WithClientset sets the kubernetes client against the kubeclient instance
func WithClientset(c *kubernetes.Clientset) KubeclientBuildOption {
return func(k *Kubeclient) {
k.clientset = c
}
}
// WithNamespace set namespace in kubeclient object
func WithNamespace(namespace string) KubeclientBuildOption {
return func(k *Kubeclient) {
k.namespace = namespace
}
}
// WithNamespace set namespace in kubeclient object
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
}
}
// NewKubeClient returns a new instance of kubeclient meant for service,
// 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
}
cs, err := k.getClientsetForPathOrDirect()
if err != nil {
return nil, errors.Wrapf(err, "failed to get clientset")
}
k.clientset = cs
return k.clientset, nil
}
// Get returns service object for given name
func (k *Kubeclient) Get(name string, opts metav1.GetOptions) (
*corev1.Service, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
return k.get(cli, name, k.namespace, opts)
}
// GetRaw returns service object for given name in byte format
func (k *Kubeclient) GetRaw(name string, opts metav1.GetOptions) (
[]byte, error) {
svc, err := k.Get(name, opts)
if err != nil {
return nil, err
}
return json.Marshal(svc)
}
// List returns list of services
func (k *Kubeclient) List(opts metav1.ListOptions) (
*corev1.ServiceList, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
return k.list(cli, k.namespace, opts)
}
// ListRaw returns list of services in byte format
func (k *Kubeclient) ListRaw(opts metav1.ListOptions) ([]byte, error) {
svcList, err := k.List(opts)
if err != nil {
return nil, err
}
return json.Marshal(svcList)
}
// Delete returns service object for given name
func (k *Kubeclient) Delete(name string, options *metav1.DeleteOptions) error {
cli, err := k.getClientOrCached()
if err != nil {
return err
}
return k.del(cli, name, k.namespace, options)
}
// Create creates a service in specified namespace in kubernetes cluster
func (k *Kubeclient) Create(service *corev1.Service) (*corev1.Service, error) {
if service == nil {
return nil, errors.New("failed to create service: nil service object")
}
cli, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to create service {%s} in namespace {%s}",
service.Name,
service.Namespace,
)
}
return k.create(cli, service, k.namespace)
}
// Update updates a service in specified namespace in kubernetes cluster
func (k *Kubeclient) Update(service *corev1.Service) (*corev1.Service, error) {
if service == nil {
return nil, errors.New("failed to update service: nil service object")
}
cli, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to update service {%s} in namespace {%s}",
service.Name,
service.Namespace,
)
}
return k.update(cli, service, k.namespace)
}
// Patch patches service object for given name
func (k *Kubeclient) Patch(
name string,
pt types.PatchType,
data []byte,
subresources ...string,
) (*corev1.Service, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, err
}
return k.patch(cli, name, k.namespace, pt, data, subresources...)
}