Skip to content

CLOUDP-239791: Replace log.Fatal calls in main.go with run() error pattern#941

Open
m1kola wants to merge 1 commit intomongodb:masterfrom
m1kola:CLOUDP-239791_improve_exit
Open

CLOUDP-239791: Replace log.Fatal calls in main.go with run() error pattern#941
m1kola wants to merge 1 commit intomongodb:masterfrom
m1kola:CLOUDP-239791_improve_exit

Conversation

@m1kola
Copy link
Copy Markdown
Contributor

@m1kola m1kola commented Mar 27, 2026

Summary

Extract operator startup logic into a run() function that returns an error, so deferred cleanup (tracer shutdown, span end) runs on failure instead of being bypassed by os.Exit via log.Fatal.

Proof of Work

CI must be green

Checklist

  • Have you linked a jira ticket and/or is the ticket in the title?
  • Have you checked whether your jira ticket required DOCSP changes?
  • Have you added changelog file?

…ttern

Extract operator startup logic into a run() function that returns an error,
so deferred cleanup (tracer shutdown, span end) runs on failure instead of
being bypassed by os.Exit via log.Fatal.
@m1kola m1kola added the skip-changelog Use this label in Pull Request to not require new changelog entry file label Mar 27, 2026
@m1kola m1kola marked this pull request as ready for review March 27, 2026 13:05
@m1kola m1kola requested review from a team as code owners March 27, 2026 13:05
Comment on lines +114 to +115
log.Error(err)
os.Exit(1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

q: why not making this a fatal? I would also wrap the error with some info.

Suggested change
log.Error(err)
os.Exit(1)
log.Fatalf("Fatal error: %v", err)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I normally prefer explicit over implicit. And log.Fatalf has an implicit exit.

But I don't mind changing it to log.Fatalf, if you prefer.

mgr, err := ctrl.NewManager(cfg, managerOptions)
if err != nil {
log.Fatal(err)
return err
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: it is usually best to wrap the error context. E.g:

Suggested change
return err
return fmt.Errorf("failed to create manager: %w", err)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is startup code in main, not a library/package. The errors from ctrl.NewManager, apiv1.AddToScheme, etc. already return descriptive messages. Wrapping them adds boilerplate without giving the caller anything useful. And there's no caller that needs to errors.Is/errors.As these, and the log output in main will print the original message anyway.

The point of the ticket was to fix the os.Exit bypass, not to improve error messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog Use this label in Pull Request to not require new changelog entry file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants