Skip to content

Commit

Permalink
Merge pull request #1061 from timstclair/npe
Browse files Browse the repository at this point in the history
Fix v2 stats conversion NPD & add test
  • Loading branch information
jimmidyson committed Jan 17, 2016
2 parents 5e3f237 + 04f3743 commit 871697f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
7 changes: 5 additions & 2 deletions info/v2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,18 @@ func ContainerStatsFromV1(spec *v1.ContainerSpec, stats []*v1.ContainerStats) []
stat.Memory = &val.Memory
}
if spec.HasNetwork {
stat.Network.Interfaces = val.Network.Interfaces
// TODO: Handle TcpStats
stat.Network = &NetworkStats{
Interfaces: val.Network.Interfaces,
}
}
if spec.HasFilesystem {
if len(val.Filesystem) == 1 {
stat.Filesystem = &FilesystemStats{
TotalUsageBytes: &val.Filesystem[0].Usage,
BaseUsageBytes: &val.Filesystem[0].BaseUsage,
}
} else {
} else if len(val.Filesystem) > 1 {
// Cannot handle multiple devices per container.
glog.Errorf("failed to handle multiple devices for container. Skipping Filesystem stats")
}
Expand Down
101 changes: 100 additions & 1 deletion info/v2/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
labels = map[string]string{"foo": "bar"}
)

func TestConvertSpec(t *testing.T) {
func TestContanierSpecFromV1(t *testing.T) {
v1Spec := v1.ContainerSpec{
CreationTime: timestamp,
Labels: labels,
Expand Down Expand Up @@ -96,6 +96,105 @@ func TestConvertSpec(t *testing.T) {
}
}

func TestContainerStatsFromV1(t *testing.T) {
v1Spec := v1.ContainerSpec{
CreationTime: timestamp,
Labels: labels,
HasCpu: true,
Cpu: v1.CpuSpec{
Limit: 2048,
MaxLimit: 4096,
Mask: "cpu_mask",
},
HasMemory: true,
Memory: v1.MemorySpec{
Limit: 2048,
Reservation: 1024,
SwapLimit: 8192,
},
HasNetwork: true,
HasFilesystem: true,
HasDiskIo: true,
HasCustomMetrics: true,
CustomMetrics: []v1.MetricSpec{{
Name: "foo",
Type: v1.MetricGauge,
Format: v1.IntType,
Units: "bars",
}},
Image: "gcr.io/kubernetes/kubernetes:v1",
}
v1Stats := v1.ContainerStats{
Timestamp: timestamp,
Memory: v1.MemoryStats{
Usage: 1,
Cache: 2,
RSS: 3,
WorkingSet: 4,
Failcnt: 5,
ContainerData: v1.MemoryStatsMemoryData{
Pgfault: 1,
Pgmajfault: 2,
},
HierarchicalData: v1.MemoryStatsMemoryData{
Pgfault: 10,
Pgmajfault: 20,
},
},
Network: v1.NetworkStats{
InterfaceStats: v1.InterfaceStats{
Name: "",
RxBytes: 1,
RxPackets: 2,
RxErrors: 3,
RxDropped: 4,
TxBytes: 5,
TxPackets: 6,
TxErrors: 7,
TxDropped: 8,
},
Interfaces: []v1.InterfaceStats{{
Name: "eth0",
RxBytes: 10,
RxPackets: 20,
RxErrors: 30,
RxDropped: 40,
TxBytes: 50,
TxPackets: 60,
TxErrors: 70,
TxDropped: 80,
}},
},
Filesystem: []v1.FsStats{{
Device: "dev0",
Limit: 500,
Usage: 100,
BaseUsage: 50,
Available: 300,
}},
}
expectedV2Stats := ContainerStats{
Timestamp: timestamp,
Cpu: &v1Stats.Cpu,
DiskIo: &v1Stats.DiskIo,
Memory: &v1Stats.Memory,
Network: &NetworkStats{
Interfaces: v1Stats.Network.Interfaces,
},
Filesystem: &FilesystemStats{
TotalUsageBytes: &v1Stats.Filesystem[0].Usage,
BaseUsageBytes: &v1Stats.Filesystem[0].BaseUsage,
},
}

v2Stats := ContainerStatsFromV1(&v1Spec, []*v1.ContainerStats{&v1Stats})
actualV2Stats := *v2Stats[0]

if !reflect.DeepEqual(expectedV2Stats, actualV2Stats) {
t.Errorf("Converted stats differs from expectation!\nExpected: %+v\n Got: %+v\n", expectedV2Stats, actualV2Stats)
}
}

func TestInstCpuStats(t *testing.T) {
tests := []struct {
last *v1.ContainerStats
Expand Down

0 comments on commit 871697f

Please sign in to comment.