...

Source file src/k8s.io/kubernetes/pkg/kubelet/container/runtime_cache.go

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

     1  /*
     2  Copyright 2015 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  //go:generate mockgen -source=runtime_cache.go  -destination=testing/mock_runtime_cache.go -package=testing RuntimeCache
    18  package container
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  	"time"
    24  )
    25  
    26  // RuntimeCache is in interface for obtaining cached Pods.
    27  type RuntimeCache interface {
    28  	GetPods(context.Context) ([]*Pod, error)
    29  	ForceUpdateIfOlder(context.Context, time.Time) error
    30  }
    31  
    32  type podsGetter interface {
    33  	GetPods(context.Context, bool) ([]*Pod, error)
    34  }
    35  
    36  // NewRuntimeCache creates a container runtime cache.
    37  func NewRuntimeCache(getter podsGetter, cachePeriod time.Duration) (RuntimeCache, error) {
    38  	return &runtimeCache{
    39  		getter:      getter,
    40  		cachePeriod: cachePeriod,
    41  	}, nil
    42  }
    43  
    44  // runtimeCache caches a list of pods. It records a timestamp (cacheTime) right
    45  // before updating the pods, so the timestamp is at most as new as the pods
    46  // (and can be slightly older). The timestamp always moves forward. Callers are
    47  // expected not to modify the pods returned from GetPods.
    48  type runtimeCache struct {
    49  	sync.Mutex
    50  	// The underlying container runtime used to update the cache.
    51  	getter podsGetter
    52  	// The interval after which the cache should be refreshed.
    53  	cachePeriod time.Duration
    54  	// Last time when cache was updated.
    55  	cacheTime time.Time
    56  	// The content of the cache.
    57  	pods []*Pod
    58  }
    59  
    60  // GetPods returns the cached pods if they are not outdated; otherwise, it
    61  // retrieves the latest pods and return them.
    62  func (r *runtimeCache) GetPods(ctx context.Context) ([]*Pod, error) {
    63  	r.Lock()
    64  	defer r.Unlock()
    65  	if time.Since(r.cacheTime) > r.cachePeriod {
    66  		if err := r.updateCache(ctx); err != nil {
    67  			return nil, err
    68  		}
    69  	}
    70  	return r.pods, nil
    71  }
    72  
    73  func (r *runtimeCache) ForceUpdateIfOlder(ctx context.Context, minExpectedCacheTime time.Time) error {
    74  	r.Lock()
    75  	defer r.Unlock()
    76  	if r.cacheTime.Before(minExpectedCacheTime) {
    77  		return r.updateCache(ctx)
    78  	}
    79  	return nil
    80  }
    81  
    82  func (r *runtimeCache) updateCache(ctx context.Context) error {
    83  	pods, timestamp, err := r.getPodsWithTimestamp(ctx)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	r.pods, r.cacheTime = pods, timestamp
    88  	return nil
    89  }
    90  
    91  // getPodsWithTimestamp records a timestamp and retrieves pods from the getter.
    92  func (r *runtimeCache) getPodsWithTimestamp(ctx context.Context) ([]*Pod, time.Time, error) {
    93  	// Always record the timestamp before getting the pods to avoid stale pods.
    94  	timestamp := time.Now()
    95  	pods, err := r.getter.GetPods(ctx, false)
    96  	return pods, timestamp, err
    97  }
    98  

View as plain text