github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/vpa/algorithm/register.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 algorithm 18 19 import ( 20 "sync" 21 22 corev1 "k8s.io/api/core/v1" 23 24 apis "github.com/kubewharf/katalyst-api/pkg/apis/autoscaling/v1alpha1" 25 workload "github.com/kubewharf/katalyst-api/pkg/apis/workload/v1alpha1" 26 ) 27 28 // ResourceRecommender is used as a common interface for in-tree VPA algorithms; 29 // all in-tree implementations should implement those functions defined here. 30 type ResourceRecommender interface { 31 Name() string 32 33 // GetRecommendedPodResources calculate the recommended resources for given SPD 34 GetRecommendedPodResources(spd *workload.ServiceProfileDescriptor, pods []*corev1.Pod) ( 35 []apis.RecommendedPodResources, []apis.RecommendedContainerResources, error) 36 } 37 38 var recommenderMap sync.Map 39 40 // RegisterRecommender indicates that all in-tree algorithm implementations should be registered here, 41 // so that the main VPA process can walk through those algorithms to produce th final resources. 42 func RegisterRecommender(p ResourceRecommender) { 43 recommenderMap.Store(p.Name(), p) 44 } 45 46 // GetRecommender returns those recommenders that have been registered 47 func GetRecommender() map[string]ResourceRecommender { 48 result := make(map[string]ResourceRecommender) 49 recommenderMap.Range(func(key, value interface{}) bool { 50 result[key.(string)] = value.(ResourceRecommender) 51 return true 52 }) 53 return result 54 }