This repository was archived by the owner on Jan 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathk8sclient.go
More file actions
411 lines (385 loc) · 13.9 KB
/
Copy pathk8sclient.go
File metadata and controls
411 lines (385 loc) · 13.9 KB
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
package testingsdk
import (
"context"
"errors"
"fmt"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"time"
)
var (
ResourceExisted = errors.New("[REX] resource is already existed")
ResourceNotFound = errors.New("[RNF] resource is not found")
ResourceNameNull = errors.New("[RNN] resource name is null")
ControlPlaneLabel = "node-role.kubernetes.io/control-plane"
defaultRetryInterval = 10 * time.Second
defaultRetryTimeout = 600 * time.Second
defaultLongRetryInterval = 20 * time.Second
defaultLongRetryTimeout = 900 * time.Second
defaultNodeInterval = 2 * time.Second
defaultNodeReadyTimeout = 20 * time.Minute
defaultNodeNotReadyTimeout = 8 * time.Minute
defaultServiceRetryInterval = 10 * time.Second
defaultServiceRetryTimeout = 10 * time.Minute
)
func waitForServiceExposure(cs kubernetes.Interface, namespace string, name string) (*apiv1.Service, error) {
var svc *apiv1.Service
var err error
err = wait.PollImmediate(defaultServiceRetryInterval, defaultServiceRetryTimeout, func() (bool, error) {
svc, err = cs.CoreV1().Services(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
// If our error is retryable, it's not a major error. So we do not need to return it as an error.
if IsRetryableError(err) {
return false, nil
}
return false, err
}
IngressList := svc.Status.LoadBalancer.Ingress
if len(IngressList) == 0 {
// we'll store an error here and continue to retry until timeout, if this was where we time out eventually, we will return an error at the end
err = fmt.Errorf("cannot find Ingress components after duration: [%d] minutes", defaultServiceRetryTimeout/time.Minute)
return false, nil
}
ip := svc.Status.LoadBalancer.Ingress[0].IP
return ip != "", nil // Once we have our IP, we can terminate the condition for polling and return the service
})
if err != nil {
return nil, err
}
return svc, nil
}
func waitForDeploymentReady(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, deployName string) error {
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
options := metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", deployName),
}
podList, err := k8sClient.CoreV1().Pods(nameSpace).List(ctx, options)
if err != nil {
if IsRetryableError(err) {
return false, nil
}
return false, fmt.Errorf("unexpected error occurred while getting deployment [%s]", deployName)
}
podCount := len(podList.Items)
containerCount := 0
podsReady := 0
containersReady := 0
for _, pod := range (*podList).Items {
// Add the total amount of containers per pod
containerCount += len(pod.Spec.Containers)
containersReadyFromCurrentPod := 0
if pod.Status.Phase == apiv1.PodRunning {
// Ref: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/
// Pod running state can mean: at least one container is still running, or is in the process of starting or restarting.
// It's possible that the container is just starting up and not fully ready, so we should also check if ContainerStatus is ready.
for _, container := range pod.Status.ContainerStatuses {
if container.Ready {
containersReadyFromCurrentPod++
}
}
if containersReadyFromCurrentPod == len(pod.Spec.Containers) {
podsReady++
containersReady += containersReadyFromCurrentPod
}
}
}
// It is possible to have a race condition where Pods are not up yet and in testing code it can think that the Deployment is ready
// Because there are no Pods up yet, though Deployment has been applied.
if podCount == 0 {
fmt.Printf("no containers or pods are ready yet")
return false, nil
}
if podsReady < podCount || containersReady < containerCount {
fmt.Printf("running pods: %v < %v; ready containers: %v < %v\n", podsReady, podCount, containersReady, containerCount)
return false, nil
}
return true, nil
})
return err
}
func waitForDeploymentDeleted(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, deployName string) (bool, error) {
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
_, err := getDeployment(ctx, k8sClient, nameSpace, deployName)
if err != nil {
if err == ResourceNotFound {
return true, nil
}
if IsRetryableError(err) {
return false, nil
}
return false, fmt.Errorf("unexpected error occurred while getting deployment [%s]", deployName)
}
return false, nil
})
if err != nil {
return false, fmt.Errorf("error occurred while checking deployment status: %v", err)
}
return true, nil
}
func waitForServiceDeleted(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, serviceName string) (bool, error) {
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
_, err := getService(ctx, k8sClient, nameSpace, serviceName)
if err != nil {
if err == ResourceNotFound {
return true, nil
}
if IsRetryableError(err) {
return false, nil
}
return false, fmt.Errorf("unexpected error occurred while getting service [%s]", serviceName)
}
return false, nil
})
if err != nil {
return false, fmt.Errorf("error occurred while checking serviceName status: %v", err)
}
return true, nil
}
func waitForNameSpaceDeleted(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string) (bool, error) {
err := wait.PollImmediate(defaultLongRetryInterval, defaultLongRetryTimeout, func() (bool, error) {
_, err := k8sClient.CoreV1().Namespaces().Get(ctx, nameSpace, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
return true, nil
}
if IsRetryableError(err) {
return false, nil
}
return false, fmt.Errorf("unexpected error occurred while getting namespace [%s]", nameSpace)
}
return false, nil
})
if err != nil {
return false, fmt.Errorf("error occurred while checking namespace status: %v", err)
}
return true, nil
}
func IsRetryableError(err error) bool {
if apierrs.IsInternalError(err) || apierrs.IsTimeout(err) || apierrs.IsServerTimeout(err) ||
apierrs.IsTooManyRequests(err) || utilnet.IsProbableEOF(err) || utilnet.IsConnectionReset(err) {
return true
}
return false
}
func getDeployment(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, deployName string) (*appsv1.Deployment, error) {
if deployName == "" {
return nil, ResourceNameNull
}
deployment, err := k8sClient.AppsV1().Deployments(nameSpace).Get(ctx, deployName, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
return nil, ResourceNotFound
}
return nil, err
}
return deployment, nil
}
func getService(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, serviceName string) (*apiv1.Service, error) {
if serviceName == "" {
return nil, ResourceNameNull
}
svc, err := k8sClient.CoreV1().Services(nameSpace).Get(ctx, serviceName, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
return nil, ResourceNotFound
}
return nil, err
}
return svc, nil
}
func getWorkerNodes(ctx context.Context, k8sClient *kubernetes.Clientset) ([]apiv1.Node, error) {
var workerNodes []apiv1.Node
var timeoutSeconds int64 = 600
nodes, err := k8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{TimeoutSeconds: &timeoutSeconds})
if err != nil {
return workerNodes, fmt.Errorf("error occurred while getting nodes: [%v]", err)
}
for _, node := range nodes.Items {
_, ok := node.Labels[ControlPlaneLabel]
if !ok {
workerNodes = append(workerNodes, node)
}
}
return workerNodes, nil
}
func getNodes(ctx context.Context, k8sClient *kubernetes.Clientset) ([]apiv1.Node, error) {
var allNodes []apiv1.Node
var timeoutSeconds int64 = 600
nodes, err := k8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{TimeoutSeconds: &timeoutSeconds})
if err != nil {
return allNodes, fmt.Errorf("error occurred while getting nodes: [%v]", err)
}
for _, node := range nodes.Items {
allNodes = append(allNodes, node)
}
return allNodes, nil
}
func createNameSpace(ctx context.Context, nsName string, k8sClient *kubernetes.Clientset) (*apiv1.Namespace, error) {
if nsName == "" {
return nil, ResourceNameNull
}
namespace := &apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: nsName,
},
}
ns, err := k8sClient.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred while creating namespace [%s]: [%v]", nsName, err)
}
return ns, nil
}
func createDeployment(ctx context.Context, k8sClient *kubernetes.Clientset, params *DeployParams, nameSpace string) (*appsv1.Deployment, error) {
if params.Name == "" {
return nil, ResourceNameNull
}
if nameSpace == "" {
nameSpace = apiv1.NamespaceDefault
}
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: params.Name,
Namespace: nameSpace,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: params.Labels,
},
Strategy: appsv1.DeploymentStrategy{
Type: appsv1.RecreateDeploymentStrategyType,
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: params.Labels,
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Image: params.ContainerParams.ContainerImage,
ImagePullPolicy: apiv1.PullAlways,
Name: params.ContainerParams.ContainerName,
Args: params.ContainerParams.Args,
Ports: []apiv1.ContainerPort{
{
ContainerPort: params.ContainerParams.ContainerPort,
},
},
},
},
},
},
},
}
if params.VolumeParams.VolumeName != "" && params.VolumeParams.PvcRef != "" && params.VolumeParams.MountPath != "" {
deployment.Spec.Template.Spec.Containers[0].VolumeMounts = []apiv1.VolumeMount{
{
Name: params.VolumeParams.VolumeName,
MountPath: params.VolumeParams.MountPath,
},
}
deployment.Spec.Template.Spec.Volumes = []apiv1.Volume{
{
Name: params.VolumeParams.VolumeName,
VolumeSource: apiv1.VolumeSource{
PersistentVolumeClaim: &apiv1.PersistentVolumeClaimVolumeSource{
ClaimName: params.VolumeParams.PvcRef,
},
},
},
}
}
newDeployment, err := k8sClient.AppsV1().Deployments(nameSpace).Create(ctx, deployment, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred while creating deployment [%s]: [%v]", params.Name, err)
}
return newDeployment, nil
}
func createLoadBalancerService(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, serviceName string, annotations map[string]string, labels map[string]string, servicePort []apiv1.ServicePort, loadBalancerIP string) (*apiv1.Service, error) {
if serviceName == "" {
return nil, ResourceNameNull
}
if nameSpace == "" {
nameSpace = apiv1.NamespaceDefault
}
svc := &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: nameSpace,
Annotations: annotations,
Labels: labels,
},
Spec: apiv1.ServiceSpec{
Ports: servicePort,
Selector: labels,
Type: "LoadBalancer",
LoadBalancerIP: loadBalancerIP,
},
}
newSVC, err := k8sClient.CoreV1().Services(nameSpace).Create(ctx, svc, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred while creating service [%s]: [%v]", serviceName, err)
}
return newSVC, nil
}
func deleteDeployment(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, deploymentName string) error {
_, err := getDeployment(ctx, k8sClient, nameSpace, deploymentName)
if err != nil {
if err == ResourceNotFound {
return fmt.Errorf("the deployment [%s] does not exist", deploymentName)
}
klog.Infof("error occurred while getting deployment [%s]: [%v]", deploymentName, err)
}
err = k8sClient.AppsV1().Deployments(nameSpace).Delete(ctx, deploymentName, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete deployment [%s]", deploymentName)
}
deploymentDeleted, err := waitForDeploymentDeleted(ctx, k8sClient, nameSpace, deploymentName)
if err != nil {
return fmt.Errorf("error occurred while deleting deployment [%s]: [%v]", deploymentName, err)
}
if !deploymentDeleted {
return fmt.Errorf("deployment [%s] still exists", deploymentName)
}
return nil
}
func deleteNameSpace(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string) error {
err := k8sClient.CoreV1().Namespaces().Delete(ctx, nameSpace, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete namespace [%s]", nameSpace)
}
namespaceDeleted, err := waitForNameSpaceDeleted(ctx, k8sClient, nameSpace)
if err != nil {
return fmt.Errorf("error occurred while deleting namespace [%s]: [%v]", nameSpace, err)
}
if !namespaceDeleted {
return fmt.Errorf("namespace [%s] still exists", nameSpace)
}
return nil
}
func deleteService(ctx context.Context, k8sClient *kubernetes.Clientset, nameSpace string, serviceName string) error {
_, err := getService(ctx, k8sClient, nameSpace, serviceName)
if err != nil {
if err == ResourceNotFound {
return fmt.Errorf("the service [%s] does not exist", serviceName)
}
klog.Infof("error occurred while getting service [%s]: [%v]", serviceName, err)
}
err = k8sClient.CoreV1().Services(nameSpace).Delete(ctx, serviceName, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete service [%s]", serviceName)
}
serviceDeleted, err := waitForServiceDeleted(ctx, k8sClient, nameSpace, serviceName)
if err != nil {
return fmt.Errorf("error occurred while deleting service [%s]: [%v]", serviceName, err)
}
if !serviceDeleted {
return fmt.Errorf("service [%s] still exists", serviceName)
}
return nil
}