Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/cli/flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/discovery"
"github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/plugin/group/types"
flavor_plugin "github.com/docker/infrakit/pkg/rpc/flavor"
"github.com/docker/infrakit/pkg/spi/flavor"
Expand All @@ -28,7 +29,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {

cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {

endpoint, err := plugins().Find(*name)
endpoint, err := plugins().Find(plugin.Name(*name))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/cli/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/discovery"
"github.com/docker/infrakit/pkg/plugin"
group_plugin "github.com/docker/infrakit/pkg/rpc/group"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/spf13/cobra"
Expand All @@ -31,7 +32,7 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
name := cmd.PersistentFlags().String("name", DefaultGroupPluginName, "Name of plugin")
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {

endpoint, err := plugins().Find(*name)
endpoint, err := plugins().Find(plugin.Name(*name))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func instancePluginCommand(plugins func() discovery.Plugins) *cobra.Command {
name := cmd.PersistentFlags().String("name", "", "Name of plugin")
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {

endpoint, err := plugins().Find(*name)
endpoint, err := plugins().Find(plugin.Name(*name))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/group/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func main() {
return err
}

instancePluginLookup := func(n string) (instance.Plugin, error) {
instancePluginLookup := func(n plugin.Name) (instance.Plugin, error) {
endpoint, err := plugins.Find(n)
if err != nil {
return nil, err
}
return instance_client.NewClient(plugin.Name(n), endpoint.Address), nil
}

flavorPluginLookup := func(n string) (flavor.Plugin, error) {
flavorPluginLookup := func(n plugin.Name) (flavor.Plugin, error) {
endpoint, err := plugins.Find(n)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/docker/infrakit/pkg/discovery"
"github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/rpc/client"
"github.com/spf13/cobra"
)
Expand All @@ -17,7 +18,7 @@ func InfoCommand(plugins func() discovery.Plugins) *cobra.Command {
}
name := cmd.PersistentFlags().String("name", "", "Name of plugin")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
endpoint, err := plugins().Find(*name)
endpoint, err := plugins().Find(plugin.Name(*name))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/discovery/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type dirPluginDiscovery struct {
}

// Find returns a plugin by name
func (r *dirPluginDiscovery) Find(name string) (*plugin.Endpoint, error) {
lookup, _ := plugin.Name(name).GetLookupAndType()
func (r *dirPluginDiscovery) Find(name plugin.Name) (*plugin.Endpoint, error) {
lookup, _ := name.GetLookupAndType()
plugins, err := r.List()
if err != nil {
return nil, err
Expand Down
13 changes: 7 additions & 6 deletions pkg/discovery/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/docker/infrakit/pkg/plugin"
rpc "github.com/docker/infrakit/pkg/rpc/instance"
"github.com/docker/infrakit/pkg/rpc/server"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -44,33 +45,33 @@ func TestDirDiscovery(t *testing.T) {
discover, err := newDirPluginDiscovery(dir)
require.NoError(t, err)

p, err := discover.Find(name1)
p, err := discover.Find(plugin.Name(name1))
require.NoError(t, err)
require.NotNil(t, p)

p, err = discover.Find(name2)
p, err = discover.Find(plugin.Name(name2))
require.NoError(t, err)
require.NotNil(t, p)

// Now we stop the servers
server1.Stop()
blockWhileFileExists(path1)

p, err = discover.Find(name1)
p, err = discover.Find(plugin.Name(name1))
require.Error(t, err)

p, err = discover.Find(name2)
p, err = discover.Find(plugin.Name(name2))
require.NoError(t, err)
require.NotNil(t, p)

server2.Stop()

blockWhileFileExists(path2)

p, err = discover.Find(name1)
p, err = discover.Find(plugin.Name(name1))
require.Error(t, err)

p, err = discover.Find(name2)
p, err = discover.Find(plugin.Name(name2))
require.Error(t, err)

list, err := discover.List()
Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Plugins provides access to plugin discovery.
type Plugins interface {
// Find looks up the plugin by name. The name can be of the form $lookup[/$subtype]. See GetLookupAndType().
Find(name string) (*plugin.Endpoint, error)
Find(name plugin.Name) (*plugin.Endpoint, error)
List() (map[string]*plugin.Endpoint, error)
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/example/flavor/combo/flavor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package main
import (
"encoding/json"
"errors"
"testing"

mock_flavor "github.com/docker/infrakit/pkg/mock/spi/flavor"
"github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/plugin/group"
"github.com/docker/infrakit/pkg/plugin/group/types"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"testing"
)

func jsonPtr(v string) *json.RawMessage {
Expand All @@ -32,8 +34,8 @@ var inst = instance.Spec{
}

func pluginLookup(plugins map[string]flavor.Plugin) group.FlavorPluginLookup {
return func(key string) (flavor.Plugin, error) {
plugin, has := plugins[key]
return func(key plugin.Name) (flavor.Plugin, error) {
plugin, has := plugins[key.String()]
if has {
return plugin, nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/example/flavor/combo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/cli"
"github.com/docker/infrakit/pkg/discovery"
"github.com/docker/infrakit/pkg/plugin"
flavor_rpc "github.com/docker/infrakit/pkg/rpc/flavor"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/spf13/cobra"
Expand All @@ -27,7 +28,7 @@ func main() {
os.Exit(1)
}

flavorPluginLookup := func(n string) (flavor.Plugin, error) {
flavorPluginLookup := func(n plugin.Name) (flavor.Plugin, error) {
endpoint, err := plugins.Find(n)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/manager/group_plugin_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/plugin"
rpc "github.com/docker/infrakit/pkg/rpc/group"
"github.com/docker/infrakit/pkg/spi/group"
)
Expand All @@ -17,7 +18,7 @@ func (m *manager) proxyForGroupPlugin(name string) (group.Plugin, error) {
// A late-binding proxy so that we don't have a problem with having to
// start up the manager as the last of all the plugins.
return NewProxy(func() (group.Plugin, error) {
endpoint, err := m.plugins.Find(name)
endpoint, err := m.plugins.Find(plugin.Name(name))
if err != nil {
return nil, err
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/plugin/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package group
import (
"errors"
"fmt"
"sync"
"time"

log "github.com/Sirupsen/logrus"
plugin_base "github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/plugin/group/types"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/spi/instance"
"sync"
"time"
)

const (
Expand All @@ -18,10 +20,10 @@ const (
)

// InstancePluginLookup helps with looking up an instance plugin by name
type InstancePluginLookup func(string) (instance.Plugin, error)
type InstancePluginLookup func(plugin_base.Name) (instance.Plugin, error)

// FlavorPluginLookup helps with looking up a flavor plugin by name
type FlavorPluginLookup func(string) (flavor.Plugin, error)
type FlavorPluginLookup func(plugin_base.Name) (flavor.Plugin, error)

// NewGroupPlugin creates a new group plugin.
func NewGroupPlugin(
Expand Down
22 changes: 12 additions & 10 deletions pkg/plugin/group/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package group
import (
"encoding/json"
"fmt"
"strings"
"sync"
"testing"
"time"

plugin_base "github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/plugin/group/types"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/stretchr/testify/require"
"strings"
"sync"
"testing"
"time"
)

const (
Expand All @@ -33,7 +35,7 @@ var (
leaderIDs = []instance.LogicalID{"192.168.0.4", "192.168.0.5", "192.168.0.6"}
)

func flavorPluginLookup(_ string) (flavor.Plugin, error) {
func flavorPluginLookup(_ plugin_base.Name) (flavor.Plugin, error) {
return &testFlavor{}, nil
}

Expand Down Expand Up @@ -86,8 +88,8 @@ func leaderProperties(logicalIDs []instance.LogicalID, data string) *json.RawMes
}

func pluginLookup(pluginName string, plugin instance.Plugin) InstancePluginLookup {
return func(key string) (instance.Plugin, error) {
if key == pluginName {
return func(key plugin_base.Name) (instance.Plugin, error) {
if key.String() == pluginName {
return plugin, nil
}
return nil, nil
Expand Down Expand Up @@ -185,7 +187,7 @@ func TestRollingUpdate(t *testing.T) {
return flavor.Unhealthy, nil
},
}
flavorLookup := func(_ string) (flavor.Plugin, error) {
flavorLookup := func(_ plugin_base.Name) (flavor.Plugin, error) {
return &flavorPlugin, nil
}

Expand Down Expand Up @@ -492,7 +494,7 @@ func TestFreeGroupWhileConverging(t *testing.T) {
return flavor.Unknown, nil
},
}
flavorLookup := func(_ string) (flavor.Plugin, error) {
flavorLookup := func(_ plugin_base.Name) (flavor.Plugin, error) {
return &flavorPlugin, nil
}

Expand Down Expand Up @@ -533,7 +535,7 @@ func TestUpdateFailsWhenInstanceIsUnhealthy(t *testing.T) {
return flavor.Healthy, nil
},
}
flavorLookup := func(_ string) (flavor.Plugin, error) {
flavorLookup := func(_ plugin_base.Name) (flavor.Plugin, error) {
return &flavorPlugin, nil
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/plugin/group/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/base64"
"encoding/json"
"fmt"

"github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/spi/instance"
)
Expand All @@ -24,13 +26,13 @@ type AllocationMethod struct {

// InstancePlugin is the structure that describes an instance plugin.
type InstancePlugin struct {
Plugin string
Plugin plugin.Name
Properties *json.RawMessage // this will be the Spec of the plugin
}

// FlavorPlugin describes the flavor configuration
type FlavorPlugin struct {
Plugin string
Plugin plugin.Name
Properties *json.RawMessage // this will be the Spec of the plugin
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/plugin/name.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugin

import (
"fmt"
"strings"
)

Expand All @@ -23,3 +24,25 @@ func (r Name) GetLookupAndType() (string, string) {
func (r Name) String() string {
return string(r)
}

// MarshalJSON implements the JSON marshaler interface
func (r *Name) MarshalJSON() ([]byte, error) {
if r == nil {
return nil, nil
}
return []byte(r.String()), nil
}

// UnmarshalJSON implements the JSON unmarshaler interface
func (r *Name) UnmarshalJSON(data []byte) error {
str := string(data)
start := strings.Index(str, "\"")
last := strings.LastIndex(str, "\"")
if start == 0 && last == len(str)-1 {
str = str[start+1 : last]
} else {
return fmt.Errorf("bad-format-for-name:%v", string(data))
}
*r = Name(str)
return nil
}
Loading