Skip to content

Commit

Permalink
Merge pull request #1561 from openmeterio/validation
Browse files Browse the repository at this point in the history
Return all validation errors from configuration
  • Loading branch information
sagikazarmark authored Oct 2, 2024
2 parents ab1a51e + ede9550 commit efcc0f1
Show file tree
Hide file tree
Showing 20 changed files with 327 additions and 113 deletions.
9 changes: 8 additions & 1 deletion cmd/balance-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func main() {

flags.String("config", "", "Configuration file")
flags.Bool("version", false, "Show version information")
flags.Bool("validate", false, "Validate configuration and exit")

_ = flags.Parse(os.Args[1:])

Expand All @@ -87,7 +88,13 @@ func main() {

err = conf.Validate()
if err != nil {
panic(err)
println("configuration error:")
println(err.Error())
os.Exit(1)
}

if v, _ := flags.GetBool("validate"); v {
os.Exit(0)
}

extraResources, _ := resource.New(
Expand Down
9 changes: 8 additions & 1 deletion cmd/notification-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func main() {

flags.String("config", "", "Configuration file")
flags.Bool("version", false, "Show version information")
flags.Bool("validate", false, "Validate configuration and exit")

_ = flags.Parse(os.Args[1:])

Expand All @@ -89,7 +90,13 @@ func main() {

err = conf.Validate()
if err != nil {
panic(err)
println("configuration error:")
println(err.Error())
os.Exit(1)
}

if v, _ := flags.GetBool("validate"); v {
os.Exit(0)
}

extraResources, _ := resource.New(
Expand Down
9 changes: 8 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func main() {

flags.String("config", "", "Configuration file")
flags.Bool("version", false, "Show version information")
flags.Bool("validate", false, "Validate configuration and exit")

_ = flags.Parse(os.Args[1:])

Expand All @@ -112,7 +113,13 @@ func main() {

err = conf.Validate()
if err != nil {
panic(err)
println("configuration error:")
println(err.Error())
os.Exit(1)
}

if v, _ := flags.GetBool("validate"); v {
os.Exit(0)
}

extraResources, _ := resource.New(
Expand Down
8 changes: 7 additions & 1 deletion cmd/sink-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func main() {

flags.String("config", "", "Configuration file")
flags.Bool("version", false, "Show version information")
flags.Bool("validate", false, "Validate configuration and exit")

_ = flags.Parse(os.Args[1:])

Expand Down Expand Up @@ -83,10 +84,15 @@ func main() {

err = conf.Validate()
if err != nil {
slog.Error("invalid configuration", slog.String("error", err.Error()))
println("configuration error:")
println(err.Error())
os.Exit(1)
}

if v, _ := flags.GetBool("validate"); v {
os.Exit(0)
}

extraResources, _ := resource.New(
ctx,
resource.WithContainer(),
Expand Down
16 changes: 9 additions & 7 deletions config/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,33 @@ type ClickHouseAggregationConfiguration struct {
}

func (c ClickHouseAggregationConfiguration) Validate() error {
var errs []error

if c.Address == "" {
return errors.New("address is required")
errs = append(errs, errors.New("address is required"))
}

if c.DialTimeout <= 0 {
return errors.New("dial timeout must be greater than 0")
errs = append(errs, errors.New("dial timeout must be greater than 0"))
}

if c.MaxOpenConns <= 0 {
return errors.New("max open connections must be greater than 0")
errs = append(errs, errors.New("max open connections must be greater than 0"))
}

if c.MaxIdleConns <= 0 {
return errors.New("max idle connections must be greater than 0")
errs = append(errs, errors.New("max idle connections must be greater than 0"))
}

if c.ConnMaxLifetime <= 0 {
return errors.New("connection max lifetime must be greater than 0")
errs = append(errs, errors.New("connection max lifetime must be greater than 0"))
}

if c.BlockBufferSize <= 0 {
return errors.New("block buffer size must be greater than 0")
errs = append(errs, errors.New("block buffer size must be greater than 0"))
}

return nil
return errors.Join(errs...)
}

func (c ClickHouseAggregationConfiguration) GetClientOptions() *clickhouse.Options {
Expand Down
10 changes: 8 additions & 2 deletions config/balanceworker.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package config

import (
"errors"

"github.com/spf13/viper"

"github.com/openmeterio/openmeter/pkg/errorsx"
)

type BalanceWorkerConfiguration struct {
ConsumerConfiguration `mapstructure:",squash"`
}

func (c BalanceWorkerConfiguration) Validate() error {
var errs []error

if err := c.ConsumerConfiguration.Validate(); err != nil {
return err
errs = append(errs, errorsx.WithPrefix(err, "consumer"))
}

return nil
return errors.Join(errs...)
}

func ConfigureBalanceWorker(v *viper.Viper) {
Expand Down
34 changes: 18 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package config

import (
"errors"
"fmt"
"strings"

"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/openmeterio/openmeter/pkg/errorsx"
"github.com/openmeterio/openmeter/pkg/models"
)

Expand Down Expand Up @@ -37,44 +37,46 @@ type Configuration struct {

// Validate validates the configuration.
func (c Configuration) Validate() error {
var errs []error

if c.Address == "" {
return errors.New("server address is required")
errs = append(errs, errors.New("server address is required"))
}

if err := c.Telemetry.Validate(); err != nil {
return fmt.Errorf("telemetry: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "telemetry"))
}

if err := c.Namespace.Validate(); err != nil {
return fmt.Errorf("namespace: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "namespace"))
}

if err := c.Ingest.Validate(); err != nil {
return fmt.Errorf("ingest: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "ingest"))
}

if err := c.Aggregation.Validate(); err != nil {
return fmt.Errorf("aggregation: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "aggregation"))
}

if err := c.Sink.Validate(); err != nil {
return fmt.Errorf("sink: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "sink"))
}

if err := c.Dedupe.Validate(); err != nil {
return fmt.Errorf("dedupe: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "dedupe"))
}

if err := c.Portal.Validate(); err != nil {
return fmt.Errorf("portal: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "portal"))
}

if err := c.Entitlements.Validate(); err != nil {
return fmt.Errorf("entitlements: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "entitlements"))
}

if len(c.Meters) == 0 {
return errors.New("no meters configured: add meter to configuration file")
errs = append(errs, errors.New("no meters configured: add meter to configuration file"))
}

for _, m := range c.Meters {
Expand All @@ -87,25 +89,25 @@ func (c Configuration) Validate() error {
}

if err := m.Validate(); err != nil {
return err
errs = append(errs, err)
}
}

if err := c.BalanceWorker.Validate(); err != nil {
return fmt.Errorf("balance worker: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "balance worker"))
}

if c.Notification.Enabled {
if err := c.Notification.Validate(); err != nil {
return fmt.Errorf("notification: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "notification"))
}

if err := c.Svix.Validate(); err != nil {
return fmt.Errorf("svix: %w", err)
errs = append(errs, errorsx.WithPrefix(err, "svix"))
}
}

return nil
return errors.Join(errs...)
}

func SetViperDefaults(v *viper.Viper, flags *pflag.FlagSet) {
Expand Down
17 changes: 11 additions & 6 deletions config/dedupe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/openmeterio/openmeter/openmeter/dedupe"
"github.com/openmeterio/openmeter/openmeter/dedupe/memorydedupe"
"github.com/openmeterio/openmeter/openmeter/dedupe/redisdedupe"
"github.com/openmeterio/openmeter/pkg/errorsx"
"github.com/openmeterio/openmeter/pkg/redis"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func (c DedupeConfiguration) Validate() error {
}

if err := c.DedupeDriverConfiguration.Validate(); err != nil {
return fmt.Errorf("driver(%s): %w", c.DedupeDriverConfiguration.DriverName(), err)
return errorsx.WithPrefix(err, fmt.Sprintf("driver(%s)", c.DriverName()))
}

return nil
Expand Down Expand Up @@ -149,11 +150,13 @@ func (c DedupeDriverMemoryConfiguration) NewDeduplicator() (dedupe.Deduplicator,
}

func (c DedupeDriverMemoryConfiguration) Validate() error {
var errs []error

if c.Size == 0 {
return errors.New("size is required")
errs = append(errs, errors.New("size is required"))
}

return nil
return errors.Join(errs...)
}

// Dedupe redis driver configuration
Expand Down Expand Up @@ -181,17 +184,19 @@ func (c DedupeDriverRedisConfiguration) NewDeduplicator() (dedupe.Deduplicator,
}

func (c DedupeDriverRedisConfiguration) Validate() error {
var errs []error

if c.Address == "" {
return errors.New("address is required")
errs = append(errs, errors.New("address is required"))
}

if c.Sentinel.Enabled {
if c.Sentinel.MasterName == "" {
return errors.New("sentinel: master name is required")
errs = append(errs, errors.New("sentinel: master name is required"))
}
}

return nil
return errors.Join(errs...)
}

// ConfigureDedupe configures some defaults in the Viper instance.
Expand Down
Loading

0 comments on commit efcc0f1

Please sign in to comment.