Skip to content

Commit 02ac661

Browse files
authored
Merge pull request kubernetes#87271 from tanjunchen/move-function-GetKubemarkMasterComponentResoureUsage
move function GetKubemarkMasterComponentResoureUsage and remove long-time TODO
2 parents 90d6484 + 69461d2 commit 02ac661

File tree

3 files changed

+67
-94
lines changed

3 files changed

+67
-94
lines changed

test/e2e/framework/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ go_library(
88
"expect.go",
99
"flake_reporting_util.go",
1010
"framework.go",
11-
"get-kubemark-resource-usage.go",
1211
"google_compute.go",
1312
"log.go",
1413
"log_size_monitoring.go",

test/e2e/framework/get-kubemark-resource-usage.go

Lines changed: 0 additions & 89 deletions
This file was deleted.

test/e2e/framework/resource_usage_gatherer.go

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package framework
1818

1919
import (
20+
"bufio"
2021
"bytes"
2122
"context"
2223
"encoding/json"
@@ -36,6 +37,9 @@ import (
3637
kubeletstatsv1alpha1 "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
3738
"k8s.io/kubernetes/pkg/master/ports"
3839
"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"
3943
)
4044

4145
// ResourceConstraint is a struct to hold constraints.
@@ -70,9 +74,6 @@ type ResourceUsagePerContainer map[string]*ContainerResourceUsage
7074
// we can't have int here, as JSON does not accept integer keys.
7175
type ResourceUsageSummary map[string][]SingleContainerSummary
7276

73-
// NoCPUConstraint is the number of constraint for CPU.
74-
const NoCPUConstraint = math.MaxFloat64
75-
7677
// PrintHumanReadable prints resource usage summary in human readable.
7778
func (s *ResourceUsageSummary) PrintHumanReadable() string {
7879
buf := &bytes.Buffer{}
@@ -183,7 +184,7 @@ type resourceGatherWorker struct {
183184
func (w *resourceGatherWorker) singleProbe() {
184185
data := make(ResourceUsagePerContainer)
185186
if w.inKubemark {
186-
kubemarkData := GetKubemarkMasterComponentsResourceUsage()
187+
kubemarkData := getKubemarkMasterComponentsResourceUsage()
187188
if data == nil {
188189
return
189190
}
@@ -556,3 +557,65 @@ func (g *ContainerResourceGatherer) StopAndSummarize(percentiles []int, constrai
556557
}
557558
return &summary, nil
558559
}
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

Comments
 (0)