-
-
Notifications
You must be signed in to change notification settings - Fork 997
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
WIP - feat(dependency): implement outputs fetching from gcs #3327
Draft
TPXP
wants to merge
1
commit into
gruntwork-io:main
Choose a base branch
from
TPXP:feat-gcs-remote-state-fetch
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.
Draft
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 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ package config | |
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
goErrors "errors" | ||
"fmt" | ||
|
@@ -16,6 +17,7 @@ import ( | |
|
||
"github.com/gruntwork-io/terragrunt/internal/cache" | ||
|
||
"cloud.google.com/go/storage" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/hashicorp/go-getter" | ||
|
@@ -911,6 +913,18 @@ func getTerragruntOutputJSONFromRemoteState( | |
|
||
ctx.TerragruntOptions.Logger.Debugf("Retrieved output from %s as json: %s using s3 bucket", targetTGOptions.TerragruntConfigPath, jsonBytes) | ||
|
||
return jsonBytes, nil | ||
case "gcs": | ||
jsonBytes, err := getTerragruntOutputJSONFromRemoteStateGCS( | ||
targetTGOptions, | ||
remoteState, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx.TerragruntOptions.Logger.Debugf("Retrieved output from %s as json: %s using GCS bucket", targetTGOptions.TerragruntConfigPath, jsonBytes) | ||
|
||
return jsonBytes, nil | ||
default: | ||
ctx.TerragruntOptions.Logger.Errorf("FetchDependencyOutputFromState is not supported for backend %s, falling back to normal method", backend) | ||
|
@@ -990,12 +1004,72 @@ func getTerragruntOutputJSONFromRemoteStateS3(terragruntOptions *options.Terragr | |
} | ||
}(result.Body) | ||
|
||
steateBody, err := io.ReadAll(result.Body) | ||
stateBody, err := io.ReadAll(result.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jsonState := string(stateBody) | ||
jsonMap := make(map[string]interface{}) | ||
|
||
err = json.Unmarshal([]byte(jsonState), &jsonMap) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jsonOutputs, err := json.Marshal(jsonMap["outputs"]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return jsonOutputs, nil | ||
} | ||
|
||
// getTerragruntOutputJSONFromRemoteStateGCS pulls the output directly from a GCS bucket without calling Terraform | ||
func getTerragruntOutputJSONFromRemoteStateGCS( | ||
terragruntOptions *options.TerragruntOptions, | ||
remoteState *remote.RemoteState, | ||
) ([]byte, error) { | ||
terragruntOptions.Logger.Debugf("Fetching outputs directly from gcs://%s/%s/default.tfstate", remoteState.Config["bucket"], remoteState.Config["prefix"]) | ||
|
||
gcsConfigExtended, err := remote.ParseExtendedGCSConfig(remoteState.Config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := remote.ValidateGCSConfig(gcsConfigExtended); err != nil { | ||
return nil, err | ||
} | ||
|
||
var gcsConfig = gcsConfigExtended.RemoteStateConfigGCS | ||
|
||
gcsClient, err := remote.CreateGCSClient(gcsConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bucket := gcsClient.Bucket(gcsConfig.Bucket) | ||
object := bucket.Object(gcsConfig.Prefix + "/default.tfstate") | ||
|
||
reader, err := object.NewReader(context.Background()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if can be propagated current context |
||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func(reader *storage.Reader) { | ||
err := reader.Close() | ||
if err != nil { | ||
terragruntOptions.Logger.Warnf("Failed to close remote state response %v", err) | ||
} | ||
}(reader) | ||
|
||
stateBody, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jsonState := string(steateBody) | ||
jsonState := string(stateBody) | ||
jsonMap := make(map[string]interface{}) | ||
|
||
err = json.Unmarshal([]byte(jsonState), &jsonMap) | ||
|
This file contains 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 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,7 @@ | ||
terraform { | ||
backend "gcs" {} | ||
} | ||
|
||
output "app1_text" { | ||
value = "app1 output" | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixtures/gcs-output-from-remote-state/env1/app1/terragrunt.hcl
This file contains 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,7 @@ | ||
include { | ||
path = find_in_parent_folders() | ||
} | ||
|
||
dependencies { | ||
paths = ["../app3"] | ||
} |
15 changes: 15 additions & 0 deletions
15
test/fixtures/gcs-output-from-remote-state/env1/app2/main.tf
This file contains 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,15 @@ | ||
terraform { | ||
backend "gcs" {} | ||
} | ||
|
||
output "app1_text" { | ||
value = var.app1_text | ||
} | ||
|
||
output "app2_text" { | ||
value = "app2 output" | ||
} | ||
|
||
output "app3_text" { | ||
value = var.app3_text | ||
} |
24 changes: 24 additions & 0 deletions
24
test/fixtures/gcs-output-from-remote-state/env1/app2/terragrunt.hcl
This file contains 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 @@ | ||
include { | ||
path = find_in_parent_folders() | ||
} | ||
|
||
dependency "app1" { | ||
config_path = "../app1" | ||
|
||
mock_outputs = { | ||
app1_text = "(known after apply-all)" | ||
} | ||
} | ||
|
||
dependency "app3" { | ||
config_path = "../app3" | ||
|
||
mock_outputs = { | ||
app3_text = "(known after apply-all)" | ||
} | ||
} | ||
|
||
inputs = { | ||
app1_text = dependency.app1.outputs.app1_text | ||
app3_text = dependency.app3.outputs.app3_text | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixtures/gcs-output-from-remote-state/env1/app2/variables.tf
This file contains 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,7 @@ | ||
variable "app1_text" { | ||
type = string | ||
} | ||
|
||
variable "app3_text" { | ||
type = string | ||
} |
This file contains 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,7 @@ | ||
terraform { | ||
backend "gcs" {} | ||
} | ||
|
||
output "app3_text" { | ||
value = "app3 output" | ||
} |
3 changes: 3 additions & 0 deletions
3
test/fixtures/gcs-output-from-remote-state/env1/app3/terragrunt.hcl
This file contains 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,3 @@ | ||
include { | ||
path = find_in_parent_folders() | ||
} |
This file contains 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,11 @@ | ||
# Configure Terragrunt to automatically store tfstate files in a GCS bucket | ||
remote_state { | ||
backend = "gcs" | ||
|
||
config = { | ||
project = "__FILL_IN_PROJECT__" | ||
location = "__FILL_IN_LOCATION__" | ||
bucket = "__FILL_IN_BUCKET_NAME__" | ||
prefix = "${path_relative_to_include()}/terraform.tfstate" | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
remoteState.Config["key"]
instead of hardcodeddefault.tfstate"
?