github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/vpa/recommend_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 vpa
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	appsv1 "k8s.io/api/apps/v1"
    26  	v1 "k8s.io/api/core/v1"
    27  	"k8s.io/apimachinery/pkg/api/resource"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  	"k8s.io/client-go/tools/cache"
    31  	cliflag "k8s.io/component-base/cli/flag"
    32  	"k8s.io/utils/pointer"
    33  
    34  	apis "github.com/kubewharf/katalyst-api/pkg/apis/autoscaling/v1alpha1"
    35  	apiworkload "github.com/kubewharf/katalyst-api/pkg/apis/workload/v1alpha1"
    36  	apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
    37  	katalystbase "github.com/kubewharf/katalyst-core/cmd/base"
    38  	"github.com/kubewharf/katalyst-core/cmd/katalyst-controller/app/options"
    39  	"github.com/kubewharf/katalyst-core/pkg/config/controller"
    40  	"github.com/kubewharf/katalyst-core/pkg/config/generic"
    41  	"github.com/kubewharf/katalyst-core/pkg/controller/vpa/util"
    42  )
    43  
    44  func TestResourceRecommendController_Run(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	type fields struct {
    48  		workload *appsv1.StatefulSet
    49  		spd      *apiworkload.ServiceProfileDescriptor
    50  		vpa      *apis.KatalystVerticalPodAutoscaler
    51  		vparec   *apis.VerticalPodAutoscalerRecommendation
    52  	}
    53  	tests := []struct {
    54  		name   string
    55  		fields fields
    56  	}{
    57  		{
    58  			name: "",
    59  			fields: fields{
    60  				vpa: &apis.KatalystVerticalPodAutoscaler{
    61  					ObjectMeta: metav1.ObjectMeta{
    62  						Name:      "vpa1",
    63  						Namespace: "default",
    64  						UID:       "vpauid1",
    65  						Annotations: map[string]string{
    66  							apiconsts.VPAAnnotationWorkloadRetentionPolicyKey: apiconsts.VPAAnnotationWorkloadRetentionPolicyDelete,
    67  						},
    68  					},
    69  					Spec: apis.KatalystVerticalPodAutoscalerSpec{
    70  						TargetRef: apis.CrossVersionObjectReference{
    71  							Kind:       "StatefulSet",
    72  							APIVersion: "apps/v1",
    73  							Name:       "sts1",
    74  						},
    75  						UpdatePolicy: apis.PodUpdatePolicy{
    76  							PodUpdatingStrategy: apis.PodUpdatingStrategyInplace,
    77  							PodMatchingStrategy: apis.PodMatchingStrategyAll,
    78  						},
    79  					},
    80  					Status: apis.KatalystVerticalPodAutoscalerStatus{
    81  						ContainerResources: []apis.ContainerResources{
    82  							{
    83  								ContainerName: pointer.String("c1"),
    84  								Requests: &apis.ContainerResourceList{
    85  									Target: map[v1.ResourceName]resource.Quantity{
    86  										v1.ResourceCPU:    resource.MustParse("1"),
    87  										v1.ResourceMemory: resource.MustParse("1Gi"),
    88  									},
    89  									UncappedTarget: map[v1.ResourceName]resource.Quantity{
    90  										v1.ResourceCPU:    resource.MustParse("1"),
    91  										v1.ResourceMemory: resource.MustParse("1Gi"),
    92  									},
    93  								},
    94  							},
    95  						},
    96  						Conditions: []apis.VerticalPodAutoscalerCondition{
    97  							{
    98  								Type:   apis.RecommendationUpdated,
    99  								Status: v1.ConditionTrue,
   100  								Reason: util.VPAConditionReasonUpdated,
   101  							},
   102  						},
   103  					},
   104  				},
   105  			},
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		tt := tt
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			t.Parallel()
   112  
   113  			ctx := context.TODO()
   114  			genericConf := &generic.GenericConfiguration{}
   115  			controllerConf := &controller.GenericControllerConfiguration{
   116  				DynamicGVResources: []string{"statefulsets.v1.apps"},
   117  			}
   118  
   119  			fss := &cliflag.NamedFlagSets{}
   120  			vpaOptions := options.NewVPAOptions()
   121  			vpaOptions.AddFlags(fss)
   122  			vpaConf := controller.NewVPAConfig()
   123  			_ = vpaOptions.ApplyTo(vpaConf)
   124  
   125  			controlCtx, err := katalystbase.GenerateFakeGenericContext(nil,
   126  				[]runtime.Object{tt.fields.spd, tt.fields.vpa, tt.fields.vparec}, []runtime.Object{tt.fields.workload})
   127  			assert.NoError(t, err)
   128  
   129  			rrc, err := NewResourceRecommendController(ctx, controlCtx, genericConf, controllerConf, vpaConf)
   130  			assert.NoError(t, err)
   131  
   132  			controlCtx.StartInformer(ctx)
   133  			go rrc.Run()
   134  			synced := cache.WaitForCacheSync(ctx.Done(), rrc.syncedFunc...)
   135  			assert.True(t, synced)
   136  			time.Sleep(10 * time.Millisecond)
   137  		})
   138  	}
   139  }