...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/dra/plugin/plugins_store.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/dra/plugin

     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 plugin
    18  
    19  import (
    20  	"sync"
    21  
    22  	"k8s.io/klog/v2"
    23  )
    24  
    25  // PluginsStore holds a list of DRA Plugins.
    26  type pluginsStore struct {
    27  	sync.RWMutex
    28  	store map[string]*plugin
    29  }
    30  
    31  // draPlugins map keeps track of all registered DRA plugins on the node
    32  // and their corresponding sockets.
    33  var draPlugins = &pluginsStore{}
    34  
    35  // Get lets you retrieve a DRA Plugin by name.
    36  // This method is protected by a mutex.
    37  func (s *pluginsStore) get(pluginName string) *plugin {
    38  	s.RLock()
    39  	defer s.RUnlock()
    40  
    41  	return s.store[pluginName]
    42  }
    43  
    44  // Set lets you save a DRA Plugin to the list and give it a specific name.
    45  // This method is protected by a mutex.
    46  func (s *pluginsStore) add(pluginName string, p *plugin) {
    47  	s.Lock()
    48  	defer s.Unlock()
    49  
    50  	if s.store == nil {
    51  		s.store = make(map[string]*plugin)
    52  	}
    53  
    54  	_, exists := s.store[pluginName]
    55  	if exists {
    56  		klog.V(1).InfoS(log("plugin: %s already registered, previous plugin will be overridden", pluginName))
    57  	}
    58  	s.store[pluginName] = p
    59  }
    60  
    61  // Delete lets you delete a DRA Plugin by name.
    62  // This method is protected by a mutex.
    63  func (s *pluginsStore) delete(pluginName string) {
    64  	s.Lock()
    65  	defer s.Unlock()
    66  
    67  	delete(s.store, pluginName)
    68  }
    69  

View as plain text