fix: cache ttl interpolation
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good

This commit is contained in:
wpetit 2025-03-18 12:20:00 +01:00
parent 1af7248a6f
commit cc5cdcea96
6 changed files with 71 additions and 17 deletions

View File

@ -32,6 +32,8 @@ func RunCommand() *cli.Command {
logger.SetFormat(logger.Format(conf.Logger.Format))
logger.SetLevel(logger.Level(conf.Logger.Level))
logger.Debug(ctx.Context, "using config", logger.F("config", conf))
projectVersion := ctx.String("projectVersion")
if conf.Proxy.Sentry.DSN != "" {

View File

@ -29,6 +29,8 @@ func RunCommand() *cli.Command {
logger.SetFormat(logger.Format(conf.Logger.Format))
logger.SetLevel(logger.Level(conf.Logger.Level))
logger.Debug(ctx.Context, "using config", logger.F("config", conf))
projectVersion := ctx.String("projectVersion")
if conf.Proxy.Sentry.DSN != "" {
@ -49,7 +51,7 @@ func RunCommand() *cli.Command {
proxy.WithServerConfig(conf.Proxy),
proxy.WithRedisConfig(conf.Redis),
proxy.WithDirectorLayers(layers...),
proxy.WithDirectorCacheTTL(time.Duration(conf.Proxy.Cache.TTL)),
proxy.WithDirectorCacheTTL(time.Duration(*conf.Proxy.Cache.TTL)),
)
addrs, srvErrs := srv.Start(ctx.Context)

View File

@ -19,7 +19,7 @@ func (is *InterpolatedString) UnmarshalYAML(value *yaml.Node) error {
return errors.WithStack(err)
}
str, err := envsubst.EvalEnv(str)
str, err := envsubst.Eval(str, getEnv)
if err != nil {
return errors.WithStack(err)
}
@ -38,7 +38,7 @@ func (ii *InterpolatedInt) UnmarshalYAML(value *yaml.Node) error {
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
}
str, err := envsubst.EvalEnv(str)
str, err := envsubst.Eval(str, getEnv)
if err != nil {
return errors.WithStack(err)
}
@ -62,7 +62,7 @@ func (ifl *InterpolatedFloat) UnmarshalYAML(value *yaml.Node) error {
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
}
str, err := envsubst.EvalEnv(str)
str, err := envsubst.Eval(str, getEnv)
if err != nil {
return errors.WithStack(err)
}
@ -86,7 +86,7 @@ func (ib *InterpolatedBool) UnmarshalYAML(value *yaml.Node) error {
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
}
str, err := envsubst.EvalEnv(str)
str, err := envsubst.Eval(str, getEnv)
if err != nil {
return errors.WithStack(err)
}
@ -101,9 +101,10 @@ func (ib *InterpolatedBool) UnmarshalYAML(value *yaml.Node) error {
return nil
}
var getEnv = os.Getenv
type InterpolatedMap struct {
Data map[string]any
getEnv func(string) string
Data map[string]any
}
func (im *InterpolatedMap) UnmarshalYAML(value *yaml.Node) error {
@ -113,10 +114,6 @@ func (im *InterpolatedMap) UnmarshalYAML(value *yaml.Node) error {
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into map", value.Value, value.Line)
}
if im.getEnv == nil {
im.getEnv = os.Getenv
}
interpolated, err := im.interpolateRecursive(data)
if err != nil {
return errors.WithStack(err)
@ -140,7 +137,7 @@ func (im InterpolatedMap) interpolateRecursive(data any) (any, error) {
}
case string:
value, err := envsubst.Eval(typ, im.getEnv)
value, err := envsubst.Eval(typ, getEnv)
if err != nil {
return nil, errors.WithStack(err)
}
@ -171,7 +168,7 @@ func (iss *InterpolatedStringSlice) UnmarshalYAML(value *yaml.Node) error {
}
for index, value := range data {
value, err := envsubst.EvalEnv(value)
value, err := envsubst.Eval(value, getEnv)
if err != nil {
return errors.WithStack(err)
}
@ -193,7 +190,7 @@ func (id *InterpolatedDuration) UnmarshalYAML(value *yaml.Node) error {
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
}
str, err := envsubst.EvalEnv(str)
str, err := envsubst.Eval(str, getEnv)
if err != nil {
return errors.WithStack(err)
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"testing"
"time"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
@ -65,7 +66,7 @@ func TestInterpolatedMap(t *testing.T) {
var interpolatedMap InterpolatedMap
if tc.Env != nil {
interpolatedMap.getEnv = func(key string) string {
getEnv = func(key string) string {
return tc.Env[key]
}
}
@ -80,3 +81,54 @@ func TestInterpolatedMap(t *testing.T) {
})
}
}
func TestInterpolatedDuration(t *testing.T) {
type testCase struct {
Path string
Env map[string]string
Assert func(t *testing.T, parsed *InterpolatedDuration)
}
testCases := []testCase{
{
Path: "testdata/environment/interpolated-duration.yml",
Env: map[string]string{
"MY_DURATION": "30s",
},
Assert: func(t *testing.T, parsed *InterpolatedDuration) {
if e, g := 30*time.Second, parsed; e != time.Duration(*g) {
t.Errorf("parsed: expected '%v', got '%v'", e, g)
}
},
},
}
for idx, tc := range testCases {
t.Run(fmt.Sprintf("Case #%d", idx), func(t *testing.T) {
data, err := os.ReadFile(tc.Path)
if err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
if tc.Env != nil {
getEnv = func(key string) string {
return tc.Env[key]
}
}
config := struct {
Duration *InterpolatedDuration `yaml:"duration"`
}{
Duration: NewInterpolatedDuration(-1),
}
if err := yaml.Unmarshal(data, &config); err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
if tc.Assert != nil {
tc.Assert(t, config.Duration)
}
})
}
}

View File

@ -113,12 +113,12 @@ func NewDefaultDialConfig() DialConfig {
}
type CacheConfig struct {
TTL InterpolatedDuration `yaml:"ttl"`
TTL *InterpolatedDuration `yaml:"ttl"`
}
func NewDefaultCacheConfig() CacheConfig {
return CacheConfig{
TTL: *NewInterpolatedDuration(time.Second * 30),
TTL: NewInterpolatedDuration(time.Second * 30),
}
}

View File

@ -0,0 +1 @@
duration: ${MY_DURATION}