diff --git a/cmd/icinga-kubernetes/main.go b/cmd/icinga-kubernetes/main.go index a0c0809c..ccba4f5d 100644 --- a/cmd/icinga-kubernetes/main.go +++ b/cmd/icinga-kubernetes/main.go @@ -66,12 +66,8 @@ func main() { klog.Fatal(errors.Wrap(err, "can't create configuration")) } - d, err := database.FromYAMLFile(configLocation) - if err != nil { - klog.Fatal(err) - } dbLog := log.WithName("database") - db, err := database.NewFromConfig(d, dbLog) + db, err := database.NewFromConfig(&cfg.Database, dbLog) if err != nil { klog.Fatal(err) } @@ -79,7 +75,7 @@ func main() { return } - hasSchema, err := dbHasSchema(db, d.Database) + hasSchema, err := dbHasSchema(db, cfg.Database.Database) if err != nil { klog.Fatal(err) } diff --git a/pkg/database/config.go b/pkg/database/config.go deleted file mode 100644 index 2b5012ab..00000000 --- a/pkg/database/config.go +++ /dev/null @@ -1,43 +0,0 @@ -package database - -import ( - "github.com/pkg/errors" -) - -// Config defines database client configuration. -type Config struct { - Type string `yaml:"type" default:"mysql"` - Host string `yaml:"host"` - Port int `yaml:"port"` - Database string `yaml:"database"` - User string `yaml:"user"` - Password string `yaml:"password"` - Options Options `yaml:"options"` -} - -// Validate checks constraints in the supplied database configuration and returns an error if they are violated. -func (c *Config) Validate() error { - switch c.Type { - case "mysql", "pgsql": - default: - return unknownDbType(c.Type) - } - - if c.Host == "" { - return errors.New("database host missing") - } - - if c.User == "" { - return errors.New("database user missing") - } - - if c.Database == "" { - return errors.New("database name missing") - } - - return nil -} - -func unknownDbType(t string) error { - return errors.Errorf(`unknown database type %q, must be one of: "mysql", "pgsql"`, t) -} diff --git a/pkg/database/database.go b/pkg/database/database.go index 4afd321d..7e87d3ef 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -33,7 +33,7 @@ var registerDriversOnce sync.Once type Database struct { *sqlx.DB - Options Options + Options database.Options log logr.Logger @@ -46,7 +46,7 @@ type Database struct { } // NewFromConfig returns a new Database connection from the given Config. -func NewFromConfig(c *Config, log logr.Logger) (*Database, error) { +func NewFromConfig(c *database.Config, log logr.Logger) (*Database, error) { registerDriversOnce.Do(func() { RegisterDrivers(log) }) @@ -99,7 +99,7 @@ func NewFromConfig(c *Config, log logr.Logger) (*Database, error) { uri.RawQuery = query.Encode() dsn = uri.String() default: - return nil, unknownDbType(c.Type) + return nil, errors.Errorf(`unknown database type %q, must be one of: "mysql", "pgsql"`, c.Type) } db, err := sqlx.Open("icinga-"+c.Type, dsn) diff --git a/pkg/database/db.go b/pkg/database/db.go deleted file mode 100644 index a658ec12..00000000 --- a/pkg/database/db.go +++ /dev/null @@ -1,35 +0,0 @@ -package database - -import ( - "github.com/creasty/defaults" - "github.com/goccy/go-yaml" - "github.com/pkg/errors" - "os" -) - -func FromYAMLFile(file string) (*Config, error) { - f, err := os.Open(file) - if err != nil { - return nil, errors.Wrap(err, "can't open YAML file "+file) - } - defer f.Close() - - c := &struct { - Database Config `yaml:"database"` - }{} - decoder := yaml.NewDecoder(f) - - if err := defaults.Set(c); err != nil { - return nil, errors.Wrap(err, "can't set config defaults") - } - - if err := decoder.Decode(c); err != nil { - return nil, errors.Wrap(err, "can't parse YAML file "+file) - } - - if err := c.Database.Validate(); err != nil { - return nil, errors.Wrap(err, "invalid configuration") - } - - return &c.Database, nil -} diff --git a/pkg/database/options.go b/pkg/database/options.go deleted file mode 100644 index 8a47b971..00000000 --- a/pkg/database/options.go +++ /dev/null @@ -1,43 +0,0 @@ -package database - -import "github.com/pkg/errors" - -// Options define user configurable database options. -type Options struct { - // Maximum number of open connections to the database. - MaxConnections int `yaml:"max_connections" default:"16"` - - // Maximum number of connections per table, - // regardless of what the connection is actually doing, - // e.g. INSERT, UPDATE, DELETE. - MaxConnectionsPerTable int `yaml:"max_connections_per_table" default:"8"` - - // MaxPlaceholdersPerStatement defines the maximum number of placeholders in an - // INSERT, UPDATE or DELETE statement. Theoretically, MySQL can handle up to 2^16-1 placeholders, - // but this increases the execution time of queries and thus reduces the number of queries - // that can be executed in parallel in a given time. - // The default is 2^13, which in our tests showed the best performance in terms of execution time and parallelism. - MaxPlaceholdersPerStatement int `yaml:"max_placeholders_per_statement" default:"8192"` - - // MaxRowsPerTransaction defines the maximum number of rows per transaction. - // The default is 2^13, which in our tests showed the best performance in terms of execution time and parallelism. - MaxRowsPerTransaction int `yaml:"max_rows_per_transaction" default:"8192"` -} - -// Validate checks constraints in the supplied database options and returns an error if they are violated. -func (o *Options) Validate() error { - if o.MaxConnections == 0 { - return errors.New("max_connections cannot be 0. Configure a value greater than zero, or use -1 for no connection limit") - } - if o.MaxConnectionsPerTable < 1 { - return errors.New("max_connections_per_table must be at least 1") - } - if o.MaxPlaceholdersPerStatement < 1 { - return errors.New("max_placeholders_per_statement must be at least 1") - } - if o.MaxRowsPerTransaction < 1 { - return errors.New("max_rows_per_transaction must be at least 1") - } - - return nil -}