Skip to content

Commit

Permalink
fakemetricsclient: use Create with gvr to populate nodes/pods metrics…
Browse files Browse the repository at this point in the history
…es instead
  • Loading branch information
ingvagabund committed Nov 8, 2024
1 parent edefa1d commit baa6650
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
14 changes: 9 additions & 5 deletions pkg/descheduler/descheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
policy "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
apiversion "k8s.io/apimachinery/pkg/version"
fakediscovery "k8s.io/client-go/discovery/fake"
"k8s.io/client-go/informers"
Expand All @@ -33,6 +34,11 @@ import (
"sigs.k8s.io/descheduler/test"
)

var (
nodesgvr = schema.GroupVersionResource{Group: "metrics.k8s.io", Version: "v1beta1", Resource: "nodes"}
podsgvr = schema.GroupVersionResource{Group: "metrics.k8s.io", Version: "v1beta1", Resource: "pods"}
)

func initPluginRegistry() {
pluginregistry.PluginRegistry = pluginregistry.NewRegistry()
pluginregistry.Register(removeduplicates.PluginName, removeduplicates.New, &removeduplicates.RemoveDuplicates{}, &removeduplicates.RemoveDuplicatesArgs{}, removeduplicates.ValidateRemoveDuplicatesArgs, removeduplicates.SetDefaults_RemoveDuplicatesArgs, pluginregistry.PluginRegistry)
Expand Down Expand Up @@ -617,16 +623,14 @@ func TestLoadAwareDescheduling(t *testing.T) {
test.BuildPodMetrics("p5", 400, 0),
}

var metricsObjs []runtime.Object
metricsClientset := fakemetricsclient.NewSimpleClientset()
for _, nodemetrics := range nodemetricses {
metricsObjs = append(metricsObjs, nodemetrics)
metricsClientset.Tracker().Create(nodesgvr, nodemetrics, "")
}
for _, podmetrics := range podmetricses {
metricsObjs = append(metricsObjs, podmetrics)
metricsClientset.Tracker().Create(podsgvr, podmetrics, podmetrics.Namespace)
}

metricsClientset := fakemetricsclient.NewSimpleClientset(metricsObjs...)

policy := lowNodeUtilizationPolicy(
api.ResourceThresholds{
v1.ResourceCPU: 30,
Expand Down
7 changes: 5 additions & 2 deletions pkg/descheduler/metricscollector/metricscollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func checkCpuNodeUsage(t *testing.T, usage map[v1.ResourceName]*resource.Quantit
}

func TestMetricsCollector(t *testing.T) {
gvr := schema.GroupVersionResource{Group: "metrics.k8s.io", Version: "v1beta1", Resource: "nodemetricses"}
gvr := schema.GroupVersionResource{Group: "metrics.k8s.io", Version: "v1beta1", Resource: "nodes"}

n1 := test.BuildTestNode("n1", 2000, 3000, 10, nil)
n2 := test.BuildTestNode("n2", 2000, 3000, 10, nil)
Expand All @@ -48,7 +48,10 @@ func TestMetricsCollector(t *testing.T) {
n3metrics := test.BuildNodeMetrics("n3", 300, 1714978816)

clientset := fakeclientset.NewSimpleClientset(n1, n2, n3)
metricsClientset := fakemetricsclient.NewSimpleClientset(n1metrics, n2metrics, n3metrics)
metricsClientset := fakemetricsclient.NewSimpleClientset()
metricsClientset.Tracker().Create(gvr, n1metrics, "")
metricsClientset.Tracker().Create(gvr, n2metrics, "")
metricsClientset.Tracker().Create(gvr, n3metrics, "")

t.Logf("Set initial node cpu usage to 1400")
collector := NewMetricsCollector(clientset, metricsClientset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,16 +1137,17 @@ func TestLowNodeUtilization(t *testing.T) {
for _, pod := range tc.pods {
objs = append(objs, pod)
}
var metricsObjs []runtime.Object

metricsClientset := fakemetricsclient.NewSimpleClientset()
for _, nodemetrics := range tc.nodemetricses {
metricsObjs = append(metricsObjs, nodemetrics)
metricsClientset.Tracker().Create(nodesgvr, nodemetrics, "")
}
for _, podmetrics := range tc.podmetricses {
metricsObjs = append(metricsObjs, podmetrics)
metricsClientset.Tracker().Create(podsgvr, podmetrics, podmetrics.Namespace)
}

fakeClient := fake.NewSimpleClientset(objs...)
metricsClientset := fakemetricsclient.NewSimpleClientset(metricsObjs...)

collector := metricscollector.NewMetricsCollector(fakeClient, metricsClientset)
err := collector.Collect(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/framework/plugins/nodeutilization/usageclients.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ type queryResult struct {
func (client *fakePromClient) URL(ep string, args map[string]string) *url.URL {
return &url.URL{}
}

func (client *fakePromClient) Do(ctx context.Context, request *http.Request) (*http.Response, []byte, error) {
jsonData, err := json.Marshal(fakePayload{
Status: "success",
Expand Down
12 changes: 9 additions & 3 deletions pkg/framework/plugins/nodeutilization/usageclients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import (
"sigs.k8s.io/descheduler/test"
)

var gvr = schema.GroupVersionResource{Group: "metrics.k8s.io", Version: "v1beta1", Resource: "nodemetricses"}
var (
nodesgvr = schema.GroupVersionResource{Group: "metrics.k8s.io", Version: "v1beta1", Resource: "nodes"}
podsgvr = schema.GroupVersionResource{Group: "metrics.k8s.io", Version: "v1beta1", Resource: "pods"}
)

func updateMetricsAndCheckNodeUtilization(
t *testing.T,
Expand All @@ -50,7 +53,7 @@ func updateMetricsAndCheckNodeUtilization(
) {
t.Logf("Set current node cpu usage to %v", newValue)
nodemetrics.Usage[v1.ResourceCPU] = *resource.NewMilliQuantity(newValue, resource.DecimalSI)
metricsClientset.Tracker().Update(gvr, nodemetrics, "")
metricsClientset.Tracker().Update(nodesgvr, nodemetrics, "")
err := collector.Collect(ctx)
if err != nil {
t.Fatalf("failed to capture metrics: %v", err)
Expand Down Expand Up @@ -92,7 +95,10 @@ func TestActualUsageClient(t *testing.T) {
n3metrics := test.BuildNodeMetrics("n3", 300, 1714978816)

clientset := fakeclientset.NewSimpleClientset(n1, n2, n3, p1, p21, p22, p3)
metricsClientset := fakemetricsclient.NewSimpleClientset(n1metrics, n2metrics, n3metrics)
metricsClientset := fakemetricsclient.NewSimpleClientset()
metricsClientset.Tracker().Create(nodesgvr, n1metrics, "")
metricsClientset.Tracker().Create(nodesgvr, n2metrics, "")
metricsClientset.Tracker().Create(nodesgvr, n3metrics, "")

ctx := context.TODO()

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit baa6650

Please sign in to comment.