github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-agent/app/agent/qrm/io_plugin.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 qrm 18 19 import ( 20 "fmt" 21 "strings" 22 "sync" 23 24 "github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/agent" 25 phconsts "github.com/kubewharf/katalyst-core/pkg/agent/utilcomponent/periodicalhandler/consts" 26 "github.com/kubewharf/katalyst-core/pkg/config" 27 ) 28 29 const ( 30 QRMPluginNameIO = "qrm_io_plugin" 31 ) 32 33 var QRMIOPluginPeriodicalHandlerGroupName = strings.Join([]string{ 34 QRMPluginNameIO, 35 phconsts.PeriodicalHandlersGroupNameSuffix, 36 }, phconsts.GroupNameSeparator) 37 38 // ioPolicyInitializers is used to store the initializing function for io resource plugin policies 39 var ioPolicyInitializers sync.Map 40 41 // RegisterIOPolicyInitializer is used to register user-defined resource plugin init functions 42 func RegisterIOPolicyInitializer(name string, initFunc agent.InitFunc) { 43 ioPolicyInitializers.Store(name, initFunc) 44 } 45 46 // getIOPolicyInitializers returns those policies with initialized functions 47 func getIOPolicyInitializers() map[string]agent.InitFunc { 48 agents := make(map[string]agent.InitFunc) 49 ioPolicyInitializers.Range(func(key, value interface{}) bool { 50 agents[key.(string)] = value.(agent.InitFunc) 51 return true 52 }) 53 return agents 54 } 55 56 // InitQRMIOPlugins initializes the io QRM plugins 57 func InitQRMIOPlugins(agentCtx *agent.GenericContext, conf *config.Configuration, extraConf interface{}, agentName string) (bool, agent.Component, error) { 58 initializers := getIOPolicyInitializers() 59 policyName := conf.IOQRMPluginConfig.PolicyName 60 61 initFunc, ok := initializers[policyName] 62 if !ok { 63 return false, agent.ComponentStub{}, fmt.Errorf("invalid policy name %v for io resource plugin", policyName) 64 } 65 66 return initFunc(agentCtx, conf, extraConf, agentName) 67 }