Documentation
¶
Overview ¶
Package config provides support for loading configuration from various sources into a struct. It is a thin wrapper around Koanf.
This is designed for use with microservices running in Kubernetes, but may work well elsewhere too. Configuration may come from the following sources:
- Environment variables, e.g. those added to a Pod's container directly or via a ConfigMap.
- JSON, YAML or TOML files, e.g. volume mounts on a Pod via a ConfigMap or Secret.
- Single value files, e.g. simple key-value pairs from a volume mount.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotStruct = errors.New("non-struct found")
Sentinel errors that can be returned by Encode.
Functions ¶
func Encode ¶ added in v0.3.26
Encode converts a configuration struct to a flattened map using the given delimiter for nested keys and all values as strings. It is intended for debugging only and is not guaranteed to produce an entirely accurate representation of the original configuration.
Values can be redacted with a JSON struct tag set to "-". For example:
type Example struct {
Password string `koanf:"password" json:"-"`
}
Encoding all primitive types is supported natively. If a type implements encoding.TextMarshaler or encoding/json.Marshaler, that result will be returned instead. Arrays, slices and maps cannot currently be encoded, but a placeholder message is returned instead of raising an error.
Private struct fields are ignored.
Types ¶
type Loader ¶ added in v0.3.21
type Loader struct {
// contains filtered or unexported fields
}
Loader provides loading of configuration values for a service into a struct.
func (*Loader) Load ¶ added in v0.3.21
Load reads configuration, using it to populate the given struct pointer.
Configuration is first loaded from environment variables, then files, and finally Kubernetes mounts. The last loaded configuration value wins if any conflicts occur.
The struct should contain fields with "koanf" tags identifying the configuration key to assign. For example:
type LoggingConfig struct {
// reads "level" into the field
Level string `koanf:"level"`
}
The field type can be anything supported by mapstructure, or an implementation of encoding.TextUnmarshaler. If configuration keys contain ".", then nested structs must be used to access the value. For example:
type Config struct {
Log struct{
// reads "log.level" into the field
Level string `koanf:"level"`
} `koanf:"log"`
}
A default value can be set using a "default" struct tag with the desired value. For example:
type LoggingConfig struct {
Level string `koanf:"level" default:"info"`
}
A default-able field type can be anything supported by defaults, or an implementation of defaults.Setter or encoding.TextUnmarshaler. An invalid default value will be skipped and will not cause an error to be returned.
type Options ¶
type Options struct {
// The prefix of environment variables to read configuration from. Matching environment
// variables have the prefix removed, are converted to lowercase and any underscores ("_") are
// replaced with periods ("."). For example, "APP_LOG_LEVEL" with a prefix of "APP_" would
// become "log.level".
EnvPrefix string
// The file paths to read configuration from. Supported file formats and extensions are:
//
// - JSON: .json
// - YAML: .yml, .yaml
// - TOML: .toml
//
// The file content is read into a map where nested map keys are joined using ".". For example,
// a JSON file containing {"log":{"level":"info"}} would become "log.level".
Files []string
// The directories of Kubernetes volume mounts to read configuration from. The filenames of the
// mounted values become the configuration keys. Any underscores ("_") in the key are replaced
// with periods ("."). For example, a configmap field of "log_level" would become "log.level".
Mounts []string
}
Options provides the values to bootstrap configuration loading.
This struct is intended to be populated on startup based on environment variables or CLI options. Support for this is provided by the flagoptions package which is based on the flag package. However, this can trivially be replaced if an alternative library is desired instead.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package flagoptions implements config.Options using the standard library flag package.
|
Package flagoptions implements config.Options using the standard library flag package. |
|
providers
|
|
|
k8smount
Package k8smount contains a Koanf provider for loading configuration from Kubernetes volume mounts, i.e.
|
Package k8smount contains a Koanf provider for loading configuration from Kubernetes volume mounts, i.e. |
|
Package splitter provides unmarshalers for Koanf config structs to convert strings to string slices using a particular delimiter.
|
Package splitter provides unmarshalers for Koanf config structs to convert strings to string slices using a particular delimiter. |