Skip to content

Commit

Permalink
Add cofidectl-test-plugin
Browse files Browse the repository at this point in the history
This gRPC plugin implements data source and provision plugins using the
default local and spire-helm plugins respectively.

It allows for integration testing of the gRPC plugin mechanism.

Fixes: #4
  • Loading branch information
markgoddard committed Dec 11, 2024
1 parent 92c6d2e commit 638a843
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cofide.yaml

# Plugins
cofidectl-connect
cofidectl-test-plugin

# cofide-demos repository
demos/cofide-demos/
7 changes: 7 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
build: test
go build -o cofidectl ./cmd/cofidectl/main.go

build-test-plugin:
go build -o cofidectl-test-plugin ./cmd/cofidectl-test-plugin/main.go

install-test-plugin: build-test-plugin
mkdir -p ~/.cofide/plugins
cp cofidectl-test-plugin ~/.cofide/plugins

test:
go run gotest.tools/gotestsum@latest --format github-actions ./...

Expand Down
74 changes: 74 additions & 0 deletions cmd/cofidectl-test-plugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 Cofide Limited.
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"
"log"
"os"

"github.com/cofide/cofidectl/internal/pkg/config"
cmdcontext "github.com/cofide/cofidectl/pkg/cmd/context"
"github.com/cofide/cofidectl/pkg/plugin"
"github.com/cofide/cofidectl/pkg/plugin/datasource"
"github.com/cofide/cofidectl/pkg/plugin/local"
"github.com/cofide/cofidectl/pkg/plugin/provision"
"github.com/cofide/cofidectl/pkg/plugin/provision/spirehelm"
"github.com/hashicorp/go-hclog"
go_plugin "github.com/hashicorp/go-plugin"
)

const (
cofideConfigFile = "cofide.yaml"
)

func main() {
log.SetFlags(0)
if err := run(); err != nil {
// This should be the only place that calls os.Exit, to ensure proper clean up.
// This includes functions that call os.Exit, e.g. cobra.CheckErr, log.Fatal
os.Exit(1)
}
}

func run() error {
cmdCtx := cmdcontext.NewCommandContext(cofideConfigFile)
defer cmdCtx.Shutdown()
go cmdCtx.HandleSignals()

// If the CLI is invoked with arguments plugin serve, the gRPC plugins are served.
if plugin.IsPluginServeCmd(os.Args[1:]) {
return serveDataSource()
}
return fmt.Errorf("unexpected command arguments: %v", os.Args[1:])
}

func serveDataSource() error {
// go-plugin client expects logs on stdout.
log.SetOutput(os.Stdout)

logger := hclog.New(&hclog.LoggerOptions{
Output: os.Stderr,
// Log at trace level in the plugin, it will be filtered in the host.
Level: hclog.Trace,
JSONFormat: true,
})

lds, err := local.NewLocalDataSource(config.NewFileLoader(cofideConfigFile))
if err != nil {
return err
}
spireHelm := spirehelm.NewSpireHelm(nil)

go_plugin.Serve(&go_plugin.ServeConfig{
HandshakeConfig: plugin.HandshakeConfig,
Plugins: map[string]go_plugin.Plugin{
datasource.DataSourcePluginName: &datasource.DataSourcePlugin{Impl: lds},
provision.ProvisionPluginName: &provision.ProvisionPlugin{Impl: spireHelm},
},
Logger: logger,
GRPCServer: go_plugin.DefaultGRPCServer,
})
return nil
}

0 comments on commit 638a843

Please sign in to comment.