-
Notifications
You must be signed in to change notification settings - Fork 84
/
env.go
232 lines (192 loc) · 7.21 KB
/
env.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright 2020 The Compass Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package env
import (
"context"
"errors"
"fmt"
"os"
"reflect"
"strings"
"github.com/kyma-incubator/compass/components/director/pkg/log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cast"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// File describes the name, path and the format of the file to be used to load the configuration in the env
type File struct {
Name string `description:"name of the configuration file"`
Location string `description:"location of the configuration file"`
Format string `description:"extension of the configuration file"`
}
// DefaultConfigFile holds the default System Broker config file properties
func DefaultConfigFile() File {
return File{
Name: "application",
Location: ".",
Format: "yml",
}
}
// CreatePFlagsForConfigFile creates pflags for setting the configuration file
func CreatePFlagsForConfigFile(set *pflag.FlagSet) {
CreatePFlags(set, struct{ File File }{File: DefaultConfigFile()})
}
// Environment represents an abstraction over the env from which System Broker configuration will be loaded
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Environment
type Environment interface {
Get(key string) interface{}
Set(key string, value interface{})
Unmarshal(value interface{}) error
BindPFlag(key string, flag *pflag.Flag) error
AllSettings() map[string]interface{}
}
// ViperEnv represents an implementation of the Environment interface that uses viper
type ViperEnv struct {
*viper.Viper
}
// EmptyFlagSet creates an empty flag set and adds the default set of flags to it
func EmptyFlagSet() *pflag.FlagSet {
set := pflag.NewFlagSet("Configuration Flags", pflag.ExitOnError)
set.AddFlagSet(pflag.CommandLine)
return set
}
// CreatePFlags Creates pflags for the value structure and adds them in the provided set
func CreatePFlags(set *pflag.FlagSet, value interface{}) {
parameters, descriptions := buildParametersAndDescriptions(value)
for i, parameter := range parameters {
if set.Lookup(parameter.Name) == nil {
switch val := parameter.DefaultValue.(type) {
case []string:
set.StringSlice(parameter.Name, val, descriptions[i])
default:
set.Var(&flag{value: val}, parameter.Name, descriptions[i])
}
}
}
}
// New creates a new environment. It accepts a flag set that should contain all the flags that the
// environment should be aware of.
func New(ctx context.Context, set *pflag.FlagSet, onConfigChangeHandlers ...func(env Environment) func(event fsnotify.Event)) (*ViperEnv, error) {
v := &ViperEnv{
Viper: viper.New(),
}
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
if err := set.Parse(os.Args[1:]); err != nil {
return nil, err
}
set.VisitAll(func(flag *pflag.Flag) {
if err := v.BindPFlag(flag.Name, flag); err != nil {
log.D().Panic(err)
}
})
if err := v.setupConfigFile(ctx, onConfigChangeHandlers...); err != nil {
return nil, err
}
return v, nil
}
func (v *ViperEnv) AllSettings() map[string]interface{} {
return v.Viper.AllSettings()
}
// Unmarshal exposes viper's Unmarshal. Prior to unmarshaling it creates the necessary env var bindings
// so that env var values are also used during the unmarshaling in case the keys are not specified as pflags or in config file.
func (v *ViperEnv) Unmarshal(value interface{}) error {
parameters := buildParameters(value)
for _, parameter := range parameters {
// These bindings are required in case the value for a particular configuration property that is a field of the specified value struct
// is only set via env variables (if we do not explicitly do a binding, viper.AllKeys() will not return a key for this configuration
// property and unmarshal will not even try to find a value for this property key when filling up the config struct)
if err := v.Viper.BindEnv(parameter.Name); err != nil {
return err
}
}
return v.Viper.Unmarshal(value)
}
func (v *ViperEnv) setupConfigFile(ctx context.Context, onConfigChangeHandlers ...func(env Environment) func(op fsnotify.Event)) error {
cfg := struct{ File File }{File: File{}}
if err := v.Unmarshal(&cfg); err != nil {
return fmt.Errorf("could not find configuration cfg: %s", err)
}
v.Viper.AddConfigPath(cfg.File.Location)
v.Viper.SetConfigName(cfg.File.Name)
v.Viper.SetConfigType(cfg.File.Format)
if err := v.Viper.ReadInConfig(); err != nil {
if err, ok := err.(viper.ConfigFileNotFoundError); ok {
log.D().Info("Config File was not found: ", err)
return nil
}
return fmt.Errorf("could not read configuration cfg: %s", err)
}
v.Viper.WatchConfig()
dynamicLogHandler := func(env Environment) func(event fsnotify.Event) {
return func(event fsnotify.Event) {
if strings.Contains(event.String(), "WRITE") || strings.Contains(event.String(), "CREATE") {
logLevel, lok := env.Get("log.level").(string)
logFormat, fok := env.Get("log.format").(string)
logOutput, ook := env.Get("log.output").(string)
if lok || fok || ook {
log.C(ctx).WithError(errors.New("conversion failed")).Errorf("Failed to convert environment variables")
}
bootstrapCorrelationID := log.Configuration().BootstrapCorrelationID
log.C(ctx).Warnf("Reconfiguring logrus logging using level %s and format %s", logLevel, logFormat)
newCtx, err := log.Configure(ctx, &log.Config{
Level: logLevel,
Format: logFormat,
Output: logOutput,
BootstrapCorrelationID: bootstrapCorrelationID,
})
if err != nil {
log.C(ctx).WithError(err).Errorf("Could not set log level to %s and log format to %s after config file modification event of type %s: %v", logLevel, logFormat, event.String(), err)
}
ctx = newCtx
}
}
}
onConfigChangeHandlers = append(onConfigChangeHandlers, dynamicLogHandler)
v.Viper.OnConfigChange(func(event fsnotify.Event) {
log.C(ctx).Warnf("configuration file was changed by event %s. Triggering on config changed handlers...", event.String())
for _, handler := range onConfigChangeHandlers {
handler(v)(event)
}
})
return nil
}
// Default creates a default environment that can be used to boot up a System Broker
func Default(ctx context.Context, additionalPFlags ...func(set *pflag.FlagSet)) (Environment, error) {
set := EmptyFlagSet()
for _, addFlags := range additionalPFlags {
addFlags(set)
}
environment, err := New(ctx, set)
if err != nil {
return nil, fmt.Errorf("error loading environment: %s", err)
}
return environment, nil
}
type flag struct {
value interface{}
}
func (f *flag) String() string {
return cast.ToString(f.value)
}
func (f *flag) Set(s string) error {
f.value = s
return nil
}
func (f *flag) Type() string {
return reflect.TypeOf(f.value).Name()
}