This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
config.go
85 lines (75 loc) · 2.84 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package config
import (
"fmt"
"io/ioutil"
"os"
"github.com/asaskevich/govalidator"
"github.com/ghodss/yaml"
"github.com/imdario/mergo"
"github.com/kyma-project/helm-broker/internal/platform/logger"
"github.com/kyma-project/helm-broker/internal/storage"
defaults "github.com/mcuadros/go-defaults"
"github.com/pkg/errors"
"github.com/vrischmann/envconfig"
)
// Config provide helm broker configuration
// Supported tags:
// - json: github.com/ghodss/yaml
// - envconfig: github.com/vrischmann/envconfig
// - default: github.com/mcuadros/go-defaults
// - valid github.com/asaskevich/govalidator
// Example of valid tag: `valid:"alphanum,required"`
// Combining many tags: tags have to be separated by WHITESPACE: `json:"port" default:"8080" valid:"required"`
type Config struct {
Logger logger.Config
KubeconfigPath string `envconfig:"optional"`
// TmpDir defines temporary directory path where addons .tgz files will be extracted
TmpDir string
Port int `default:"8070"`
StatusPort int `default:"8071"`
MetricsPort int `default:"8072"`
Storage []storage.Config `valid:"required"`
HelmDriver string `default:"secrets"`
}
// Load method has following strategy:
// 1. Check env variable 'APP_CONFIG_FILE_NAME', if exists load configuration from specified file
// 2. Read configuration from environment variables (will override configuration from file)
// 3. Apply defaults
// 4. Validate
func Load(verbose bool) (*Config, error) {
outCfg := Config{}
cfgFile := os.Getenv("APP_CONFIG_FILE_NAME")
if cfgFile != "" {
b, err := ioutil.ReadFile(cfgFile)
if err != nil {
return nil, errors.Wrapf(err, "while opening config file [%s]", cfgFile)
}
fileConfig := Config{}
if err := yaml.Unmarshal(b, &fileConfig); err != nil {
return nil, errors.Wrap(err, "while unmarshalling config from file")
}
outCfg = fileConfig
// fmt.Printf used, because logger will be created after reading configuration
if verbose {
fmt.Printf("Config after applying values from file: %+v\n", outCfg)
}
}
envConf := Config{}
if err := envconfig.InitWithOptions(&envConf, envconfig.Options{Prefix: "APP", AllOptional: true, AllowUnexported: true}); err != nil {
return nil, errors.Wrap(err, "while reading configuration from environment variables")
}
if err := mergo.Merge(&outCfg, &envConf, mergo.WithOverride); err != nil {
return nil, errors.Wrap(err, "while merging config from environment variables")
}
if verbose {
fmt.Printf("Config after applying values from environment variables: %+v\n", outCfg)
}
defaults.SetDefaults(&outCfg)
if verbose {
fmt.Printf("Config after applying defaults: %+v\n", outCfg)
}
if _, err := govalidator.ValidateStruct(outCfg); err != nil {
return nil, errors.Wrap(err, "while validating configuration object")
}
return &outCfg, nil
}