1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-26 13:29:11 +01:00
Files
descheduler/pkg/framework/pluginregistry/pluginregistry.go
Jan Chaloupka 1974c12e0f Extend plugin's New with a context.Context
The new context.Context can be later used for passing a contextualized
logger. Or, other initialization steps that require the context.
2025-05-19 12:23:44 +02:00

74 lines
1.9 KiB
Go

/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pluginregistry
import (
"context"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
frameworktypes "sigs.k8s.io/descheduler/pkg/framework/types"
)
var PluginRegistry Registry
type PluginUtilities struct {
PluginBuilder PluginBuilder
PluginType interface{}
// Just an example instance of this PluginArg so we can avoid having
// to deal with reflect Types
PluginArgInstance runtime.Object
PluginArgValidator PluginArgValidator
PluginArgDefaulter PluginArgDefaulter
}
type PluginBuilder = func(ctx context.Context, args runtime.Object, handle frameworktypes.Handle) (frameworktypes.Plugin, error)
type (
PluginArgValidator = func(args runtime.Object) error
PluginArgDefaulter = func(args runtime.Object)
)
type Registry map[string]PluginUtilities
func NewRegistry() Registry {
return Registry{}
}
func Register(
name string,
builderFunc PluginBuilder,
pluginType interface{},
exampleArg runtime.Object,
pluginArgValidator PluginArgValidator,
pluginArgDefaulter PluginArgDefaulter,
registry Registry,
) {
if _, ok := registry[name]; ok {
klog.V(10).InfoS("Plugin already registered", "plugin", name)
} else {
registry[name] = PluginUtilities{
PluginBuilder: builderFunc,
PluginType: pluginType,
PluginArgInstance: exampleArg,
PluginArgValidator: pluginArgValidator,
PluginArgDefaulter: pluginArgDefaulter,
}
}
}