-
Notifications
You must be signed in to change notification settings - Fork 7
/
kube_list_quotas.go
89 lines (70 loc) · 2.38 KB
/
kube_list_quotas.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
package svc
import (
"context"
"github.com/nerdalize/nerd/pkg/kubevisor"
corev1 "k8s.io/api/core/v1"
)
//ListQuotaItem describes a namespace quota
type ListQuotaItem struct {
RequestCPU int64
LimitCPU int64
LimitMemory int64
RequestMemory int64
UseRequestCPU int64
UseLimitCPU int64
UseLimitMemory int64
UseRequestMemory int64
Labels map[string]string
}
//NodeLimitedQuota is used when no quota is configured
var NodeLimitedQuota = ListQuotaItem{}
//ListQuotasInput is the input to ListQuotas
type ListQuotasInput struct{}
//ListQuotasOutput is the output to ListQuotas
type ListQuotasOutput struct {
Items []*ListQuotaItem
}
//ListQuotas will list quotas on kubernetes
func (k *Kube) ListQuotas(ctx context.Context, in *ListQuotasInput) (out *ListQuotasOutput, err error) {
if err = k.checkInput(ctx, in); err != nil {
return nil, err
}
quotas := "as{}
err = k.visor.ListResources(ctx, kubevisor.ResourceTypeQuota, quotas, nil, nil)
if err != nil {
return nil, err
}
out = &ListQuotasOutput{}
for _, q := range quotas.Items {
reqCPU, _ := q.Status.Hard[corev1.ResourceRequestsCPU]
reqMem, _ := q.Status.Hard[corev1.ResourceRequestsMemory]
limCPU, _ := q.Status.Hard[corev1.ResourceLimitsCPU]
limMem, _ := q.Status.Hard[corev1.ResourceLimitsMemory]
useReqCPU, _ := q.Status.Used[corev1.ResourceRequestsCPU]
useReqMem, _ := q.Status.Used[corev1.ResourceRequestsMemory]
useLimCPU, _ := q.Status.Used[corev1.ResourceLimitsCPU]
useLimMem, _ := q.Status.Used[corev1.ResourceLimitsMemory]
out.Items = append(out.Items, &ListQuotaItem{
RequestCPU: reqCPU.MilliValue(),
RequestMemory: reqMem.MilliValue(),
LimitCPU: limCPU.MilliValue(),
LimitMemory: limMem.MilliValue(),
UseLimitCPU: useLimCPU.MilliValue(),
UseRequestCPU: useReqCPU.MilliValue(),
UseLimitMemory: useLimMem.MilliValue(),
UseRequestMemory: useReqMem.MilliValue(),
Labels: q.Labels,
})
}
return out, nil
}
//quotas implements the list transformer interface to allow the kubevisor the manage names for us
type quotas struct{ *corev1.ResourceQuotaList }
func (quotas *quotas) Transform(fn func(in kubevisor.ManagedNames) (out kubevisor.ManagedNames)) {
for i, j1 := range quotas.ResourceQuotaList.Items {
quotas.Items[i] = *(fn(&j1).(*corev1.ResourceQuota))
}
}
func (quotas *quotas) Len() int {
return len(quotas.ResourceQuotaList.Items)
}