Skip to content

Commit 4537a0a

Browse files
committed
add config
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
1 parent 32fdccb commit 4537a0a

4 files changed

Lines changed: 158 additions & 6 deletions

File tree

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ module github.com/flant/addon-operator
33
go 1.22.8
44

55
require (
6+
github.com/caarlos0/env/v11 v11.2.2
67
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
7-
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e
8+
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241205040953-7b376bae249c
89
github.com/deckhouse/module-sdk v0.1.0
910
github.com/dominikbraun/graph v0.23.0
1011
github.com/ettle/strcase v0.2.0
1112
github.com/flant/kube-client v1.2.2
12-
github.com/flant/shell-operator v1.5.2
13+
github.com/flant/shell-operator v0.0.0-20241206091450-31a456b28f94
1314
github.com/go-chi/chi/v5 v5.1.0
1415
github.com/go-openapi/loads v0.19.5
1516
github.com/go-openapi/spec v0.19.8

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
5050
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
5151
github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70=
5252
github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
53+
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
54+
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
5355
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
5456
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
5557
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -87,8 +89,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
8789
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8890
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
8991
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
90-
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e h1:QUQy+5Bv7/UzhfrytiG3c5gfLGhPppepVbRpbMisVIw=
91-
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e/go.mod h1:Mk5HRzkc5pIcDIZ2JJ6DPuuqnwhXVkb3you8M8Mg+4w=
92+
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241205040953-7b376bae249c h1:dK30IW9uGg0DvSy+IcdQ6zwEBRV55R7tEtaruEKYkSA=
93+
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241205040953-7b376bae249c/go.mod h1:Mk5HRzkc5pIcDIZ2JJ6DPuuqnwhXVkb3you8M8Mg+4w=
9294
github.com/deckhouse/module-sdk v0.1.0 h1:YgMnr0vmjVLwIdQfTE1ytR2nPndO2SfQprcW45wsT34=
9395
github.com/deckhouse/module-sdk v0.1.0/go.mod h1:0gxlSr0WRrGnB82J+45FBIXNP/fKl8+yEvRTzrygiAE=
9496
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
@@ -138,8 +140,8 @@ github.com/flant/kube-client v1.2.2 h1:27LBs+PKJEFnkQXjPU9eIps7a7iyI13AKcSYj897D
138140
github.com/flant/kube-client v1.2.2/go.mod h1:eMa3aJ6V1PRWSQ/RCROkObDpY4S74uM84SJS4G/LINg=
139141
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee h1:evii83J+/6QGNvyf6tjQ/p27DPY9iftxIBb37ALJRTg=
140142
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee/go.mod h1:f+REaGl/+pZR97rbTcwHEka/MAipoQQ2Mc0iQUj4ak0=
141-
github.com/flant/shell-operator v1.5.2 h1:OPYveDCI1ZZDrtxORqarSIQIAcZEGW9ZUugCZtLmiZM=
142-
github.com/flant/shell-operator v1.5.2/go.mod h1:2vTLeyYLaq4Tp0NPilCHZy22rDGSS4uBsbPONj9YGRY=
143+
github.com/flant/shell-operator v0.0.0-20241206091450-31a456b28f94 h1:dTa+dI4RMnb9IsazX+saGxhzph4DumqOjHugTLThlzs=
144+
github.com/flant/shell-operator v0.0.0-20241206091450-31a456b28f94/go.mod h1:suTnsKwcUr4AhWbMz/RvfiN1L7bgovhXqtApqYka02k=
143145
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
144146
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
145147
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=

pkg/app/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func DefineStartCommandFlags(kpApp *kingpin.Application, cmd *kingpin.CmdClause)
170170
shapp.DefineJqFlags(cmd)
171171
shapp.DefineLoggingFlags(cmd)
172172

173+
// TODO: working with global values not need to be in flags definition
173174
shapp.DebugUnixSocket = DefaultDebugUnixSocket
174175
shapp.DefineDebugFlags(kpApp, cmd)
175176
}

