github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-agent/app/agent/qrm/network_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  	"sync"
    22  
    23  	"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/agent"
    24  	"github.com/kubewharf/katalyst-core/pkg/config"
    25  )
    26  
    27  const (
    28  	QRMPluginNameNetwork = "qrm_network_plugin"
    29  )
    30  
    31  // networkPolicyInitializers is used to store the initializing function for network resource plugin policies
    32  var networkPolicyInitializers sync.Map
    33  
    34  // RegisterNetworkPolicyInitializer is used to register user-defined resource plugin init functions
    35  func RegisterNetworkPolicyInitializer(name string, initFunc agent.InitFunc) {
    36  	networkPolicyInitializers.Store(name, initFunc)
    37  }
    38  
    39  // getNetworkPolicyInitializers returns those policies with initialized functions
    40  func getNetworkPolicyInitializers() map[string]agent.InitFunc {
    41  	agents := make(map[string]agent.InitFunc)
    42  	networkPolicyInitializers.Range(func(key, value interface{}) bool {
    43  		agents[key.(string)] = value.(agent.InitFunc)
    44  		return true
    45  	})
    46  	return agents
    47  }
    48  
    49  // InitQRMNetworkPlugins initializes the network QRM plugins
    50  func InitQRMNetworkPlugins(agentCtx *agent.GenericContext, conf *config.Configuration, extraConf interface{}, agentName string) (bool, agent.Component, error) {
    51  	initializers := getNetworkPolicyInitializers()
    52  	policyName := conf.NetworkQRMPluginConfig.PolicyName
    53  
    54  	initFunc, ok := initializers[policyName]
    55  	if !ok {
    56  		return false, agent.ComponentStub{}, fmt.Errorf("invalid policy name %v for network resource plugin", policyName)
    57  	}
    58  
    59  	return initFunc(agentCtx, conf, extraConf, agentName)
    60  }