...

Source file src/k8s.io/kubernetes/pkg/kubelet/runtimeclass/runtimeclass_manager.go

Documentation: k8s.io/kubernetes/pkg/kubelet/runtimeclass

     1  /*
     2  Copyright 2018 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 runtimeclass
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/api/errors"
    23  	"k8s.io/client-go/informers"
    24  	clientset "k8s.io/client-go/kubernetes"
    25  	nodev1 "k8s.io/client-go/listers/node/v1"
    26  )
    27  
    28  // Manager caches RuntimeClass API objects, and provides accessors to the Kubelet.
    29  type Manager struct {
    30  	informerFactory informers.SharedInformerFactory
    31  	lister          nodev1.RuntimeClassLister
    32  }
    33  
    34  // NewManager returns a new RuntimeClass Manager. Run must be called before the manager can be used.
    35  func NewManager(client clientset.Interface) *Manager {
    36  	const resyncPeriod = 0
    37  
    38  	factory := informers.NewSharedInformerFactory(client, resyncPeriod)
    39  	lister := factory.Node().V1().RuntimeClasses().Lister()
    40  
    41  	return &Manager{
    42  		informerFactory: factory,
    43  		lister:          lister,
    44  	}
    45  }
    46  
    47  // Start starts syncing the RuntimeClass cache with the apiserver.
    48  func (m *Manager) Start(stopCh <-chan struct{}) {
    49  	m.informerFactory.Start(stopCh)
    50  }
    51  
    52  // WaitForCacheSync exposes the WaitForCacheSync method on the informer factory for testing
    53  // purposes.
    54  func (m *Manager) WaitForCacheSync(stopCh <-chan struct{}) {
    55  	m.informerFactory.WaitForCacheSync(stopCh)
    56  }
    57  
    58  // LookupRuntimeHandler returns the RuntimeHandler string associated with the given RuntimeClass
    59  // name (or the default of "" for nil). If the RuntimeClass is not found, it returns an
    60  // errors.NotFound error.
    61  func (m *Manager) LookupRuntimeHandler(runtimeClassName *string) (string, error) {
    62  	if runtimeClassName == nil || *runtimeClassName == "" {
    63  		// The default RuntimeClass always resolves to the empty runtime handler.
    64  		return "", nil
    65  	}
    66  
    67  	name := *runtimeClassName
    68  
    69  	rc, err := m.lister.Get(name)
    70  	if err != nil {
    71  		if errors.IsNotFound(err) {
    72  			return "", err
    73  		}
    74  		return "", fmt.Errorf("failed to lookup RuntimeClass %s: %v", name, err)
    75  	}
    76  
    77  	return rc.Handler, nil
    78  }
    79  

View as plain text