pkg/app/app_config.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/caarlos0/env/v11"
7+
shapp "github.com/flant/shell-operator/pkg/app"
8+
)
9+
10+
type appConfig struct {
11+
TmpDir string `env:"TMP_DIR"`
12+
PrometheusMetricsPrefix string `env:"PROMETHEUS_METRICS_PREFIX"`
13+
}
14+
15+
func newAppConfig() *appConfig {
16+
return &appConfig{}
17+
}
18+
19+
type Config struct {
20+
SOp *shapp.Config `env:"-"`
21+
AppConfig *appConfig `envPrefix:"ADDON_OPERATOR_"`
22+
23+
ready bool
24+
}
25+
26+
func newConfig() *Config {
27+
return &Config{
28+
AppConfig: newAppConfig(),
29+
SOp: shapp.MustGetConfig(),
30+
}
31+
}
32+
33+
func (cfg *Config) Parse() error {
34+
if cfg.ready {
35+
return nil
36+
}
37+
38+
opts := env.Options{
39+
Prefix: "",
40+
}
41+
42+
err := env.ParseWithOptions(cfg, opts)
43+
if err != nil {
44+
return fmt.Errorf("failed to parse config: %w", err)
45+
}
46+
47+
return nil
48+
}
49+
50+
func (cfg *Config) InitConfig() {
51+
cfg.OverrideDefaults()
52+
cfg.SetupGlobalVars()
53+
54+
cfg.SetReady()
55+
}
56+
57+
func (cfg *Config) OverrideDefaults() {
58+
if cfg.IsReady() {
59+
return
60+
}
61+
62+
setIfNotEmpty(&shapp.TempDir, DefaultTempDir)
63+
setIfNotEmpty(&shapp.PrometheusMetricsPrefix, DefaultPrometheusMetricsPrefix)
64+
setIfNotEmpty(&shapp.DebugUnixSocket, DefaultDebugUnixSocket)
65+
}
66+
67+
func (cfg *Config) SetupGlobalVars() {
68+
if cfg.IsReady() {
69+
return
70+
}
71+
72+
cfg.SOp.SetupGlobalVars()
73+
74+
setIfNotEmpty(&shapp.TempDir, cfg.AppConfig.TmpDir)
75+
setIfNotEmpty(&shapp.PrometheusMetricsPrefix, cfg.AppConfig.PrometheusMetricsPrefix)
76+
}
77+
78+
func (cfg *Config) IsReady() bool {
79+
return cfg.ready
80+
}
81+
82+
func (cfg *Config) SetReady() {
83+
cfg.SOp.SetReady()
84+
cfg.ready = true
85+
}
86+
87+
var configInstance *Config
88+
89+
func MustGetConfig() *Config {
90+
cfg, err := GetConfig()
91+
if err != nil {
92+
panic(err)
93+
}
94+
95+
return cfg
96+
}
97+
98+
func MustGetAndInitConfig() *Config {
99+
cfg, err := GetAndInitConfig()
100+
if err != nil {
101+
panic(err)
102+
}
103+
104+
return cfg
105+
}
106+
107+
func GetConfig() (*Config, error) {
108+
if configInstance != nil {
109+
return configInstance, nil
110+
}
111+
112+
cfg := newConfig()
113+
err := cfg.Parse()
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
configInstance = cfg
119+
120+
return configInstance, nil
121+
}
122+
123+
func GetAndInitConfig() (*Config, error) {
124+
cfg, err := GetConfig()
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
cfg.InitConfig()
130+
131+
return cfg, nil
132+
}
133+
134+
func setIfNotEmpty[T comparable](v *T, env T) {
135+
if !isZero(env) {
136+
*v = env
137+
}
138+
}
139+
140+
func setSliceIfNotEmpty[T any](v *[]T, env []T) {
141+
if len(env) != 0 {
142+
*v = env
143+
}
144+
}
145+
146+
func isZero[T comparable](v T) bool {
147+
return v == *new(T)
148+
}

0 commit comments

Comments
 (0)