github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-agent/app/agent/qrm/cpu_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  	QRMPluginNameCPU = "qrm_cpu_plugin"
    31  )
    32  
    33  var QRMCPUPluginPeriodicalHandlerGroupName = strings.Join([]string{
    34  	QRMPluginNameCPU,
    35  	phconsts.PeriodicalHandlersGroupNameSuffix,
    36  }, phconsts.GroupNameSeparator)
    37  
    38  // cpuPolicyInitializers is used to store the initializing function for cpu resource-plugin policies
    39  var cpuPolicyInitializers sync.Map
    40  
    41  // RegisterCPUPolicyInitializer is used to register user-defined cpu resource plugin init functions
    42  func RegisterCPUPolicyInitializer(name string, initFunc agent.InitFunc) {
    43  	cpuPolicyInitializers.Store(name, initFunc)
    44  }
    45  
    46  // GetCPUPolicyInitializers returns those policies with initialized functions
    47  func getCPUPolicyInitializers() map[string]agent.InitFunc {
    48  	agents := make(map[string]agent.InitFunc)
    49  	cpuPolicyInitializers.Range(func(key, value interface{}) bool {
    50  		agents[key.(string)] = value.(agent.InitFunc)
    51  		return true
    52  	})
    53  	return agents
    54  }
    55  
    56  func InitQRMCPUPlugins(agentCtx *agent.GenericContext, conf *config.Configuration, extraConf interface{}, agentName string) (bool, agent.Component, error) {
    57  	initializers := getCPUPolicyInitializers()
    58  	policyName := conf.CPUQRMPluginConfig.PolicyName
    59  
    60  	initFunc, ok := initializers[policyName]
    61  	if !ok {
    62  		return false, agent.ComponentStub{}, fmt.Errorf("invalid policy name %v for cpu resource plugin", policyName)
    63  	}
    64  
    65  	return initFunc(agentCtx, conf, extraConf, agentName)
    66  }