Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d6335e5

Browse files
committedJan 15, 2025·
Load config from both env and yaml
1 parent 5713379 commit d6335e5

File tree

6 files changed

+52
-24
lines changed

6 files changed

+52
-24
lines changed
 

‎cmd/icinga-kubernetes/main.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ const expectedSchemaVersion = "0.2.0"
4848
func main() {
4949
runtime.ReallyCrash = true
5050

51-
var configLocation string
51+
var glue daemon.ConfigFlagGlue
5252
var showVersion bool
5353
var clusterName string
5454

5555
klog.InitFlags(nil)
5656
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
5757

5858
pflag.BoolVar(&showVersion, "version", false, "print version and exit")
59-
pflag.StringVar(&configLocation, "config", "./config.yml", "path to the config file")
59+
pflag.StringVar(&glue.Config, "config", daemon.DefaultConfigPath, "path to the config file")
6060
pflag.StringVar(&clusterName, "cluster-name", "", "name of the current cluster")
6161

6262
loadingRules := kclientcmd.NewDefaultClientConfigLoadingRules()
@@ -97,8 +97,11 @@ func main() {
9797
log := klog.NewKlogr()
9898

9999
var cfg daemon.Config
100-
err = config.FromYAMLFile(configLocation, &cfg)
101-
if err != nil {
100+
101+
if err = config.Load(&cfg, config.LoadOptions{
102+
Flags: glue,
103+
EnvOptions: config.EnvOptions{Prefix: "IFK_"},
104+
}); err != nil {
102105
klog.Fatal(errors.Wrap(err, "can't create configuration"))
103106
}
104107

‎go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/go-logr/logr v1.4.2
88
github.com/go-sql-driver/mysql v1.8.1
99
github.com/google/uuid v1.6.0
10-
github.com/icinga/icinga-go-library v0.3.2-0.20241118194934-1a19cd696d37
10+
github.com/icinga/icinga-go-library v0.5.1-0.20250115065315-113387df7fbe
1111
github.com/jmoiron/sqlx v1.4.0
1212
github.com/lib/pq v1.10.9
1313
github.com/okzk/sdnotify v0.0.0-20240725214427-1c1fdd37c5ac
@@ -17,7 +17,7 @@ require (
1717
github.com/spf13/pflag v1.0.5
1818
go.uber.org/zap v1.27.0
1919
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
20-
golang.org/x/sync v0.9.0
20+
golang.org/x/sync v0.10.0
2121
k8s.io/api v0.31.1
2222
k8s.io/apimachinery v0.31.1
2323
k8s.io/client-go v0.31.1
@@ -27,7 +27,7 @@ require (
2727

2828
require (
2929
filippo.io/edwards25519 v1.1.0 // indirect
30-
github.com/caarlos0/env/v11 v11.2.2 // indirect
30+
github.com/caarlos0/env/v11 v11.3.1 // indirect
3131
github.com/creasty/defaults v1.8.0 // indirect
3232
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3333
github.com/emicklei/go-restful/v3 v3.12.1 // indirect

‎go.sum

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
22
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
33
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
44
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
5-
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
6-
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
5+
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
6+
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
77
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
88
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
99
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -60,8 +60,8 @@ github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73
6060
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6161
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6262
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
63-
github.com/icinga/icinga-go-library v0.3.2-0.20241118194934-1a19cd696d37 h1:b1xWtyFSPlCBTgP7sajx4fb+zecPh0LOXnXsJ7n3r9o=
64-
github.com/icinga/icinga-go-library v0.3.2-0.20241118194934-1a19cd696d37/go.mod h1:zBVLixVxt+FIxOct/DDTBzbu02BlvPZ0Mhcq8t6ErxE=
63+
github.com/icinga/icinga-go-library v0.5.1-0.20250115065315-113387df7fbe h1:oVsEcC7QGKGhiWapuKqC9kWLoIRoGOoicfqeum8ZMh8=
64+
github.com/icinga/icinga-go-library v0.5.1-0.20250115065315-113387df7fbe/go.mod h1:0PTW/N+Xio77P0QnxxMQeqRrETsJ8cVoFs88V4nDA3M=
6565
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
6666
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
6767
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
@@ -144,8 +144,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
144144
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
145145
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
146146
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
147-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
148-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
147+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
148+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
149149
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
150150
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
151151
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -179,8 +179,8 @@ golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbht
179179
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
180180
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
181181
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
182-
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
183-
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
182+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
183+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
184184
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
185185
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
186186
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

‎pkg/daemon/config.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import (
77
"github.com/icinga/icinga-kubernetes/pkg/notifications"
88
)
99

10+
// DefaultConfigPath specifies the default location of Icinga DB's config.yml for package installations.
11+
// const DefaultConfigPath = "/etc/icinga-kubernetes/config.yml"
12+
const DefaultConfigPath = "./config.yml"
13+
1014
// Config defines Icinga Kubernetes config.
1115
type Config struct {
12-
Database database.Config `yaml:"database"`
13-
Logging logging.Config `yaml:"logging"`
14-
Notifications notifications.Config `yaml:"notifications"`
15-
Prometheus metrics.PrometheusConfig `yaml:"prometheus"`
16+
Database database.Config `yaml:"database" envPrefix:"DATABASE_"`
17+
Logging logging.Config `yaml:"logging" envPrefix:"LOGGING_"`
18+
Notifications notifications.Config `yaml:"notifications" envPrefix:"NOTIFICATIONS_"`
19+
Prometheus metrics.PrometheusConfig `yaml:"prometheus" envPrefix:"PROMETHEUS_"`
1620
}
1721

1822
// Validate checks constraints in the supplied configuration and returns an error if they are violated.
@@ -31,3 +35,24 @@ func (c *Config) Validate() error {
3135

3236
return c.Notifications.Validate()
3337
}
38+
39+
// ConfigFlagGlue provides a glue struct for the CLI config flag.
40+
type ConfigFlagGlue struct {
41+
// Config is the path to the config file
42+
Config string
43+
}
44+
45+
// GetConfigPath retrieves the path to the configuration file.
46+
// It returns the path specified via the command line, or DefaultConfigPath if none is provided.
47+
func (f ConfigFlagGlue) GetConfigPath() string {
48+
if f.Config == "" {
49+
return DefaultConfigPath
50+
}
51+
52+
return f.Config
53+
}
54+
55+
// IsExplicitConfigPath indicates whether the configuration file path was explicitly set.
56+
func (f ConfigFlagGlue) IsExplicitConfigPath() bool {
57+
return f.Config != ""
58+
}

‎pkg/metrics/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package metrics
22

33
// PrometheusConfig defines Prometheus configuration.
44
type PrometheusConfig struct {
5-
Url string `yaml:"url"`
5+
Url string `yaml:"url" env:"URL"`
66
}
77

88
// Validate checks constraints in the supplied Prometheus configuration and returns an error if they are violated.

‎pkg/notifications/config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88

99
type Config struct {
1010
// If URL is the empty string, notifications are disabled.
11-
Url string `yaml:"url"`
12-
Username string `yaml:"username"`
13-
Password string `yaml:"password"`
14-
KubernetesWebUrl string `yaml:"kubernetes_web_url" default:"http://localhost/icingaweb2/kubernetes"`
11+
Url string `yaml:"url" env:"URL"`
12+
Username string `yaml:"username" env:"USERNAME"`
13+
Password string `yaml:"password" env:"PASSWORD"`
14+
KubernetesWebUrl string `yaml:"kubernetes_web_url" env:"KUBERNETES_WEB_URL" default:"http://localhost/icingaweb2/kubernetes"`
1515
}
1616

1717
// Validate checks constraints in the supplied configuration and returns an error if they are violated.

0 commit comments

Comments
 (0)
Please sign in to comment.