Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Conversion from tfjson to consume schema from cli output #174

Merged
merged 7 commits into from
Feb 21, 2022

Conversation

turkenh
Copy link
Member

@turkenh turkenh commented Dec 15, 2021

Description of your changes

Fixes #175 in the scope of #131

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

See the provider-gcp schema generated using the conversion here: crossplane-contrib/provider-jet-gcp#38

Tested with provider-github with the updated jet template in this PR: crossplane-contrib/provider-jet-template#13

@turkenh turkenh requested review from muvaf and ulucinar and removed request for muvaf December 15, 2021 07:12
@turkenh turkenh changed the title Conversion from tfjson to consume schema from cli output [WIP] Conversion from tfjson to consume schema from cli output Dec 15, 2021
@turkenh turkenh force-pushed the conversion-from-tfjson branch from 5773a40 to 617a1c3 Compare January 24, 2022 14:16
@turkenh turkenh force-pushed the conversion-from-tfjson branch 2 times, most recently from 3f6f2fc to 0359734 Compare February 2, 2022 12:30
@turkenh turkenh force-pushed the conversion-from-tfjson branch from 0359734 to 90e8f03 Compare February 3, 2022 07:29
@turkenh turkenh marked this pull request as ready for review February 3, 2022 07:32
@turkenh turkenh changed the title [WIP] Conversion from tfjson to consume schema from cli output Conversion from tfjson to consume schema from cli output Feb 3, 2022
@turkenh turkenh force-pushed the conversion-from-tfjson branch 2 times, most recently from 6d2e2b9 to 4290d82 Compare February 3, 2022 08:55
Copy link
Member

@muvaf muvaf left a comment

Choose a reason for hiding this comment

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

Thanks @turkenh ! Do you have data about whether we see CRD changes with this in bigger providers, like jet-aws or jet-gcp with full set?

pkg/types/conversion/sdkv1/sdkv1.go Show resolved Hide resolved
pkg/types/conversion/tfjson/tfjson.go Show resolved Hide resolved
// What we are trying to achieve here is to convert a lower level
// representation of resource schema map, e.g. output of `terraform providers schema -json`
// to plugin sdk representation. This is mostly the opposite of what the
// following method is doing: https://github.com/hashicorp/terraform-plugin-sdk/blob/7e0a333644f1971a936995677b7a106140a0659f/helper/schema/core_schema.go#L43
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how long we'd like to keep the backward compatibility, especially if there is no CRD API change, but the next step could be to make builder work with CLI schema directly and do the sdk -> cli schema conversion using this referenced tool. You may consider doing that in this PR, too, if the conversions we have here are too similar to that file anyway.

Copy link
Member Author

@turkenh turkenh Feb 9, 2022

Choose a reason for hiding this comment

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

As mentioned in the next comment lines, I agree that ultimately our builder should work with cli schema. However, I still see value to experiment current approach with new providers for a while.

I liked the idea of working with CLI schema in type builder by doing the conversion from sdk -> cli schema, but I wouldn't prefer to do in this PR because:

  • Making type builder working with CLI would require a good amount of work and I would not prefer to delay this feature for new providers since there are some people struggling with the current approach, i.e. importing tf provider go package.
  • Since the cli schema is a more limited representation than the sdk one, we would still lose some information, e.g. int/float distinction or lack of required attribute for a deeply nested object field, etc. So, at this point (having one conversion already implemented), I don't see much benefit of spending time the other way.

Stating this, I believe we can prioritize to work on a follow-up PR updating type builder to work with cli schema and speed up the experimenting process.

@turkenh
Copy link
Member Author

turkenh commented Feb 7, 2022

Thanks @turkenh ! Do you have data about whether we see CRD changes with this in bigger providers, like jet-aws or jet-gcp with full set?

@muvaf, not the full set but there is this PR generated with the default resource set in provider-jet-gcp.

Basically, there are two types of schema changes that I observed:

  • Int64 to Float64 changes in API types and format: int64, type: integer to type: number in corresponding CRD schema
  • Some nested resources lose some information like description or required because of a more limited representation.

I haven't dug further since we first want to experiment with new providers first.

@turkenh turkenh requested a review from muvaf February 9, 2022 08:51
@turkenh
Copy link
Member Author

turkenh commented Feb 9, 2022

@muvaf I've resolved/responded to your comments and this is now ready for another pass.

Copy link
Member

@muvaf muvaf left a comment

Choose a reason for hiding this comment

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

Thanks @turkenh ! LGTM!

I think the only risk with this change is the usage of float64 which upstream folks have some concerns about it. Though I'm not sure how IntOrString type would work in our runtime tools. Maybe it's no different than others since we use fieldpath?

