github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/kcc/cnc_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 corev1 "k8s.io/api/core/v1" 26 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/client-go/tools/cache" 29 30 "github.com/kubewharf/katalyst-api/pkg/apis/config/v1alpha1" 31 katalyst_base "github.com/kubewharf/katalyst-core/cmd/base" 32 "github.com/kubewharf/katalyst-core/pkg/config" 33 "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic/crd" 34 kcctarget "github.com/kubewharf/katalyst-core/pkg/controller/kcc/target" 35 "github.com/kubewharf/katalyst-core/pkg/metrics" 36 ) 37 38 func TestCustomNodeConfigController_Run(t *testing.T) { 39 t.Parallel() 40 41 type args struct { 42 cncAndKCCList []runtime.Object 43 kccTargetList []runtime.Object 44 updateFunc func(ctx context.Context, genericContext *katalyst_base.GenericContext, cncAndKCCList, kccTargetList []runtime.Object) 45 kccConfig *config.Configuration 46 } 47 tests := []struct { 48 name string 49 args args 50 }{ 51 { 52 name: "kcc target all valid", 53 args: args{ 54 cncAndKCCList: []runtime.Object{ 55 &v1alpha1.KatalystCustomConfig{ 56 ObjectMeta: v1.ObjectMeta{ 57 Name: "test-kcc", 58 Namespace: "default", 59 }, 60 Spec: v1alpha1.KatalystCustomConfigSpec{ 61 TargetType: crd.AdminQoSConfigurationGVR, 62 NodeLabelSelectorAllowedKeyList: []v1alpha1.PriorityNodeLabelSelectorAllowedKeyList{ 63 { 64 Priority: 0, 65 KeyList: []string{"aa"}, 66 }, 67 }, 68 }, 69 }, 70 &v1alpha1.CustomNodeConfig{ 71 ObjectMeta: v1.ObjectMeta{ 72 Name: "node-1", 73 }, 74 }, 75 }, 76 kccTargetList: []runtime.Object{ 77 &v1alpha1.AdminQoSConfiguration{ 78 TypeMeta: v1.TypeMeta{ 79 Kind: crd.ResourceKindAdminQoSConfiguration, 80 APIVersion: v1alpha1.SchemeGroupVersion.String(), 81 }, 82 ObjectMeta: v1.ObjectMeta{ 83 Name: "default", 84 Namespace: "default", 85 }, 86 Spec: v1alpha1.AdminQoSConfigurationSpec{ 87 Config: v1alpha1.AdminQoSConfig{ 88 EvictionConfig: &v1alpha1.EvictionConfig{ 89 ReclaimedResourcesEvictionConfig: &v1alpha1.ReclaimedResourcesEvictionConfig{ 90 EvictionThreshold: map[corev1.ResourceName]float64{ 91 corev1.ResourceCPU: 5.0, 92 }, 93 }, 94 }, 95 }, 96 }, 97 }, 98 }, 99 updateFunc: func(ctx context.Context, genericContext *katalyst_base.GenericContext, cncAndKCCList, kccTargetList []runtime.Object) { 100 for _, obj := range cncAndKCCList { 101 if cnc, ok := obj.(*v1alpha1.CustomNodeConfig); ok { 102 nodeConfig, err := genericContext.Client.InternalClient.ConfigV1alpha1().CustomNodeConfigs().Get(ctx, cnc.Name, v1.GetOptions{ResourceVersion: "0"}) 103 assert.NoError(t, err) 104 nodeConfig.Labels = map[string]string{ 105 "aa": "bb", 106 } 107 108 _, err = genericContext.Client.InternalClient.ConfigV1alpha1().CustomNodeConfigs().Update(ctx, nodeConfig, v1.UpdateOptions{}) 109 assert.NoError(t, err) 110 } 111 } 112 }, 113 }, 114 }, 115 } 116 for _, tt := range tests { 117 tt := tt 118 t.Run(tt.name, func(t *testing.T) { 119 t.Parallel() 120 121 genericContext, err := katalyst_base.GenerateFakeGenericContext(nil, tt.args.cncAndKCCList, tt.args.kccTargetList) 122 assert.NoError(t, err) 123 conf := generateTestConfiguration(t) 124 125 ctx := context.Background() 126 targetHandler := kcctarget.NewKatalystCustomConfigTargetHandler( 127 ctx, 128 genericContext.Client, 129 conf.ControllersConfiguration.KCCConfig, 130 genericContext.InternalInformerFactory.Config().V1alpha1().KatalystCustomConfigs(), 131 ) 132 133 cnc, err := NewCustomNodeConfigController( 134 ctx, 135 conf.GenericConfiguration, 136 conf.GenericControllerConfiguration, 137 conf.KCCConfig, 138 genericContext.Client, 139 genericContext.InternalInformerFactory.Config().V1alpha1().CustomNodeConfigs(), 140 metrics.DummyMetrics{}, 141 targetHandler, 142 ) 143 assert.NoError(t, err) 144 145 genericContext.StartInformer(ctx) 146 go targetHandler.Run() 147 go cnc.Run() 148 149 cache.WaitForCacheSync(cnc.ctx.Done(), cnc.syncedFunc...) 150 time.Sleep(1 * time.Second) 151 152 tt.args.updateFunc(ctx, genericContext, tt.args.cncAndKCCList, tt.args.kccTargetList) 153 time.Sleep(1 * time.Second) 154 }) 155 } 156 }