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

Add Terraform outputs to status.atProvider #38

Merged
merged 6 commits into from
Apr 6, 2022

Conversation

bobh66
Copy link
Collaborator

@bobh66 bobh66 commented Mar 27, 2022

Signed-off-by: Bob Haddleton bob.haddleton@nokia.com

Description of your changes

Add the non-sensitive Terraform module outputs to the status.atProvider.outputs section of the Workspace object

Fixes #36

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable test to ensure this PR is ready for review.

How has this code been tested

I tested these changes locally in a kind cluster using a mock Terraform module that outputs all 5 value types:

Spec:
  Deletion Policy:  Delete
  For Provider:
    Module:  // Modules _must_ use remote state. The provider does not persist state.
terraform {
  backend "kubernetes" {
    secret_suffix     = "xp"
    namespace         = "default"
    config_path = "/home/bobh/.kube/config"
    config_context = "kind-provider-terraform-dev"
  }
}
resource "random_integer" "foo" {
        min = 1
        max = 10
}
output "string" {
  value = "bar"
  sensitive = false
}

output "number" {
  value = 1.9
  sensitive = false
}

output "object" {
  value = random_integer.foo
  sensitive = false
}

output "tuple" {
  value = ["foo", "bar"]
  sensitive = false
}

output "bool" {
  value = false
  sensitive = false
}

output "sensitive" {
  value = "SENSITIVE"
  sensitive = true
}
    Source:  Inline
  Provider Config Ref:
    Name:  default
Status:
  At Provider:
    Outputs:
      Bool:    false
      Number:  1.9
      Object:  {"id":"9","keepers":null,"max":10,"min":1,"result":9,"seed":null}
      String:  bar
      Tuple:   ["foo","bar"]

Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>
@ytsarev
Copy link
Collaborator

ytsarev commented Mar 28, 2022

@bobh66 thanks a lot for your contribution! Can you please investigate make check-diff locally?

Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>
@bobh66
Copy link
Collaborator Author

bobh66 commented Mar 28, 2022

Hi @ytsarev - should be fixed now. Thanks!

Copy link
Collaborator

@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.

Overall looks great, couple of suggestions

internal/controller/workspace/workspace.go Outdated Show resolved Hide resolved
internal/controller/workspace/workspace_test.go Outdated Show resolved Hide resolved
Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>
Copy link
Collaborator

@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.

Please make check-diff again and commit :)

@bobh66
Copy link
Collaborator Author

bobh66 commented Mar 31, 2022

Hi @ytsarev - it appears that exposing a "generic" output object which can contain any type to the API is either impossible or beyond my limited understanding of go. I can get it to work functionally but the tools complain about exposing an interface{} to the API schema. I thought about reusing the ConnectionDetails format but that converts everything to bytes which end up base64 encoded. Unless you have suggestions for exposing a generic object as status output, I think I need to go back to the string format for everything. The Crossplane transform "convert" option can convert strings to int/bool/etc. That leaves tuples and maps in their stringified format, so anyone using Terraform will need to expose the specific output values that they want individually, instead of exposing an entire tuple or map. Any ideas how to make the output more generic while keeping the tools happy would be greatly appreciated. Thanks!

Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>
@bobh66
Copy link
Collaborator Author

bobh66 commented Apr 4, 2022

@ytsarev lint works locally, does this need a retry? Thanks

@ytsarev
Copy link
Collaborator

ytsarev commented Apr 4, 2022

@bobh66 lint job also fails for me in another PR and also works locally - will investigate.

Regarding 'generic' status, I recall you were initially using x-kubernetes-preserve-unknown-fields which conceptually should allow arbitrary JSON. Why it didn't work?

@bobh66
Copy link
Collaborator Author

bobh66 commented Apr 4, 2022

@ytsarev When I used preserve-unknown-fields I get this error from check-diff:

github.com/crossplane-contrib/provider-terraform/apis/v1alpha1:-: name requested for invalid type interface{}
github.com/crossplane-contrib/provider-terraform/apis/v1alpha1:-: invalid map value type interface{}
Error: /home/runner/work/provider-terraform/provider-terraform/apis/v1alpha1/workspace_types.go:116:21: map values must be a named type, not *ast.InterfaceType

https://github.com/crossplane-contrib/provider-terraform/runs/5767133263?check_suite_focus=true#step:8:16

which I interpreted as saying that it is not allowed to have an object defined in the API without a specified schema.

If there is a different/better way to do this I'd be happy to go back to that implementation, but I could not see a way forward from these errors.

Thanks

@ytsarev
Copy link
Collaborator

ytsarev commented Apr 4, 2022

@bobh66 you indeed can't return interface but you should be able to return JSON and/or use https://pkg.go.dev/reflect to dynamically control the type... I will try to play with it soon

@ytsarev
Copy link
Collaborator

ytsarev commented Apr 4, 2022

@bobh66 re lint job, it is fixed in 5f0be75 , please rebase with master. Thanks!

@bobh66
Copy link
Collaborator Author

bobh66 commented Apr 5, 2022

@ytsarev What would you think about merging this as a first step, after any needed rework, and then in a separate PR we can refactor as needed to support tuples and objects? This change provides access to strings and numbers, which will be the majority of use cases.

@ytsarev
Copy link
Collaborator

ytsarev commented Apr 6, 2022

@bobh66 agreed! Can you please rebase with master again and we are good to merge

@bobh66
Copy link
Collaborator Author

bobh66 commented Apr 6, 2022

@ytsarev - done. Thanks!

Copy link
Collaborator

@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.

@bobh66 incredibly useful contribution, thanks a lot!

@ytsarev ytsarev merged commit 262445d into crossplane-contrib:master Apr 6, 2022
bobh66 referenced this pull request in bobh66/provider-terraform Jul 10, 2022
* Add Terraform outputs to status.atProvider

Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>

* Add autogenerated code changes

Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>

* Refactor code to use native types and add documentation to README file

Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>

* Revert back to the stringified values, as interface cannot be exposed.

Signed-off-by: Bob Haddleton <bob.haddleton@nokia.com>
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.

Publish non-sensitive outputs in atProvider
2 participants