-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: bjenuhb <Basanth_JenuHB@intuit.com>
- Loading branch information
1 parent
342abcd
commit aa366db
Showing
17 changed files
with
203 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,6 @@ rules: | |
verbs: | ||
- get | ||
- create | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
|
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 |
---|---|---|
|
@@ -953,8 +953,6 @@ rules: | |
verbs: | ||
- get | ||
- create | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
|
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 |
---|---|---|
|
@@ -862,8 +862,6 @@ rules: | |
verbs: | ||
- get | ||
- create | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
|
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 |
---|---|---|
|
@@ -18,8 +18,6 @@ rules: | |
verbs: | ||
- get | ||
- create | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
|
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 |
---|---|---|
|
@@ -891,8 +891,6 @@ rules: | |
verbs: | ||
- get | ||
- create | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
|
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 |
---|---|---|
|
@@ -891,8 +891,6 @@ rules: | |
verbs: | ||
- get | ||
- create | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
|
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 |
---|---|---|
|
@@ -891,8 +891,6 @@ rules: | |
verbs: | ||
- get | ||
- create | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "" | ||
resources: | ||
|
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 |
---|---|---|
@@ -1,26 +1,6 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
v1 "k8s.io/client-go/listers/core/v1" | ||
) | ||
|
||
type ResourceCache struct { | ||
v1.ServiceAccountLister | ||
v1.SecretLister | ||
} | ||
|
||
func NewResourceCache(client kubernetes.Interface, ctx context.Context, namespace string) *ResourceCache { | ||
informerFactory := informers.NewSharedInformerFactoryWithOptions(client, time.Minute*20, informers.WithNamespace(namespace)) | ||
cache := &ResourceCache{ | ||
ServiceAccountLister: informerFactory.Core().V1().ServiceAccounts().Lister(), | ||
SecretLister: informerFactory.Core().V1().Secrets().Lister(), | ||
} | ||
informerFactory.Start(ctx.Done()) | ||
informerFactory.WaitForCacheSync(ctx.Done()) | ||
return cache | ||
type Interface interface { | ||
Get(key string) (any, bool) | ||
Add(key string, value any) | ||
} |
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,44 @@ | ||
package cache | ||
|
||
import ( | ||
"time" | ||
|
||
"k8s.io/utils/lru" | ||
) | ||
|
||
var currentTime = time.Now | ||
|
||
type lruTtlCache struct { | ||
timeout time.Duration | ||
cache *lru.Cache | ||
} | ||
|
||
type item struct { | ||
expiryTime time.Time | ||
value any | ||
} | ||
|
||
func NewLRUTtlCache(timeout time.Duration, size int) *lruTtlCache { | ||
return &lruTtlCache{ | ||
timeout: timeout, | ||
cache: lru.New(size), | ||
} | ||
} | ||
|
||
func (c *lruTtlCache) Get(key string) (any, bool) { | ||
if data, ok := c.cache.Get(key); ok { | ||
item := data.(*item) | ||
if currentTime().Before(item.expiryTime) { | ||
return item.value, true | ||
} | ||
c.cache.Remove(key) | ||
} | ||
return nil, false | ||
} | ||
|
||
func (c *lruTtlCache) Add(key string, value any) { | ||
c.cache.Add(key, &item{ | ||
expiryTime: currentTime().Add(c.timeout), | ||
value: value, | ||
}) | ||
} |
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,64 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewTimedCache(t *testing.T) { | ||
|
||
t.Run("NewLRUTtlCache should return a new instance", func(t *testing.T) { | ||
cache := NewLRUTtlCache(time.Second, 1) | ||
assert.NotNil(t, cache) | ||
}) | ||
|
||
t.Run("TimedCache should cache based on LRU size", func(t *testing.T) { | ||
cache := NewLRUTtlCache(time.Second*10, 2) | ||
cache.Add("one", "one") | ||
cache.Add("two", "two") | ||
|
||
// Both "one" and "two" should be available since maxSize is 2 | ||
_, ok := cache.Get("one") | ||
assert.True(t, ok) | ||
|
||
_, ok = cache.Get("two") | ||
assert.True(t, ok) | ||
|
||
// "three" should be available since its newly added | ||
cache.Add("three", "three") | ||
_, ok = cache.Get("three") | ||
assert.True(t, ok) | ||
|
||
// "one" should not be available since maxSize is 2 | ||
_, ok = cache.Get("one") | ||
assert.False(t, ok) | ||
}) | ||
|
||
t.Run("TimedCache should cache based on timeout", func(t *testing.T) { | ||
tempCurrentTime := currentTime | ||
|
||
cache := NewLRUTtlCache(time.Minute*1, 2) | ||
|
||
currentTime = getTimeFunc(0, 0) | ||
cache.Add("one", "one") | ||
|
||
currentTime = getTimeFunc(0, 30) | ||
_, ok := cache.Get("one") | ||
assert.True(t, ok) | ||
|
||
currentTime = getTimeFunc(1, 30) | ||
// "one" should not be available since timeout is 1 min | ||
_, ok = cache.Get("one") | ||
assert.False(t, ok) | ||
currentTime = tempCurrentTime | ||
}) | ||
|
||
} | ||
|
||
func getTimeFunc(min int, sec int) func() time.Time { | ||
return func() time.Time { | ||
return time.Date(0, 0, 0, 0, min, sec, 0, time.UTC) | ||
} | ||
} |
Oops, something went wrong.