Skip to content

Commit e1f4d5b

Browse files
author
Kenichi Omichi
committed
Add nodeHasControlPlanePods()
DeprecatedMightBeMasterNode() has been marked as deprecated and we need to find alternative way for callers of the function. In NewResourceUsageGatherer(), the function was called for distinguishing the specified pods are running on master nodes, and the gatherer gathers those pods' resource usage. This adds nodeHasControlPlanePods() to gistinguish the specified pods are running on nodes which are operating control plane pods (kube-scheduler and kube-controller-manager) and replace callers of DeprecatedMightBeMasterNode() with this new function as better way.
1 parent 5afc42d commit e1f4d5b

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

test/e2e/framework/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ go_library(
6565
"//test/e2e/framework/node:go_default_library",
6666
"//test/e2e/framework/pod:go_default_library",
6767
"//test/e2e/framework/ssh:go_default_library",
68-
"//test/e2e/system:go_default_library",
6968
"//test/utils:go_default_library",
7069
"//test/utils/image:go_default_library",
7170
"//vendor/github.com/onsi/ginkgo:go_default_library",

test/e2e/framework/resource_usage_gatherer.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"encoding/json"
2424
"fmt"
2525
"math"
26+
"regexp"
2627
"sort"
2728
"strconv"
2829
"strings"
@@ -32,10 +33,10 @@ import (
3233

3334
v1 "k8s.io/api/core/v1"
3435
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36+
"k8s.io/apimachinery/pkg/fields"
3537
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3638
clientset "k8s.io/client-go/kubernetes"
3739
kubeletstatsv1alpha1 "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
38-
"k8s.io/kubernetes/test/e2e/system"
3940

4041
// TODO: Remove the following imports (ref: https://github.com/kubernetes/kubernetes/issues/81245)
4142
e2essh "k8s.io/kubernetes/test/e2e/framework/ssh"
@@ -370,6 +371,29 @@ const (
370371
MasterAndDNSNodes NodesSet = 2
371372
)
372373

374+
// nodeHasControlPlanePods returns true if specified node has control plane pods
375+
// (kube-scheduler and/or kube-controller-manager).
376+
func nodeHasControlPlanePods(c clientset.Interface, nodeName string) (bool, error) {
377+
regKubeScheduler := regexp.MustCompile("kube-scheduler-.*")
378+
regKubeControllerManager := regexp.MustCompile("kube-controller-manager-.*")
379+
380+
podList, err := c.CoreV1().Pods(metav1.NamespaceSystem).List(context.TODO(), metav1.ListOptions{
381+
FieldSelector: fields.OneTermEqualSelector("spec.nodeName", nodeName).String(),
382+
})
383+
if err != nil {
384+
return false, err
385+
}
386+
if len(podList.Items) < 1 {
387+
Logf("Can't find any pods in namespace %s to grab metrics from", metav1.NamespaceSystem)
388+
}
389+
for _, pod := range podList.Items {
390+
if regKubeScheduler.MatchString(pod.Name) || regKubeControllerManager.MatchString(pod.Name) {
391+
return true, nil
392+
}
393+
}
394+
return false, nil
395+
}
396+
373397
// NewResourceUsageGatherer returns a new ContainerResourceGatherer.
374398
func NewResourceUsageGatherer(c clientset.Interface, options ResourceGathererOptions, pods *v1.PodList) (*ContainerResourceGatherer, error) {
375399
g := ContainerResourceGatherer{
@@ -404,11 +428,23 @@ func NewResourceUsageGatherer(c clientset.Interface, options ResourceGathererOpt
404428
}
405429
dnsNodes := make(map[string]bool)
406430
for _, pod := range pods.Items {
407-
if (options.Nodes == MasterNodes) && !system.DeprecatedMightBeMasterNode(pod.Spec.NodeName) {
408-
continue
431+
if options.Nodes == MasterNodes {
432+
isControlPlane, err := nodeHasControlPlanePods(c, pod.Spec.NodeName)
433+
if err != nil {
434+
return nil, err
435+
}
436+
if !isControlPlane {
437+
continue
438+
}
409439
}
410-
if (options.Nodes == MasterAndDNSNodes) && !system.DeprecatedMightBeMasterNode(pod.Spec.NodeName) && pod.Labels["k8s-app"] != "kube-dns" {
411-
continue
440+
if options.Nodes == MasterAndDNSNodes {
441+
isControlPlane, err := nodeHasControlPlanePods(c, pod.Spec.NodeName)
442+
if err != nil {
443+
return nil, err
444+
}
445+
if !isControlPlane && pod.Labels["k8s-app"] != "kube-dns" {
446+
continue
447+
}
412448
}
413449
for _, container := range pod.Status.InitContainerStatuses {
414450
g.containerIDs = append(g.containerIDs, container.Name)
@@ -427,7 +463,11 @@ func NewResourceUsageGatherer(c clientset.Interface, options ResourceGathererOpt
427463
}
428464

429465
for _, node := range nodeList.Items {
430-
if options.Nodes == AllNodes || system.DeprecatedMightBeMasterNode(node.Name) || dnsNodes[node.Name] {
466+
isControlPlane, err := nodeHasControlPlanePods(c, node.Name)
467+
if err != nil {
468+
return nil, err
469+
}
470+
if options.Nodes == AllNodes || isControlPlane || dnsNodes[node.Name] {
431471
g.workerWg.Add(1)
432472
g.workers = append(g.workers, resourceGatherWorker{
433473
c: c,

0 commit comments

Comments
 (0)