/* Copyright 2018 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package core import ( "testing" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" "k8s.io/autoscaler/cluster-autoscaler/clusterstate/utils" "k8s.io/autoscaler/cluster-autoscaler/config" "k8s.io/autoscaler/cluster-autoscaler/context" "k8s.io/autoscaler/cluster-autoscaler/expander/random" "k8s.io/autoscaler/cluster-autoscaler/metrics" "k8s.io/autoscaler/cluster-autoscaler/simulator" "k8s.io/autoscaler/cluster-autoscaler/utils/errors" "k8s.io/autoscaler/cluster-autoscaler/utils/labels" "github.com/stretchr/testify/assert" apiv1 "k8s.io/api/core/v1" kube_client "k8s.io/client-go/kubernetes" kube_record "k8s.io/client-go/tools/record" schedulercache "k8s.io/kubernetes/pkg/scheduler/cache" ) type nodeConfig struct { name string cpu int64 memory int64 gpu int64 ready bool group string } type podConfig struct { name string cpu int64 memory int64 gpu int64 node string } type groupSizeChange struct { groupName string sizeChange int } type scaleTestConfig struct { nodes []nodeConfig pods []podConfig extraPods []podConfig expectedScaleUpOptions []groupSizeChange // we expect that all those options should be included in expansion options passed to expander strategy scaleUpOptionToChoose groupSizeChange // this will be selected by assertingStrategy.BestOption expectedFinalScaleUp groupSizeChange // we expect this to be delivered via scale-up event expectedScaleDowns []string options config.AutoscalingOptions } // NewScaleTestAutoscalingContext creates a new test autoscaling context for scaling tests. func NewScaleTestAutoscalingContext(options config.AutoscalingOptions, fakeClient kube_client.Interface, provider cloudprovider.CloudProvider) context.AutoscalingContext { fakeRecorder := kube_record.NewFakeRecorder(5) fakeLogRecorder, _ := utils.NewStatusMapRecorder(fakeClient, "kube-system", fakeRecorder, false) return context.AutoscalingContext{ AutoscalingOptions: options, AutoscalingKubeClients: context.AutoscalingKubeClients{ ClientSet: fakeClient, Recorder: fakeRecorder, LogRecorder: fakeLogRecorder, }, CloudProvider: provider, PredicateChecker: simulator.NewTestPredicateChecker(), ExpanderStrategy: random.NewStrategy(), } } type mockAutoprovisioningNodeGroupManager struct { t *testing.T } func (p *mockAutoprovisioningNodeGroupManager) CreateNodeGroup(context *context.AutoscalingContext, nodeGroup cloudprovider.NodeGroup) (cloudprovider.NodeGroup, errors.AutoscalerError) { newNodeGroup, err := nodeGroup.Create() assert.NoError(p.t, err) metrics.RegisterNodeGroupCreation() return newNodeGroup, nil } func (p *mockAutoprovisioningNodeGroupManager) RemoveUnneededNodeGroups(context *context.AutoscalingContext) error { if !context.AutoscalingOptions.NodeAutoprovisioningEnabled { return nil } nodeGroups := context.CloudProvider.NodeGroups() for _, nodeGroup := range nodeGroups { if !nodeGroup.Autoprovisioned() { continue } targetSize, err := nodeGroup.TargetSize() assert.NoError(p.t, err) if targetSize > 0 { continue } nodes, err := nodeGroup.Nodes() assert.NoError(p.t, err) if len(nodes) > 0 { continue } err = nodeGroup.Delete() assert.NoError(p.t, err) } return nil } func (p *mockAutoprovisioningNodeGroupManager) CleanUp() { } type mockAutoprovisioningNodeGroupListProcessor struct { t *testing.T } func (p *mockAutoprovisioningNodeGroupListProcessor) Process(context *context.AutoscalingContext, nodeGroups []cloudprovider.NodeGroup, nodeInfos map[string]*schedulercache.NodeInfo, unschedulablePods []*apiv1.Pod) ([]cloudprovider.NodeGroup, map[string]*schedulercache.NodeInfo, error) { machines, err := context.CloudProvider.GetAvailableMachineTypes() assert.NoError(p.t, err) bestLabels := labels.BestLabelSet(unschedulablePods) for _, machineType := range machines { nodeGroup, err := context.CloudProvider.NewNodeGroup(machineType, bestLabels, map[string]string{}, []apiv1.Taint{}, map[string]resource.Quantity{}) assert.NoError(p.t, err) nodeInfo, err := nodeGroup.TemplateNodeInfo() assert.NoError(p.t, err) nodeInfos[nodeGroup.Id()] = nodeInfo nodeGroups = append(nodeGroups, nodeGroup) } return nodeGroups, nodeInfos, nil } func (p *mockAutoprovisioningNodeGroupListProcessor) CleanUp() { }