-
Notifications
You must be signed in to change notification settings - Fork 24
HYPERFLEET-1370 - refactor: introduce typed container for API server dependencies #309
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
Open
kuudori
wants to merge
1
commit into
openshift-hyperfleet:main
Choose a base branch
from
kuudori:HYPERFLEET-1370-refactor-typed-container
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package container | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/auth" | ||
| ) | ||
|
|
||
| func (c *Container) JWTHandler() (*auth.JWTHandler, error) { | ||
| if c.jwtHandler == nil { | ||
| jwtHandler, err := auth.NewJWTHandler( | ||
| context.Background(), | ||
| auth.JWTHandlerConfig{ | ||
| Issuers: c.cfg.Server.JWT.Configs, | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to create JWT handler: %w", err) | ||
| } | ||
| c.jwtHandler = jwtHandler | ||
| } | ||
| return c.jwtHandler, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package container | ||
|
|
||
| import ( | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/auth" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/config" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/dao" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/db" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/services" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/validators" | ||
| ) | ||
|
|
||
| // Container lazily constructs and caches application dependencies during | ||
| // sequential startup. It is not safe for concurrent initialization. | ||
| // | ||
| // Container owns dependencies only. Assembling them into a running API server | ||
| // is the composition root's job — see buildAPIServer in the servecmd package. | ||
| // | ||
| // TODO(HYPERFLEET-1371): Once the environments/ package is removed, | ||
| // Container should source SessionFactory directly (e.g. from config/Viper) | ||
| // rather than accepting it as a constructor parameter. Close() should also | ||
| // close the SessionFactory at that point. | ||
| type Container struct { | ||
| cfg *config.ApplicationConfig | ||
| sessionFactory db.SessionFactory | ||
|
|
||
| resourceDao dao.ResourceDao | ||
| resourceLabelDao dao.ResourceLabelDao | ||
| adapterStatusDao dao.AdapterStatusDao | ||
| resourceConditionDao dao.ResourceConditionDao | ||
| genericDao dao.GenericDao | ||
|
|
||
| resourceService services.ResourceService | ||
| adapterStatusService services.AdapterStatusService | ||
| genericService services.GenericService | ||
|
|
||
| schemaValidator *validators.SchemaValidator | ||
| jwtHandler *auth.JWTHandler | ||
| } | ||
|
|
||
| func NewContainer(cfg *config.ApplicationConfig, sessionFactory db.SessionFactory) *Container { | ||
| return &Container{cfg: cfg, sessionFactory: sessionFactory} | ||
| } | ||
|
|
||
| func (c *Container) SessionFactory() db.SessionFactory { | ||
| return c.sessionFactory | ||
| } | ||
|
|
||
| func (c *Container) Close() { | ||
| if c.jwtHandler != nil { | ||
| c.jwtHandler.Close() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package container | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/gomega" | ||
|
|
||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/config" | ||
| dbmocks "github.com/openshift-hyperfleet/hyperfleet-api/pkg/db/mocks" | ||
| ) | ||
|
|
||
| func newTestContainer(t *testing.T) *Container { | ||
| t.Helper() | ||
|
|
||
| sessionFactory := dbmocks.NewMockSessionFactory() | ||
| t.Cleanup(func() { _ = sessionFactory.Close() }) | ||
|
|
||
| return NewContainer(config.NewApplicationConfig(), sessionFactory) | ||
| } | ||
|
|
||
| func TestContainerCachesDAOsAndServices(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
| c := newTestContainer(t) | ||
|
|
||
| Expect(c.SessionFactory()).NotTo(BeNil()) | ||
|
|
||
| Expect(c.ResourceDao()).NotTo(BeNil()) | ||
| Expect(c.ResourceDao()).To(BeIdenticalTo(c.ResourceDao())) | ||
| Expect(c.ResourceLabelDao()).NotTo(BeNil()) | ||
| Expect(c.ResourceLabelDao()).To(BeIdenticalTo(c.ResourceLabelDao())) | ||
| Expect(c.AdapterStatusDao()).NotTo(BeNil()) | ||
| Expect(c.AdapterStatusDao()).To(BeIdenticalTo(c.AdapterStatusDao())) | ||
| Expect(c.ResourceConditionDao()).NotTo(BeNil()) | ||
| Expect(c.ResourceConditionDao()).To(BeIdenticalTo(c.ResourceConditionDao())) | ||
| Expect(c.GenericDao()).NotTo(BeNil()) | ||
| Expect(c.GenericDao()).To(BeIdenticalTo(c.GenericDao())) | ||
|
|
||
| Expect(c.GenericService()).NotTo(BeNil()) | ||
| Expect(c.GenericService()).To(BeIdenticalTo(c.GenericService())) | ||
| Expect(c.AdapterStatusService()).NotTo(BeNil()) | ||
| Expect(c.AdapterStatusService()).To(BeIdenticalTo(c.AdapterStatusService())) | ||
| Expect(c.ResourceService()).NotTo(BeNil()) | ||
| Expect(c.ResourceService()).To(BeIdenticalTo(c.ResourceService())) | ||
| } | ||
|
|
||
| func TestContainerConstructionIsLazy(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
| c := newTestContainer(t) | ||
|
|
||
| Expect(c.resourceDao).To(BeNil()) | ||
| Expect(c.resourceLabelDao).To(BeNil()) | ||
| Expect(c.adapterStatusDao).To(BeNil()) | ||
| Expect(c.resourceConditionDao).To(BeNil()) | ||
| Expect(c.genericDao).To(BeNil()) | ||
| Expect(c.resourceService).To(BeNil()) | ||
| Expect(c.adapterStatusService).To(BeNil()) | ||
| Expect(c.genericService).To(BeNil()) | ||
| Expect(c.schemaValidator).To(BeNil()) | ||
| Expect(c.jwtHandler).To(BeNil()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package container | ||
|
|
||
| import ( | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/dao" | ||
| ) | ||
|
|
||
| func (c *Container) ResourceDao() dao.ResourceDao { | ||
| if c.resourceDao == nil { | ||
| c.resourceDao = dao.NewResourceDao(c.sessionFactory) | ||
| } | ||
| return c.resourceDao | ||
| } | ||
|
|
||
| func (c *Container) ResourceLabelDao() dao.ResourceLabelDao { | ||
| if c.resourceLabelDao == nil { | ||
| c.resourceLabelDao = dao.NewResourceLabelDao(c.sessionFactory) | ||
| } | ||
| return c.resourceLabelDao | ||
| } | ||
|
|
||
| func (c *Container) AdapterStatusDao() dao.AdapterStatusDao { | ||
| if c.adapterStatusDao == nil { | ||
| c.adapterStatusDao = dao.NewAdapterStatusDao(c.sessionFactory) | ||
| } | ||
| return c.adapterStatusDao | ||
| } | ||
|
|
||
| func (c *Container) ResourceConditionDao() dao.ResourceConditionDao { | ||
| if c.resourceConditionDao == nil { | ||
| c.resourceConditionDao = dao.NewResourceConditionDao(c.sessionFactory) | ||
| } | ||
| return c.resourceConditionDao | ||
| } | ||
|
|
||
| func (c *Container) GenericDao() dao.GenericDao { | ||
| if c.genericDao == nil { | ||
| c.genericDao = dao.NewGenericDao(c.sessionFactory) | ||
| } | ||
| return c.genericDao | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package container | ||
|
|
||
| import ( | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/services" | ||
| ) | ||
|
|
||
| func (c *Container) ResourceService() services.ResourceService { | ||
| if c.resourceService == nil { | ||
| c.resourceService = services.NewResourceService( | ||
| c.ResourceDao(), | ||
| c.ResourceLabelDao(), | ||
| c.AdapterStatusDao(), | ||
| c.ResourceConditionDao(), | ||
| c.GenericService(), | ||
| ) | ||
| } | ||
| return c.resourceService | ||
| } | ||
|
|
||
| func (c *Container) AdapterStatusService() services.AdapterStatusService { | ||
| if c.adapterStatusService == nil { | ||
| c.adapterStatusService = services.NewAdapterStatusService(c.AdapterStatusDao()) | ||
| } | ||
| return c.adapterStatusService | ||
| } | ||
|
|
||
| func (c *Container) GenericService() services.GenericService { | ||
| if c.genericService == nil { | ||
| c.genericService = services.NewGenericService(c.GenericDao()) | ||
| } | ||
| return c.genericService | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package container | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/logger" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/validators" | ||
| ) | ||
|
|
||
| func (c *Container) SchemaValidator() *validators.SchemaValidator { | ||
| if c.schemaValidator == nil { | ||
| schemaPath := c.cfg.Server.OpenAPISchemaPath | ||
| schemaValidator, err := validators.NewSchemaValidator(schemaPath) | ||
| if err != nil { | ||
| panic("unable to create schema validator: " + err.Error()) | ||
| } | ||
| c.schemaValidator = schemaValidator | ||
| logger.With(context.Background(), logger.FieldSchemaPath, schemaPath).Info("Schema validation enabled") | ||
| } | ||
| return c.schemaValidator | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.