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 support for gRPC server for the new plugin system #1292

Merged
merged 5 commits into from
Mar 21, 2022
Merged

Conversation

wata727
Copy link
Member

@wata727 wata727 commented Dec 31, 2021

See also terraform-linters/tflint-plugin-sdk#135

This PR adds support for gRPC server for the new plugin system. See terraform-linters/tflint-plugin-sdk#135 for motivation and purpose for this transition. The gRPC server implements:

  • GetModuleContent
    • Corresponds to net/rpc's Attributes(), Blocks(), Resources(), ModuleCalls(), Backend(), Config(), and RootProvider() methods. Returns a module content based on the passed schema. The interface is inspired by the hcl package. Also, by taking a module context, you can now access the root module in the same way.
  • GetFile
    • Corresponds to net/rpc's File() method.
  • GetFiles
    • Corresponds to net/rpc's Files() method. Newly, this method takes a module context and can return a list of files in the root module.
  • GetRuleConfigContent
    • Corresponds to net/rpc's RuleConfig() method. This method takes a schema as an argument, and now you can get attributes more flexibly.
  • EvaluateExpr
    • Corresponds to net/rpc's EvalExpr(), EvalExprOnRootCtx(), and IsNullExpr() methods. Supports evaluation of the root module by taking a module context as an argument. We also added cty.DynamicPseudoType as a valid type, which allows decoding to cty.Value. Therefore, IsNullExpr has been removed (you can decode to cty.Value from expr, and check the return value of cty.Value.IsNull()).
  • EmitIssue
    • Corresponds to net/rpc's EmitIssue(). Unlike before, it does not request the expr that caused the issue. Instead, the range of the issue determines if it is occurring on the expr.

The biggest change is support for GetModuleContent. Previously, we used Terraform's internal packages to get attributes and blocks from configs.Config, but this should be avoided for the purposes of this transition. To achieve this, GetModuleContent gets the attributes and blocks based on the schema from the hcl.File parsed in the standard way.

Some Terraform language semantics need to be reproduced to eliminate the dependency on Terraform internal packages. Here, the following is reimplemented:

Unlike in a previous way, these are not decoded in advance but are decoded and resolved at the time of getting contents. In particular, dynamic blocks need to be resolved recursively, so performance degradation is expected. However, performance issues are left here as future improvements.

These implementations exist in Runner, but this is obviously not a good idea. Perhaps it should be implemented in another layer like Loader. However, here I dare to implement it this way to minimize changes. Refactoring, including removing the net/rpc-based implementation, will be done later.

Also, in the new SDK, the tflint.Error has been removed and standard error wrapping (fmt.Errorf("%w", err)) has been adopted. Along with that, custom errors have been removed from TFLint as well. Others include following breaking changes in the SDK, such as changing the rule severity from a string to an enum.

@wata727 wata727 force-pushed the grpc_plugin branch 5 times, most recently from 5cecedd to c25d2e5 Compare January 10, 2022 12:52
@wata727 wata727 force-pushed the grpc_plugin branch 5 times, most recently from 27ca3b0 to 2be3d98 Compare February 13, 2022 08:28
@wata727 wata727 force-pushed the grpc_plugin branch 3 times, most recently from 974ecfd to 55b4b9e Compare March 13, 2022 16:05
@wata727 wata727 marked this pull request as ready for review March 13, 2022 17:02
@wata727 wata727 merged commit 4a1f43c into master Mar 21, 2022
@wata727 wata727 deleted the grpc_plugin branch March 21, 2022 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant