@@ -17,6 +17,7 @@ limitations under the License.
17
17
package framework
18
18
19
19
import (
20
+ "bufio"
20
21
"bytes"
21
22
"context"
22
23
"encoding/json"
@@ -36,6 +37,9 @@ import (
36
37
kubeletstatsv1alpha1 "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
37
38
"k8s.io/kubernetes/pkg/master/ports"
38
39
"k8s.io/kubernetes/test/e2e/system"
40
+
41
+ // TODO: Remove the following imports (ref: https://github.com/kubernetes/kubernetes/issues/81245)
42
+ e2essh "k8s.io/kubernetes/test/e2e/framework/ssh"
39
43
)
40
44
41
45
// ResourceConstraint is a struct to hold constraints.
@@ -70,9 +74,6 @@ type ResourceUsagePerContainer map[string]*ContainerResourceUsage
70
74
// we can't have int here, as JSON does not accept integer keys.
71
75
type ResourceUsageSummary map [string ][]SingleContainerSummary
72
76
73
- // NoCPUConstraint is the number of constraint for CPU.
74
- const NoCPUConstraint = math .MaxFloat64
75
-
76
77
// PrintHumanReadable prints resource usage summary in human readable.
77
78
func (s * ResourceUsageSummary ) PrintHumanReadable () string {
78
79
buf := & bytes.Buffer {}
@@ -183,7 +184,7 @@ type resourceGatherWorker struct {
183
184
func (w * resourceGatherWorker ) singleProbe () {
184
185
data := make (ResourceUsagePerContainer )
185
186
if w .inKubemark {
186
- kubemarkData := GetKubemarkMasterComponentsResourceUsage ()
187
+ kubemarkData := getKubemarkMasterComponentsResourceUsage ()
187
188
if data == nil {
188
189
return
189
190
}
@@ -556,3 +557,65 @@ func (g *ContainerResourceGatherer) StopAndSummarize(percentiles []int, constrai
556
557
}
557
558
return & summary , nil
558
559
}
560
+
561
+ // kubemarkResourceUsage is a struct for tracking the resource usage of kubemark.
562
+ type kubemarkResourceUsage struct {
563
+ Name string
564
+ MemoryWorkingSetInBytes uint64
565
+ CPUUsageInCores float64
566
+ }
567
+
568
+ func getMasterUsageByPrefix (prefix string ) (string , error ) {
569
+ sshResult , err := e2essh .SSH (fmt .Sprintf ("ps ax -o %%cpu,rss,command | tail -n +2 | grep %v | sed 's/\\ s+/ /g'" , prefix ), GetMasterHost ()+ ":22" , TestContext .Provider )
570
+ if err != nil {
571
+ return "" , err
572
+ }
573
+ return sshResult .Stdout , nil
574
+ }
575
+
576
+ // getKubemarkMasterComponentsResourceUsage returns the resource usage of kubemark which contains multiple combinations of cpu and memory usage for each pod name.
577
+ func getKubemarkMasterComponentsResourceUsage () map [string ]* kubemarkResourceUsage {
578
+ result := make (map [string ]* kubemarkResourceUsage )
579
+ // Get kubernetes component resource usage
580
+ sshResult , err := getMasterUsageByPrefix ("kube" )
581
+ if err != nil {
582
+ Logf ("Error when trying to SSH to master machine. Skipping probe. %v" , err )
583
+ return nil
584
+ }
585
+ scanner := bufio .NewScanner (strings .NewReader (sshResult ))
586
+ for scanner .Scan () {
587
+ var cpu float64
588
+ var mem uint64
589
+ var name string
590
+ fmt .Sscanf (strings .TrimSpace (scanner .Text ()), "%f %d /usr/local/bin/kube-%s" , & cpu , & mem , & name )
591
+ if name != "" {
592
+ // Gatherer expects pod_name/container_name format
593
+ fullName := name + "/" + name
594
+ result [fullName ] = & kubemarkResourceUsage {Name : fullName , MemoryWorkingSetInBytes : mem * 1024 , CPUUsageInCores : cpu / 100 }
595
+ }
596
+ }
597
+ // Get etcd resource usage
598
+ sshResult , err = getMasterUsageByPrefix ("bin/etcd" )
599
+ if err != nil {
600
+ Logf ("Error when trying to SSH to master machine. Skipping probe" )
601
+ return nil
602
+ }
603
+ scanner = bufio .NewScanner (strings .NewReader (sshResult ))
604
+ for scanner .Scan () {
605
+ var cpu float64
606
+ var mem uint64
607
+ var etcdKind string
608
+ fmt .Sscanf (strings .TrimSpace (scanner .Text ()), "%f %d /bin/sh -c /usr/local/bin/etcd" , & cpu , & mem )
609
+ dataDirStart := strings .Index (scanner .Text (), "--data-dir" )
610
+ if dataDirStart < 0 {
611
+ continue
612
+ }
613
+ fmt .Sscanf (scanner .Text ()[dataDirStart :], "--data-dir=/var/%s" , & etcdKind )
614
+ if etcdKind != "" {
615
+ // Gatherer expects pod_name/container_name format
616
+ fullName := "etcd/" + etcdKind
617
+ result [fullName ] = & kubemarkResourceUsage {Name : fullName , MemoryWorkingSetInBytes : mem * 1024 , CPUUsageInCores : cpu / 100 }
618
+ }
619
+ }
620
+ return result
621
+ }
0 commit comments