github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/eventhandlers/cache_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 eventhandlers
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/api/resource"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  
    28  	apis "github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
    29  	"github.com/kubewharf/katalyst-api/pkg/consts"
    30  	schedulercache "github.com/kubewharf/katalyst-core/pkg/scheduler/cache"
    31  )
    32  
    33  var makeCachedPod = func(uid types.UID, name string, res v1.ResourceList, node string) *v1.Pod {
    34  	pod := &v1.Pod{
    35  		ObjectMeta: metav1.ObjectMeta{
    36  			Namespace: "n1",
    37  			Name:      name,
    38  			UID:       uid,
    39  		},
    40  		Spec: v1.PodSpec{
    41  			Containers: []v1.Container{
    42  				{
    43  					Name: "c1",
    44  					Resources: v1.ResourceRequirements{
    45  						Limits:   res,
    46  						Requests: res,
    47  					},
    48  				},
    49  			},
    50  			NodeName: node,
    51  		},
    52  	}
    53  	return pod
    54  }
    55  
    56  var makeCachedCNR = func(name string, res v1.ResourceList) *apis.CustomNodeResource {
    57  	cnr := &apis.CustomNodeResource{
    58  		ObjectMeta: metav1.ObjectMeta{Name: name},
    59  		Status: apis.CustomNodeResourceStatus{
    60  			Resources: apis.Resources{
    61  				Allocatable: &res,
    62  			},
    63  		},
    64  	}
    65  	return cnr
    66  }
    67  
    68  func Test_CalculateQoSResource(t *testing.T) {
    69  	t.Parallel()
    70  
    71  	cache := schedulercache.GetCache()
    72  
    73  	_, err := cache.GetNodeInfo("c1")
    74  	assert.NotNil(t, err)
    75  
    76  	t.Log("####1: add pod with not-exist cnr")
    77  	p1 := makeCachedPod("p1", "p1", map[v1.ResourceName]resource.Quantity{
    78  		consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(2000, resource.DecimalSI),
    79  		consts.ReclaimedResourceMemory:   *resource.NewQuantity(2*1024*0o124*1024, resource.DecimalSI),
    80  	}, "c1")
    81  	addPodToCache(p1)
    82  
    83  	node, err := cache.GetNodeInfo("c1")
    84  	assert.Nil(t, err)
    85  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMilliCPU, int64(0))
    86  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMemory, int64(0))
    87  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMilliCPU, int64(2000))
    88  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMemory, int64(2*1024*0o124*1024))
    89  
    90  	t.Log("### 2: update pod")
    91  	p1 = makeCachedPod("p1", "p1", map[v1.ResourceName]resource.Quantity{
    92  		consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(3000, resource.DecimalSI),
    93  		consts.ReclaimedResourceMemory:   *resource.NewQuantity(3*1024*0o124*1024, resource.DecimalSI),
    94  	}, "c1")
    95  	updatePodInCache(p1, p1)
    96  
    97  	node, err = cache.GetNodeInfo("c1")
    98  	assert.Nil(t, err)
    99  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMilliCPU, int64(0))
   100  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMemory, int64(0))
   101  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMilliCPU, int64(3000))
   102  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMemory, int64(3*1024*0o124*1024))
   103  
   104  	t.Log("### 3: add new pod")
   105  	p2 := makeCachedPod("p2", "p2", map[v1.ResourceName]resource.Quantity{
   106  		consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(2000, resource.DecimalSI),
   107  		consts.ReclaimedResourceMemory:   *resource.NewQuantity(2*1024*0o124*1024, resource.DecimalSI),
   108  	}, "c1")
   109  	addPodToCache(p2)
   110  
   111  	node, err = cache.GetNodeInfo("c1")
   112  	assert.Nil(t, err)
   113  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMilliCPU, int64(0))
   114  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMemory, int64(0))
   115  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMilliCPU, int64(5000))
   116  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMemory, int64(5*1024*0o124*1024))
   117  
   118  	t.Log("### 4: add cnr")
   119  	c1 := makeCachedCNR("c1", map[v1.ResourceName]resource.Quantity{
   120  		consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(8000, resource.DecimalSI),
   121  		consts.ReclaimedResourceMemory:   *resource.NewQuantity(8*1024*0o124*1024, resource.DecimalSI),
   122  	})
   123  	addCNRToCache(c1)
   124  
   125  	node, err = cache.GetNodeInfo("c1")
   126  	assert.Nil(t, err)
   127  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMilliCPU, int64(8000))
   128  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMemory, int64(8*1024*0o124*1024))
   129  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMilliCPU, int64(5000))
   130  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMemory, int64(5*1024*0o124*1024))
   131  
   132  	t.Log("### 5: remove pod")
   133  	deletePodFromCache(p1)
   134  
   135  	node, err = cache.GetNodeInfo("c1")
   136  	assert.Nil(t, err)
   137  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMilliCPU, int64(8000))
   138  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMemory, int64(8*1024*0o124*1024))
   139  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMilliCPU, int64(2000))
   140  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMemory, int64(2*1024*0o124*1024))
   141  
   142  	t.Log("### 6: update cnr")
   143  	c1 = makeCachedCNR("c1", map[v1.ResourceName]resource.Quantity{
   144  		consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(9000, resource.DecimalSI),
   145  		consts.ReclaimedResourceMemory:   *resource.NewQuantity(9*1024*0o124*1024, resource.DecimalSI),
   146  	})
   147  	updateNodeInCache(c1, c1)
   148  
   149  	node, err = cache.GetNodeInfo("c1")
   150  	assert.Nil(t, err)
   151  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMilliCPU, int64(9000))
   152  	assert.Equal(t, node.QoSResourcesAllocatable.ReclaimedMemory, int64(9*1024*0o124*1024))
   153  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMilliCPU, int64(2000))
   154  	assert.Equal(t, node.QoSResourcesRequested.ReclaimedMemory, int64(2*1024*0o124*1024))
   155  
   156  	t.Log("### 7: delete cnr")
   157  	deleteCNRFromCache(c1)
   158  
   159  	_, err = cache.GetNodeInfo("c1")
   160  	assert.NotNil(t, err)
   161  }