This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
180 lines (168 loc) · 4.55 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
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
package app
import (
"crypto/rand"
"encoding"
"encoding/base64"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
const (
envPrefix = "CS_UI"
defaultSessionCookieName = "user_session"
defaultSessionKeyLength = 50
defaultAssetsDir = "./client/build"
// assumes ui is running in-cluster
DefaultPrometheusEndpoint = "http://prometheus:9090"
DefaultSessionExpiration = 72 * time.Hour
DefaultServerAddress = "127.0.0.1:8000"
)
var (
// order and priority of struct tags to check
tagNames = []string{"env", "toml", "yaml", "json"}
// sentinel types
durationType = reflect.TypeOf(time.Duration(0))
textUnmarshaler = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
)
type Config struct {
KubeConfig *rest.Config
Scheme *runtime.Scheme
Debug bool `env:"debug"`
InCluster bool `env:"in_cluster"`
PrometheusEndpoint string `env:"prometheus_endpoint"`
SessionCookieName string `env:"session_cookie_name"`
SessionKey string `env:"session_key"`
SessionExpiration time.Duration `env:"session_expiration"`
MarketplaceNoAppsMsg string `env:"marketplace_no_apps_msg"`
MarketplaceNoAppsLink string `env:"marketplace_no_apps_link"`
MarketplaceContact string `env:"marketplace_contact"`
DisablePProf bool `env:"disable_pprof"`
AssetsDir string `env:"assets_dir"`
Address string `env:"address"`
CertFile string `env:"cert_file"`
KeyFile string `env:"key_file"`
}
func (c *Config) applyDefaults() {
if c.SessionCookieName == "" {
c.SessionCookieName = defaultSessionCookieName
}
if c.SessionExpiration == 0 {
c.SessionExpiration = DefaultSessionExpiration
}
if c.AssetsDir == "" {
c.AssetsDir = defaultAssetsDir
}
if c.Address == "" {
c.Address = DefaultServerAddress
}
if len(c.SessionKey) == 0 {
b := make([]byte, defaultSessionKeyLength)
if _, err := rand.Read(b); err != nil {
// unlikely but fatal
panic(err)
}
c.SessionKey = base64.RawStdEncoding.EncodeToString(b)
}
if c.Scheme == nil {
c.Scheme = scheme.Scheme
}
}
func (c *Config) FromEnv() error {
return fromEnv(reflect.ValueOf(c), envPrefix)
}
func joinNonEmpty(sep string, ss ...string) string {
result := ""
for _, s := range ss {
if s == "" {
continue
}
if result != "" {
result += sep
}
result += s
}
return result
}
func fromEnv(v reflect.Value, prefix string) error {
t := v.Type()
// special cases
if t == durationType {
if e := os.Getenv(prefix); e != "" {
d, err := time.ParseDuration(e)
if err != nil {
return errors.Wrapf(err, "failed to parse %q as %s", prefix, t)
}
v.Set(reflect.ValueOf(d))
}
return nil
} else if t.Implements(textUnmarshaler) {
if e := os.Getenv(prefix); e != "" {
return v.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(e))
}
return nil
}
switch t.Kind() {
case reflect.Ptr:
if e := v.Elem(); !e.IsValid() {
// skip nil pointers, but we could do this if we wanted to initialize:
//v.Set(reflect.New(t.Elem()))
return nil
}
return fromEnv(v.Elem(), prefix)
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
fv := v.Field(i)
if !fv.CanSet() {
continue
}
pre := prefix
if !ft.Anonymous {
n := ft.Name
for _, tagName := range tagNames {
if tag, ok := ft.Tag.Lookup(tagName); ok && tag != "-" {
n = strings.Split(tag, ",")[0]
break
}
}
pre = joinNonEmpty("_", pre, strings.ToUpper(n))
}
if err := fromEnv(fv, pre); err != nil {
return err
}
}
case reflect.String:
if e := os.Getenv(prefix); e != "" {
v.SetString(e)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if e := os.Getenv(prefix); e != "" {
n, err := strconv.ParseInt(e, 10, 64)
if err != nil {
return errors.Wrapf(err, "failed to parse %q as %s", prefix, t)
}
v.SetInt(n)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if e := os.Getenv(prefix); e != "" {
n, err := strconv.ParseUint(e, 10, 64)
if err != nil {
return errors.Wrapf(err, "failed to parse %q as %s", prefix, t)
}
v.SetUint(n)
}
case reflect.Bool:
if e := os.Getenv(prefix); e != "" {
e = strings.ToLower(e)
// idk man
v.SetBool(e != "f" && e != "false" && e != "no")
}
}
return nil
}