github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/sysadvisor/plugin/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 plugin is the package that defines the SysAdvisor plugin, and 18 // those strategies must implement SysAdvisorPlugin interface. 19 20 package plugin 21 22 import ( 23 "context" 24 "sync" 25 26 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache" 27 "github.com/kubewharf/katalyst-core/pkg/config" 28 "github.com/kubewharf/katalyst-core/pkg/metaserver" 29 metricspool "github.com/kubewharf/katalyst-core/pkg/metrics/metrics-pool" 30 ) 31 32 // SysAdvisorPlugin performs resource control computation based on agent resources. 33 type SysAdvisorPlugin interface { 34 Name() string 35 Init() error 36 Run(ctx context.Context) 37 } 38 39 var _ SysAdvisorPlugin = DummySysAdvisorPlugin{} 40 41 type DummySysAdvisorPlugin struct{} 42 43 func (d DummySysAdvisorPlugin) Name() string { return "dummy-sysadvisor-plugin" } 44 func (d DummySysAdvisorPlugin) Init() error { return nil } 45 func (d DummySysAdvisorPlugin) Run(_ context.Context) {} 46 47 // AdvisorPluginInitFunc is used to initialize a particular inter SysAdvisor plugin. 48 type AdvisorPluginInitFunc func(pluginName string, conf *config.Configuration, extraConf interface{}, 49 emitterPool metricspool.MetricsEmitterPool, metaServer *metaserver.MetaServer, 50 metaCache metacache.MetaCache) (SysAdvisorPlugin, error) 51 52 var advisorPluginInitializers sync.Map 53 54 func RegisterAdvisorPlugin(plugin string, f AdvisorPluginInitFunc) { 55 advisorPluginInitializers.Store(plugin, f) 56 } 57 58 func GetRegisteredAdvisorPlugins() map[string]AdvisorPluginInitFunc { 59 plugins := make(map[string]AdvisorPluginInitFunc) 60 advisorPluginInitializers.Range(func(key, value interface{}) bool { 61 plugins[key.(string)] = value.(AdvisorPluginInitFunc) 62 return true 63 }) 64 return plugins 65 }