Skip to content

Commit

Permalink
initial support for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Nov 27, 2016
1 parent ab924d4 commit eb08c87
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 1 deletion.
2 changes: 2 additions & 0 deletions backends/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/HeavyHorst/remco/backends/etcd"
"github.com/HeavyHorst/remco/backends/file"
"github.com/HeavyHorst/remco/backends/mock"
"github.com/HeavyHorst/remco/backends/plugin"
"github.com/HeavyHorst/remco/backends/redis"
"github.com/HeavyHorst/remco/backends/vault"
"github.com/HeavyHorst/remco/backends/zookeeper"
Expand All @@ -29,6 +30,7 @@ type Config struct {
Redis *redis.Config
Zookeeper *zookeeper.Config
Mock *mock.Config
Plugin []plugin.Plugin
}

func (c *Config) GetBackends() []template.BackendConfig {
Expand Down
89 changes: 89 additions & 0 deletions backends/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* This file is part of remco.
* © 2016 The Remco Authors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

package plugin

import (
"context"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"path"

"github.com/HeavyHorst/easyKV"
berr "github.com/HeavyHorst/remco/backends/error"
"github.com/HeavyHorst/remco/log"
"github.com/HeavyHorst/remco/template"
"github.com/Sirupsen/logrus"
"github.com/natefinch/pie"
)

type plug struct {
client *rpc.Client
}

// Plugin represents the config for a plugin.
type Plugin struct {
// the path to the plugin executable
Path string
Config map[string]interface{}
template.Backend
}

// Connect creates the connection to the plugin and sends the config map to the same.
func (p *Plugin) Connect() (template.Backend, error) {
if p == nil {
return template.Backend{}, berr.ErrNilConfig
}

p.Backend.Name = path.Base(p.Path)

client, err := pie.StartProviderCodec(jsonrpc.NewClientCodec, os.Stderr, p.Path)
if err != nil {
return p.Backend, err
}

if p.Backend.Watch {
log.WithFields(logrus.Fields{
"backend": p.Backend.Name,
}).Warn("Watch is not supported, using interval instead")
p.Backend.Watch = false
}

plugin := &plug{client}
if err := plugin.Init(p.Config); err != nil {
return p.Backend, err
}

p.Backend.ReadWatcher = plugin
return p.Backend, nil
}

// Init sends the config map to the plugin
// the plugin can then run some initialization tasks
func (p *plug) Init(config map[string]interface{}) error {
var result bool
return p.client.Call("Plugin.Init", config, &result)
}

// GetValues queries the plugin for keys
func (p *plug) GetValues(keys []string) (result map[string]string, err error) {
err = p.client.Call("Plugin.GetValues", keys, &result)
return result, err
}

// Close closes the client connection
func (p *plug) Close() {
p.client.Call("Plugin.Close", nil, nil)
p.client.Close()
}

// WatchPrefix is not supported for now
func (p *plug) WatchPrefix(prefix string, ctx context.Context, opts ...easyKV.WatchOption) (uint64, error) {
return 0, easyKV.ErrWatchNotSupported
}
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,15 @@ func (c *Configuration) configureLogger() {

func (r *Resource) Init(ctx context.Context, reapLock *sync.RWMutex) (*template.Resource, error) {
var backendList []template.Backend
backendConfigs := r.Backend.GetBackends()

for _, v := range r.Backend.Plugin {
backendConfigs = append(backendConfigs, &v)
}

// try to connect to all backends
// connection to all backends must succeed to continue
for _, config := range r.Backend.GetBackends() {
for _, config := range backendConfigs {
retryloop:
for {
select {
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/natefinch/pie/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

183 changes: 183 additions & 0 deletions vendor/github.com/natefinch/pie/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions vendor/github.com/natefinch/pie/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eb08c87

Please sign in to comment.