Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kubernetes secret provider] Add cache for the secrets #3822

Merged
merged 29 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
dadc79b
Add secret cache
constanca-m Nov 27, 2023
ea3a100
Add changelog
constanca-m Nov 27, 2023
a71101b
Remove assignment
constanca-m Nov 27, 2023
125330f
Change TTL default to 1 min
constanca-m Nov 27, 2023
09ba2b6
- Remove secrets based on last access
constanca-m Nov 27, 2023
4908655
- Remove secrets based on last access
constanca-m Nov 27, 2023
a291bae
- Update duration config format
constanca-m Nov 27, 2023
9f5b92c
Add unit test
constanca-m Nov 27, 2023
9d94859
Add unit test
constanca-m Nov 27, 2023
6d30148
- Use assert.eventually
constanca-m Nov 28, 2023
d7c8756
- Fix typos
constanca-m Nov 28, 2023
b3ba219
- Move key validation to addToCache
constanca-m Nov 28, 2023
8913d04
fix race in tests
constanca-m Nov 28, 2023
0fcd65a
fix race in tests
constanca-m Nov 28, 2023
57ad12c
fix race in tests
constanca-m Nov 28, 2023
8533ec4
fix race in tests
constanca-m Nov 28, 2023
c65ae20
Rename TTLUpdate to refresh interval.
constanca-m Dec 4, 2023
87f0453
Add context timeout.
constanca-m Dec 5, 2023
f02823e
Switch reading lock to writing lock.
constanca-m Dec 6, 2023
97d6c84
Switch reading lock to writing lock.
constanca-m Dec 6, 2023
0f8d86a
- Add disable cache option
constanca-m Dec 8, 2023
70c16ca
Rename cache fields
constanca-m Dec 11, 2023
7f40d9c
Changes config names
constanca-m Dec 13, 2023
62862a3
Merge maps
constanca-m Dec 13, 2023
7642f09
Merge maps
constanca-m Dec 13, 2023
ab76805
Merge maps, fix mistake
constanca-m Dec 14, 2023
4f3ab48
Change locks to reading locks
constanca-m Dec 14, 2023
044e7d1
Change locks to reading locks
constanca-m Dec 14, 2023
8b7da6c
Remove read lock for iteration
constanca-m Dec 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename TTLUpdate to refresh interval.
  • Loading branch information
constanca-m committed Dec 4, 2023
commit c65ae201dc938f74140f8d9e349570ac868bc9ef
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type Config struct {
KubeConfig string `config:"kube_config"`
KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`

TTLUpdate time.Duration `config:"ttl_update"`
TTLDelete time.Duration `config:"ttl_delete"`
RefreshInterval time.Duration `config:"refresh_interval"`
TTLDelete time.Duration `config:"ttl_delete"`
}

func (c *Config) InitDefaults() {
c.TTLUpdate = 60 * time.Second
c.RefreshInterval = 60 * time.Second
c.TTLDelete = 1 * time.Hour
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ func getK8sClient(kubeconfig string, opt kubernetes.KubeClientOptions) (k8sclien
return kubernetes.GetKubernetesClient(kubeconfig, opt)
}

// Update the secrets in the cache every TTL minutes
// Update the secrets in the cache every RefreshInterval
func (p *contextProviderK8sSecrets) updateSecrets(ctx context.Context) {
timer := time.NewTimer(p.config.TTLUpdate)
timer := time.NewTimer(p.config.RefreshInterval)
for {
select {
case <-ctx.Done():
return
case <-timer.C:
p.updateCache()
timer.Reset(p.config.TTLUpdate)
timer.Reset(p.config.RefreshInterval)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func Test_K8sSecretsProvider_Check_TTL(t *testing.T) {
ttlDelete, err := time.ParseDuration("1s")
require.NoError(t, err)

ttlUpdate, err := time.ParseDuration("100ms")
refreshInterval, err := time.ParseDuration("100ms")
require.NoError(t, err)

secret := &v1.Secret{
Expand All @@ -167,8 +167,8 @@ func Test_K8sSecretsProvider_Check_TTL(t *testing.T) {
logger := logp.NewLogger("test_k8s_secrets")

c := map[string]interface{}{
"ttl_update": ttlUpdate,
"ttl_delete": ttlDelete,
"refresh_interval": refreshInterval,
"ttl_delete": ttlDelete,
}
cfg, err := config.NewConfigFrom(c)
require.NoError(t, err)
Expand Down Expand Up @@ -237,11 +237,11 @@ func Test_K8sSecretsProvider_Check_TTL(t *testing.T) {
require.NoError(t, err)

// wait for ttl update
<-time.After(ttlUpdate)
<-time.After(refreshInterval)
assert.Eventuallyf(t, func() bool {
val, found = fp.Fetch(key)
return found && val == newPass
}, ttlUpdate*3, ttlUpdate, "Failed to update the secret value after TTL update has passed.")
}, refreshInterval*3, refreshInterval, "Failed to update the secret value after TTL update has passed.")

// After TTL delete, secret should no longer be found in cache since it was never
// fetched during that time
Expand Down