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 4 commits
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
32 changes: 32 additions & 0 deletions changelog/fragments/1701091034-add-cache-for-secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: feature

# Change summary; a 80ish characters long description of the change.
summary: add cache for secrets when using kubernetes secret provider

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
#description:

# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component: elastic-agent

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
pr: https://github.com/elastic/elastic-agent/pull/3822

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
issue: https://github.com/elastic/elastic-agent/issues/3594
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ import "github.com/elastic/elastic-agent-autodiscover/kubernetes"
type Config struct {
KubeConfig string `config:"kube_config"`
KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`

TTL int `config:"ttl"`
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *Config) InitDefaults() {
c.TTL = 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"strings"
"sync"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "k8s.io/client-go/kubernetes"
Expand All @@ -33,6 +34,9 @@ type contextProviderK8sSecrets struct {

clientMx sync.Mutex
client k8sclient.Interface

secretsCacheMx sync.Mutex
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
secretsCache map[string]string
}

// ContextProviderBuilder builds the context provider.
Expand All @@ -46,19 +50,102 @@ func ContextProviderBuilder(logger *logger.Logger, c *config.Config, managed boo
return nil, errors.New(err, "failed to unpack configuration")
}
return &contextProviderK8sSecrets{
logger: logger,
config: &cfg,
logger: logger,
config: &cfg,
secretsCache: make(map[string]string),
}, nil
}

func (p *contextProviderK8sSecrets) Fetch(key string) (string, bool) {
// key = "kubernetes_secrets.somenamespace.somesecret.value"
return p.getFromCache(key)
}

// Run initializes the k8s secrets context provider.
func (p *contextProviderK8sSecrets) Run(ctx context.Context, comm corecomp.ContextProviderComm) error {
client, err := getK8sClientFunc(p.config.KubeConfig, p.config.KubeClientOptions)
if err != nil {
p.logger.Debugf("Kubernetes_secrets provider skipped, unable to connect: %s", err)
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
p.clientMx.Lock()
p.client = client
p.clientMx.Unlock()
go p.updateSecrets(ctx)

<-comm.Done()

p.clientMx.Lock()
p.client = nil
p.clientMx.Unlock()
return comm.Err()
}

func getK8sClient(kubeconfig string, opt kubernetes.KubeClientOptions) (k8sclient.Interface, error) {
return kubernetes.GetKubernetesClient(kubeconfig, opt)
}

// Update the secrets in the cache every TTL minutes
func (p *contextProviderK8sSecrets) updateSecrets(ctx context.Context) {
d := time.Duration(p.config.TTL) * time.Minute
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
timer := time.NewTimer(d)

for {
select {
case <-ctx.Done():
return
case <-timer.C:
p.updateCache()
timer.Reset(d)
}
}
}

func (p *contextProviderK8sSecrets) updateCache() {
p.secretsCacheMx.Lock()
for name := range p.secretsCache {
newValue, ok := p.fetchSecret(name)
constanca-m marked this conversation as resolved.
Show resolved Hide resolved

// remove the secret from the cache
if !ok {
delete(p.secretsCache, name)
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
} else {
p.secretsCache[name] = newValue
}
}
p.secretsCacheMx.Unlock()
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
}

func (p *contextProviderK8sSecrets) getFromCache(key string) (string, bool) {
p.secretsCacheMx.Lock()
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
value, ok := p.secretsCache[key]
p.secretsCacheMx.Unlock()

// if value is still not present in cache, it is possible we haven't tried to fetch it yet
if !ok {
value, ok = p.addToCache(key)
}
return value, ok
}

func (p *contextProviderK8sSecrets) addToCache(key string) (string, bool) {
value, ok := p.fetchSecret(key)
if ok {
p.secretsCacheMx.Lock()
p.secretsCache[key] = value
p.secretsCacheMx.Unlock()
}
return value, ok
}

func (p *contextProviderK8sSecrets) fetchSecret(key string) (string, bool) {
p.clientMx.Lock()
client := p.client
p.clientMx.Unlock()
if client == nil {
return "", false
}

// key = "kubernetes_secrets.somenamespace.somesecret.value"
tokens := strings.Split(key, ".")
if len(tokens) > 0 && tokens[0] != "kubernetes_secrets" {
return "", false
Expand Down Expand Up @@ -87,26 +174,6 @@ func (p *contextProviderK8sSecrets) Fetch(key string) (string, bool) {
return "", false
}
secretString := secret.Data[secretVar]
return string(secretString), true
}

// Run initializes the k8s secrets context provider.
func (p *contextProviderK8sSecrets) Run(ctx context.Context, comm corecomp.ContextProviderComm) error {
client, err := getK8sClientFunc(p.config.KubeConfig, p.config.KubeClientOptions)
if err != nil {
p.logger.Debugf("Kubernetes_secrets provider skipped, unable to connect: %s", err)
return nil
}
p.clientMx.Lock()
p.client = client
p.clientMx.Unlock()
<-comm.Done()
p.clientMx.Lock()
p.client = nil
p.clientMx.Unlock()
return comm.Err()
}

func getK8sClient(kubeconfig string, opt kubernetes.KubeClientOptions) (k8sclient.Interface, error) {
return kubernetes.GetKubernetesClient(kubeconfig, opt)
return string(secretString), true
}
Loading