Skip to content

Commit 2f378d1

Browse files
committed
make key fields private
1 parent 197124e commit 2f378d1

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

modules/common/client/cache/cache.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import (
1414
var (
1515
// cache is a global resource cache that maps our custom keys
1616
// created based on information provided by the specific resource client.
17-
// It stores resource lists in order to speed up the client response times.
17+
// It stores resource lists to speed up the client response times.
1818
cache *theine.Cache[string, any]
1919

2020
// cacheLocks is used as a set that holds information about
2121
// cache keys that are currently fetching the latest data from the
22-
// kubernetes API server in background. It allows us to avoid
23-
// multiple concurrent update calls being sent to the K8S API.
24-
// Once lock is removed, next update call can be initiated.
22+
// kubernetes API server in the background.
23+
// It allows us to avoid multiple concurrent update calls being sent
24+
// to the Kubernetes API.
25+
// Once the lock is removed, the next update call can be initiated.
2526
cacheLocks sync.Map
2627
)
2728

@@ -33,7 +34,7 @@ func init() {
3334
}
3435

3536
// Get gives access to cache entries. It requires Key structure
36-
// to be provided which is used to calculate cache key sha.
37+
// to be provided which is used to calculate cache key SHA.
3738
func Get[T any](key Key) (T, bool, error) {
3839
typedValue := lo.Empty[T]()
3940

@@ -51,7 +52,7 @@ func Get[T any](key Key) (T, bool, error) {
5152
}
5253

5354
// Set allows updating cache with specific values.
54-
// It requires Key structure to be provided which is used to calculate cache key sha.
55+
// It requires Key structure to be provided which is used to calculate cache key SHA.
5556
func Set[T any](key Key, value T) error {
5657
cacheKey, err := key.SHA()
5758
if err != nil {
@@ -62,7 +63,7 @@ func Set[T any](key Key, value T) error {
6263
return nil
6364
}
6465

65-
// DeferredLoad updates cache in background with the data fetched using the loadFunc.
66+
// DeferredLoad updates cache in the background with the data fetched using the loadFunc.
6667
func DeferredLoad[T any](key Key, loadFunc func() (T, error)) {
6768
go func() {
6869
cacheKey, err := key.SHA()

modules/common/client/cache/key.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ var contextCache *theine.Cache[string, string]
2222
// a unique cache key SHA. It is used when
2323
// `cluster-context-enabled=false`.
2424
type key struct {
25-
// Kind is a kubernetes resource kind
26-
Kind types.ResourceKind
25+
// kind is a kubernetes resource kind
26+
kind types.ResourceKind
2727

28-
// Namespaces is a kubernetes resource namespace
29-
Namespace string
28+
// namespace is a kubernetes resource namespace
29+
namespace string
3030

3131
// Opts is a list options object used by the kubernetes client.
32-
Opts metav1.ListOptions
32+
opts metav1.ListOptions
3333
}
3434

3535
// SHA calculates sha based on the internal key fields.
3636
func (k key) SHA() (string, error) {
37-
k.Opts = metav1.ListOptions{LabelSelector: k.Opts.LabelSelector, FieldSelector: k.Opts.FieldSelector}
37+
k.opts = metav1.ListOptions{LabelSelector: k.opts.LabelSelector, FieldSelector: k.opts.FieldSelector}
3838
return helpers.HashObject(k)
3939
}
4040

@@ -44,8 +44,8 @@ func (k key) SHA() (string, error) {
4444
type Key struct {
4545
key
4646

47-
// Token is an auth token used to exchange it for the context ID.
48-
Token string
47+
// token is an auth token used to exchange it for the context ID.
48+
token string
4949

5050
// context is an internal identifier used in conjunction with the key
5151
// structure fields to create a cache key SHA that will be unique across
@@ -62,28 +62,25 @@ func (k Key) SHA() (sha string, err error) {
6262
return k.key.SHA()
6363
}
6464

65-
contextKey, exists := contextCache.Get(k.Token)
65+
contextKey, exists := contextCache.Get(k.token)
6666
if !exists {
67-
contextKey, err = exchangeToken(k.Token)
67+
contextKey, err = exchangeToken(k.token)
6868
if err != nil {
6969
return "", err
7070
}
7171

72-
contextCache.SetWithTTL(k.Token, contextKey, 1, args.CacheTTL())
72+
contextCache.SetWithTTL(k.token, contextKey, 1, args.CacheTTL())
7373
}
7474

75-
k.Opts = metav1.ListOptions{LabelSelector: k.Opts.LabelSelector, FieldSelector: k.Opts.FieldSelector}
76-
k.Token = ""
75+
k.opts = metav1.ListOptions{LabelSelector: k.opts.LabelSelector, FieldSelector: k.opts.FieldSelector}
76+
k.token = ""
7777
k.context = contextKey
7878
return helpers.HashObject(k)
7979
}
8080

8181
// NewKey creates a new cache Key.
8282
func NewKey(kind types.ResourceKind, namespace, token string, opts metav1.ListOptions) Key {
83-
return Key{
84-
key: key{Kind: kind, Namespace: namespace, Opts: opts},
85-
Token: token,
86-
}
83+
return Key{key: key{kind, namespace, opts}, token: token}
8784
}
8885

8986
type tokenExchangeTransport struct {

0 commit comments

Comments
 (0)