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

🐛 String output does not have expected format #284

Closed
LennartKoot opened this issue Oct 24, 2023 · 1 comment · Fixed by #289
Closed

🐛 String output does not have expected format #284

LennartKoot opened this issue Oct 24, 2023 · 1 comment · Fixed by #289
Labels
bug Something isn't working

Comments

@LennartKoot
Copy link

Operator Version, Kind and Kubernetes Version

  • Operator version: 2.0.0-beta8
  • Kind: Module and Workspace
  • Kubernetes version: 1.26.3

YAML Manifest File

apiVersion: app.terraform.io/v1alpha2
kind: Module
metadata:
  name: module
spec:
  organization: organization
  name: some-module
  token:
    secretKeyRef:
      name: tfc-token
      key: token
  destroyOnDeletion: true
  module:
    source: some-module
    version: 1.0.0
  workspace:
    name: some_workspace
  outputs:
  - name: string_output
    sensitive: false

Output Log

N/A

Output of relevant kubectl commands

N/A

Steps To Reproduce

  1. Add a Workspace
  2. Add a Module that runs on the added workspace that will have a string output:
output "string_output" {
  value = <<EOT
some string
with a newline
EOT
}
  1. Wait for the module to run.
  2. Check the created value in the outputs ConfigMap.

Expected Behavior

I expect to have the actual string value in the ConfigMap, so with actual newline characters.

Actual Behavior

The string in the output contains \n as the actual characters instead of a newline.

Additional Context

According to this piece of code, all output is parsed to JSON, which makes sense for objects as output:
https://github.com/hashicorp/terraform-cloud-operator/blob/082ac4aa99b593b5b79ccde99abc0ae0de48dbc0/controllers/module_controller_outputs.go#L80C2-L92C3

(BTW, this same logic is also present in workspace_controller_outputs.go)

Because of the trimming of the ", the string output is not a valid JSON string and cannot be unmarshaled back to a string.
But even if it would be a valid JSON string, it still makes working with string outputs difficult, as you always have to add some logic before using the string. Especially in the case where you are mounting the ConfigMap key to a file, it would be nice to have the actual string value so the file contains actual newlines (and other special characters) and you can just read the file directly without worrying about the format.

For a real world example; We encountered this while creating an OpenSSH key and wanting to mount the key as a file so our application can use it. It broke while moving from the Flux Terraform Controller to this operator. (We have worked around this issue for now by replacing \n with actual newlines when reading the file).

References

N/A

Community Note

  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
@LennartKoot LennartKoot added the bug Something isn't working label Oct 24, 2023
@arybolovlev
Copy link
Contributor

Hi @LennartKoot,

Thank you for reporting this issue. It happens during JSON marshaling, this operation adds an escape symbol to \n as it is a reserved one in JSON.

Here is the output I used to validate this issue:

output "multiline_string" {
  value     = <<EOT
hello
world
EOT
  sensitive = false
}

Before marshaling the string that TFC returns looks like this: "hello\nworld", after marshaling "\"hello\\nworld\"".

I have prepared a fix that will introduce better handling for different types that Terraform supports. With this fix the above outputs looks like this in a config map:

apiVersion: v1
data:
  multiline_string: |
    hello
    world
  number: "5"
kind: ConfigMap

I hope it will be available in the next version.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants