runtimeclass_manager.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package runtimeclass
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/api/errors"
  17. "k8s.io/client-go/informers"
  18. clientset "k8s.io/client-go/kubernetes"
  19. nodev1beta1 "k8s.io/client-go/listers/node/v1beta1"
  20. )
  21. // Manager caches RuntimeClass API objects, and provides accessors to the Kubelet.
  22. type Manager struct {
  23. informerFactory informers.SharedInformerFactory
  24. lister nodev1beta1.RuntimeClassLister
  25. }
  26. // NewManager returns a new RuntimeClass Manager. Run must be called before the manager can be used.
  27. func NewManager(client clientset.Interface) *Manager {
  28. const resyncPeriod = 0
  29. factory := informers.NewSharedInformerFactory(client, resyncPeriod)
  30. lister := factory.Node().V1beta1().RuntimeClasses().Lister()
  31. return &Manager{
  32. informerFactory: factory,
  33. lister: lister,
  34. }
  35. }
  36. // Start starts syncing the RuntimeClass cache with the apiserver.
  37. func (m *Manager) Start(stopCh <-chan struct{}) {
  38. m.informerFactory.Start(stopCh)
  39. }
  40. // WaitForCacheSync exposes the WaitForCacheSync method on the informer factory for testing
  41. // purposes.
  42. func (m *Manager) WaitForCacheSync(stopCh <-chan struct{}) {
  43. m.informerFactory.WaitForCacheSync(stopCh)
  44. }
  45. // LookupRuntimeHandler returns the RuntimeHandler string associated with the given RuntimeClass
  46. // name (or the default of "" for nil). If the RuntimeClass is not found, it returns an
  47. // errors.NotFound error.
  48. func (m *Manager) LookupRuntimeHandler(runtimeClassName *string) (string, error) {
  49. if runtimeClassName == nil || *runtimeClassName == "" {
  50. // The default RuntimeClass always resolves to the empty runtime handler.
  51. return "", nil
  52. }
  53. name := *runtimeClassName
  54. rc, err := m.lister.Get(name)
  55. if err != nil {
  56. if errors.IsNotFound(err) {
  57. return "", err
  58. }
  59. return "", fmt.Errorf("Failed to lookup RuntimeClass %s: %v", name, err)
  60. }
  61. return rc.Handler, nil
  62. }