configmap_manager.go 5.4 KB

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