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

Terraform Plan Output in Status Field #226

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

Conversation

balu-ce
Copy link

@balu-ce balu-ce commented Jan 5, 2024

Description of your changes

This PR is to add the terraform plan output in the status field of crd. This can be integrated with managment polices to apply based on plan

Fixes #223

I have:

  • Run make reviewable to ensure this PR is ready for review.

How has this code been tested

With Local setup as well as been deployed in one of our eks cluster and tested it

@Upbound-CLA
Copy link

Upbound-CLA commented Jan 5, 2024

CLA assistant check
All committers have signed the CLA.

@balu-ce balu-ce requested a review from samof76 January 8, 2024 06:25
internal/terraform/terraform.go Show resolved Hide resolved
internal/terraform/terraform.go Show resolved Hide resolved
internal/terraform/terraform.go Outdated Show resolved Hide resolved
internal/terraform/terraform.go Outdated Show resolved Hide resolved
internal/terraform/terraform.go Outdated Show resolved Hide resolved
apis/v1beta1/workspace_types.go Outdated Show resolved Hide resolved
@ulucinar
Copy link
Contributor

Thank you @balu-ce for the PR. I believe the plan output in the status can be beneficial in understanding the modifications to be applied to the external resource, and as described in this PR, it can be used in tandem with the management policies to implement a workflow where the updates to the external resources are first reviewed and then approved (via adding the Update management policy).

My biggest concern here is if the plan output contains sensitive information, then we will be exposing it in status.atProvider. Do we know whether Terraform can potentially leak sensitive data in the plan output?

balu-ce and others added 6 commits January 22, 2024 19:49
Co-authored-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
Co-authored-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
Co-authored-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
Co-authored-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
Co-authored-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
Co-authored-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
@balu-ce
Copy link
Author

balu-ce commented Jan 22, 2024

I agree @ulucinar , can the team have their variable as sensitive in terraform defined so that the values will not be shown in plan . For ref hashicorp/terraform#27577.
https://developer.hashicorp.com/terraform/tutorials/configuration-language/sensitive-variables

@balu-ce balu-ce requested a review from ulucinar January 22, 2024 14:34
Copy link
Member

@ytsarev ytsarev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there are valid concerns around the size of CR and performance / functional issues that it may bring, I suggest we make this functionality optional and controlled by the flag in ProviderConfig.

@balu-ce
Copy link
Author

balu-ce commented Jan 28, 2024

I agree that we can have a flag but cant we just keep it in workspace object itself . Any Specific reason to bring at config level ?

@balu-ce balu-ce requested a review from ytsarev January 28, 2024 06:45
@ytsarev
Copy link
Member

ytsarev commented Jan 29, 2024

@balu-ce you are right, let's keep it in Workspace spec, it will provide more granular control.

@balu-ce
Copy link
Author

balu-ce commented Jan 29, 2024

@ytsarev Please review the PR, added a conditional value under workspace spec

@ytsarev
Copy link
Member

ytsarev commented Jan 30, 2024

/test-examples="examples/workspace-inline-aws.yaml"

@balu-ce
Copy link
Author

balu-ce commented Jan 31, 2024

@ytsarev Added in example

@balu-ce balu-ce requested a review from negz January 31, 2024 06:04
apis/v1beta1/workspace_types.go Outdated Show resolved Hide resolved
apis/v1beta1/workspace_types.go Outdated Show resolved Hide resolved
apis/v1beta1/workspace_types.go Outdated Show resolved Hide resolved
@negz
Copy link
Member

negz commented Jan 31, 2024

For the record, I'm not convinced that we should do this at all. See #223 (comment).

Copy link
Collaborator

@bobh66 bobh66 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the value in this but I'm not sure it should be in status. @ytsarev what do you think? I can really go either way.

@@ -13,6 +13,7 @@ spec:
# For simple cases you can use an inline source to specify the content of
# main.tf as opaque, inline HCL.
source: Inline
showPlan: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be includePlan ?

