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

refact / split APIServer.Run() method #3215

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
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 @@
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 @@
}

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