/
kube.go
171 lines (151 loc) · 4.52 KB
/
kube.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
/*
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
*
* 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 framework
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const nvidiaResourceName = "nvidia.com/gpu"
// KubeClient is a kubernetes client
type KubeClient struct {
client *kubernetes.Clientset
}
// NewKubeClient creates a new KubeClient instance
func NewKubeClient(k8sConfig *rest.Config) (*KubeClient, error) {
client, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
return nil, err
}
return &KubeClient{client: client}, nil
}
// CreateNamespace creates a new namespace
func (c *KubeClient) CreateNamespace(
ctx context.Context,
namespace string,
labels map[string]string,
) (*corev1.Namespace, error) {
namespaceObj := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Labels: labels,
},
Status: corev1.NamespaceStatus{},
}
return c.client.CoreV1().Namespaces().Create(ctx, namespaceObj, metav1.CreateOptions{})
}
// DeleteNamespace deletes the namespace
func (c *KubeClient) DeleteNamespace(
ctx context.Context,
namespace string,
) error {
return c.client.CoreV1().Namespaces().Delete(ctx, namespace, metav1.DeleteOptions{})
}
// GetPodsByLabel returns a list of pods that matches with the label selector
func (c *KubeClient) GetPodsByLabel(ctx context.Context, namespace string, labelMap map[string]string) ([]corev1.Pod, error) {
podList, err := c.client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labelMap).String(),
})
if err != nil {
return nil, err
}
return podList.Items, nil
}
func (c *KubeClient) CheckPodStatus(ctx context.Context,
namespace, podName string,
condition func(namespace, podName string, status corev1.PodStatus) (bool, error),
) (bool, error) {
pod, err := c.client.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return false, fmt.Errorf("unexpected error getting pod %s; err: %w", podName, err)
}
if condition != nil {
return condition(namespace, podName, pod.Status)
}
for _, c := range pod.Status.ContainerStatuses {
if c.State.Waiting != nil && c.State.Waiting.Reason == "CrashLoopBackOff" {
return false, fmt.Errorf("pod %s in namespace %s is in CrashLoopBackOff", pod.Name, pod.Namespace)
}
}
return false, nil
}
// CreatePod creates a new pod in the defined namespace
func (c *KubeClient) CreatePod(ctx context.Context,
namespace string,
labels map[string]string,
name string,
containerName string,
image string,
) (*corev1.Pod, error) {
quantity, _ := resource.ParseQuantity("1")
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: containerName,
Image: image,
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
nvidiaResourceName: quantity,
},
},
},
},
},
}
return c.client.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})
}
// DeletePod deletes a pod in the defined namespace
func (c *KubeClient) DeletePod(ctx context.Context,
namespace string,
name string,
) error {
return c.client.CoreV1().Pods(namespace).Delete(ctx, name, metav1.DeleteOptions{})
}
// DoHttpRequest makes http request to path on the pod
func (c *KubeClient) DoHttpRequest(ctx context.Context,
namespace string,
name string,
port uint,
path string,
) ([]byte, error) {
result := c.client.CoreV1().RESTClient().Get().
Namespace(namespace).
Resource("pods").
Name(fmt.Sprintf("%s:%d", name, port)).
SubResource("proxy").
Suffix(path).
Do(ctx)
if result.Error() != nil {
return nil, result.Error()
}
rawResponse, err := result.Raw()
if err != nil {
return nil, err
}
return rawResponse, nil
}