123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /*
- Copyright 2017 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package configmap
- import (
- "fmt"
- "time"
- "k8s.io/api/core/v1"
- clientset "k8s.io/client-go/kubernetes"
- podutil "k8s.io/kubernetes/pkg/api/v1/pod"
- corev1 "k8s.io/kubernetes/pkg/apis/core/v1"
- "k8s.io/kubernetes/pkg/kubelet/util/manager"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/util/clock"
- "k8s.io/apimachinery/pkg/util/sets"
- "k8s.io/apimachinery/pkg/watch"
- )
- type Manager interface {
- // Get configmap by configmap namespace and name.
- GetConfigMap(namespace, name string) (*v1.ConfigMap, error)
- // WARNING: Register/UnregisterPod functions should be efficient,
- // i.e. should not block on network operations.
- // RegisterPod registers all configmaps from a given pod.
- RegisterPod(pod *v1.Pod)
- // UnregisterPod unregisters configmaps from a given pod that are not
- // used by any other registered pod.
- UnregisterPod(pod *v1.Pod)
- }
- // simpleConfigMapManager implements ConfigMap Manager interface with
- // simple operations to apiserver.
- type simpleConfigMapManager struct {
- kubeClient clientset.Interface
- }
- func NewSimpleConfigMapManager(kubeClient clientset.Interface) Manager {
- return &simpleConfigMapManager{kubeClient: kubeClient}
- }
- func (s *simpleConfigMapManager) GetConfigMap(namespace, name string) (*v1.ConfigMap, error) {
- return s.kubeClient.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
- }
- func (s *simpleConfigMapManager) RegisterPod(pod *v1.Pod) {
- }
- func (s *simpleConfigMapManager) UnregisterPod(pod *v1.Pod) {
- }
- // configMapManager keeps a cache of all configmaps necessary
- // for registered pods. Different implementation of the store
- // may result in different semantics for freshness of configmaps
- // (e.g. ttl-based implementation vs watch-based implementation).
- type configMapManager struct {
- manager manager.Manager
- }
- func (c *configMapManager) GetConfigMap(namespace, name string) (*v1.ConfigMap, error) {
- object, err := c.manager.GetObject(namespace, name)
- if err != nil {
- return nil, err
- }
- if configmap, ok := object.(*v1.ConfigMap); ok {
- return configmap, nil
- }
- return nil, fmt.Errorf("unexpected object type: %v", object)
- }
- func (c *configMapManager) RegisterPod(pod *v1.Pod) {
- c.manager.RegisterPod(pod)
- }
- func (c *configMapManager) UnregisterPod(pod *v1.Pod) {
- c.manager.UnregisterPod(pod)
- }
- func getConfigMapNames(pod *v1.Pod) sets.String {
- result := sets.NewString()
- podutil.VisitPodConfigmapNames(pod, func(name string) bool {
- result.Insert(name)
- return true
- })
- return result
- }
- const (
- defaultTTL = time.Minute
- )
- // NewCachingConfigMapManager creates a manager that keeps a cache of all configmaps
- // necessary for registered pods.
- // It implement the following logic:
- // - whenever a pod is create or updated, the cached versions of all configmaps
- // are invalidated
- // - every GetObject() call tries to fetch the value from local cache; if it is
- // not there, invalidated or too old, we fetch it from apiserver and refresh the
- // value in cache; otherwise it is just fetched from cache
- func NewCachingConfigMapManager(kubeClient clientset.Interface, getTTL manager.GetObjectTTLFunc) Manager {
- getConfigMap := func(namespace, name string, opts metav1.GetOptions) (runtime.Object, error) {
- return kubeClient.CoreV1().ConfigMaps(namespace).Get(name, opts)
- }
- configMapStore := manager.NewObjectStore(getConfigMap, clock.RealClock{}, getTTL, defaultTTL)
- return &configMapManager{
- manager: manager.NewCacheBasedManager(configMapStore, getConfigMapNames),
- }
- }
- // NewWatchingConfigMapManager creates a manager that keeps a cache of all configmaps
- // necessary for registered pods.
- // It implements the following logic:
- // - whenever a pod is created or updated, we start inidvidual watches for all
- // referenced objects that aren't referenced from other registered pods
- // - every GetObject() returns a value from local cache propagated via watches
- func NewWatchingConfigMapManager(kubeClient clientset.Interface) Manager {
- listConfigMap := func(namespace string, opts metav1.ListOptions) (runtime.Object, error) {
- return kubeClient.CoreV1().ConfigMaps(namespace).List(opts)
- }
- watchConfigMap := func(namespace string, opts metav1.ListOptions) (watch.Interface, error) {
- return kubeClient.CoreV1().ConfigMaps(namespace).Watch(opts)
- }
- newConfigMap := func() runtime.Object {
- return &v1.ConfigMap{}
- }
- gr := corev1.Resource("configmap")
- return &configMapManager{
- manager: manager.NewWatchBasedManager(listConfigMap, watchConfigMap, newConfigMap, gr, getConfigMapNames),
- }
- }
|