2014-12-17 23:18:58 +01:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2015-08-17 22:41:50 +02:00
|
|
|
"sort"
|
2014-12-17 23:18:58 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-23 21:45:04 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-01-16 20:32:15 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
2014-12-17 23:18:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Version is a major/minor version pair of the form Major.Minor
|
|
|
|
// Major version upgrades indicate structure or type changes
|
|
|
|
// Minor version upgrades should be strictly additive
|
|
|
|
type Version string
|
|
|
|
|
|
|
|
// MajorMinorVersion constructs a Version from its Major and Minor components
|
|
|
|
func MajorMinorVersion(major, minor uint) Version {
|
|
|
|
return Version(fmt.Sprintf("%d.%d", major, minor))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (version Version) major() (uint, error) {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 20:32:03 +01:00
|
|
|
majorPart, _, _ := strings.Cut(string(version), ".")
|
2014-12-17 23:18:58 +01:00
|
|
|
major, err := strconv.ParseUint(majorPart, 10, 0)
|
|
|
|
return uint(major), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Major returns the major version portion of a Version
|
|
|
|
func (version Version) Major() uint {
|
|
|
|
major, _ := version.major()
|
|
|
|
return major
|
|
|
|
}
|
|
|
|
|
|
|
|
func (version Version) minor() (uint, error) {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 20:32:03 +01:00
|
|
|
_, minorPart, _ := strings.Cut(string(version), ".")
|
2014-12-17 23:18:58 +01:00
|
|
|
minor, err := strconv.ParseUint(minorPart, 10, 0)
|
|
|
|
return uint(minor), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minor returns the minor version portion of a Version
|
|
|
|
func (version Version) Minor() uint {
|
|
|
|
minor, _ := version.minor()
|
|
|
|
return minor
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionedParseInfo defines how a specific version of a configuration should
|
|
|
|
// be parsed into the current version
|
|
|
|
type VersionedParseInfo struct {
|
|
|
|
// Version is the version which this parsing information relates to
|
|
|
|
Version Version
|
|
|
|
// ParseAs defines the type which a configuration file of this version
|
|
|
|
// should be parsed into
|
|
|
|
ParseAs reflect.Type
|
|
|
|
// ConversionFunc defines a method for converting the parsed configuration
|
|
|
|
// (of type ParseAs) into the current configuration version
|
|
|
|
// Note: this method signature is very unclear with the absence of generics
|
|
|
|
ConversionFunc func(interface{}) (interface{}, error)
|
|
|
|
}
|
|
|
|
|
2015-08-17 22:41:50 +02:00
|
|
|
type envVar struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
type envVars []envVar
|
|
|
|
|
|
|
|
func (a envVars) Len() int { return len(a) }
|
|
|
|
func (a envVars) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a envVars) Less(i, j int) bool { return a[i].name < a[j].name }
|
|
|
|
|
2014-12-17 23:18:58 +01:00
|
|
|
// Parser can be used to parse a configuration file and environment of a defined
|
|
|
|
// version into a unified output structure
|
|
|
|
type Parser struct {
|
|
|
|
prefix string
|
|
|
|
mapping map[Version]VersionedParseInfo
|
2015-08-17 22:41:50 +02:00
|
|
|
env envVars
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser returns a *Parser with the given environment prefix which handles
|
|
|
|
// versioned configurations which match the given parseInfos
|
|
|
|
func NewParser(prefix string, parseInfos []VersionedParseInfo) *Parser {
|
2015-08-17 22:41:50 +02:00
|
|
|
p := Parser{prefix: prefix, mapping: make(map[Version]VersionedParseInfo)}
|
2014-12-17 23:18:58 +01:00
|
|
|
|
|
|
|
for _, parseInfo := range parseInfos {
|
|
|
|
p.mapping[parseInfo.Version] = parseInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, env := range os.Environ() {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 20:32:03 +01:00
|
|
|
k, v, _ := strings.Cut(env, "=")
|
|
|
|
p.env = append(p.env, envVar{k, v})
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
|
|
|
|
2015-08-17 22:41:50 +02:00
|
|
|
// We must sort the environment variables lexically by name so that
|
|
|
|
// more specific variables are applied before less specific ones
|
|
|
|
// (i.e. REGISTRY_STORAGE before
|
|
|
|
// REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY). This sucks, but it's a
|
|
|
|
// lot simpler and easier to get right than unmarshalling map entries
|
|
|
|
// into temporaries and merging with the existing entry.
|
|
|
|
sort.Sort(p.env)
|
|
|
|
|
2014-12-17 23:18:58 +01:00
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse reads in the given []byte and environment and writes the resulting
|
|
|
|
// configuration into the input v
|
|
|
|
//
|
|
|
|
// Environment variables may be used to override configuration parameters other
|
|
|
|
// than version, following the scheme below:
|
|
|
|
// v.Abc may be replaced by the value of PREFIX_ABC,
|
|
|
|
// v.Abc.Xyz may be replaced by the value of PREFIX_ABC_XYZ, and so forth
|
|
|
|
func (p *Parser) Parse(in []byte, v interface{}) error {
|
|
|
|
var versionedStruct struct {
|
|
|
|
Version Version
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(in, &versionedStruct); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parseInfo, ok := p.mapping[versionedStruct.Version]
|
|
|
|
if !ok {
|
2019-02-05 01:01:04 +01:00
|
|
|
return fmt.Errorf("unsupported version: %q", versionedStruct.Version)
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
parseAs := reflect.New(parseInfo.ParseAs)
|
|
|
|
err := yaml.Unmarshal(in, parseAs.Interface())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-17 22:41:50 +02:00
|
|
|
for _, envVar := range p.env {
|
|
|
|
pathStr := envVar.name
|
|
|
|
if strings.HasPrefix(pathStr, strings.ToUpper(p.prefix)+"_") {
|
|
|
|
path := strings.Split(pathStr, "_")
|
|
|
|
|
|
|
|
err = p.overwriteFields(parseAs, pathStr, path[1:], envVar.value)
|
|
|
|
if err != nil {
|
2022-07-05 18:57:12 +02:00
|
|
|
return fmt.Errorf("parsing environment variable %s: %v", pathStr, err)
|
2015-08-17 22:41:50 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := parseInfo.ConversionFunc(parseAs.Interface())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reflect.ValueOf(v).Elem().Set(reflect.Indirect(reflect.ValueOf(c)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-17 22:41:50 +02:00
|
|
|
// overwriteFields replaces configuration values with alternate values specified
|
|
|
|
// through the environment. Precondition: an empty path slice must never be
|
|
|
|
// passed in.
|
|
|
|
func (p *Parser) overwriteFields(v reflect.Value, fullpath string, path []string, payload string) error {
|
2014-12-17 23:18:58 +01:00
|
|
|
for v.Kind() == reflect.Ptr {
|
2015-08-17 22:41:50 +02:00
|
|
|
if v.IsNil() {
|
|
|
|
panic("encountered nil pointer while handling environment variable " + fullpath)
|
|
|
|
}
|
2014-12-17 23:18:58 +01:00
|
|
|
v = reflect.Indirect(v)
|
|
|
|
}
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Struct:
|
2015-08-17 22:41:50 +02:00
|
|
|
return p.overwriteStruct(v, fullpath, path, payload)
|
|
|
|
case reflect.Map:
|
|
|
|
return p.overwriteMap(v, fullpath, path, payload)
|
|
|
|
case reflect.Interface:
|
|
|
|
if v.NumMethod() == 0 {
|
|
|
|
if !v.IsNil() {
|
|
|
|
return p.overwriteFields(v.Elem(), fullpath, path, payload)
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
2015-08-17 22:41:50 +02:00
|
|
|
// Interface was empty; create an implicit map
|
|
|
|
var template map[string]interface{}
|
|
|
|
wrappedV := reflect.MakeMap(reflect.TypeOf(template))
|
|
|
|
v.Set(wrappedV)
|
|
|
|
return p.overwriteMap(wrappedV, fullpath, path, payload)
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-17 22:41:50 +02:00
|
|
|
func (p *Parser) overwriteStruct(v reflect.Value, fullpath string, path []string, payload string) error {
|
|
|
|
// Generate case-insensitive map of struct fields
|
|
|
|
byUpperCase := make(map[string]int)
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
sf := v.Type().Field(i)
|
|
|
|
upper := strings.ToUpper(sf.Name)
|
|
|
|
if _, present := byUpperCase[upper]; present {
|
|
|
|
panic(fmt.Sprintf("field name collision in configuration object: %s", sf.Name))
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
2015-08-17 22:41:50 +02:00
|
|
|
byUpperCase[upper] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldIndex, present := byUpperCase[path[0]]
|
|
|
|
if !present {
|
|
|
|
logrus.Warnf("Ignoring unrecognized environment variable %s", fullpath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
field := v.Field(fieldIndex)
|
|
|
|
sf := v.Type().Field(fieldIndex)
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
// Env var specifies this field directly
|
|
|
|
fieldVal := reflect.New(sf.Type)
|
|
|
|
err := yaml.Unmarshal([]byte(payload), fieldVal.Interface())
|
2014-12-17 23:18:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-17 22:41:50 +02:00
|
|
|
field.Set(reflect.Indirect(fieldVal))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the field is nil, must create an object
|
|
|
|
switch sf.Type.Kind() {
|
2014-12-17 23:18:58 +01:00
|
|
|
case reflect.Map:
|
2015-08-17 22:41:50 +02:00
|
|
|
if field.IsNil() {
|
|
|
|
field.Set(reflect.MakeMap(sf.Type))
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
if field.IsNil() {
|
2017-11-14 15:16:02 +01:00
|
|
|
field.Set(reflect.New(field.Type().Elem()))
|
2015-08-17 22:41:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.overwriteFields(field, fullpath, path[1:], payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) overwriteMap(m reflect.Value, fullpath string, path []string, payload string) error {
|
|
|
|
if m.Type().Key().Kind() != reflect.String {
|
|
|
|
// non-string keys unsupported
|
|
|
|
logrus.Warnf("Ignoring environment variable %s involving map with non-string keys", fullpath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(path) > 1 {
|
|
|
|
// If a matching key exists, get its value and continue the
|
|
|
|
// overwriting process.
|
2014-12-17 23:18:58 +01:00
|
|
|
for _, k := range m.MapKeys() {
|
2015-08-17 22:41:50 +02:00
|
|
|
if strings.ToUpper(k.String()) == path[0] {
|
|
|
|
mapValue := m.MapIndex(k)
|
|
|
|
// If the existing value is nil, we want to
|
|
|
|
// recreate it instead of using this value.
|
|
|
|
if (mapValue.Kind() == reflect.Ptr ||
|
|
|
|
mapValue.Kind() == reflect.Interface ||
|
|
|
|
mapValue.Kind() == reflect.Map) &&
|
|
|
|
mapValue.IsNil() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return p.overwriteFields(mapValue, fullpath, path[1:], payload)
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-17 22:41:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// (Re)create this key
|
|
|
|
var mapValue reflect.Value
|
|
|
|
if m.Type().Elem().Kind() == reflect.Map {
|
|
|
|
mapValue = reflect.MakeMap(m.Type().Elem())
|
|
|
|
} else {
|
|
|
|
mapValue = reflect.New(m.Type().Elem())
|
|
|
|
}
|
|
|
|
if len(path) > 1 {
|
|
|
|
err := p.overwriteFields(mapValue, fullpath, path[1:], payload)
|
2014-12-17 23:18:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-17 22:41:50 +02:00
|
|
|
} else {
|
|
|
|
err := yaml.Unmarshal([]byte(payload), mapValue.Interface())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-17 23:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-17 22:41:50 +02:00
|
|
|
|
|
|
|
m.SetMapIndex(reflect.ValueOf(strings.ToLower(path[0])), reflect.Indirect(mapValue))
|
|
|
|
|
2014-12-17 23:18:58 +01:00
|
|
|
return nil
|
|
|
|
}
|