mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
* feat(nodes limits)Review the resource assignement when creating a Kubernetes application EE-437 * feat(nodes limits) review feedback EE-437 * feat(nodes limits) workaround for lodash cloneDeep not working in production mode EE-437 * feat(nodes limits) calculate max cpu of slide bar with floor function instead of round function EE-437 * feat(nodes limits) another review feedback EE-437 * feat(nodes limits) cleanup code EE-437 * feat(nodes limits) EE-437 pr feedback update * feat(nodes limits) EE-437 rebase onto develop branch * feat(nodes limits) EE-437 another pr feedback update Co-authored-by: Simon Meng <simon.meng@portainer.io>
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package cli
|
|
|
|
import (
|
|
portainer "github.com/portainer/portainer/api"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
// GetNodesLimits gets the CPU and Memory limits(unused resources) of all nodes in the current k8s endpoint connection
|
|
func (kcl *KubeClient) GetNodesLimits() (portainer.K8sNodesLimits, error) {
|
|
nodesLimits := make(portainer.K8sNodesLimits)
|
|
|
|
nodes, err := kcl.cli.CoreV1().Nodes().List(metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pods, err := kcl.cli.CoreV1().Pods("").List(metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, item := range nodes.Items {
|
|
cpu := item.Status.Allocatable.Cpu().MilliValue()
|
|
memory := item.Status.Allocatable.Memory().Value()
|
|
|
|
nodesLimits[item.ObjectMeta.Name] = &portainer.K8sNodeLimits{
|
|
CPU: cpu,
|
|
Memory: memory,
|
|
}
|
|
}
|
|
|
|
for _, item := range pods.Items {
|
|
if nodeLimits, ok := nodesLimits[item.Spec.NodeName]; ok {
|
|
for _, container := range item.Spec.Containers {
|
|
nodeLimits.CPU -= container.Resources.Requests.Cpu().MilliValue()
|
|
nodeLimits.Memory -= container.Resources.Requests.Memory().Value()
|
|
}
|
|
}
|
|
}
|
|
|
|
return nodesLimits, nil
|
|
}
|