-
Notifications
You must be signed in to change notification settings - Fork 57
/
kube.go
353 lines (314 loc) · 9.84 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
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
package kube
import (
"bytes"
"context"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/src-cli/internal/scout"
"gopkg.in/inf.v0"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
metav1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
)
// GetPods fetches all pods in a given namespace.
func GetPods(ctx context.Context, cfg *scout.Config) ([]corev1.Pod, error) {
podInterface := cfg.K8sClient.CoreV1().Pods(cfg.Namespace)
podList, err := podInterface.List(ctx, metav1.ListOptions{})
if err != nil {
return []corev1.Pod{}, errors.Wrap(err, "could not list pods")
}
if len(podList.Items) == 0 {
msg := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFA500"))
fmt.Println(msg.Render(`
No pods exist in this namespace.
Did you mean to use the --namespace flag?
If you are attempting to check
resources for a docker deployment, you
must use the --docker flag.
See --help for more info.
`))
os.Exit(1)
}
return podList.Items, nil
}
// GetPod returns a pod object with the given name from a list of pods.
func GetPod(podName string, pods []corev1.Pod) (corev1.Pod, error) {
for _, p := range pods {
if p.Name == podName {
return p, nil
}
}
return corev1.Pod{}, errors.New("no pod with this name exists in this namespace")
}
// GetPodMetrics fetches metrics for a given pod from the Kubernetes Metrics API.
// It accepts:
// - ctx: The context for the request
// - cfg: The scout config containing Kubernetes clientsets
// - pod: The pod specification
//
// It returns:
// - podMetrics: The PodMetrics object containing metrics for the pod
// - Any error that occurred while fetching the metrics
func GetPodMetrics(ctx context.Context, cfg *scout.Config, pod corev1.Pod) (*metav1beta1.PodMetrics, error) {
podMetrics, err := cfg.MetricsClient.
MetricsV1beta1().
PodMetricses(cfg.Namespace).
Get(ctx, pod.Name, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrap(err, "failed to get pod metrics")
}
return podMetrics, nil
}
// GetLimits generates resource limits for containers in a pod.
//
// It accepts:
// - ctx: The context for the request
// - cfg: The scout config containing Kubernetes clientsets
// - pod: The pod specification
// - containerMetrics: A pointer to a ContainerMetrics struct to populate
//
// It populates the containerMetrics.Limits field with a map of container names
// to resource limits (CPU, memory, storage) for each container in the pod.
//
// It returns:
// - Any error that occurred while fetching resource limits
func AddLimits(ctx context.Context, cfg *scout.Config, pod *corev1.Pod, containerMetrics *scout.ContainerMetrics) error {
for _, container := range pod.Spec.Containers {
containerName := container.Name
capacity, err := GetPvcCapacity(ctx, cfg, container, pod)
if err != nil {
return errors.Wrap(err, "while getting storage capacity of PV")
}
rsrcs := scout.Resources{
Cpu: container.Resources.Limits.Cpu().ToDec(),
Memory: container.Resources.Limits.Memory().ToDec(),
Storage: capacity,
}
containerMetrics.Limits[containerName] = rsrcs
}
return nil
}
// GetUsage generates resource usage statistics for a Kubernetes container.
//
// It accepts:
// - ctx: The context for the request
// - cfg: The scout config containing Kubernetes clientsets
// - metrics: Container resource limits
// - pod: The pod specification
// - container: Container metrics from the Metrics API
//
// It returns:
// - usageStats: A UsageStats struct containing the resource usage info
// - Any error that occurred while generating the usage stats
func GetUsage(
ctx context.Context,
cfg *scout.Config,
metrics scout.ContainerMetrics,
pod corev1.Pod,
container metav1beta1.ContainerMetrics,
) (scout.UsageStats, error) {
var usageStats scout.UsageStats
usageStats.ContainerName = container.Name
cpuUsage, err := GetRawUsage(container.Usage, "cpu")
if err != nil {
return usageStats, errors.Wrap(err, "failed to get raw CPU usage")
}
memUsage, err := GetRawUsage(container.Usage, "memory")
if err != nil {
return usageStats, errors.Wrap(err, "failed to get raw memory usage")
}
limits := metrics.Limits[container.Name]
var storageCapacity float64
var storageUsage float64
if limits.Storage != nil {
storageCapacity, storageUsage, err = GetStorageUsage(ctx, cfg, pod.Name, container.Name)
if err != nil {
return usageStats, errors.Wrap(err, "failed to get storage usage")
}
}
usageStats.CpuCores = limits.Cpu
usageStats.CpuUsage = scout.GetPercentage(
cpuUsage,
limits.Cpu.AsApproximateFloat64()*scout.ABillion,
)
usageStats.Memory = limits.Memory
usageStats.MemoryUsage = scout.GetPercentage(
memUsage,
limits.Memory.AsApproximateFloat64(),
)
if limits.Storage == nil {
storageDec := *inf.NewDec(0, 0)
usageStats.Storage = resource.NewDecimalQuantity(storageDec, resource.Format("DecimalSI"))
} else {
usageStats.Storage = limits.Storage
}
usageStats.StorageUsage = scout.GetPercentage(
storageUsage,
storageCapacity,
)
if metrics.Limits[container.Name].Storage == nil {
usageStats.Storage = nil
}
return usageStats, nil
}
// GetRawUsage returns the raw usage value for a given resource type from a Kubernetes ResourceList.
//
// It accepts:
// - usages: A Kubernetes ResourceList containing usage values
// - targetKey: The resource type to get the usage for (e.g. "cpu" or "memory")
//
// It returns:
// - The raw usage value for the target resource type
// - Any error that occurred while parsing the usage value
func GetRawUsage(usages corev1.ResourceList, targetKey string) (float64, error) {
var usage *inf.Dec
for key, val := range usages {
if key.String() == targetKey {
usage = val.AsDec().SetScale(0)
}
}
toFloat, err := strconv.ParseFloat(usage.String(), 64)
if err != nil {
return 0, errors.Wrap(err, "failed to convert inf.Dec type to float")
}
return toFloat, nil
}
// GetPvcCapacity returns the storage capacity of a PersistentVolumeClaim mounted to a container.
//
// It accepts:
// - ctx: The context for the request
// - cfg: The scout config containing Kubernetes clientsets
// - container: The container specification
// - pod: The pod specification
//
// It returns:
// - The storage capacity of the PVC in bytes
// - Any error that occurred while fetching the PVC
//
// If no PVC is mounted to the container, nil is returned for the capacity and no error.
func GetPvcCapacity(ctx context.Context, cfg *scout.Config, container corev1.Container, pod *corev1.Pod) (*resource.Quantity, error) {
for _, vm := range container.VolumeMounts {
for _, v := range pod.Spec.Volumes {
if v.Name == vm.Name && v.PersistentVolumeClaim != nil {
pvc, err := cfg.K8sClient.
CoreV1().
PersistentVolumeClaims(cfg.Namespace).
Get(
ctx,
v.PersistentVolumeClaim.ClaimName,
metav1.GetOptions{},
)
if err != nil {
return nil, errors.Wrapf(
err,
"failed to get PVC %s",
v.PersistentVolumeClaim.ClaimName,
)
}
return pvc.Status.Capacity.Storage().ToDec(), nil
}
}
}
return nil, nil
}
// GetStorageUsage returns the storage capacity and usage for a given pod and container.
//
// It accepts:
// - ctx: The context for the request
// - cfg: The scout config containing Kubernetes clientsets
// - podName: The name of the pod
// - containerName: The name of the container
//
// It returns:
// - storageCapacity: The total storage capacity for the container in bytes
// - storageUsage: The used storage for the container in bytes
// - Any error that occurred while fetching the storage usage
func GetStorageUsage(
ctx context.Context,
cfg *scout.Config,
podName string,
containerName string,
) (float64, float64, error) {
var storageCapacity float64
var storageUsage float64
stateless := []string{
"cadvisor",
"pgsql-exporter",
"executor",
"dind",
"github-proxy",
"jaeger",
"node-exporter",
"otel-agent",
"otel-collector",
"precise-code-intel-worker",
"redis-exporter",
"repo-updater",
"frontend",
"syntect-server",
"worker",
}
// if pod is stateless, return 0 for capacity and usage
if scout.Contains(stateless, containerName) {
return storageCapacity, storageUsage, nil
}
req := cfg.K8sClient.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(cfg.Namespace).
SubResource("exec")
req.VersionedParams(&corev1.PodExecOptions{
Container: containerName,
Command: []string{"df"},
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(cfg.RestConfig, "POST", req.URL())
if err != nil {
return 0, 0, err
}
var stdout, stderr bytes.Buffer
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdout: &stdout,
Stderr: &stderr,
})
if err != nil {
return 0, 0, err
}
lines := strings.Split(stdout.String(), "\n")
for _, line := range lines[1 : len(lines)-1] {
fields := strings.Fields(line)
if acceptedFileSystem(fields[0]) {
capacityFloat, err := strconv.ParseFloat(fields[1], 64)
if err != nil {
return 0, 0, errors.Wrap(err, "could not convert string to float64")
}
usageFloat, err := strconv.ParseFloat(fields[2], 64)
if err != nil {
return 0, 0, errors.Wrap(err, "could not convert string to float64")
}
return capacityFloat, usageFloat, nil
}
}
return 0, 0, nil
}
// acceptedFileSystem checks if a given file system, represented
// as a string, is an accepted system.
//
// It returns:
// - True if the file system matches the pattern '/dev/sd[a-z]$'
// - False otherwise
func acceptedFileSystem(fileSystem string) bool {
matched, _ := regexp.MatchString(`/dev/sd[a-z]$`, fileSystem)
return matched
}