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

Use custom Terraform provider release to generate schema.json #1318

Merged

Conversation

mergenci
Copy link
Collaborator

@mergenci mergenci commented May 22, 2024

Description of your changes

Code generation depends on JSON schema of terraform-provider-aws (config/schema.json). Converting a Terraform provider's schema to JSON is performed by Terraform CLI, which requires a provider binary. Therefore, when we introduce a new resource, such as MQ User, to our fork of terraform-provider-aws, we also release the provider as binary. This PR introduces necessary changes to download custom provider release and let Terraform CLI use it instead of the official provider.

The following is an example .terraformrc configuration generated by make:

provider_installation {
  filesystem_mirror {
    path = ".cache/tools/darwin_arm64"
    include = ["hashicorp/aws"]
  }
  direct {
    exclude = ["hashicorp/aws"]
  }
}

When using the above configuration, Terraform CLI locates the provider binary at registry.terraform.io/hashicorp/aws/5.31.0/darwin_arm64/terraform-provider-aws, where 5.31.0 is the example provider version, inside the filesystem mirror directory.

Custom provider release is installed at .cache/tools/darwin_arm64/registry.terraform.io/hashicorp/aws/5.31.0/darwin_arm64/terraform-provider-aws on an Apple Silicon Mac. On other platforms, installation location is the equivalent path, for instance .cache/tools/linux_amd64/registry.terraform.io/hashicorp/aws/5.31.0/linux_amd64/terraform-provider-aws on an Intel/AMD Linux).

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

  1. Remove config/schema.json.
  2. Run make config/schema.json.
  3. Observe that config/schema.json is created.

Above steps will not result in any difference in config/schema.json. During development, I used a development build of the provider that has a new resource, aws_mq_user, added. In that case, I observed that aws_mq_user schema exists in the output, config/schema.json.

@mergenci mergenci force-pushed the custom-terraform-provider-build branch 3 times, most recently from 76feab1 to b7dfb86 Compare May 23, 2024 10:39
Copy link
Collaborator

@ulucinar ulucinar left a comment

Choose a reason for hiding this comment

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

Thanks @mergenci for changing the provider's build pipeline so that we can use the newly added native resource schemas during the code generation phase. We will hopefully remove the need for this when we switch from using the JSON provider schema to the Go schema.

@@ -169,12 +172,21 @@ $(TERRAFORM):
@rm -fr $(TOOLS_HOST_DIR)/tmp-terraform
@$(OK) installing terraform $(HOSTOS)-$(HOSTARCH)

$(TERRAFORM_PROVIDER_SCHEMA): $(TERRAFORM)
$(TERRAFORM_PROVIDER):
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to incorporate the upjet iteration from the consumed release version as part of this target's name. Otherwise, when for instance, we release v5.31.0-upjet.2, this target may decide not to download the correct version and use the older, already cached v5.31.0-upjet.1 version.

Makefile Outdated
@@ -169,12 +172,21 @@ $(TERRAFORM):
@rm -fr $(TOOLS_HOST_DIR)/tmp-terraform
@$(OK) installing terraform $(HOSTOS)-$(HOSTARCH)

$(TERRAFORM_PROVIDER_SCHEMA): $(TERRAFORM)
$(TERRAFORM_PROVIDER):
@$(INFO) installing terraform provider $(SAFEHOST_PLATFORM)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
@$(INFO) installing terraform provider $(SAFEHOST_PLATFORM)
@$(INFO) installing terraform AWS provider for the platform $(SAFEHOST_PLATFORM)

$(TERRAFORM_PROVIDER):
@$(INFO) installing terraform provider $(SAFEHOST_PLATFORM)
@mkdir -p $$(dirname $(TERRAFORM_PROVIDER))
@curl -fsSL https://github.com/upbound/terraform-provider-aws/releases/download/$(TERRAFORM_PROVIDER_RELEASE)/terraform-provider-aws_$(TERRAFORM_PROVIDER_RELEASE)_$(SAFEHOST_PLATFORM).zip -o $(TERRAFORM_PROVIDER).zip
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please see the comment above about incorporating the upjet release iteration number.

@rm -fr $(TERRAFORM_PROVIDER).zip
@$(OK) installing terraform provider $(SAFEHOST_PLATFORM)

$(TERRAFORM_PROVIDER_SCHEMA): $(TERRAFORM) $(TERRAFORM_PROVIDER)
@$(INFO) generating provider schema for $(TERRAFORM_PROVIDER_SOURCE) $(TERRAFORM_PROVIDER_VERSION)
@mkdir -p $(TERRAFORM_WORKDIR)
@echo '{"terraform":[{"required_providers":[{"provider":{"source":"'"$(TERRAFORM_PROVIDER_SOURCE)"'","version":"'"$(TERRAFORM_PROVIDER_VERSION)"'"}}],"required_version":"'"$(TERRAFORM_VERSION)"'"}]}' > $(TERRAFORM_WORKDIR)/main.tf.json
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Currently when you examine the provider block, it looks like we are using a vanilla Terraform AWS provider (because the URI is till hashicorp/aws:v5.31.0) however, we are using a custom built binary. We may, in a future PR, consider making this more explicit by changing the Terraform provider URI.

Makefile Outdated
@$(INFO) installing terraform provider $(SAFEHOST_PLATFORM)
@mkdir -p $$(dirname $(TERRAFORM_PROVIDER))
@curl -fsSL https://github.com/upbound/terraform-provider-aws/releases/download/$(TERRAFORM_PROVIDER_RELEASE)/terraform-provider-aws_$(TERRAFORM_PROVIDER_RELEASE)_$(SAFEHOST_PLATFORM).zip -o $(TERRAFORM_PROVIDER).zip
@unzip $(TERRAFORM_PROVIDER).zip -d $$(dirname $(TERRAFORM_PROVIDER))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
@unzip $(TERRAFORM_PROVIDER).zip -d $$(dirname $(TERRAFORM_PROVIDER))
@unzip $(TERRAFORM_PROVIDER).zip -d $(dir $(TERRAFORM_PROVIDER))

Signed-off-by: Cem Mergenci <cmergenci@gmail.com>
@mergenci mergenci force-pushed the custom-terraform-provider-build branch from b7dfb86 to 2932f78 Compare May 23, 2024 11:41
Copy link
Collaborator

@ulucinar ulucinar left a comment

Choose a reason for hiding this comment

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

Thanks @mergenci, lgtm.

@mergenci
Copy link
Collaborator Author

Thank you for your valuable reviews @ulucinar 🙏

@mergenci mergenci merged commit 55737bd into crossplane-contrib:main May 23, 2024
11 checks passed
@mergenci mergenci deleted the custom-terraform-provider-build branch May 23, 2024 12:17
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.

2 participants