Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail when the builder can't load its configuration. #5731

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fail when the builder can't load its configuration.
If the builder can't load its build configuration file, it logs an error
and continues. This results in building a collector that is not the one
the user specified. Exit with a fatal error instead.

Signed-off-by: James Peach <[email protected]>
  • Loading branch information
jpeach committed Jul 25, 2022
commit 652a662f13694adfb54ba54855f929613ea0aa9a
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 💡 Enhancements 💡

- `ocb` now exits with an error if it fails to load the build configuration. (#5731)

### 🧰 Bug fixes 🧰

## v0.56.0 Beta
Expand Down
22 changes: 11 additions & 11 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ var (
// Command is the main entrypoint for this application
func Command() (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "ocb",
SilenceUsage: true, // Don't print usage on Run error.
SilenceErrors: true, // Don't print errors; main does it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not relevant to the PR: I remember that I've seen an issue with errors in the collector being printed twice, I feel that this was the issue.

Use: "ocb",
Long: fmt.Sprintf("OpenTelemetry Collector Builder (%s)", version) + `

ocb generates a custom OpenTelemetry Collector binary using the
Expand All @@ -50,13 +52,11 @@ build configuration given by the "--config" argument.
return err
}
if err := cfg.Validate(); err != nil {
cfg.Logger.Error("invalid configuration", zap.Error(err))
return err
return fmt.Errorf("invalid configuration: %w", err)
}

if err := cfg.ParseModules(); err != nil {
cfg.Logger.Error("invalid module configuration", zap.Error(err))
return err
return fmt.Errorf("invalid module configuration: %w", err)
}

return builder.GenerateAndCompile(cfg)
Expand Down Expand Up @@ -86,30 +86,30 @@ build configuration given by the "--config" argument.
cmd.AddCommand(versionCommand())

if err := k.Load(posflag.Provider(cmd.Flags(), ".", k), nil); err != nil {
cfg.Logger.Error("failed to load command line arguments", zap.Error(err))
return nil, fmt.Errorf("failed to load command line arguments: %w", err)
}

return cmd, nil
}

func initConfig() error {
cfg.Logger.Info("OpenTelemetry Collector Builder", zap.String("version", version), zap.String("date", date))
cfg.Logger.Info("OpenTelemetry Collector Builder",
zap.String("version", version), zap.String("date", date))

// load the config file
if err := k.Load(file.Provider(cfgFile), yaml.Parser()); err != nil {
cfg.Logger.Error("failed to load config file", zap.String("config-file", cfgFile), zap.Error(err))
return fmt.Errorf("failed to load configuration file: %w", err)
}

// handle env variables
if err := k.Load(env.Provider("", ".", func(s string) string {
return strings.ReplaceAll(s, ".", "_")
}), nil); err != nil {
cfg.Logger.Error("failed to load env var", zap.Error(err))
return fmt.Errorf("failed to load environment variables: %w", err)
}

if err := k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{Tag: "mapstructure"}); err != nil {
cfg.Logger.Error("failed to unmarshal config", zap.Error(err))
return err
return fmt.Errorf("failed to unmarshal configuration: %w", err)
}

cfg.Logger.Info("Using config file", zap.String("path", cfgFile))
Expand Down