...

Source file src/k8s.io/kubernetes/pkg/scheduler/framework/runtime/registry.go

Documentation: k8s.io/kubernetes/pkg/scheduler/framework/runtime

     1  /*
     2  Copyright 2019 The Kubernetes 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 runtime
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/util/json"
    25  	"k8s.io/kubernetes/pkg/scheduler/framework"
    26  	plfeature "k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature"
    27  	"sigs.k8s.io/yaml"
    28  )
    29  
    30  // PluginFactory is a function that builds a plugin.
    31  type PluginFactory = func(ctx context.Context, configuration runtime.Object, f framework.Handle) (framework.Plugin, error)
    32  
    33  // PluginFactoryWithFts is a function that builds a plugin with certain feature gates.
    34  type PluginFactoryWithFts func(context.Context, runtime.Object, framework.Handle, plfeature.Features) (framework.Plugin, error)
    35  
    36  // FactoryAdapter can be used to inject feature gates for a plugin that needs
    37  // them when the caller expects the older PluginFactory method.
    38  func FactoryAdapter(fts plfeature.Features, withFts PluginFactoryWithFts) PluginFactory {
    39  	return func(ctx context.Context, plArgs runtime.Object, fh framework.Handle) (framework.Plugin, error) {
    40  		return withFts(ctx, plArgs, fh, fts)
    41  	}
    42  }
    43  
    44  // DecodeInto decodes configuration whose type is *runtime.Unknown to the interface into.
    45  func DecodeInto(obj runtime.Object, into interface{}) error {
    46  	if obj == nil {
    47  		return nil
    48  	}
    49  	configuration, ok := obj.(*runtime.Unknown)
    50  	if !ok {
    51  		return fmt.Errorf("want args of type runtime.Unknown, got %T", obj)
    52  	}
    53  	if configuration.Raw == nil {
    54  		return nil
    55  	}
    56  
    57  	switch configuration.ContentType {
    58  	// If ContentType is empty, it means ContentTypeJSON by default.
    59  	case runtime.ContentTypeJSON, "":
    60  		return json.Unmarshal(configuration.Raw, into)
    61  	case runtime.ContentTypeYAML:
    62  		return yaml.Unmarshal(configuration.Raw, into)
    63  	default:
    64  		return fmt.Errorf("not supported content type %s", configuration.ContentType)
    65  	}
    66  }
    67  
    68  // Registry is a collection of all available plugins. The framework uses a
    69  // registry to enable and initialize configured plugins.
    70  // All plugins must be in the registry before initializing the framework.
    71  type Registry map[string]PluginFactory
    72  
    73  // Register adds a new plugin to the registry. If a plugin with the same name
    74  // exists, it returns an error.
    75  func (r Registry) Register(name string, factory PluginFactory) error {
    76  	if _, ok := r[name]; ok {
    77  		return fmt.Errorf("a plugin named %v already exists", name)
    78  	}
    79  	r[name] = factory
    80  	return nil
    81  }
    82  
    83  // Unregister removes an existing plugin from the registry. If no plugin with
    84  // the provided name exists, it returns an error.
    85  func (r Registry) Unregister(name string) error {
    86  	if _, ok := r[name]; !ok {
    87  		return fmt.Errorf("no plugin named %v exists", name)
    88  	}
    89  	delete(r, name)
    90  	return nil
    91  }
    92  
    93  // Merge merges the provided registry to the current one.
    94  func (r Registry) Merge(in Registry) error {
    95  	for name, factory := range in {
    96  		if err := r.Register(name, factory); err != nil {
    97  			return err
    98  		}
    99  	}
   100  	return nil
   101  }
   102  

View as plain text