configmap_manager.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright 2017 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 configmap
  14. import (
  15. "fmt"
  16. "time"
  17. "k8s.io/api/core/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  20. corev1 "k8s.io/kubernetes/pkg/apis/core/v1"
  21. "k8s.io/kubernetes/pkg/kubelet/util/manager"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/util/clock"
  25. "k8s.io/apimachinery/pkg/util/sets"
  26. "k8s.io/apimachinery/pkg/watch"
  27. )
  28. type Manager interface {
  29. // Get configmap by configmap namespace and name.
  30. GetConfigMap(namespace, name string) (*v1.ConfigMap, error)
  31. // WARNING: Register/UnregisterPod functions should be efficient,
  32. // i.e. should not block on network operations.
  33. // RegisterPod registers all configmaps from a given pod.
  34. RegisterPod(pod *v1.Pod)
  35. // UnregisterPod unregisters configmaps from a given pod that are not
  36. // used by any other registered pod.
  37. UnregisterPod(pod *v1.Pod)
  38. }
  39. // simpleConfigMapManager implements ConfigMap Manager interface with
  40. // simple operations to apiserver.
  41. type simpleConfigMapManager struct {
  42. kubeClient clientset.Interface
  43. }
  44. func NewSimpleConfigMapManager(kubeClient clientset.Interface) Manager {
  45. return &simpleConfigMapManager{kubeClient: kubeClient}
  46. }
  47. func (s *simpleConfigMapManager) GetConfigMap(namespace, name string) (*v1.ConfigMap, error) {
  48. return s.kubeClient.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
  49. }
  50. func (s *simpleConfigMapManager) RegisterPod(pod *v1.Pod) {
  51. }
  52. func (s *simpleConfigMapManager) UnregisterPod(pod *v1.Pod) {
  53. }
  54. // configMapManager keeps a cache of all configmaps necessary
  55. // for registered pods. Different implementation of the store
  56. // may result in different semantics for freshness of configmaps
  57. // (e.g. ttl-based implementation vs watch-based implementation).
  58. type configMapManager struct {
  59. manager manager.Manager
  60. }
  61. func (c *configMapManager) GetConfigMap(namespace, name string) (*v1.ConfigMap, error) {
  62. object, err := c.manager.GetObject(namespace, name)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if configmap, ok := object.(*v1.ConfigMap); ok {
  67. return configmap, nil
  68. }
  69. return nil, fmt.Errorf("unexpected object type: %v", object)
  70. }
  71. func (c *configMapManager) RegisterPod(pod *v1.Pod) {
  72. c.manager.RegisterPod(pod)
  73. }
  74. func (c *configMapManager) UnregisterPod(pod *v1.Pod) {
  75. c.manager.UnregisterPod(pod)
  76. }
  77. func getConfigMapNames(pod *v1.Pod) sets.String {
  78. result := sets.NewString()
  79. podutil.VisitPodConfigmapNames(pod, func(name string) bool {
  80. result.Insert(name)
  81. return true
  82. })
  83. return result
  84. }
  85. const (
  86. defaultTTL = time.Minute
  87. )
  88. // NewCachingConfigMapManager creates a manager that keeps a cache of all configmaps
  89. // necessary for registered pods.
  90. // It implement the following logic:
  91. // - whenever a pod is create or updated, the cached versions of all configmaps
  92. // are invalidated
  93. // - every GetObject() call tries to fetch the value from local cache; if it is
  94. // not there, invalidated or too old, we fetch it from apiserver and refresh the
  95. // value in cache; otherwise it is just fetched from cache
  96. func NewCachingConfigMapManager(kubeClient clientset.Interface, getTTL manager.GetObjectTTLFunc) Manager {
  97. getConfigMap := func(namespace, name string, opts metav1.GetOptions) (runtime.Object, error) {
  98. return kubeClient.CoreV1().ConfigMaps(namespace).Get(name, opts)
  99. }
  100. configMapStore := manager.NewObjectStore(getConfigMap, clock.RealClock{}, getTTL, defaultTTL)
  101. return &configMapManager{
  102. manager: manager.NewCacheBasedManager(configMapStore, getConfigMapNames),
  103. }
  104. }
  105. // NewWatchingConfigMapManager creates a manager that keeps a cache of all configmaps
  106. // necessary for registered pods.
  107. // It implements the following logic:
  108. // - whenever a pod is created or updated, we start inidvidual watches for all
  109. // referenced objects that aren't referenced from other registered pods
  110. // - every GetObject() returns a value from local cache propagated via watches
  111. func NewWatchingConfigMapManager(kubeClient clientset.Interface) Manager {
  112. listConfigMap := func(namespace string, opts metav1.ListOptions) (runtime.Object, error) {
  113. return kubeClient.CoreV1().ConfigMaps(namespace).List(opts)
  114. }
  115. watchConfigMap := func(namespace string, opts metav1.ListOptions) (watch.Interface, error) {
  116. return kubeClient.CoreV1().ConfigMaps(namespace).Watch(opts)
  117. }
  118. newConfigMap := func() runtime.Object {
  119. return &v1.ConfigMap{}
  120. }
  121. gr := corev1.Resource("configmap")
  122. return &configMapManager{
  123. manager: manager.NewWatchBasedManager(listConfigMap, watchConfigMap, newConfigMap, gr, getConfigMapNames),
  124. }
  125. }