Skip to content

Commit

Permalink
daemons: name init functions and have one init
Browse files Browse the repository at this point in the history
This change refactors and names 3 of `cmd/daemon` `init` functions so
that the order of their execution and what they do is clearer.

Signed-off-by: Maciej Kwiek <maciej@isovalent.com>
  • Loading branch information
nebril committed Oct 22, 2021
1 parent 1efd3c5 commit f5a1621
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions daemon/cmd/daemon_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,6 @@ var (
bootstrapStats = bootstrapStatistics{}
)

func init() {
RootCmd.SetFlagErrorFunc(func(_ *cobra.Command, e error) error {
time.Sleep(fatalSleep)
return e
})
logrus.RegisterExitHandler(func() {
time.Sleep(fatalSleep)
},
)
}

// Execute sets up gops, installs the cleanup signal handler and invokes
// the root command. This function only returns when an interrupt
// signal has been received. This is intended to be called by main.main().
Expand All @@ -165,6 +154,23 @@ func skipInit(basePath string) bool {
}

func init() {
setupSleepBeforeFatal()
initializeFlags()
registerBootstrapMetrics()
}

func setupSleepBeforeFatal() {
RootCmd.SetFlagErrorFunc(func(_ *cobra.Command, e error) error {
time.Sleep(fatalSleep)
return e
})
logrus.RegisterExitHandler(func() {
time.Sleep(fatalSleep)
},
)
}

func initializeFlags() {
if skipInit(path.Base(os.Args[0])) {
log.Debug("Skipping preparation of cilium-agent environment")
return
Expand Down
2 changes: 1 addition & 1 deletion daemon/cmd/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ var (
metricBootstrapTimes prometheus.ObserverVec
)

func init() {
func registerBootstrapMetrics() {
metricBootstrapTimes = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: metrics.Namespace,
Subsystem: metrics.SubsystemAgent,
Expand Down

0 comments on commit f5a1621

Please sign in to comment.