Skip to content

Commit

Permalink
Remove unused net/rpc based plugin implementations (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Mar 21, 2022
1 parent a74d3f5 commit be882d0
Show file tree
Hide file tree
Showing 42 changed files with 163 additions and 2,803 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NOTE: This plugin system is experimental. This means that API compatibility is f

## Requirements

- TFLint v0.30+
- TFLint v0.35+
- Go v1.18

## Usage
Expand All @@ -23,6 +23,8 @@ For more details on the API, see [tflint](https://pkg.go.dev/github.com/terrafor

![architecture](architecture.png)

This plugin system uses [go-plugin](https://github.com/hashicorp/go-plugin). TFLint launches the plugin as a sub-process and communicates with the plugin over RPC. The plugin acts as a server, while TFLint acts as a client that sends inspection requests to the plugin.
This plugin system uses [go-plugin](https://github.com/hashicorp/go-plugin). TFLint launches the plugin as a sub-process and communicates with the plugin over gRPC. The plugin acts as a server, while TFLint acts as a client that sends inspection requests to the plugin.

On the other hand, the plugin sends various requests to a server (TFLint) to get detailed runtime contexts (e.g. variables and expressions). This means that TFLint and plugins can act as both a server and a client.

These implementations are included in the [plugin/host2plugin](https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk/plugin/host2plugin) and [plugin/plugin2host]((https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk/plugin/plugin2host)) packages.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ require (
github.com/google/go-cmp v0.5.7
github.com/hashicorp/go-hclog v1.2.0
github.com/hashicorp/go-plugin v1.4.3
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/zclconf/go-cty v1.10.0
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.27.1
Expand Down
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,13 @@ github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM=
github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-plugin v1.4.3 h1:DXmvivbWD5qdiBts9TpBC7BYL1Aia5sxbRgQB+v6UZM=
github.com/hashicorp/go-plugin v1.4.3/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc=
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
Expand Down Expand Up @@ -113,7 +107,6 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty v1.10.0 h1:mp9ZXQeIcN8kAwuqorjH+Q+njbJKjLrvB2yIh4q7U+0=
Expand All @@ -136,12 +129,10 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
2 changes: 1 addition & 1 deletion helper/doc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package helper contains implementations for plugin testing.
//
// You can test the implemented rules using the mock Runner that is not
// an RPC client. It is similar to TFLint's Runner, but is implemented
// an gRPC client. It is similar to TFLint's Runner, but is implemented
// from scratch to avoid Terraform dependencies.
//
// Some implementations of the mock Runner have been simplified. As a result,
Expand Down
30 changes: 15 additions & 15 deletions helper/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
Expand All @@ -15,11 +14,18 @@ import (

// Runner is a mock that satisfies the Runner interface for plugin testing.
type Runner struct {
files map[string]*hcl.File
Issues Issues

tfconfig *configs.Config
config Config
files map[string]*hcl.File
config Config
variables map[string]*Variable
}

// Variable is an implementation of variables in Terraform language
type Variable struct {
Name string
Default cty.Value
DeclRange hcl.Range
}

// Config is a pseudo TFLint config file object for testing from plugins.
Expand Down Expand Up @@ -141,7 +147,7 @@ func (r *Runner) EvaluateExpr(expr hcl.Expression, ret interface{}, opts *tflint
}

variables := map[string]cty.Value{}
for _, variable := range r.tfconfig.Module.Variables {
for _, variable := range r.variables {
variables[variable.Name] = variable.Default
}
workspace, success := os.LookupEnv("TF_WORKSPACE")
Expand Down Expand Up @@ -203,12 +209,6 @@ func (r *Runner) AddLocalFile(name string, file *hcl.File) bool {
}

func (r *Runner) initFromFiles() error {
r.tfconfig = &configs.Config{
Module: &configs.Module{
Variables: map[string]*configs.Variable{},
},
}

for _, file := range r.files {
content, _, diags := file.Body.PartialContent(configFileSchema)
if diags.HasErrors() {
Expand All @@ -218,11 +218,11 @@ func (r *Runner) initFromFiles() error {
for _, block := range content.Blocks {
switch block.Type {
case "variable":
variable, diags := simpleDecodeVariableBlock(block)
variable, diags := decodeVariableBlock(block)
if diags.HasErrors() {
return diags
}
r.tfconfig.Module.Variables[variable.Name] = variable
r.variables[variable.Name] = variable
default:
continue
}
Expand All @@ -232,8 +232,8 @@ func (r *Runner) initFromFiles() error {
return nil
}

func simpleDecodeVariableBlock(block *hcl.Block) (*configs.Variable, hcl.Diagnostics) {
v := &configs.Variable{
func decodeVariableBlock(block *hcl.Block) (*Variable, hcl.Diagnostics) {
v := &Variable{
Name: block.Labels[0],
DeclRange: block.DefRange,
}
Expand Down
2 changes: 1 addition & 1 deletion helper/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (r *dummyRule) Enabled() bool { return true }
func (r *dummyRule) Severity() tflint.Severity { return tflint.ERROR }
func (r *dummyRule) Check(tflint.Runner) error { return nil }

func Test_EmitIssueOnExpr(t *testing.T) {
func Test_EmitIssue(t *testing.T) {
src := `
resource "aws_instance" "foo" {
instance_type = "t2.micro"
Expand Down
76 changes: 0 additions & 76 deletions plugin/client.go

This file was deleted.

7 changes: 2 additions & 5 deletions plugin/doc.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Package plugin contains the implementations needed to make
// the built binary act as a plugin.
//
// A plugin is implemented as an RPC server and the host acts
// A plugin is implemented as an gRPC server and the host acts
// as the client, sending analysis requests to the plugin.
// Note that the server-client relationship here is the opposite of
// the communication that takes place during the checking phase.
//
// Implementation details are hidden in go-plugin. This package is
// essentially a wrapper for go-plugin.
// See host2plugin for implementation details.
package plugin
35 changes: 6 additions & 29 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
package plugin

import (
"encoding/gob"
"net/rpc"

plugin "github.com/hashicorp/go-plugin"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/host2plugin"

// Import this package to initialize the global logger
_ "github.com/terraform-linters/tflint-plugin-sdk/logger"
)

// handShakeConfig is used for UX. ProcotolVersion will be updated by incompatible changes.
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 9,
MagicCookieKey: "TFLINT_RULESET_PLUGIN",
MagicCookieValue: "5adSn1bX8nrDfgBqiAqqEkC6OE1h3iD8SqbMc5UUONx8x3xCF0KlPDsBRNDjoYDP",
}

// RuleSetPlugin is a wrapper to satisfy the interface of go-plugin.
type RuleSetPlugin struct {
impl tflint.RPCRuleSet
}

// Server returns an RPC server acting as a plugin.
func (p *RuleSetPlugin) Server(b *plugin.MuxBroker) (interface{}, error) {
return &Server{impl: p.impl, broker: b}, nil
}

// Client returns an RPC client for the host.
func (RuleSetPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &Client{rpcClient: c, broker: b}, nil
}
// ServeOpts is an option for serving a plugin.
// Each plugin can pass a RuleSet that represents its own functionality.
type ServeOpts = host2plugin.ServeOpts

func init() {
gob.Register(tflint.Error{})
}
// Serve is a wrapper of plugin.Serve. This is entrypoint of all plugins.
var Serve = host2plugin.Serve
59 changes: 0 additions & 59 deletions plugin/server.go

This file was deleted.

28 changes: 0 additions & 28 deletions terraform/addrs/module.go

This file was deleted.

Loading

0 comments on commit be882d0

Please sign in to comment.