/
capacity.go
46 lines (36 loc) · 1.05 KB
/
capacity.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
package k8s
import (
"context"
"strings"
"github.com/convox/convox/pkg/structs"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/resource"
am "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (p *Provider) CapacityGet() (*structs.Capacity, error) {
ns, err := p.Cluster.CoreV1().Nodes().List(context.TODO(), am.ListOptions{})
if err != nil {
return nil, errors.WithStack(err)
}
c := &structs.Capacity{}
for _, n := range ns.Items {
c.ClusterCPU += n.Status.Capacity.Cpu().MilliValue()
c.ClusterMemory += n.Status.Capacity.Memory().ScaledValue(resource.Mega)
}
filters := []string{
"system=convox",
"type in (process,service)",
}
ps, err := p.Cluster.CoreV1().Pods("").List(context.TODO(), am.ListOptions{LabelSelector: strings.Join(filters, ",")})
if err != nil {
return nil, err
}
for _, p := range ps.Items {
c.ProcessCount += 1
for _, pc := range p.Spec.Containers {
c.ProcessCPU += pc.Resources.Requests.Cpu().MilliValue()
c.ProcessMemory += pc.Resources.Requests.Memory().ScaledValue(resource.Mega)
}
}
return c, nil
}