mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-27 03:44:04 +00:00
This is needed to allow efficient preemption simulations: during preemption, we remove/add pods from each node before running the filter plugins again to evaluate whether removing/adding specific pods will allow the incoming pod to be scheduled on the node. Instead of calling prefilter again, we should allow the plugin to do incremental update to its pre-computed state.
113 lines
3.2 KiB
Go
113 lines
3.2 KiB
Go
/*
|
|
Copyright 2019 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 v1alpha1
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
// NotFound is the not found error message.
|
|
NotFound = "not found"
|
|
)
|
|
|
|
// ContextData is a generic type for arbitrary data stored in PluginContext.
|
|
type ContextData interface {
|
|
// Clone is an interface to make a copy of ContextData. For performance reasons,
|
|
// clone should make shallow copies for members (e.g., slices or maps) that are not
|
|
// impacted by PreFilter's optional AddPod/RemovePod methods.
|
|
Clone() ContextData
|
|
}
|
|
|
|
// ContextKey is the type of keys stored in PluginContext.
|
|
type ContextKey string
|
|
|
|
// PluginContext provides a mechanism for plugins to store and retrieve arbitrary data.
|
|
// ContextData stored by one plugin can be read, altered, or deleted by another plugin.
|
|
// PluginContext does not provide any data protection, as all plugins are assumed to be
|
|
// trusted.
|
|
type PluginContext struct {
|
|
mx sync.RWMutex
|
|
storage map[ContextKey]ContextData
|
|
}
|
|
|
|
// NewPluginContext initializes a new PluginContext and returns its pointer.
|
|
func NewPluginContext() *PluginContext {
|
|
return &PluginContext{
|
|
storage: make(map[ContextKey]ContextData),
|
|
}
|
|
}
|
|
|
|
// Clone creates a copy of PluginContext and returns its pointer. Clone returns
|
|
// nil if the context being cloned is nil.
|
|
func (c *PluginContext) Clone() *PluginContext {
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
copy := NewPluginContext()
|
|
for k, v := range c.storage {
|
|
copy.Write(k, v.Clone())
|
|
}
|
|
return copy
|
|
}
|
|
|
|
// Read retrieves data with the given "key" from PluginContext. If the key is not
|
|
// present an error is returned.
|
|
// This function is not thread safe. In multi-threaded code, lock should be
|
|
// acquired first.
|
|
func (c *PluginContext) Read(key ContextKey) (ContextData, error) {
|
|
if v, ok := c.storage[key]; ok {
|
|
return v, nil
|
|
}
|
|
return nil, errors.New(NotFound)
|
|
}
|
|
|
|
// Write stores the given "val" in PluginContext with the given "key".
|
|
// This function is not thread safe. In multi-threaded code, lock should be
|
|
// acquired first.
|
|
func (c *PluginContext) Write(key ContextKey, val ContextData) {
|
|
c.storage[key] = val
|
|
}
|
|
|
|
// Delete deletes data with the given key from PluginContext.
|
|
// This function is not thread safe. In multi-threaded code, lock should be
|
|
// acquired first.
|
|
func (c *PluginContext) Delete(key ContextKey) {
|
|
delete(c.storage, key)
|
|
}
|
|
|
|
// Lock acquires PluginContext lock.
|
|
func (c *PluginContext) Lock() {
|
|
c.mx.Lock()
|
|
}
|
|
|
|
// Unlock releases PluginContext lock.
|
|
func (c *PluginContext) Unlock() {
|
|
c.mx.Unlock()
|
|
}
|
|
|
|
// RLock acquires PluginContext read lock.
|
|
func (c *PluginContext) RLock() {
|
|
c.mx.RLock()
|
|
}
|
|
|
|
// RUnlock releases PluginContext read lock.
|
|
func (c *PluginContext) RUnlock() {
|
|
c.mx.RUnlock()
|
|
}
|