Skip to content

Commit

Permalink
feat: GetEnvValueFromCache
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Feb 27, 2022
1 parent 63c0d21 commit 2d8fd48
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ go 1.16
require (
github.com/AlekSi/pointer v1.1.0
github.com/TomOnTime/utfutil v0.0.0-20210710122150-437f72b26edf
github.com/flanksource/commons v1.5.12
github.com/flanksource/commons v1.5.13
github.com/gomarkdown/markdown v0.0.0-20210820032736-385812cbea76
github.com/hairyhenderson/gomplate/v3 v3.6.0
github.com/mitchellh/mapstructure v1.3.3
github.com/mitchellh/reflectwalk v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1
github.com/sergi/go-diff v1.0.0
github.com/sirupsen/logrus v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
Expand Down
56 changes: 52 additions & 4 deletions shortcuts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"
"strings"
"time"

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

// GetSecret returns the data of a secret or nil for any error
func (c *Client) GetSecretV2(ctx context.Context, namespace, name string) (*map[string][]byte, error) {
k8s, err := c.GetClientset()
if err != nil {
return nil, err
}
secret, err := k8s.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return &secret.Data, nil
}

// GetConfigMap returns the data of a secret or nil for any error
func (c *Client) GetConfigMap(namespace, name string) *map[string]string {
k8s, err := c.GetClientset()
Expand All @@ -334,15 +349,48 @@ func (c *Client) GetConfigMap(namespace, name string) *map[string]string {
return &cm.Data
}

// GetConfigMap returns the data of a secret or nil for any error
func (c *Client) GetConfigMapV2(ctx context.Context, namespace, name string) (*map[string]string, error) {
k8s, err := c.GetClientset()
if err != nil {
return nil, err
}
cm, err := k8s.CoreV1().ConfigMaps(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return &cm.Data, nil
}

// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 10 minutes
var envCache = cache.New(5*time.Minute, 10*time.Minute)

func (c *Client) GetEnvValueFromCache(input EnvVar, namespace string, expiry time.Duration) (string, string, error) {
if input.ValueFrom == nil {
return input.Name, input.Value, nil
}
if value, found := envCache.Get(input.GetCacheKey()); found {
return input.Name, value.(string), nil
}
_, value, err := c.GetEnvValue(input, namespace)
if err != nil {
return "", "", err
}
envCache.Set(input.GetCacheKey(), value, expiry)
return input.Name, value, nil
}

func (c *Client) GetEnvValue(input EnvVar, namespace string) (string, string, error) {
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
if input.Value != "" {
return input.Name, input.Value, nil
}
if input.ValueFrom != nil {
if input.ValueFrom.SecretKeyRef != nil {
secret := c.GetSecret(namespace, input.ValueFrom.SecretKeyRef.Name)
secret, err := c.GetSecretV2(ctx, namespace, input.ValueFrom.SecretKeyRef.Name)
if secret == nil {
return "", "", perrors.New(fmt.Sprintf("Could not get contents of secret %v from namespace %v", input.ValueFrom.SecretKeyRef.Name, namespace))
return "", "", perrors.New(fmt.Sprintf("Could not get contents of secret %v from namespace %v: %v", input.ValueFrom.SecretKeyRef.Name, namespace, err))
}

value, ok := (*secret)[input.ValueFrom.SecretKeyRef.Key]
Expand All @@ -352,9 +400,9 @@ func (c *Client) GetEnvValue(input EnvVar, namespace string) (string, string, er
return input.Name, string(value), nil
}
if input.ValueFrom.ConfigMapKeyRef != nil {
cm := c.GetConfigMap(namespace, input.ValueFrom.ConfigMapKeyRef.Name)
cm, err := c.GetConfigMapV2(ctx, namespace, input.ValueFrom.ConfigMapKeyRef.Name)
if cm == nil {
return "", "", perrors.New(fmt.Sprintf("Could not get contents of configmap %v from namespace %v", input.ValueFrom.ConfigMapKeyRef.Name, namespace))
return "", "", perrors.New(fmt.Sprintf("Could not get contents of configmap %v from namespace %v: %v", input.ValueFrom.ConfigMapKeyRef.Name, namespace, err))
}
value, ok := (*cm)[input.ValueFrom.ConfigMapKeyRef.Key]
if !ok {
Expand Down
24 changes: 24 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ type EnvVar struct {
ValueFrom *EnvVarSource `json:"valueFrom,omitempty" yaml:"valueFrom,omitempty" protobuf:"bytes,3,opt,name=valueFrom"`
}

func (e EnvVar) GetKey() string {
if e.ValueFrom.SecretKeyRef != nil {
return e.ValueFrom.SecretKeyRef.Key
}
if e.ValueFrom.ConfigMapKeyRef != nil {
return e.ValueFrom.ConfigMapKeyRef.Key
}
return ""
}

func (e EnvVar) GetCacheKey() string {
if e.ValueFrom == nil {
return e.Name
}
if e.ValueFrom.SecretKeyRef != nil {

return e.Name + e.ValueFrom.SecretKeyRef.Name + e.ValueFrom.SecretKeyRef.Key
}
if e.ValueFrom.ConfigMapKeyRef != nil {
return e.Name + e.ValueFrom.ConfigMapKeyRef.Name + e.ValueFrom.ConfigMapKeyRef.Key
}
return e.Name
}

func (e EnvVar) IsEmpty() bool {
return e.Value == "" && e.ValueFrom == nil
}
Expand Down

0 comments on commit 2d8fd48

Please sign in to comment.