github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/kcc/kcc_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 kcc
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  	corev1 "k8s.io/api/core/v1"
    27  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/client-go/tools/cache"
    30  
    31  	"github.com/kubewharf/katalyst-api/pkg/apis/config/v1alpha1"
    32  	katalyst_base "github.com/kubewharf/katalyst-core/cmd/base"
    33  	"github.com/kubewharf/katalyst-core/cmd/katalyst-controller/app/options"
    34  	"github.com/kubewharf/katalyst-core/pkg/config"
    35  	"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic/crd"
    36  	"github.com/kubewharf/katalyst-core/pkg/consts"
    37  	kcctarget "github.com/kubewharf/katalyst-core/pkg/controller/kcc/target"
    38  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    39  )
    40  
    41  func generateTestConfiguration(t *testing.T) *config.Configuration {
    42  	testConfiguration, err := options.NewOptions().Config()
    43  	require.NoError(t, err)
    44  	require.NotNil(t, testConfiguration)
    45  	return testConfiguration
    46  }
    47  
    48  func generateTestDeletionTimestamp() *v1.Time {
    49  	now := v1.Now()
    50  	return &now
    51  }
    52  
    53  func TestKatalystCustomConfigController_Run(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	type args struct {
    57  		kccList       []runtime.Object
    58  		kccTargetList []runtime.Object
    59  		kccConfig     *config.Configuration
    60  	}
    61  	tests := []struct {
    62  		name string
    63  		args args
    64  	}{
    65  		{
    66  			name: "kcc target not found",
    67  			args: args{
    68  				kccList: []runtime.Object{
    69  					&v1alpha1.KatalystCustomConfig{
    70  						ObjectMeta: v1.ObjectMeta{
    71  							Name:      "test-kcc",
    72  							Namespace: "default",
    73  						},
    74  						Spec: v1alpha1.KatalystCustomConfigSpec{
    75  							TargetType: crd.AdminQoSConfigurationGVR,
    76  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
    77  								{
    78  									Priority: 0,
    79  									KeyList:  []string{"aa"},
    80  								},
    81  							},
    82  						},
    83  					},
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name: "kcc and kcc target are all valid",
    89  			args: args{
    90  				kccList: []runtime.Object{
    91  					&v1alpha1.KatalystCustomConfig{
    92  						ObjectMeta: v1.ObjectMeta{
    93  							Name:      "test-kcc",
    94  							Namespace: "default",
    95  						},
    96  						Spec: v1alpha1.KatalystCustomConfigSpec{
    97  							TargetType: crd.AdminQoSConfigurationGVR,
    98  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
    99  								{
   100  									Priority: 0,
   101  									KeyList:  []string{"aa"},
   102  								},
   103  							},
   104  						},
   105  					},
   106  				},
   107  				kccTargetList: []runtime.Object{
   108  					&v1alpha1.AdminQoSConfiguration{
   109  						TypeMeta: v1.TypeMeta{
   110  							Kind:       crd.ResourceKindAdminQoSConfiguration,
   111  							APIVersion: v1alpha1.SchemeGroupVersion.String(),
   112  						},
   113  						ObjectMeta: v1.ObjectMeta{
   114  							Name:      "default",
   115  							Namespace: "default",
   116  						},
   117  						Spec: v1alpha1.AdminQoSConfigurationSpec{
   118  							Config: v1alpha1.AdminQoSConfig{
   119  								EvictionConfig: &v1alpha1.EvictionConfig{
   120  									ReclaimedResourcesEvictionConfig: &v1alpha1.ReclaimedResourcesEvictionConfig{
   121  										EvictionThreshold: map[corev1.ResourceName]float64{
   122  											corev1.ResourceCPU: 5.0,
   123  										},
   124  									},
   125  								},
   126  							},
   127  						},
   128  					},
   129  				},
   130  			},
   131  		},
   132  		{
   133  			name: "more than one kcc with same gvr",
   134  			args: args{
   135  				kccList: []runtime.Object{
   136  					&v1alpha1.KatalystCustomConfig{
   137  						ObjectMeta: v1.ObjectMeta{
   138  							Name:      "test-kcc",
   139  							Namespace: "default",
   140  						},
   141  						Spec: v1alpha1.KatalystCustomConfigSpec{
   142  							TargetType: crd.AdminQoSConfigurationGVR,
   143  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   144  								{
   145  									Priority: 0,
   146  									KeyList:  []string{"aa"},
   147  								},
   148  							},
   149  						},
   150  					},
   151  					&v1alpha1.KatalystCustomConfig{
   152  						ObjectMeta: v1.ObjectMeta{
   153  							Name:      "test-kcc-1",
   154  							Namespace: "default",
   155  						},
   156  						Spec: v1alpha1.KatalystCustomConfigSpec{
   157  							TargetType: crd.AdminQoSConfigurationGVR,
   158  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   159  								{
   160  									Priority: 0,
   161  									KeyList:  []string{"aa"},
   162  								},
   163  							},
   164  						},
   165  					},
   166  				},
   167  				kccTargetList: []runtime.Object{
   168  					&v1alpha1.AdminQoSConfiguration{
   169  						TypeMeta: v1.TypeMeta{
   170  							Kind:       crd.ResourceKindAdminQoSConfiguration,
   171  							APIVersion: v1alpha1.SchemeGroupVersion.String(),
   172  						},
   173  						ObjectMeta: v1.ObjectMeta{
   174  							Name:      "default",
   175  							Namespace: "default",
   176  						},
   177  						Spec: v1alpha1.AdminQoSConfigurationSpec{
   178  							Config: v1alpha1.AdminQoSConfig{
   179  								EvictionConfig: &v1alpha1.EvictionConfig{
   180  									ReclaimedResourcesEvictionConfig: &v1alpha1.ReclaimedResourcesEvictionConfig{
   181  										EvictionThreshold: map[corev1.ResourceName]float64{
   182  											corev1.ResourceCPU: 5.0,
   183  										},
   184  									},
   185  								},
   186  							},
   187  						},
   188  					},
   189  				},
   190  			},
   191  		},
   192  		{
   193  			name: "handle finalizer terminating",
   194  			args: args{
   195  				kccList: []runtime.Object{
   196  					&v1alpha1.KatalystCustomConfig{
   197  						ObjectMeta: v1.ObjectMeta{
   198  							Name:              "test-kcc",
   199  							Namespace:         "default",
   200  							DeletionTimestamp: generateTestDeletionTimestamp(),
   201  							Finalizers: []string{
   202  								consts.KatalystCustomConfigFinalizerKCC,
   203  							},
   204  						},
   205  						Spec: v1alpha1.KatalystCustomConfigSpec{
   206  							TargetType: crd.AdminQoSConfigurationGVR,
   207  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   208  								{
   209  									Priority: 0,
   210  									KeyList:  []string{"aa"},
   211  								},
   212  							},
   213  						},
   214  					},
   215  					&v1alpha1.KatalystCustomConfig{
   216  						ObjectMeta: v1.ObjectMeta{
   217  							Name:      "test-kcc-1",
   218  							Namespace: "default",
   219  						},
   220  						Spec: v1alpha1.KatalystCustomConfigSpec{
   221  							TargetType: crd.AdminQoSConfigurationGVR,
   222  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   223  								{
   224  									Priority: 0,
   225  									KeyList:  []string{"aa"},
   226  								},
   227  							},
   228  						},
   229  					},
   230  				},
   231  				kccTargetList: []runtime.Object{
   232  					&v1alpha1.AdminQoSConfiguration{
   233  						TypeMeta: v1.TypeMeta{
   234  							Kind:       crd.ResourceKindAdminQoSConfiguration,
   235  							APIVersion: v1alpha1.SchemeGroupVersion.String(),
   236  						},
   237  						ObjectMeta: v1.ObjectMeta{
   238  							Name:      "default",
   239  							Namespace: "default",
   240  						},
   241  						Spec: v1alpha1.AdminQoSConfigurationSpec{
   242  							Config: v1alpha1.AdminQoSConfig{
   243  								EvictionConfig: &v1alpha1.EvictionConfig{
   244  									ReclaimedResourcesEvictionConfig: &v1alpha1.ReclaimedResourcesEvictionConfig{
   245  										EvictionThreshold: map[corev1.ResourceName]float64{
   246  											corev1.ResourceCPU: 5.0,
   247  										},
   248  									},
   249  								},
   250  							},
   251  						},
   252  					},
   253  				},
   254  			},
   255  		},
   256  		{
   257  			name: "handle finalizer normal",
   258  			args: args{
   259  				kccList: []runtime.Object{
   260  					&v1alpha1.KatalystCustomConfig{
   261  						ObjectMeta: v1.ObjectMeta{
   262  							Name:              "test-kcc",
   263  							Namespace:         "default",
   264  							DeletionTimestamp: generateTestDeletionTimestamp(),
   265  							Finalizers: []string{
   266  								consts.KatalystCustomConfigFinalizerKCC,
   267  							},
   268  						},
   269  						Spec: v1alpha1.KatalystCustomConfigSpec{
   270  							TargetType: crd.AdminQoSConfigurationGVR,
   271  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   272  								{
   273  									Priority: 0,
   274  									KeyList:  []string{"aa"},
   275  								},
   276  							},
   277  						},
   278  					},
   279  					&v1alpha1.KatalystCustomConfig{
   280  						ObjectMeta: v1.ObjectMeta{
   281  							Name:      "test-kcc-1",
   282  							Namespace: "default",
   283  						},
   284  						Spec: v1alpha1.KatalystCustomConfigSpec{
   285  							TargetType: crd.AdminQoSConfigurationGVR,
   286  							NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   287  								{
   288  									Priority: 0,
   289  									KeyList:  []string{"aa"},
   290  								},
   291  							},
   292  						},
   293  					},
   294  				},
   295  				kccTargetList: []runtime.Object{},
   296  			},
   297  		},
   298  	}
   299  	for _, tt := range tests {
   300  		tt := tt
   301  		t.Run(tt.name, func(t *testing.T) {
   302  			t.Parallel()
   303  
   304  			genericContext, err := katalyst_base.GenerateFakeGenericContext(nil, tt.args.kccList, tt.args.kccTargetList)
   305  			assert.NoError(t, err)
   306  			conf := generateTestConfiguration(t)
   307  
   308  			ctx := context.Background()
   309  			targetHandler := kcctarget.NewKatalystCustomConfigTargetHandler(
   310  				ctx,
   311  				genericContext.Client,
   312  				conf.ControllersConfiguration.KCCConfig,
   313  				genericContext.InternalInformerFactory.Config().V1alpha1().KatalystCustomConfigs(),
   314  			)
   315  
   316  			kcc, err := NewKatalystCustomConfigController(
   317  				ctx,
   318  				conf.GenericConfiguration,
   319  				conf.GenericControllerConfiguration,
   320  				conf.KCCConfig,
   321  				genericContext.Client,
   322  				genericContext.InternalInformerFactory.Config().V1alpha1().KatalystCustomConfigs(),
   323  				metrics.DummyMetrics{},
   324  				targetHandler,
   325  			)
   326  			assert.NoError(t, err)
   327  
   328  			genericContext.StartInformer(ctx)
   329  			go targetHandler.Run()
   330  			go kcc.Run()
   331  
   332  			cache.WaitForCacheSync(kcc.ctx.Done(), kcc.syncedFunc...)
   333  			time.Sleep(100 * time.Millisecond)
   334  		})
   335  	}
   336  }
   337  
   338  func Test_checkNodeLabelSelectorAllowedKeyList(t *testing.T) {
   339  	t.Parallel()
   340  
   341  	type args struct {
   342  		kcc *v1alpha1.KatalystCustomConfig
   343  	}
   344  	tests := []struct {
   345  		name  string
   346  		args  args
   347  		want  string
   348  		want1 bool
   349  	}{
   350  		{
   351  			name: "test-1",
   352  			args: args{
   353  				kcc: &v1alpha1.KatalystCustomConfig{
   354  					Spec: v1alpha1.KatalystCustomConfigSpec{
   355  						NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   356  							{
   357  								Priority: 0,
   358  								KeyList:  []string{"aa"},
   359  							},
   360  							{
   361  								Priority: 1,
   362  								KeyList:  []string{"cc"},
   363  							},
   364  						},
   365  					},
   366  				},
   367  			},
   368  			want:  "",
   369  			want1: true,
   370  		},
   371  		{
   372  			name: "test-2",
   373  			args: args{
   374  				kcc: &v1alpha1.KatalystCustomConfig{
   375  					Spec: v1alpha1.KatalystCustomConfigSpec{
   376  						NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{
   377  							{
   378  								Priority: 0,
   379  								KeyList:  []string{"aa"},
   380  							},
   381  							{
   382  								Priority: 0,
   383  								KeyList:  []string{"cc"},
   384  							},
   385  						},
   386  					},
   387  				},
   388  			},
   389  			want:  "duplicated priority: [0]",
   390  			want1: false,
   391  		},
   392  	}
   393  	for _, tt := range tests {
   394  		tt := tt
   395  		t.Run(tt.name, func(t *testing.T) {
   396  			t.Parallel()
   397  
   398  			got, got1 := checkNodeLabelSelectorAllowedKeyList(tt.args.kcc)
   399  			assert.Equalf(t, tt.want, got, "checkNodeLabelSelectorAllowedKeyList(%v)", tt.args.kcc)
   400  			assert.Equalf(t, tt.want1, got1, "checkNodeLabelSelectorAllowedKeyList(%v)", tt.args.kcc)
   401  		})
   402  	}
   403  }