Another thing regarding float is how Terraform works with it, i.e. if we print 0.0 instead of 0 in main.tf.json, would TF CLI and TF provider be OK with that?

pkg/config/provider.go Outdated Show resolved Hide resolved
@turkenh turkenh force-pushed the conversion-from-tfjson branch 2 times, most recently from 2524442 to 03e41b7 Compare February 21, 2022 07:50
@turkenh
Copy link
Member Author

turkenh commented Feb 21, 2022

Ok, I did some testing and writing here for future reference.

I've selected google google_compute_autoscaler resource and it autoscaling_policy.cpu_utilization.target as it expects a float value.

Code Generation:

Here is how it looks like in provider schema tf output:

"target": {
    "type": "number",
    "description": "The target CPU utilization that the autoscaler should maintain.\nMust be a float value in the range (0, 1]. If not specified, the\ndefault is 0.6.\n\nIf the CPU level is below the target utilization, the autoscaler\nscales down the number of instances until it reaches the minimum\nnumber of instances you specified or until the average CPU of\nyour instances reaches the target utilization.\n\nIf the average CPU is above the target utilization, the autoscaler\nscales up until it reaches the maximum number of instances you\nspecified or until the average utilization reaches the target\nutilization.",
    "description_kind": "plain",
    "required": true
}

In the generated schema:

Target *float64 `json:"target" tf:"target,omitempty"`

In CRD Spec:

target:
  description: "The target CPU utilization ...."
  type: number

Runtime:

Created following CR:

apiVersion: compute.gcp.jet.crossplane.io/v1alpha1
kind: Autoscaler
metadata:
  name: numbertest
spec:
  forProvider:
    zone: us-central1-a
    target: projects/<redacted>/zones/us-central1-a/instanceGroupManagers/hasan-another
    autoscalingPolicy:
      - minReplicas: 1
        maxReplicas: 4
        cooldownPeriod: 59
        cpuUtilization:
          - target: 0.43

Generated main.tf by Terrajet:

{
  "resource": {
    "google_compute_autoscaler": {
      "numbertest": {
        "autoscaling_policy": [
          {
            "cooldown_period": 59,
            "cpu_utilization": [
              {
                "target": 0.43
              }
            ],
            "max_replicas": 4,
            "min_replicas": 1
          }
        ]
        ...
      }
    }
  },
}

Resource successfully created and reported Synced & Ready as true. And here is how it looks like in GCP console:

Screen Shot 2022-02-21 at 13 12 41

Then I updated target to 1 as below:

    autoscalingPolicy:
        ...
        cpuUtilization:
          - target: 1

With the generated main.tf:

            "cpu_utilization": [
              {
                "target": 1
              }
            ],

It updated and synced as below:

Screen Shot 2022-02-21 at 13 14 43

And setting this as 1.0 or 1.00 in CR spec does not change anything as k8s API (or kubectl?) returns unchanged with kubectl apply.

@turkenh
Copy link
Member Author

turkenh commented Feb 21, 2022

Thanks @turkenh ! LGTM!

I think the only risk with this change is the usage of float64 which upstream folks have some concerns about it. Though I'm not sure how IntOrString type would work in our runtime tools. Maybe it's no different than others since we use fieldpath?

Another thing regarding float is how Terraform works with it, i.e. if we print 0.0 instead of 0 in main.tf.json, would TF CLI and TF provider be OK with that?

@muvaf in the above experiment, I've validated that we can work with numbers and can both provide integers and/or floats for such fields. With that, I am feeling good with float64.

Signed-off-by: Hasan Turken <turkenh@gmail.com>
Signed-off-by: Hasan Turken <turkenh@gmail.com>
Signed-off-by: Hasan Turken <turkenh@gmail.com>
Signed-off-by: Hasan Turken <turkenh@gmail.com>
Signed-off-by: Hasan Turken <turkenh@gmail.com>
Signed-off-by: Hasan Turken <turkenh@gmail.com>
Fixes crossplane#226

Signed-off-by: Hasan Turken <turkenh@gmail.com>
@turkenh turkenh force-pushed the conversion-from-tfjson branch from 03e41b7 to 9a0c15d Compare February 21, 2022 10:22
@turkenh turkenh merged commit 391b759 into crossplane:main Feb 21, 2022
@turkenh turkenh deleted the conversion-from-tfjson branch February 21, 2022 10:28
@muvaf
Copy link
Member

muvaf commented Feb 22, 2022

@turkenh Thanks for that extensive test! I think this could warrant a Terrajet release.

@muvaf
Copy link
Member

muvaf commented Feb 22, 2022

Actually let's get #195 as well included in the release.

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

Successfully merging this pull request may close these issues.

Generate provider schema only with CLI output
2 participants