Skip to content

Commit

Permalink
Load config from both env and yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Jan 15, 2025
1 parent 5713379 commit 6d9ce31
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
11 changes: 7 additions & 4 deletions cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ const expectedSchemaVersion = "0.2.0"
func main() {
runtime.ReallyCrash = true

var configLocation string
var glue daemon.ConfigFlagGlue
var showVersion bool
var clusterName string

klog.InitFlags(nil)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

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

loadingRules := kclientcmd.NewDefaultClientConfigLoadingRules()
Expand Down Expand Up @@ -97,8 +97,11 @@ func main() {
log := klog.NewKlogr()

var cfg daemon.Config
err = config.FromYAMLFile(configLocation, &cfg)
if err != nil {

if err = config.Load(&cfg, config.LoadOptions{
Flags: glue,
EnvOptions: config.EnvOptions{Prefix: "IFK_"},
}); err != nil {
klog.Fatal(errors.Wrap(err, "can't create configuration"))
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-logr/logr v1.4.2
github.com/go-sql-driver/mysql v1.8.1
github.com/google/uuid v1.6.0
github.com/icinga/icinga-go-library v0.3.2-0.20241118194934-1a19cd696d37
github.com/icinga/icinga-go-library v0.5.1-0.20250115065315-113387df7fbe
github.com/jmoiron/sqlx v1.4.0
github.com/lib/pq v1.10.9
github.com/okzk/sdnotify v0.0.0-20240725214427-1c1fdd37c5ac
Expand All @@ -17,7 +17,7 @@ require (
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/sync v0.9.0
golang.org/x/sync v0.10.0
k8s.io/api v0.31.1
k8s.io/apimachinery v0.31.1
k8s.io/client-go v0.31.1
Expand All @@ -27,7 +27,7 @@ require (

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/caarlos0/env/v11 v11.2.2 // indirect
github.com/caarlos0/env/v11 v11.3.1 // indirect
github.com/creasty/defaults v1.8.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -62,6 +62,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/icinga/icinga-go-library v0.3.2-0.20241118194934-1a19cd696d37 h1:b1xWtyFSPlCBTgP7sajx4fb+zecPh0LOXnXsJ7n3r9o=
github.com/icinga/icinga-go-library v0.3.2-0.20241118194934-1a19cd696d37/go.mod h1:zBVLixVxt+FIxOct/DDTBzbu02BlvPZ0Mhcq8t6ErxE=
github.com/icinga/icinga-go-library v0.5.1-0.20250115065315-113387df7fbe h1:oVsEcC7QGKGhiWapuKqC9kWLoIRoGOoicfqeum8ZMh8=
github.com/icinga/icinga-go-library v0.5.1-0.20250115065315-113387df7fbe/go.mod h1:0PTW/N+Xio77P0QnxxMQeqRrETsJ8cVoFs88V4nDA3M=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
Expand Down Expand Up @@ -179,8 +181,8 @@ golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbht
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
33 changes: 29 additions & 4 deletions pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/notifications"
)

// DefaultConfigPath specifies the default location of Icinga DB's config.yml for package installations.
// const DefaultConfigPath = "/etc/icinga-kubernetes/config.yml"
const DefaultConfigPath = "./config.yml"

// Config defines Icinga Kubernetes config.
type Config struct {
Database database.Config `yaml:"database"`
Logging logging.Config `yaml:"logging"`
Notifications notifications.Config `yaml:"notifications"`
Prometheus metrics.PrometheusConfig `yaml:"prometheus"`
Database database.Config `yaml:"database" envPrefix:"DATABASE_"`
Logging logging.Config `yaml:"logging" envPrefix:"LOGGING_"`
Notifications notifications.Config `yaml:"notifications" envPrefix:"NOTIFICATIONS_"`
Prometheus metrics.PrometheusConfig `yaml:"prometheus" envPrefix:"PROMETHEUS_"`
}

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

return c.Notifications.Validate()
}

// ConfigFlagGlue provides a glue struct for the CLI config flag.
type ConfigFlagGlue struct {
// Config is the path to the config file
Config string
}

// GetConfigPath retrieves the path to the configuration file.
// It returns the path specified via the command line, or DefaultConfigPath if none is provided.
func (f ConfigFlagGlue) GetConfigPath() string {
if f.Config == "" {
return DefaultConfigPath
}

return f.Config
}

// IsExplicitConfigPath indicates whether the configuration file path was explicitly set.
func (f ConfigFlagGlue) IsExplicitConfigPath() bool {
return f.Config != ""
}
2 changes: 1 addition & 1 deletion pkg/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package metrics

// PrometheusConfig defines Prometheus configuration.
type PrometheusConfig struct {
Url string `yaml:"url"`
Url string `yaml:"url" env:"URL"`
}

// Validate checks constraints in the supplied Prometheus configuration and returns an error if they are violated.
Expand Down
8 changes: 4 additions & 4 deletions pkg/notifications/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

type Config struct {
// If URL is the empty string, notifications are disabled.
Url string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
KubernetesWebUrl string `yaml:"kubernetes_web_url" default:"http://localhost/icingaweb2/kubernetes"`
Url string `yaml:"url" env:"URL"`
Username string `yaml:"username" env:"USERNAME"`
Password string `yaml:"password" env:"PASSWORD"`
KubernetesWebUrl string `yaml:"kubernetes_web_url" env:"KUBERNETES_WEB_URL" default:"http://localhost/icingaweb2/kubernetes"`
}

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

0 comments on commit 6d9ce31

Please sign in to comment.