Skip to content

Commit

Permalink
Fix env override parsing for FailedJobsRetentionTime
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 committed Oct 12, 2023
1 parent ab0e524 commit 2f8657a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
5 changes: 3 additions & 2 deletions cmd/offloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/mattermost/calls-offloader/service"

"github.com/BurntSushi/toml"
"github.com/kelseyhightower/envconfig"
)

// loadConfig reads the config file and returns a new Config,
Expand All @@ -29,9 +28,11 @@ func loadConfig(path string) (service.Config, error) {
} else if _, err := toml.DecodeFile(path, &cfg); err != nil {
return cfg, fmt.Errorf("failed to decode config file: %w", err)
}
if err := envconfig.Process("", &cfg); err != nil {

if err := cfg.ParseFromEnv(); err != nil {
return cfg, err
}

return cfg, nil
}

Expand Down
21 changes: 18 additions & 3 deletions service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ package service

import (
"fmt"
"os"
"regexp"
"strconv"
"time"

"github.com/mattermost/calls-offloader/logger"
"github.com/mattermost/calls-offloader/service/api"
"github.com/mattermost/calls-offloader/service/auth"

"github.com/kelseyhightower/envconfig"
)

var (
Expand Down Expand Up @@ -71,14 +74,14 @@ func (c StoreConfig) IsValid() error {
type JobAPIType string

const (
JobAPITypeDocker = "docker"
JobAPITypeKubernetes = "kubernetes"
JobAPITypeDocker JobAPIType = "docker"
JobAPITypeKubernetes = "kubernetes"
)

type JobsConfig struct {
APIType JobAPIType `toml:"api_type"`
MaxConcurrentJobs int `toml:"max_concurrent_jobs"`
FailedJobsRetentionTime time.Duration `toml:"failed_jobs_retention_time"`
FailedJobsRetentionTime time.Duration `toml:"failed_jobs_retention_time" ignored:"true"`
}

// We need some custom parsing since duration doesn't support days.
Expand Down Expand Up @@ -170,6 +173,18 @@ type Config struct {
Logger logger.Config
}

func (c *Config) ParseFromEnv() error {
if val := os.Getenv("JOBS_FAILEDJOBSRETENTIONTIME"); val != "" {
d, err := parseRetentionTime(val)
if err != nil {
return fmt.Errorf("failed to parse FailedJobsRetentionTime: %w", err)
}
c.Jobs.FailedJobsRetentionTime = d
}

return envconfig.Process("", c)
}

func (c Config) IsValid() error {
if err := c.API.IsValid(); err != nil {
return err
Expand Down
31 changes: 31 additions & 0 deletions service/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package service

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -67,3 +68,33 @@ func TestParseRetentionTime(t *testing.T) {
})
}
}

func TestParseFromEnv(t *testing.T) {
t.Run("no env", func(t *testing.T) {
var cfg Config
err := cfg.ParseFromEnv()
require.NoError(t, err)
require.Empty(t, cfg)
})

t.Run("FailedJobsRetentionTime", func(t *testing.T) {
os.Setenv("JOBS_FAILEDJOBSRETENTIONTIME", "1d")
defer os.Unsetenv("JOBS_FAILEDJOBSRETENTIONTIME")

var cfg Config
err := cfg.ParseFromEnv()
require.NoError(t, err)
require.Equal(t, time.Hour*24, cfg.Jobs.FailedJobsRetentionTime)
})

t.Run("override", func(t *testing.T) {
var cfg Config
cfg.Jobs.APIType = JobAPITypeKubernetes

os.Setenv("JOBS_APITYPE", "docker")
defer os.Unsetenv("JOBS_APITYPE")
err := cfg.ParseFromEnv()
require.NoError(t, err)
require.Equal(t, JobAPITypeDocker, cfg.Jobs.APIType)
})
}

0 comments on commit 2f8657a

Please sign in to comment.