github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/evictionmanager/plugin/resource/reclaimed_resources_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/api/resource"
    27  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/client-go/tools/events"
    30  
    31  	"github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
    32  	"github.com/kubewharf/katalyst-api/pkg/consts"
    33  	pluginapi "github.com/kubewharf/katalyst-api/pkg/protocol/evictionplugin/v1alpha1"
    34  	katalyst_base "github.com/kubewharf/katalyst-core/cmd/base"
    35  	"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/options"
    36  	"github.com/kubewharf/katalyst-core/pkg/client"
    37  	"github.com/kubewharf/katalyst-core/pkg/config"
    38  	"github.com/kubewharf/katalyst-core/pkg/metaserver"
    39  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent"
    40  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/cnr"
    41  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/node"
    42  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod"
    43  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    44  )
    45  
    46  func generateTestConfiguration(t *testing.T, nodeName string) *config.Configuration {
    47  	testConfiguration, err := options.NewOptions().Config()
    48  	require.NoError(t, err)
    49  	require.NotNil(t, testConfiguration)
    50  	testConfiguration.NodeName = nodeName
    51  	return testConfiguration
    52  }
    53  
    54  func generateTestMetaServer(clientSet *client.GenericClientSet, conf *config.Configuration, pods []*corev1.Pod) *metaserver.MetaServer {
    55  	return &metaserver.MetaServer{
    56  		MetaAgent: &agent.MetaAgent{
    57  			PodFetcher: &pod.PodFetcherStub{PodList: pods},
    58  			NodeFetcher: node.NewRemoteNodeFetcher(conf.BaseConfiguration, conf.NodeConfiguration,
    59  				clientSet.KubeClient.CoreV1().Nodes()),
    60  			CNRFetcher: cnr.NewCachedCNRFetcher(conf.BaseConfiguration, conf.CNRConfiguration,
    61  				clientSet.InternalClient.NodeV1alpha1().CustomNodeResources()),
    62  		},
    63  	}
    64  }
    65  
    66  func TestNewReclaimedResourcesEvictionPlugin(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	testNodeName := "test-node"
    70  	testConf := generateTestConfiguration(t, testNodeName)
    71  	pods := []*corev1.Pod{
    72  		{
    73  			ObjectMeta: v1.ObjectMeta{
    74  				Name: "test-pod-1",
    75  				Annotations: map[string]string{
    76  					consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelReclaimedCores,
    77  				},
    78  			},
    79  			Spec: corev1.PodSpec{
    80  				Containers: []corev1.Container{
    81  					{
    82  						Name: "test-container-1",
    83  						Resources: corev1.ResourceRequirements{
    84  							Requests: corev1.ResourceList{
    85  								consts.ReclaimedResourceMilliCPU: resource.MustParse("5000"),
    86  							},
    87  						},
    88  					},
    89  				},
    90  			},
    91  		},
    92  		{
    93  			ObjectMeta: v1.ObjectMeta{
    94  				Name: "test-pod-2",
    95  				Annotations: map[string]string{
    96  					consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelReclaimedCores,
    97  				},
    98  			},
    99  			Spec: corev1.PodSpec{
   100  				Containers: []corev1.Container{
   101  					{
   102  						Name: "test-container-2",
   103  						Resources: corev1.ResourceRequirements{
   104  							Requests: corev1.ResourceList{
   105  								consts.ReclaimedResourceMilliCPU: resource.MustParse("5000"),
   106  							},
   107  						},
   108  					},
   109  				},
   110  			},
   111  		},
   112  		{
   113  			ObjectMeta: v1.ObjectMeta{
   114  				Name: "test-pod-2",
   115  			},
   116  		},
   117  	}
   118  	ctx, err := katalyst_base.GenerateFakeGenericContext(nil, []runtime.Object{
   119  		&v1alpha1.CustomNodeResource{
   120  			ObjectMeta: v1.ObjectMeta{
   121  				Name: testNodeName,
   122  			},
   123  			Status: v1alpha1.CustomNodeResourceStatus{
   124  				Resources: v1alpha1.Resources{
   125  					Allocatable: &corev1.ResourceList{
   126  						consts.ReclaimedResourceMilliCPU: resource.MustParse("1000"),
   127  					},
   128  				},
   129  			},
   130  		},
   131  	}, nil)
   132  	assert.NoError(t, err)
   133  
   134  	testMetaServer := generateTestMetaServer(ctx.Client, testConf, pods)
   135  
   136  	plugin := NewReclaimedResourcesEvictionPlugin(ctx.Client, &events.FakeRecorder{}, testMetaServer,
   137  		metrics.DummyMetrics{}, testConf)
   138  	assert.NoError(t, err)
   139  
   140  	met, err := plugin.ThresholdMet(context.TODO())
   141  	assert.NoError(t, err)
   142  	assert.NotNil(t, met)
   143  
   144  	evictionPods, err := plugin.GetTopEvictionPods(context.TODO(), &pluginapi.GetTopEvictionPodsRequest{
   145  		ActivePods:    pods,
   146  		TopN:          1,
   147  		EvictionScope: met.EvictionScope,
   148  	})
   149  	assert.NoError(t, err)
   150  	assert.NotNil(t, evictionPods)
   151  	assert.NotEqual(t, 0, len(evictionPods.GetTargetPods()))
   152  
   153  	evictPods, err := plugin.GetEvictPods(context.TODO(), &pluginapi.GetEvictPodsRequest{
   154  		ActivePods: pods,
   155  	})
   156  	assert.NoError(t, err)
   157  	assert.NotNil(t, evictPods)
   158  }