-
Notifications
You must be signed in to change notification settings - Fork 44
CLOUDP-239791: Replace log.Fatal calls in main.go with run() error pattern #941
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -110,6 +110,13 @@ func (c *crdsToWatch) String() string { | |||||
| } | ||||||
|
|
||||||
| func main() { | ||||||
| if err := run(); err != nil { | ||||||
| log.Error(err) | ||||||
| os.Exit(1) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func run() error { | ||||||
| flag.Parse() | ||||||
| // If no CRDs are specified, we set default to non-multicluster CRDs | ||||||
| if len(crds) == 0 { | ||||||
|
|
@@ -199,13 +206,13 @@ func main() { | |||||
|
|
||||||
| mgr, err := ctrl.NewManager(cfg, managerOptions) | ||||||
| if err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is startup code in The point of the ticket was to fix the |
||||||
| } | ||||||
| log.Info("Registering Components.") | ||||||
|
|
||||||
| // Setup Scheme for all resources | ||||||
| if err := apiv1.AddToScheme(scheme); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| // memberClusterObjectsMap is a map of clusterName -> clusterObject | ||||||
|
|
@@ -214,7 +221,7 @@ func main() { | |||||
| if slices.Contains(crds, mongoDBMultiClusterCRDPlural) { | ||||||
| memberClustersNames, err := getMemberClusters(ctx, cfg, currentNamespace) | ||||||
| if err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| log.Infof("Watching Member clusters: %s", memberClustersNames) | ||||||
|
|
@@ -225,7 +232,7 @@ func main() { | |||||
|
|
||||||
| memberClusterClients, err := multicluster.CreateMemberClusterClients(memberClustersNames, multicluster.GetKubeConfigPath()) | ||||||
| if err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| // Add the cluster object to the manager corresponding to each member clusters. | ||||||
|
|
@@ -253,35 +260,35 @@ func main() { | |||||
| log.Infof("Adding cluster %s to cluster map.", k) | ||||||
| memberClusterObjectsMap[k] = cluster | ||||||
| if err = mgr.Add(cluster); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Setup all Controllers | ||||||
| if slices.Contains(crds, mongoDBCRDPlural) { | ||||||
| if err := setupMongoDBCRD(ctx, mgr, imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, forceEnterprise, enableClusterMongoDBRoles, agentDebug, agentDebugImage, memberClusterObjectsMap); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| if slices.Contains(crds, mongoDBOpsManagerCRDPlural) { | ||||||
| if err := setupMongoDBOpsManagerCRD(ctx, mgr, memberClusterObjectsMap, imageUrls, initDatabaseNonStaticImageVersion, initOpsManagerImageVersion); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| if slices.Contains(crds, mongoDBUserCRDPlural) { | ||||||
| if err := setupMongoDBUserCRD(ctx, mgr, memberClusterObjectsMap); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| if slices.Contains(crds, mongoDBMultiClusterCRDPlural) { | ||||||
| if err := setupMongoDBMultiClusterCRD(ctx, mgr, imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, forceEnterprise, enableClusterMongoDBRoles, agentDebug, agentDebugImage, memberClusterObjectsMap); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| if slices.Contains(crds, mongoDBSearchCRDPlural) { | ||||||
| if err := setupMongoDBSearchCRD(ctx, mgr); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -302,7 +309,7 @@ func main() { | |||||
| env.ReadOrPanic(mcoConstruct.VersionUpgradeHookImageEnv), | ||||||
| env.ReadOrPanic(mcoConstruct.ReadinessProbeImageEnv), | ||||||
| ); err != nil { | ||||||
| log.Fatal(err) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -331,9 +338,7 @@ func main() { | |||||
|
|
||||||
| log.Info("Starting the Cmd.") | ||||||
|
|
||||||
| if err := mgr.Start(ctx); err != nil { | ||||||
| log.Fatal(err) | ||||||
| } | ||||||
| return mgr.Start(ctx) | ||||||
| } | ||||||
|
|
||||||
| func startRootSpan(currentNamespace string, spanIDHex string, traceCtx context.Context) (context.Context, trace.Span) { | ||||||
|
|
||||||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.Fatalfhas an implicit exit.But I don't mind changing it to
log.Fatalf, if you prefer.