github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/spd/indicator-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 indicator_plugin
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  
    25  	apiworkload "github.com/kubewharf/katalyst-api/pkg/apis/workload/v1alpha1"
    26  	katalystbase "github.com/kubewharf/katalyst-core/cmd/base"
    27  	"github.com/kubewharf/katalyst-core/pkg/config/controller"
    28  	"github.com/kubewharf/katalyst-core/pkg/util/native"
    29  )
    30  
    31  // IndicatorPlugin represent an implementation for indicator sources;
    32  // each plugin will implement its own indicator-obtain logic and use
    33  // IndicatorUpdater to set the latest indicator info.
    34  type IndicatorPlugin interface {
    35  	// Run starts the main loop for indicator plugin
    36  	Run()
    37  	Name() string
    38  
    39  	// GetSupportedBusinessIndicatorSpec + GetSupportedSystemIndicatorSpec + GetSupportedBusinessIndicatorStatus
    40  	// Those methods below returns the supported indicator names, if any name
    41  	// is not supported by any indicator plugin, the controller will clear in CR.
    42  	GetSupportedBusinessIndicatorSpec() []apiworkload.ServiceBusinessIndicatorName
    43  	GetSupportedSystemIndicatorSpec() []apiworkload.ServiceSystemIndicatorName
    44  	GetSupportedExtendedIndicatorSpec() []string
    45  	GetSupportedBusinessIndicatorStatus() []apiworkload.ServiceBusinessIndicatorName
    46  }
    47  
    48  type DummyIndicatorPlugin struct {
    49  	SystemSpecNames     []apiworkload.ServiceSystemIndicatorName
    50  	BusinessSpecNames   []apiworkload.ServiceBusinessIndicatorName
    51  	BusinessStatusNames []apiworkload.ServiceBusinessIndicatorName
    52  	ExtendedSpecNames   []string
    53  }
    54  
    55  var _ IndicatorPlugin = DummyIndicatorPlugin{}
    56  
    57  func (d DummyIndicatorPlugin) Run()         {}
    58  func (d DummyIndicatorPlugin) Name() string { return "dummy-indicator-plugin" }
    59  func (d DummyIndicatorPlugin) GetSupportedBusinessIndicatorSpec() []apiworkload.ServiceBusinessIndicatorName {
    60  	return d.BusinessSpecNames
    61  }
    62  
    63  func (d DummyIndicatorPlugin) GetSupportedSystemIndicatorSpec() []apiworkload.ServiceSystemIndicatorName {
    64  	return d.SystemSpecNames
    65  }
    66  
    67  func (d DummyIndicatorPlugin) GetSupportedExtendedIndicatorSpec() []string {
    68  	return d.ExtendedSpecNames
    69  }
    70  
    71  func (d DummyIndicatorPlugin) GetSupportedBusinessIndicatorStatus() []apiworkload.ServiceBusinessIndicatorName {
    72  	return d.BusinessStatusNames
    73  }
    74  
    75  // pluginInitializers is used to store the initializing function for each plugin
    76  var pluginInitializers sync.Map
    77  
    78  type InitFunc func(ctx context.Context, conf *controller.SPDConfig, extraConf interface{},
    79  	spdWorkloadInformer map[schema.GroupVersionResource]native.DynamicInformer,
    80  	controlCtx *katalystbase.GenericContext, updater IndicatorUpdater) (IndicatorPlugin, error)
    81  
    82  // RegisterPluginInitializer is used to register user-defined indicator plugins
    83  func RegisterPluginInitializer(name string, initFunc InitFunc) {
    84  	pluginInitializers.Store(name, initFunc)
    85  }
    86  
    87  // GetPluginInitializers returns initialized functions of indicator plugins
    88  func GetPluginInitializers() map[string]InitFunc {
    89  	plugins := make(map[string]InitFunc)
    90  	pluginInitializers.Range(func(key, value interface{}) bool {
    91  		plugins[key.(string)] = value.(InitFunc)
    92  		return true
    93  	})
    94  	return plugins
    95  }