Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Node resource statistics #192

Merged
merged 3 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"time"

"k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down
4 changes: 2 additions & 2 deletions src/backend/controllers/kubernetes/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ func checkResourceAvailable(ns *models.Namespace, cli *kubernetes.Clientset, kub
}

if clusterMetas.ResourcesLimit.Memory != 0 &&
clusterMetas.ResourcesLimit.Memory-(namespaceResourceUsed.Memory+requestResourceList.Memory)/1024 < 0 {
clusterMetas.ResourcesLimit.Memory-(namespaceResourceUsed.Memory+requestResourceList.Memory)/(1024*1024*1024) < 0 {
return &errors.ErrorResult{
Code: http.StatusForbidden,
SubCode: base.ErrorSubCodeInsufficientResource,
Msg: fmt.Sprintf("request namespace resource (memory:%dGi) is not enough for this deploy", requestResourceList.Memory/1024),
Msg: fmt.Sprintf("request namespace resource (memory:%dGi) is not enough for this deploy", requestResourceList.Memory/(1024*1024*1024)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/controllers/kubernetes/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (c *KubeNamespaceController) Resources() {
syncResourceMap.Store(m.Cluster.Name, common.Resource{
Usage: &common.ResourceList{
Cpu: resourceUsage.Cpu / 1000,
Memory: resourceUsage.Memory / 1024,
Memory: resourceUsage.Memory / (1024 * 1024 * 1024),
},
Limit: &common.ResourceList{
Cpu: clusterMetas.ResourcesLimit.Cpu,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/controllers/kubernetes/statefulset/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ func checkResourceAvailable(ns *models.Namespace, cli *kubernetes.Clientset, kub
}

if clusterMetas.ResourcesLimit.Memory != 0 &&
clusterMetas.ResourcesLimit.Memory-(namespaceResourceUsed.Memory+requestResourceList.Memory)/1024 < 0 {
clusterMetas.ResourcesLimit.Memory-(namespaceResourceUsed.Memory+requestResourceList.Memory)/(1024*1024*1024) < 0 {
return &errors.ErrorResult{
Code: http.StatusForbidden,
SubCode: base.ErrorSubCodeInsufficientResource,
Msg: fmt.Sprintf("request namespace resource (memory:%dGi) is not enough for this deploy", requestResourceList.Memory/1024),
Msg: fmt.Sprintf("request namespace resource (memory:%dGi) is not enough for this deploy", requestResourceList.Memory/(1024*1024*1024)),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/backend/resources/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func ContainersResourceList(containers []v1.Container) *ResourceList {
for _, container := range containers {
// unit m
cpuUsage += container.Resources.Limits.Cpu().MilliValue()
// unit Mi
memoryUsage += container.Resources.Limits.Memory().Value() / (1024 * 1024)
// unit Byte
memoryUsage += container.Resources.Limits.Memory().Value()
}
return &ResourceList{
Cpu: cpuUsage,
Expand Down
9 changes: 5 additions & 4 deletions src/backend/resources/namespace/namespace.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package namespace

import (
"github.com/Qihoo360/wayne/src/backend/resources/common"
"github.com/Qihoo360/wayne/src/backend/util"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/Qihoo360/wayne/src/backend/resources/common"
"github.com/Qihoo360/wayne/src/backend/util"
)

func UpdateNamespace(cli *kubernetes.Clientset, ns *v1.Namespace) (*v1.Namespace, error) {
Expand Down Expand Up @@ -60,12 +61,12 @@ func ResourcesOfAppByNamespace(cli *kubernetes.Clientset, namespace, selector st
if result[pod.Labels[util.AppLabelKey]] == nil {
result[pod.Labels[util.AppLabelKey]] = &common.ResourceApp{
Cpu: resourceList.Cpu / 1000,
Memory: resourceList.Memory / 1024,
Memory: resourceList.Memory / (1024 * 1024 * 1024),
PodNum: 1,
}
} else {
result[pod.Labels[util.AppLabelKey]].Cpu += resourceList.Cpu / 1000
result[pod.Labels[util.AppLabelKey]].Memory += resourceList.Memory / 1024
result[pod.Labels[util.AppLabelKey]].Memory += resourceList.Memory / (1024 * 1024 * 1024)
result[pod.Labels[util.AppLabelKey]].PodNum += 1
}
}
Expand Down
102 changes: 100 additions & 2 deletions src/backend/resources/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,35 @@ import (
"k8s.io/client-go/kubernetes"

"github.com/Qihoo360/wayne/src/backend/client"
"github.com/Qihoo360/wayne/src/backend/resources/common"
)

type NodeStatistics struct {
Total int `json:"total,omitempty"`
Details map[string]int `json:"details,omitempty"`
}

type NodeListResult struct {
NodeSummary NodeListSummary `json:"nodeSummary"`
CpuSummary ResourceSummary `json:"cpuSummary"`
MemorySummary ResourceSummary `json:"memorySummary"`
Nodes []Node `json:"nodes"`
}

type NodeListSummary struct {
// total nodes count
Total int64
// ready nodes count
Ready int64
// Schedulable nodes count
Schedulable int64
}

type ResourceSummary struct {
Total int64
Used int64
}

type Node struct {
Name string `json:"name,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Expand Down Expand Up @@ -44,22 +66,98 @@ func GetNodeCounts(indexer *client.CacheIndexer) (int, error) {
return len(nodeList), nil
}

func ListNode(indexer *client.CacheIndexer) ([]Node, error) {
func ListNode(indexer *client.CacheIndexer) (NodeListResult, error) {
nodeList := indexer.Node.List()
nodes := make([]Node, 0)
ready := 0
schedulable := 0

// unit m 1 core = 1000m
var avaliableCpu int64 = 0
// unit Byte
var avaliableMemory int64 = 0

avaliableNodeMap := make(map[string]*v1.Node)

for _, node := range nodeList {
cacheNode, ok := node.(*v1.Node)
if !ok {
continue
}
isReady := false
isSchedulable := false
for _, condition := range cacheNode.Status.Conditions {
if condition.Type == v1.NodeReady && condition.Status == v1.ConditionTrue {
ready += 1
isReady = true
}

}
if !cacheNode.Spec.Unschedulable {
schedulable += 1
isSchedulable = true
}

if isReady && isSchedulable {
avaliableNodeMap[cacheNode.Name] = cacheNode

cpuQuantity := cacheNode.Status.Allocatable[v1.ResourceCPU]
memoryQuantity := cacheNode.Status.Allocatable[v1.ResourceMemory]
// unit m
avaliableCpu += cpuQuantity.MilliValue()
// unit Byte
avaliableMemory += memoryQuantity.Value()
}

nodes = append(nodes, toNode(*cacheNode))
}

sort.Slice(nodes, func(i, j int) bool {
return nodes[i].Name < nodes[j].Name
})

return nodes, nil
resourceList := podUsedResourcesOnAvaliableNode(indexer, avaliableNodeMap)

return NodeListResult{
NodeSummary: NodeListSummary{
Total: int64(len(nodes)),
Ready: int64(ready),
Schedulable: int64(schedulable),
},
CpuSummary: ResourceSummary{
Total: avaliableCpu / 1000,
Used: resourceList.Cpu / 1000,
},
MemorySummary: ResourceSummary{
Total: avaliableMemory / (1024 * 1024 * 1024),
Used: resourceList.Memory / (1024 * 1024 * 1024),
},
Nodes: nodes,
}, nil
}

func podUsedResourcesOnAvaliableNode(indexer *client.CacheIndexer, avaliableNodeMap map[string]*v1.Node) common.ResourceList {
result := common.ResourceList{}
cachePods := indexer.Pod.List()
for _, p := range cachePods {
cachePod, ok := p.(*v1.Pod)
if !ok {
continue
}

// Exclude Pod on Unavailable Node
_, ok = avaliableNodeMap[cachePod.Spec.NodeName]
if cachePod.Status.Phase == v1.PodFailed || cachePod.Status.Phase == v1.PodSucceeded || cachePod.DeletionTimestamp != nil || !ok {
continue
}

resourceList := common.ContainersResourceList(cachePod.Spec.Containers)

result.Cpu += resourceList.Cpu
result.Memory += resourceList.Memory
}

return result
}

func toNode(knode v1.Node) Node {
Expand Down
16 changes: 11 additions & 5 deletions src/backend/resources/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package pod
import (
"time"

"github.com/Qihoo360/wayne/src/backend/client"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/resources/common"
"k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"

"github.com/Qihoo360/wayne/src/backend/client"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/resources/common"
)

type PodStatistics struct {
Expand Down Expand Up @@ -169,7 +170,7 @@ func toPod(kpod *v1.Pod) *Pod {
Namespace: kpod.Namespace,
PodIp: kpod.Status.PodIP,
NodeName: kpod.Spec.NodeName,
State: getPodStatusStatus(kpod),
State: getPodStatus(kpod),
}

if kpod.Status.StartTime != nil {
Expand All @@ -193,7 +194,12 @@ func toPod(kpod *v1.Pod) *Pod {
}

// getPodStatus returns the pod state
func getPodStatusStatus(pod *v1.Pod) string {
func getPodStatus(pod *v1.Pod) string {
// Terminating
if pod.DeletionTimestamp != nil {
return "Terminating"
}

// not running
if pod.Status.Phase != v1.PodRunning {
return string(pod.Status.Phase)
Expand Down
79 changes: 16 additions & 63 deletions src/backend/routers/commentsRouter_controllers_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,45 @@ import (
)

func init() {
beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressController"],

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"],
beego.ControllerComments{
Method: "List",
Router: `/`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Params: nil,
},
Params: nil})

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"],
beego.ControllerComments{
Method: "Create",
Router: `/`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil,
},
beego.ControllerComments{
Method: "Delete",
Router: `/:id([0-9]+)`,
AllowHTTPMethods: []string{"delete"},
MethodParams: param.Make(),
Params: nil,
},
Params: nil})

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"],
beego.ControllerComments{
Method: "Get",
Router: `/:id([0-9]+)`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Params: nil,
},
Params: nil})

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"],
beego.ControllerComments{
Method: "Update",
Router: `/:id([0-9]+)`,
AllowHTTPMethods: []string{"put"},
MethodParams: param.Make(),
Params: nil,
},
beego.ControllerComments{
Method: "GetNames",
Router: `/names`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Params: nil,
},
beego.ControllerComments{
Method: "UpdateOrders",
Router: `/updateorders`,
AllowHTTPMethods: []string{"put"},
MethodParams: param.Make(),
Params: nil,
},
)
Params: nil})

beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"] = append(beego.GlobalControllerRouter["github.com/Qihoo360/wayne/src/backend/controllers/ingress:IngressTplController"],
beego.ControllerComments{
Method: "List",
Router: `/`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Params: nil,
},
beego.ControllerComments{
Method: "Create",
Router: `/`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil,
},
beego.ControllerComments{
Method: "Update",
Router: `/:id`,
AllowHTTPMethods: []string{"put"},
MethodParams: param.Make(),
Params: nil,
},
beego.ControllerComments{
Method: "Delete",
Router: `/:id`,
AllowHTTPMethods: []string{"delete"},
MethodParams: param.Make(),
Params: nil,
},
beego.ControllerComments{
Method: "Get",
Router: `/:id([0-9]+)`,
AllowHTTPMethods: []string{"get"},
AllowHTTPMethods: []string{"delete"},
MethodParams: param.Make(),
Params: nil,
},
)
Params: nil})

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class NodesComponent implements OnInit, OnDestroy {

this.nodeClient.list(this.cluster).subscribe(
response => {
const nodes = response.data;
const nodes = response.data.nodes;
this.inventory.size = nodes.length;
this.inventory.reset(nodes);
this.nodes = this.inventory.all;
Expand Down