Skip to content

Commit

Permalink
refact / split APIServer.Run() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 5, 2024
1 parent fb0117e commit 6fa1be5
Showing 1 changed file with 67 additions and 57 deletions.
124 changes: 67 additions & 57 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,72 @@ func (s *APIServer) Router() (*gin.Engine, error) {
return s.router, nil
}

func (s *APIServer) apicPush() error {
if err := s.apic.Push(); err != nil {
log.Errorf("capi push: %s", err)
return err
}

Check warning on line 308 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L306-L308

Added lines #L306 - L308 were not covered by tests

return nil
}

func (s *APIServer) apicPull() error {
if err := s.apic.Pull(); err != nil {
log.Errorf("capi pull: %s", err)
return err
}

Check warning on line 317 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L315-L317

Added lines #L315 - L317 were not covered by tests

return nil
}

func (s *APIServer) papiPull() error {
if err := s.papi.Pull(); err != nil {
log.Errorf("papi pull: %s", err)
return err
}

Check warning on line 326 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L322-L326

Added lines #L322 - L326 were not covered by tests

return nil

Check warning on line 328 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L328

Added line #L328 was not covered by tests
}

func (s *APIServer) papiSync() error {
if err := s.papi.SyncDecisions(); err != nil {
log.Errorf("capi decisions sync: %s", err)
return err
}

Check warning on line 335 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L331-L335

Added lines #L331 - L335 were not covered by tests

return nil

Check warning on line 337 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L337

Added line #L337 was not covered by tests
}

func (s *APIServer) initAPIC() {
s.apic.pushTomb.Go(s.apicPush)
s.apic.pullTomb.Go(s.apicPull)

// csConfig.API.Server.ConsoleConfig.ShareCustomScenarios

Check warning on line 344 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L343-L344

Added lines #L343 - L344 were not covered by tests
if s.apic.apiClient.IsEnrolled() {
if s.consoleConfig.IsPAPIEnabled() {
if s.papi.URL != "" {
log.Info("Starting PAPI decision receiver")
s.papi.pullTomb.Go(s.papiPull)
s.papi.syncTomb.Go(s.papiSync)
} else {
log.Warnf("papi_url is not set in online_api_credentials.yaml, can't synchronize with the console. Run cscli console enable console_management to add it.")
}
} else {
log.Warningf("Machine is not allowed to synchronize decisions, you can enable it with `cscli console enable console_management`")
}

Check warning on line 356 in pkg/apiserver/apiserver.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiserver/apiserver.go#L346-L356

Added lines #L346 - L356 were not covered by tests
}

s.apic.metricsTomb.Go(func() error {
s.apic.SendMetrics(make(chan bool))
return nil
})

s.apic.metricsTomb.Go(func() error {
s.apic.SendUsageMetrics()
return nil
})
}

func (s *APIServer) Run(apiReady chan bool) error {
defer trace.CatchPanic("lapi/runServer")

Expand All @@ -316,63 +382,7 @@ func (s *APIServer) Run(apiReady chan bool) error {
}

if s.apic != nil {
s.apic.pushTomb.Go(func() error {
if err := s.apic.Push(); err != nil {
log.Errorf("capi push: %s", err)
return err
}

return nil
})

s.apic.pullTomb.Go(func() error {
if err := s.apic.Pull(); err != nil {
log.Errorf("capi pull: %s", err)
return err
}

return nil
})

// csConfig.API.Server.ConsoleConfig.ShareCustomScenarios
if s.apic.apiClient.IsEnrolled() {
if s.consoleConfig.IsPAPIEnabled() {
if s.papi.URL != "" {
log.Info("Starting PAPI decision receiver")
s.papi.pullTomb.Go(func() error {
if err := s.papi.Pull(); err != nil {
log.Errorf("papi pull: %s", err)
return err
}

return nil
})

s.papi.syncTomb.Go(func() error {
if err := s.papi.SyncDecisions(); err != nil {
log.Errorf("capi decisions sync: %s", err)
return err
}

return nil
})
} else {
log.Warnf("papi_url is not set in online_api_credentials.yaml, can't synchronize with the console. Run cscli console enable console_management to add it.")
}
} else {
log.Warningf("Machine is not allowed to synchronize decisions, you can enable it with `cscli console enable console_management`")
}
}

s.apic.metricsTomb.Go(func() error {
s.apic.SendMetrics(make(chan bool))
return nil
})

s.apic.metricsTomb.Go(func() error {
s.apic.SendUsageMetrics()
return nil
})
s.initAPIC()
}

s.httpServerTomb.Go(func() error {
Expand Down

0 comments on commit 6fa1be5

Please sign in to comment.