From 041549ef8daa1bf422089e70b8a9f21c7bc023bd Mon Sep 17 00:00:00 2001 From: Piotr Szczesniak Date: Mon, 13 Jul 2015 10:05:14 +0200 Subject: [PATCH] Added exporting cpu/mem limits based on Kubernetes rather than cadvisor Fixes #399 --- sinks/api/decoder_test.go | 3 +-- sinks/api/v1/decoder_test.go | 3 +-- sinks/api/v1/supported_metrics.go | 3 +-- sources/kube_nodes.go | 3 +++ sources/nodes/coreos_test.go | 4 ++-- sources/nodes/kube.go | 4 ++++ sources/nodes/types.go | 4 ++++ 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/sinks/api/decoder_test.go b/sinks/api/decoder_test.go index 46d77ec5c4..8fefce89d8 100644 --- a/sinks/api/decoder_test.go +++ b/sinks/api/decoder_test.go @@ -197,8 +197,7 @@ func TestRealInput(t *testing.T) { case "cpu/limit": value, ok := entry.Point.Value.(int64) require.True(t, ok) - expected := (spec.Cpu.Limit * 1000) / 1024 - assert.Equal(t, expected, value) + assert.Equal(t, spec.Cpu.Limit, value) case "memory/limit": value, ok := entry.Point.Value.(int64) require.True(t, ok) diff --git a/sinks/api/v1/decoder_test.go b/sinks/api/v1/decoder_test.go index 18fa97e75c..d387d11f5b 100644 --- a/sinks/api/v1/decoder_test.go +++ b/sinks/api/v1/decoder_test.go @@ -195,8 +195,7 @@ func TestRealInput(t *testing.T) { case "cpu/limit": value, ok := entry.Point.Value.(int64) require.True(t, ok) - expected := (spec.Cpu.Limit * 1000) / 1024 - assert.Equal(t, expected, value) + assert.Equal(t, spec.Cpu.Limit, value) case "memory/limit": value, ok := entry.Point.Value.(int64) require.True(t, ok) diff --git a/sinks/api/v1/supported_metrics.go b/sinks/api/v1/supported_metrics.go index 456df33652..d90526dca3 100644 --- a/sinks/api/v1/supported_metrics.go +++ b/sinks/api/v1/supported_metrics.go @@ -66,8 +66,7 @@ var statMetrics = []SupportedStatMetric{ return spec.HasCpu && (spec.Cpu.Limit > 0) }, GetValue: func(spec *cadvisor.ContainerSpec, stat *cadvisor.ContainerStats) []InternalPoint { - // Normalize to a conversion factor of 1000. - return []InternalPoint{{Value: int64(spec.Cpu.Limit*1000) / 1024}} + return []InternalPoint{{Value: int64(spec.Cpu.Limit)}} }, OnlyExportIfChanged: true, }, diff --git a/sources/kube_nodes.go b/sources/kube_nodes.go index fe00120e21..8844c96fb2 100644 --- a/sources/kube_nodes.go +++ b/sources/kube_nodes.go @@ -82,6 +82,9 @@ func (self *kubeNodeMetrics) updateStats(host nodes.Host, info nodes.Info, start hostContainer = &hostCopy containers = append(containers[:hostIndex], containers[hostIndex+1:]...) } + // This is temporary workaround for #399. + hostContainer.Spec.Cpu.Limit = info.CpuCapacity + hostContainer.Spec.Memory.Limit = info.MemCapacity return hostContainer, containers, nil } diff --git a/sources/nodes/coreos_test.go b/sources/nodes/coreos_test.go index 013bd89bf8..6ca5d51f1d 100644 --- a/sources/nodes/coreos_test.go +++ b/sources/nodes/coreos_test.go @@ -63,8 +63,8 @@ func TestSuccessCase(t *testing.T) { nodeList, err := nodesApi.List() require.NoError(t, err) assert.Len(t, nodeList.Items, 2) - assert.Equal(t, nodeList.Items["a"], Info{"1.2.3.4", "1.2.3.4", ""}) - assert.Equal(t, nodeList.Items["b"], Info{"1.2.3.5", "1.2.3.5", ""}) + assert.Equal(t, nodeList.Items["a"], Info{"1.2.3.4", "1.2.3.4", "", 0, 0}) + assert.Equal(t, nodeList.Items["b"], Info{"1.2.3.5", "1.2.3.5", "", 0, 0}) } func TestFailureCase(t *testing.T) { diff --git a/sources/nodes/kube.go b/sources/nodes/kube.go index 7ba85647f9..d7508c49b8 100644 --- a/sources/nodes/kube.go +++ b/sources/nodes/kube.go @@ -92,6 +92,10 @@ func (self *kubeNodes) getNodeInfoAndHostname(node api.Node) (Info, string, erro if node.Spec.ExternalID != "" { nodeInfo.ExternalID = node.Spec.ExternalID } + cpu := node.Status.Capacity[api.ResourceCPU] + mem := node.Status.Capacity[api.ResourceMemory] + nodeInfo.CpuCapacity = uint64(cpu.MilliValue()) + nodeInfo.MemCapacity = uint64(mem.Value()) return nodeInfo, hostname, nodeErr } diff --git a/sources/nodes/types.go b/sources/nodes/types.go index 49909f96e1..7f3db6c50a 100644 --- a/sources/nodes/types.go +++ b/sources/nodes/types.go @@ -30,6 +30,10 @@ type Info struct { InternalIP string // An optional ID assigned to nodes by either the cloud provider or user. ExternalID string + // Cpu capacity of node in millicores. + CpuCapacity uint64 + // Memory capacity of node in bytes. + MemCapacity uint64 } // NodeList contains the nodes that an instance of heapster is required to