-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Shared Plugin RPC Host (#3238)
* start to plugin host impl wip * comments * minor bug updates * updates per plugin config migration * small updates to sharedHost plugin option * update to isolate plugin cache * rename of utils to cache * print line removing * tidy * removing print out * additional comments * shared host load test * addition of caching tests for plugins * fmt * changelog * additional plugin cache tests * update shared host tests * review comments * addition cache test cases * fix typo * fix comment * update plugin `KillClient` for shared hosts * changelog update * fix test by omitting SharedHost from the yml when its false * update: migration of `sharedHost` flag to plugin manifest * scaffolding and plugin host check changes * update tests * format and lint * update plugin cache test * update property comment * update plugin cache to use full plugin path * cleanup of plugin tests * lint * update plugin sharedHost tests * update to plugin docs for `Sharedhost` flag * test: fix TestPluginLoadSharedHost * refac: plugin cach * Update docs/docs/contributing/01-plugins.md * refac: plugin cache func should be private * test: improve plugin kill assertions Co-authored-by: Thomas Bruyelle <[email protected]> Co-authored-by: Thomas Bruyelle <[email protected]>
- Loading branch information
1 parent
e2cc76c
commit 015b140
Showing
11 changed files
with
432 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/gob" | ||
"fmt" | ||
"net" | ||
"path" | ||
|
||
hplugin "github.com/hashicorp/go-plugin" | ||
|
||
"github.com/ignite/cli/ignite/pkg/cache" | ||
) | ||
|
||
const ( | ||
cacheFileName = "ignite_plugin_cache.db" | ||
cacheNamespace = "plugin.rpc.context" | ||
) | ||
|
||
var storageCache *cache.Cache[hplugin.ReattachConfig] | ||
|
||
func init() { | ||
gob.Register(hplugin.ReattachConfig{}) | ||
gob.Register(&net.UnixAddr{}) | ||
} | ||
|
||
func writeConfigCache(pluginPath string, conf hplugin.ReattachConfig) error { | ||
if pluginPath == "" { | ||
return fmt.Errorf("provided path is invalid: %s", pluginPath) | ||
} | ||
if conf.Addr == nil { | ||
return fmt.Errorf("plugin Address info cannot be empty") | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return err | ||
} | ||
return cache.Put(pluginPath, conf) | ||
} | ||
|
||
func readConfigCache(pluginPath string) (hplugin.ReattachConfig, error) { | ||
if pluginPath == "" { | ||
return hplugin.ReattachConfig{}, fmt.Errorf("provided path is invalid: %s", pluginPath) | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return hplugin.ReattachConfig{}, err | ||
} | ||
return cache.Get(pluginPath) | ||
} | ||
|
||
func checkConfCache(pluginPath string) bool { | ||
if pluginPath == "" { | ||
return false | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return false | ||
} | ||
_, err = cache.Get(pluginPath) | ||
return err == nil | ||
} | ||
|
||
func deleteConfCache(pluginPath string) error { | ||
if pluginPath == "" { | ||
return fmt.Errorf("provided path is invalid: %s", pluginPath) | ||
} | ||
cache, err := newCache() | ||
if err != nil { | ||
return err | ||
} | ||
return cache.Delete(pluginPath) | ||
} | ||
|
||
func newCache() (*cache.Cache[hplugin.ReattachConfig], error) { | ||
cacheRootDir, err := PluginsPath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if storageCache == nil { | ||
storage, err := cache.NewStorage(path.Join(cacheRootDir, cacheFileName)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := cache.New[hplugin.ReattachConfig](storage, cacheNamespace) | ||
storageCache = &c | ||
} | ||
return storageCache, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package plugin | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
hplugin "github.com/hashicorp/go-plugin" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadWriteConfigCache(t *testing.T) { | ||
t.Run("Should cache plugin config and read from cache", func(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
unixFD, _ := net.ResolveUnixAddr("unix", "/var/folders/5k/sv4bxrs102n_6rr7430jc7j80000gn/T/plugin193424090") | ||
|
||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: unixFD, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.NoError(t, err) | ||
|
||
c, err := readConfigCache(path) | ||
require.NoError(t, err) | ||
require.Equal(t, rc, c) | ||
}) | ||
|
||
t.Run("Should error writing bad plugin config to cache", func(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: nil, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("Should error with invalid plugin path", func(t *testing.T) { | ||
const path = "" | ||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: nil, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestDeleteConfCache(t *testing.T) { | ||
t.Run("Delete plugin config after write to cache should remove from cache", func(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
unixFD, _ := net.ResolveUnixAddr("unix", "/var/folders/5k/sv4bxrs102n_6rr7430jc7j80000gn/T/plugin193424090") | ||
|
||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: unixFD, | ||
Pid: 24464, | ||
} | ||
|
||
err := writeConfigCache(path, rc) | ||
require.NoError(t, err) | ||
|
||
err = deleteConfCache(path) | ||
require.NoError(t, err) | ||
|
||
// there should be an error after deleting the config from the cache | ||
_, err = readConfigCache(path) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("Delete plugin config should return error given empty path", func(t *testing.T) { | ||
const path = "" | ||
err := deleteConfCache(path) | ||
require.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestCheckConfCache(t *testing.T) { | ||
const path = "/path/to/awesome/plugin" | ||
unixFD, _ := net.ResolveUnixAddr("unix", "/var/folders/5k/sv4bxrs102n_6rr7430jc7j80000gn/T/plugin193424090") | ||
|
||
rc := hplugin.ReattachConfig{ | ||
Protocol: hplugin.ProtocolNetRPC, | ||
ProtocolVersion: hplugin.CoreProtocolVersion, | ||
Addr: unixFD, | ||
Pid: 24464, | ||
} | ||
|
||
t.Run("Cache should be hydrated", func(t *testing.T) { | ||
err := writeConfigCache(path, rc) | ||
require.NoError(t, err) | ||
require.Equal(t, true, checkConfCache(path)) | ||
}) | ||
|
||
t.Run("Cache should be empty", func(t *testing.T) { | ||
_ = deleteConfCache(path) | ||
require.Equal(t, false, checkConfCache(path)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.