@@ -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
323338func (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+
337384func (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 {
0 commit comments