Skip to content

Commit 2d8fd48

Browse files
committed
feat: GetEnvValueFromCache
1 parent 63c0d21 commit 2d8fd48

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ go 1.16
55
require (
66
github.com/AlekSi/pointer v1.1.0
77
github.com/TomOnTime/utfutil v0.0.0-20210710122150-437f72b26edf
8-
github.com/flanksource/commons v1.5.12
8+
github.com/flanksource/commons v1.5.13
99
github.com/gomarkdown/markdown v0.0.0-20210820032736-385812cbea76
1010
github.com/hairyhenderson/gomplate/v3 v3.6.0
1111
github.com/mitchellh/mapstructure v1.3.3
1212
github.com/mitchellh/reflectwalk v1.0.0
13+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
1314
github.com/pkg/errors v0.9.1
1415
github.com/sergi/go-diff v1.0.0
1516
github.com/sirupsen/logrus v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT
515515
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
516516
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
517517
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
518+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
519+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
518520
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
519521
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
520522
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=

shortcuts.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/AlekSi/pointer"
910
"github.com/mitchellh/mapstructure"
11+
"github.com/patrickmn/go-cache"
1012
perrors "github.com/pkg/errors"
1113
v1 "k8s.io/api/core/v1"
1214
networkingv1 "k8s.io/api/networking/v1"
@@ -319,6 +321,19 @@ func (c *Client) GetSecret(namespace, name string) *map[string][]byte {
319321
return &secret.Data
320322
}
321323

324+
// GetSecret returns the data of a secret or nil for any error
325+
func (c *Client) GetSecretV2(ctx context.Context, namespace, name string) (*map[string][]byte, error) {
326+
k8s, err := c.GetClientset()
327+
if err != nil {
328+
return nil, err
329+
}
330+
secret, err := k8s.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
331+
if err != nil {
332+
return nil, err
333+
}
334+
return &secret.Data, nil
335+
}
336+
322337
// GetConfigMap returns the data of a secret or nil for any error
323338
func (c *Client) GetConfigMap(namespace, name string) *map[string]string {
324339
k8s, err := c.GetClientset()
@@ -334,15 +349,48 @@ func (c *Client) GetConfigMap(namespace, name string) *map[string]string {
334349
return &cm.Data
335350
}
336351

352+
// GetConfigMap returns the data of a secret or nil for any error
353+
func (c *Client) GetConfigMapV2(ctx context.Context, namespace, name string) (*map[string]string, error) {
354+
k8s, err := c.GetClientset()
355+
if err != nil {
356+
return nil, err
357+
}
358+
cm, err := k8s.CoreV1().ConfigMaps(namespace).Get(ctx, name, metav1.GetOptions{})
359+
if err != nil {
360+
return nil, err
361+
}
362+
return &cm.Data, nil
363+
}
364+
365+
// Create a cache with a default expiration time of 5 minutes, and which
366+
// purges expired items every 10 minutes
367+
var envCache = cache.New(5*time.Minute, 10*time.Minute)
368+
369+
func (c *Client) GetEnvValueFromCache(input EnvVar, namespace string, expiry time.Duration) (string, string, error) {
370+
if input.ValueFrom == nil {
371+
return input.Name, input.Value, nil
372+
}
373+
if value, found := envCache.Get(input.GetCacheKey()); found {
374+
return input.Name, value.(string), nil
375+
}
376+
_, value, err := c.GetEnvValue(input, namespace)
377+
if err != nil {
378+
return "", "", err
379+
}
380+
envCache.Set(input.GetCacheKey(), value, expiry)
381+
return input.Name, value, nil
382+
}
383+
337384
func (c *Client) GetEnvValue(input EnvVar, namespace string) (string, string, error) {
385+
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
338386
if input.Value != "" {
339387
return input.Name, input.Value, nil
340388
}
341389
if input.ValueFrom != nil {
342390
if input.ValueFrom.SecretKeyRef != nil {
343-
secret := c.GetSecret(namespace, input.ValueFrom.SecretKeyRef.Name)
391+
secret, err := c.GetSecretV2(ctx, namespace, input.ValueFrom.SecretKeyRef.Name)
344392
if secret == nil {
345-
return "", "", perrors.New(fmt.Sprintf("Could not get contents of secret %v from namespace %v", input.ValueFrom.SecretKeyRef.Name, namespace))
393+
return "", "", perrors.New(fmt.Sprintf("Could not get contents of secret %v from namespace %v: %v", input.ValueFrom.SecretKeyRef.Name, namespace, err))
346394
}
347395

348396
value, ok := (*secret)[input.ValueFrom.SecretKeyRef.Key]
@@ -352,9 +400,9 @@ func (c *Client) GetEnvValue(input EnvVar, namespace string) (string, string, er
352400
return input.Name, string(value), nil
353401
}
354402
if input.ValueFrom.ConfigMapKeyRef != nil {
355-
cm := c.GetConfigMap(namespace, input.ValueFrom.ConfigMapKeyRef.Name)
403+
cm, err := c.GetConfigMapV2(ctx, namespace, input.ValueFrom.ConfigMapKeyRef.Name)
356404
if cm == nil {
357-
return "", "", perrors.New(fmt.Sprintf("Could not get contents of configmap %v from namespace %v", input.ValueFrom.ConfigMapKeyRef.Name, namespace))
405+
return "", "", perrors.New(fmt.Sprintf("Could not get contents of configmap %v from namespace %v: %v", input.ValueFrom.ConfigMapKeyRef.Name, namespace, err))
358406
}
359407
value, ok := (*cm)[input.ValueFrom.ConfigMapKeyRef.Key]
360408
if !ok {

types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ type EnvVar struct {
4343
ValueFrom *EnvVarSource `json:"valueFrom,omitempty" yaml:"valueFrom,omitempty" protobuf:"bytes,3,opt,name=valueFrom"`
4444
}
4545

46+
func (e EnvVar) GetKey() string {
47+
if e.ValueFrom.SecretKeyRef != nil {
48+
return e.ValueFrom.SecretKeyRef.Key
49+
}
50+
if e.ValueFrom.ConfigMapKeyRef != nil {
51+
return e.ValueFrom.ConfigMapKeyRef.Key
52+
}
53+
return ""
54+
}
55+
56+
func (e EnvVar) GetCacheKey() string {
57+
if e.ValueFrom == nil {
58+
return e.Name
59+
}
60+
if e.ValueFrom.SecretKeyRef != nil {
61+
62+
return e.Name + e.ValueFrom.SecretKeyRef.Name + e.ValueFrom.SecretKeyRef.Key
63+
}
64+
if e.ValueFrom.ConfigMapKeyRef != nil {
65+
return e.Name + e.ValueFrom.ConfigMapKeyRef.Name + e.ValueFrom.ConfigMapKeyRef.Key
66+
}
67+
return e.Name
68+
}
69+
4670
func (e EnvVar) IsEmpty() bool {
4771
return e.Value == "" && e.ValueFrom == nil
4872
}

0 commit comments

Comments
 (0)