From 0de024e5e8f5d77de01577e4cc4c4ff1d3927158 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Thu, 4 Apr 2024 19:08:38 +0200 Subject: [PATCH 1/3] Avoid creating a temporal variable for svcInfo --- internal/servicedeployer/terraform.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/servicedeployer/terraform.go b/internal/servicedeployer/terraform.go index 67430e2a6a..c95efbc82a 100644 --- a/internal/servicedeployer/terraform.go +++ b/internal/servicedeployer/terraform.go @@ -113,7 +113,6 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn env: tfEnvironment, shutdownTimeout: 300 * time.Second, } - outCtxt := svcInfo p, err := compose.NewProject(service.project, service.ymlPaths...) if err != nil { @@ -121,7 +120,7 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn } // Clean service logs - err = files.RemoveContent(outCtxt.Logs.Folder.Local) + err = files.RemoveContent(svcInfo.Logs.Folder.Local) if err != nil { return nil, fmt.Errorf("removing service logs failed: %w", err) } @@ -134,7 +133,7 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn if err != nil { return nil, fmt.Errorf("could not get Docker Compose configuration for service: %w", err) } - outCtxt.CustomProperties, err = buildTerraformAliases(serviceComposeConfig) + svcInfo.CustomProperties, err = buildTerraformAliases(serviceComposeConfig) if err != nil { return nil, fmt.Errorf("can't build Terraform aliases: %w", err) } @@ -153,18 +152,18 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn if err != nil { processServiceContainerLogs(ctx, p, compose.CommandOptions{ Env: opts.Env, - }, outCtxt.Name) + }, svcInfo.Name) //lint:ignore ST1005 error starting with product name can be capitalized return nil, fmt.Errorf("Terraform deployer is unhealthy: %w", err) } - outCtxt.Agent.Host.NamePrefix = "docker-fleet-agent" + svcInfo.Agent.Host.NamePrefix = "docker-fleet-agent" - err = addTerraformOutputs(outCtxt) + err = addTerraformOutputs(svcInfo) if err != nil { return nil, fmt.Errorf("could not handle terraform output: %w", err) } - service.svcInfo = outCtxt + service.svcInfo = svcInfo return &service, nil } From cd2732d2c15565299cd5204a88130011d544ae7c Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Thu, 4 Apr 2024 19:54:18 +0200 Subject: [PATCH 2/3] Use reference to add terraform outputs --- internal/servicedeployer/terraform.go | 4 +-- internal/servicedeployer/terraform_test.go | 30 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/internal/servicedeployer/terraform.go b/internal/servicedeployer/terraform.go index c95efbc82a..9c0be72e1f 100644 --- a/internal/servicedeployer/terraform.go +++ b/internal/servicedeployer/terraform.go @@ -48,7 +48,7 @@ type TerraformServiceDeployer struct { // addTerraformOutputs method reads the terraform outputs generated in the json format and // adds them to the custom properties of ServiceInfo and can be used in the handlebars template // like `{{TF_OUTPUT_queue_url}}` where `queue_url` is the output configured -func addTerraformOutputs(svcInfo ServiceInfo) error { +func addTerraformOutputs(svcInfo *ServiceInfo) error { // Read the `output.json` file where terraform outputs are generated outputFile := filepath.Join(svcInfo.OutputDir, terraformOutputJsonFile) content, err := os.ReadFile(outputFile) @@ -159,7 +159,7 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn svcInfo.Agent.Host.NamePrefix = "docker-fleet-agent" - err = addTerraformOutputs(svcInfo) + err = addTerraformOutputs(&svcInfo) if err != nil { return nil, fmt.Errorf("could not handle terraform output: %w", err) } diff --git a/internal/servicedeployer/terraform_test.go b/internal/servicedeployer/terraform_test.go index 3b06c8a977..d768856cd3 100644 --- a/internal/servicedeployer/terraform_test.go +++ b/internal/servicedeployer/terraform_test.go @@ -64,6 +64,29 @@ func TestAddTerraformOutputs(t *testing.T) { "TF_OUTPUT_queue_url": "https://sqs.us-east-1.amazonaws.com/1234654/elastic-package-aws-logs-queue-someId", }, }, + { + testName: "add_single_value_output", + runId: "99999", + svcInfo: ServiceInfo{ + Test: struct{ RunID string }{"99999"}, + CustomProperties: map[string]interface{}{ + "TF_foo": "bar", + }, + }, + content: []byte( + `{ + "queue_url": { + "sensitive": false, + "type": "string", + "value": "https://sqs.us-east-1.amazonaws.com/1234654/elastic-package-aws-logs-queue-someId" + } + }`, + ), + expectedProps: map[string]interface{}{ + "TF_OUTPUT_queue_url": "https://sqs.us-east-1.amazonaws.com/1234654/elastic-package-aws-logs-queue-someId", + "TF_foo": "bar", + }, + }, { testName: "multiple_value_output", runId: "23465", @@ -138,7 +161,10 @@ func TestAddTerraformOutputs(t *testing.T) { for _, tc := range testCases { t.Run(tc.testName, func(t *testing.T) { - tc.svcInfo.CustomProperties = make(map[string]interface{}) + if tc.svcInfo.CustomProperties == nil { + tc.svcInfo.CustomProperties = make(map[string]interface{}) + } + tc.svcInfo.OutputDir = t.TempDir() if err := os.WriteFile(tc.svcInfo.OutputDir+"/tfOutputValues.json", tc.content, 0777); err != nil { @@ -146,7 +172,7 @@ func TestAddTerraformOutputs(t *testing.T) { } // Test that the terraform output values are generated correctly - err := addTerraformOutputs(tc.svcInfo) + err := addTerraformOutputs(&tc.svcInfo) if tc.expectedError { require.Error(t, err) return From b026045c0304d04308ce88b521b54e4baef55934 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Thu, 4 Apr 2024 20:10:02 +0200 Subject: [PATCH 3/3] Remove map initialization in test --- internal/servicedeployer/terraform_test.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/internal/servicedeployer/terraform_test.go b/internal/servicedeployer/terraform_test.go index d768856cd3..a1c5b2f291 100644 --- a/internal/servicedeployer/terraform_test.go +++ b/internal/servicedeployer/terraform_test.go @@ -31,7 +31,7 @@ func TestAddTerraformOutputs(t *testing.T) { content: []byte( ``, ), - expectedProps: map[string]interface{}{}, + expectedProps: nil, expectedError: true, }, { @@ -43,7 +43,7 @@ func TestAddTerraformOutputs(t *testing.T) { content: []byte( `{}`, ), - expectedProps: map[string]interface{}{}, + expectedProps: nil, }, { testName: "single_value_output", @@ -161,10 +161,6 @@ func TestAddTerraformOutputs(t *testing.T) { for _, tc := range testCases { t.Run(tc.testName, func(t *testing.T) { - if tc.svcInfo.CustomProperties == nil { - tc.svcInfo.CustomProperties = make(map[string]interface{}) - } - tc.svcInfo.OutputDir = t.TempDir() if err := os.WriteFile(tc.svcInfo.OutputDir+"/tfOutputValues.json", tc.content, 0777); err != nil {