Skip to content

Commit

Permalink
#221 Remove context.Background
Browse files Browse the repository at this point in the history
  • Loading branch information
jelemux committed Dec 13, 2024
1 parent 36ddf86 commit b139cb0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions controllers/doguSupportManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const SupportModeEnvVar = "SUPPORT_MODE"

// podTemplateResourceGenerator is used to generate pod templates.
type podTemplateResourceGenerator interface {
GetPodTemplate(doguResource *k8sv2.Dogu, dogu *core.Dogu) (*corev1.PodTemplateSpec, error)
GetPodTemplate(ctx context.Context, doguResource *k8sv2.Dogu, dogu *core.Dogu) (*corev1.PodTemplateSpec, error)
}

// doguSupportManager is used to handle the support mode for dogus.
Expand Down Expand Up @@ -79,7 +79,7 @@ func (dsm *doguSupportManager) updateDeployment(ctx context.Context, doguResourc
return fmt.Errorf("failed to get dogu descriptor of dogu %s: %w", doguResource.Name, err)
}

podTemplate, err := dsm.podTemplateResourceGenerator.GetPodTemplate(doguResource, dogu)
podTemplate, err := dsm.podTemplateResourceGenerator.GetPodTemplate(ctx, doguResource, dogu)
if err != nil {
return fmt.Errorf("failed to get pod template for dogu %s in support action: %w", doguResource.Name, err)
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/mock_podTemplateResourceGenerator_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions controllers/resource/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ type CollectApplier interface {
// podTemplateResourceGenerator is used to generate pod templates.
type podTemplateResourceGenerator interface {
// GetPodTemplate returns a pod template for the given dogu.
GetPodTemplate(doguResource *k8sv2.Dogu, dogu *cesappcore.Dogu) (*v1.PodTemplateSpec, error)
GetPodTemplate(ctx context.Context, doguResource *k8sv2.Dogu, dogu *cesappcore.Dogu) (*v1.PodTemplateSpec, error)
}

// DoguResourceGenerator is used to generate kubernetes resources for the dogu.
type DoguResourceGenerator interface {
podTemplateResourceGenerator

// CreateDoguDeployment creates a new instance of a deployment with a given dogu.json and dogu custom resource.
CreateDoguDeployment(doguResource *k8sv2.Dogu, dogu *cesappcore.Dogu) (*apps.Deployment, error)
CreateDoguDeployment(ctx context.Context, doguResource *k8sv2.Dogu, dogu *cesappcore.Dogu) (*apps.Deployment, error)
// CreateDoguService creates a new instance of a service with the given dogu custom resource and container image.
// The container image is used to extract the exposed ports. The created service is rather meant for cluster-internal
// apps and dogus (f. e. postgresql) which do not need external access. The given container image config provides
Expand Down
5 changes: 3 additions & 2 deletions controllers/resource/mock_DoguResourceGenerator_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions controllers/resource/resource_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func NewResourceGenerator(scheme *runtime.Scheme, requirementsGenerator requirem
}

// CreateDoguDeployment creates a new instance of a deployment with a given dogu.json and dogu custom resource.
func (r *resourceGenerator) CreateDoguDeployment(doguResource *k8sv2.Dogu, dogu *core.Dogu) (*appsv1.Deployment, error) {
podTemplate, err := r.GetPodTemplate(doguResource, dogu)
func (r *resourceGenerator) CreateDoguDeployment(ctx context.Context, doguResource *k8sv2.Dogu, dogu *core.Dogu) (*appsv1.Deployment, error) {
podTemplate, err := r.GetPodTemplate(ctx, doguResource, dogu)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *resourceGenerator) CreateDoguDeployment(doguResource *k8sv2.Dogu, dogu
}

// GetPodTemplate returns a pod template for the given dogu.
func (r *resourceGenerator) GetPodTemplate(doguResource *k8sv2.Dogu, dogu *core.Dogu) (*corev1.PodTemplateSpec, error) {
func (r *resourceGenerator) GetPodTemplate(ctx context.Context, doguResource *k8sv2.Dogu, dogu *core.Dogu) (*corev1.PodTemplateSpec, error) {
volumes, err := createVolumes(doguResource, dogu)
if err != nil {
return nil, err
Expand All @@ -133,12 +133,12 @@ func (r *resourceGenerator) GetPodTemplate(doguResource *k8sv2.Dogu, dogu *core.
return nil, err
}

hostAliases, err := r.hostAliasGenerator.Generate(context.Background())
hostAliases, err := r.hostAliasGenerator.Generate(ctx)
if err != nil {
return nil, err
}

resourceRequirements, err := r.requirementsGenerator.Generate(context.Background(), dogu)
resourceRequirements, err := r.requirementsGenerator.Generate(ctx, dogu)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions controllers/resource/resource_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestResourceGenerator_GetDoguDeployment(t *testing.T) {
client.Params = "invalid"

// when
_, err := generator.CreateDoguDeployment(ldapDoguResource, ldapDogu)
_, err := generator.CreateDoguDeployment(nil, ldapDoguResource, ldapDogu)

// then
require.Error(t, err)
Expand All @@ -78,7 +78,7 @@ func TestResourceGenerator_GetDoguDeployment(t *testing.T) {
additionalImages: testAdditionalImages,
}

actualDeployment, err := generator.CreateDoguDeployment(ldapDoguResource, ldapDogu)
actualDeployment, err := generator.CreateDoguDeployment(nil, ldapDoguResource, ldapDogu)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestResourceGenerator_GetDoguDeployment(t *testing.T) {
additionalImages: testAdditionalImages,
}

actualDeployment, err := generator.CreateDoguDeployment(ldapDoguResource, ldapDogu)
actualDeployment, err := generator.CreateDoguDeployment(nil, ldapDoguResource, ldapDogu)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestResourceGenerator_GetDoguDeployment(t *testing.T) {
additionalImages: testAdditionalImages,
}

actualDeployment, err := generator.CreateDoguDeployment(ldapDoguResource, ldapDogu)
actualDeployment, err := generator.CreateDoguDeployment(nil, ldapDoguResource, ldapDogu)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestResourceGenerator_GetDoguDeployment(t *testing.T) {
config.Stage = config.StageDevelopment

// when
actualDeployment, err := generator.CreateDoguDeployment(ldapDoguResource, ldapDogu)
actualDeployment, err := generator.CreateDoguDeployment(nil, ldapDoguResource, ldapDogu)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestResourceGenerator_GetDoguDeployment(t *testing.T) {
defer func() { ctrl.SetControllerReference = oldMethod }()

// when
_, err := generator.CreateDoguDeployment(ldapDoguResource, ldapDogu)
_, err := generator.CreateDoguDeployment(nil, ldapDoguResource, ldapDogu)

// then
require.Error(t, err)
Expand All @@ -234,7 +234,7 @@ func TestResourceGenerator_GetDoguDeployment(t *testing.T) {
}

// when
_, err := generatorFail.CreateDoguDeployment(ldapDoguResource, ldapDogu)
_, err := generatorFail.CreateDoguDeployment(nil, ldapDoguResource, ldapDogu)

// then
require.ErrorIs(t, err, assert.AnError)
Expand Down
2 changes: 1 addition & 1 deletion controllers/resource/upserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewUpserter(client client.Client, generator DoguResourceGenerator) *upserte
// All parameters are mandatory except deploymentPatch which may be nil.
// The deploymentPatch can be used to arbitrarily alter the deployment after resource generation.
func (u *upserter) UpsertDoguDeployment(ctx context.Context, doguResource *k8sv2.Dogu, dogu *core.Dogu, deploymentPatch func(*appsv1.Deployment)) (*appsv1.Deployment, error) {
newDeployment, err := u.generator.CreateDoguDeployment(doguResource, dogu)
newDeployment, err := u.generator.CreateDoguDeployment(ctx, doguResource, dogu)
if err != nil {
return nil, fmt.Errorf("failed to generate deployment: %w", err)
}
Expand Down

0 comments on commit b139cb0

Please sign in to comment.