@ytsarev
Copy link
Member

ytsarev commented Mar 26, 2024

@bobh66 what alternatives to status do you have in mind?

@bobh66
Copy link
Collaborator

bobh66 commented Mar 26, 2024

@ytsarev two possibilities:

  • an Event with the plan stored in the message field
  • a Plan child resource of the Workspace

both have advantages and drawbacks, as does the status, and I don't have a good feeling for which is "better"

@ytsarev
Copy link
Member

ytsarev commented Apr 3, 2024

@bobh66 I don't have a preference either:

  • the tf plan seems too much for the single Event
  • New Plan resource seems to be overkilling solution

status to figure out what is happening without getting into the pod seems to be 'good enough'.

On the other hand, we should probably consolidate this PR with #248 as there is an intersecting data stream in both.

Copy link
Member

@ytsarev ytsarev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@balu-ce

Thanks a lot for your contribution!

I've performed the manual e2e test of this PR with https://github.com/upbound/provider-terraform/blob/main/examples/workspace-inline-aws.yaml and spec.forProvider.includePlan: true

Unfortunately I was not able to see the plan in status apart from No change in the terraform plan message:

  status:
    atProvider:
      checksum: b1d1f9e12361c1ef23e23c7efd1eccff
      outputs:
        subnet_data:
          arn: arn:aws:ec2:eu-central-1:123456:subnet/subnet-06fa8cd7759bb8832
          id: subnet-06fa8cd7759bb8832
        vpc_id: vpc-0332256723323320a
      tfPlan: No change in the terraform plan

Whenever I try to emulate the configuration drift by manually editing Name tag on VPC resource, the Workspace reconciles but at that moment the tfPlan field disappears from the status:

  status:
    atProvider:
      outputs:
        subnet_data:
          arn: arn:aws:ec2:eu-central-1:123456:subnet/subnet-06fa8cd7759bb8832
          id: subnet-06fa8cd7759bb8832
        vpc_id: vpc-0332256723323320a

After another reconciliation loop it gets back to tfPlan: No change in the terraform plan

So effectively I do not see any actional output in this scenario. Am I missing something with my test flow?

Please also squash the commits in this PR as some of them are conflicting with the rebase with main

Thanks!

@ytsarev
Copy link
Member

ytsarev commented Apr 3, 2024

@bobh66 after the above e2e experiment - the Event actually looks like a better candidate - we could look into the tf plan data that relates to the previous reconciliation loops

@bobh66
Copy link
Collaborator

bobh66 commented Apr 3, 2024

@ytsarev The plan text could be gzipped/base64-encoded to keep it small, and the Event should only be created when there is a diff detected. The number of available plan Events will depend on how long the API server retains the data. It seems like a reasonable "kubernetes-ish" solution.

@bobh66
Copy link
Collaborator

bobh66 commented Apr 3, 2024

Another option would be to converge with #248 and send the data to the pod logs, though I do like the idea of creating an Event when a diff is detected and including the plan output in the Event.

@balu-ce
Copy link
Author

balu-ce commented May 14, 2024

@ytsarev @bobh66 This tfPlan disappeared due to it sets the status object again. Basically the update is not calling the observe. Also the use of show plan is to integrate with conditional before apply. If you execute the code with setting management policy to only observe, it will work as expected. In this way we can integrate with tight approval or we need to call observe in update

@ytsarev
Copy link
Member

ytsarev commented May 14, 2024

@balu-ce thanks for the clarification. In this case, this flow requires documentation.

@balu-ce
Copy link
Author

balu-ce commented May 14, 2024

@ytsarev Yes, Agree.

@balu-ce
Copy link
Author

balu-ce commented May 15, 2024

@ytsarev @bobh66 Is there is any plan to bring this feature in next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Policy as Code against the planfile on the Update / Create
7 participants