-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
2,494 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2021 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cache | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"sigs.k8s.io/cli-utils/pkg/object" | ||
) | ||
|
||
// ResourceCache stores unstructured resource objects in memory | ||
type ResourceCache interface { | ||
// Put the resource into the cache, generating the ObjMetadata from the object. | ||
Put(obj *unstructured.Unstructured) error | ||
// Set the resource in the cache using the supplied key. | ||
Set(objMeta object.ObjMetadata, obj *unstructured.Unstructured) | ||
// Get the resource associated with the key from the cache. | ||
// Returns (nil, true) if not found in the cache. | ||
Get(objMeta object.ObjMetadata) (*unstructured.Unstructured, bool) | ||
// Remove the resource associated with the key from the cache. | ||
Remove(objMeta object.ObjMetadata) | ||
// Clear the cache. | ||
Clear() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2020 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cache | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/cli-utils/pkg/object" | ||
) | ||
|
||
// ResourceCacheMap stores unstructured resource objects in a map. | ||
// ResourceCacheMap is NOT thread-safe. | ||
type ResourceCacheMap struct { | ||
cache map[object.ObjMetadata]*unstructured.Unstructured | ||
} | ||
|
||
// NewResourceCacheMap returns a new empty ResourceCacheMap | ||
func NewResourceCacheMap() *ResourceCacheMap { | ||
return &ResourceCacheMap{ | ||
cache: make(map[object.ObjMetadata]*unstructured.Unstructured), | ||
} | ||
} | ||
|
||
// Load adds the resources into the cache, replacing any existing resource with | ||
// the same ID. Returns an error if any resource is invalid. | ||
func (rc *ResourceCacheMap) Load(values ...*unstructured.Unstructured) error { | ||
for _, value := range values { | ||
key, err := object.UnstructuredToObjMeta(value) | ||
if err != nil { | ||
return fmt.Errorf("failed to create resource cache key: %w", err) | ||
} | ||
rc.cache[key] = value | ||
} | ||
return nil | ||
} | ||
|
||
// Put adds the resource into the cache, replacing any existing resource with | ||
// the same ID. Returns an error if resource is invalid. | ||
func (rc *ResourceCacheMap) Put(value *unstructured.Unstructured) error { | ||
key, err := object.UnstructuredToObjMeta(value) | ||
if err != nil { | ||
return fmt.Errorf("failed to create resource cache key: %w", err) | ||
} | ||
rc.Set(key, value) | ||
return nil | ||
} | ||
|
||
// Set the resource in the cache using the supplied key, and replacing any | ||
// existing resource with the same key. | ||
func (rc *ResourceCacheMap) Set(key object.ObjMetadata, value *unstructured.Unstructured) { | ||
rc.cache[key] = value | ||
} | ||
|
||
// Get retrieves the resource associated with the key from the cache. | ||
// Returns (nil, true) if not found in the cache. | ||
func (rc *ResourceCacheMap) Get(key object.ObjMetadata) (*unstructured.Unstructured, bool) { | ||
obj, found := rc.cache[key] | ||
if klog.V(4).Enabled() { | ||
if found { | ||
klog.Infof("resource cache hit: %s", key) | ||
} else { | ||
klog.Infof("resource cache miss: %s", key) | ||
} | ||
} | ||
return obj, found | ||
} | ||
|
||
// Remove the resource associated with the key from the cache. | ||
func (rc *ResourceCacheMap) Remove(key object.ObjMetadata) { | ||
delete(rc.cache, key) | ||
} | ||
|
||
// Clear the cache. | ||
func (rc *ResourceCacheMap) Clear() { | ||
rc.cache = make(map[object.ObjMetadata]*unstructured.Unstructured) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,7 @@ const ( | |
Created | ||
Unchanged | ||
Configured | ||
Mutated | ||
) | ||
|
||
type ApplyEvent struct { | ||
|
Oops, something went wrong.