...

Source file src/sigs.k8s.io/cli-utils/pkg/apply/cache/resource_cache_map.go

Documentation: sigs.k8s.io/cli-utils/pkg/apply/cache

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package cache
     5  
     6  import (
     7  	"sync"
     8  
     9  	"k8s.io/klog/v2"
    10  	"sigs.k8s.io/cli-utils/pkg/kstatus/status"
    11  	"sigs.k8s.io/cli-utils/pkg/object"
    12  )
    13  
    14  // ResourceCacheMap stores ResourceStatus objects in a map indexed by resource ID.
    15  // ResourceCacheMap is thread-safe.
    16  type ResourceCacheMap struct {
    17  	mu    sync.RWMutex
    18  	cache map[object.ObjMetadata]ResourceStatus
    19  }
    20  
    21  // NewResourceCacheMap returns a new empty ResourceCacheMap
    22  func NewResourceCacheMap() *ResourceCacheMap {
    23  	return &ResourceCacheMap{
    24  		cache: make(map[object.ObjMetadata]ResourceStatus),
    25  	}
    26  }
    27  
    28  // Load resources into the cache, generating the ID from the resource itself.
    29  // Existing resources with the same ID will be replaced.
    30  func (rc *ResourceCacheMap) Load(values ...ResourceStatus) {
    31  	rc.mu.Lock()
    32  	defer rc.mu.Unlock()
    33  
    34  	for _, value := range values {
    35  		id := object.UnstructuredToObjMetadata(value.Resource)
    36  		rc.cache[id] = value
    37  	}
    38  }
    39  
    40  // Put the resource into the cache using the supplied ID, replacing any
    41  // existing resource with the same ID.
    42  func (rc *ResourceCacheMap) Put(id object.ObjMetadata, value ResourceStatus) {
    43  	rc.mu.Lock()
    44  	defer rc.mu.Unlock()
    45  
    46  	rc.cache[id] = value
    47  }
    48  
    49  // Get retrieves the resource associated with the ID from the cache.
    50  // Returns (nil, true) if not found in the cache.
    51  func (rc *ResourceCacheMap) Get(id object.ObjMetadata) ResourceStatus {
    52  	rc.mu.RLock()
    53  	defer rc.mu.RUnlock()
    54  
    55  	obj, found := rc.cache[id]
    56  	if klog.V(6).Enabled() {
    57  		if found {
    58  			klog.V(6).Infof("resource cache hit: %s", id)
    59  		} else {
    60  			klog.V(6).Infof("resource cache miss: %s", id)
    61  		}
    62  	}
    63  	if !found {
    64  		return ResourceStatus{
    65  			Resource:      nil,
    66  			Status:        status.UnknownStatus,
    67  			StatusMessage: "resource not cached",
    68  		}
    69  	}
    70  	return obj
    71  }
    72  
    73  // Remove the resource associated with the ID from the cache.
    74  func (rc *ResourceCacheMap) Remove(id object.ObjMetadata) {
    75  	rc.mu.Lock()
    76  	defer rc.mu.Unlock()
    77  
    78  	delete(rc.cache, id)
    79  }
    80  
    81  // Clear the cache.
    82  func (rc *ResourceCacheMap) Clear() {
    83  	rc.mu.Lock()
    84  	defer rc.mu.Unlock()
    85  
    86  	rc.cache = make(map[object.ObjMetadata]ResourceStatus)
    87  }
    88  

View as plain text