Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Metadata SPI #396

Merged
merged 16 commits into from
Feb 20, 2017
Prev Previous commit
Next Next commit
bug fixes
  • Loading branch information
David Chung committed Feb 18, 2017
commit d0c864e985c8bca5b84c8b9c5c1de47a0cd4ed29
12 changes: 8 additions & 4 deletions examples/flavor/swarm/flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func (s *baseFlavor) List(path metadata.Path) ([]string, error) {
return nil, err
}
data := map[string]interface{}{
"status": status,
"node": node,
"local": map[string]interface{}{
"status": status,
"node": node,
},
}
return metadata_plugin.List(path, data), nil
}
Expand All @@ -102,8 +104,10 @@ func (s *baseFlavor) Get(path metadata.Path) (*types.Any, error) {
return nil, err
}
data := map[string]interface{}{
"status": status,
"node": node,
"local": map[string]interface{}{
"status": status,
"node": node,
},
}
return metadata_plugin.GetValue(path, data)
}
Expand Down
19 changes: 9 additions & 10 deletions examples/flavor/swarm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,19 @@ func main() {
workerFlavor := NewWorkerFlavor(DockerClient, wt)

cli.RunPlugin(*name,

// Metadata plugins
metadata_plugin.PluginServer(metadata.NewPluginFromData(map[string]interface{}{
"version": cli.Version,
"revision": cli.Revision,
"version": cli.Version,
"revision": cli.Revision,
"implements": flavor_spi.InterfaceSpec,
})).WithTypes(
map[string]metadata_spi.Plugin{
"manager": metadata.NewPluginFromData(map[string]interface{}{
"implements": flavor_spi.InterfaceSpec,
"local": managerFlavor,
}),
"worker": metadata.NewPluginFromData(map[string]interface{}{
"implements": flavor_spi.InterfaceSpec,
"local": workerFlavor,
}),
"manager": managerFlavor,
"worker": workerFlavor,
}),

// Flavor plugins
flavor_plugin.PluginServerWithTypes(
map[string]flavor_spi.Plugin{
"manager": managerFlavor,
Expand Down
11 changes: 5 additions & 6 deletions examples/flavor/swarm/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/client"
group_types "github.com/docker/infrakit/pkg/plugin/group/types"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/template"
"github.com/docker/infrakit/pkg/types"
)

// NewManagerFlavor creates a flavor.Plugin that creates manager and worker nodes connected in a swarm.
func NewManagerFlavor(connect func(Spec) (client.APIClient, error), templ *template.Template) flavor.Plugin {
return &managerFlavor{&baseFlavor{initScript: templ, getDockerClient: connect}}
func NewManagerFlavor(connect func(Spec) (client.APIClient, error), templ *template.Template) *ManagerFlavor {
return &ManagerFlavor{&baseFlavor{initScript: templ, getDockerClient: connect}}
}

type managerFlavor struct {
type ManagerFlavor struct {
*baseFlavor
}

func (s *managerFlavor) Validate(flavorProperties *types.Any, allocation group_types.AllocationMethod) error {
func (s *ManagerFlavor) Validate(flavorProperties *types.Any, allocation group_types.AllocationMethod) error {

if err := s.baseFlavor.Validate(flavorProperties, allocation); err != nil {
return err
Expand All @@ -46,7 +45,7 @@ func (s *managerFlavor) Validate(flavorProperties *types.Any, allocation group_t
}

// Prepare sets up the provisioner / instance plugin's spec based on information about the swarm to join.
func (s *managerFlavor) Prepare(flavorProperties *types.Any,
func (s *ManagerFlavor) Prepare(flavorProperties *types.Any,
instanceSpec instance.Spec, allocation group_types.AllocationMethod) (instance.Spec, error) {
return s.baseFlavor.prepare("manager", flavorProperties, instanceSpec, allocation)
}
13 changes: 5 additions & 8 deletions examples/flavor/swarm/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@ package main

import (
"fmt"
//"time"

log "github.com/Sirupsen/logrus"
docker_types "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
//"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
group_types "github.com/docker/infrakit/pkg/plugin/group/types"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/template"
"github.com/docker/infrakit/pkg/types"
"golang.org/x/net/context"
)

// NewWorkerFlavor creates a flavor.Plugin that creates manager and worker nodes connected in a swarm.
func NewWorkerFlavor(connect func(Spec) (client.APIClient, error), templ *template.Template) flavor.Plugin {
return &workerFlavor{&baseFlavor{initScript: templ, getDockerClient: connect}}
func NewWorkerFlavor(connect func(Spec) (client.APIClient, error), templ *template.Template) *WorkerFlavor {
return &WorkerFlavor{&baseFlavor{initScript: templ, getDockerClient: connect}}
}

type workerFlavor struct {
type WorkerFlavor struct {
*baseFlavor
}

// Prepare sets up the provisioner / instance plugin's spec based on information about the swarm to join.
func (s *workerFlavor) Prepare(flavorProperties *types.Any, instanceSpec instance.Spec,
func (s *WorkerFlavor) Prepare(flavorProperties *types.Any, instanceSpec instance.Spec,
allocation group_types.AllocationMethod) (instance.Spec, error) {
return s.baseFlavor.prepare("worker", flavorProperties, instanceSpec, allocation)
}

// Drain in the case of worker will force a node removal in the swarm.
func (s *workerFlavor) Drain(flavorProperties *types.Any, inst instance.Description) error {
func (s *WorkerFlavor) Drain(flavorProperties *types.Any, inst instance.Description) error {
if flavorProperties == nil {
return fmt.Errorf("missing config")
}
Expand Down
31 changes: 18 additions & 13 deletions pkg/plugin/metadata/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"

"github.com/docker/infrakit/pkg/spi/metadata"
_ "github.com/docker/infrakit/pkg/spi/metadata"
"github.com/docker/infrakit/pkg/types"
)

Expand Down Expand Up @@ -69,7 +69,9 @@ func List(path []string, object interface{}) []string {
case reflect.Struct:
vt := val.Type()
for i := 0; i < vt.NumField(); i++ {
list = append(list, vt.Field(i).Name)
if vt.Field(i).PkgPath == "" {
list = append(list, vt.Field(i).Name)
}
}
}

Expand Down Expand Up @@ -117,17 +119,17 @@ func put(p []string, value interface{}, store map[string]interface{}) bool {
}

func get(path []string, object interface{}) interface{} {
if impl, is := object.(metadata.Plugin); is {
value, err := impl.Get(metadata.Path(path))
if err != nil {
return err
}
temp := map[string]interface{}{}
if err := value.Decode(&temp); err == nil {
return temp
}
return nil
}
// if impl, is := object.(metadata.Plugin); is {
// value, err := impl.Get(metadata.Path(path))
// if err != nil {
// return err
// }
// temp := map[string]interface{}{}
// if err := value.Decode(&temp); err == nil {
// return temp
// }
// return nil
// }

if len(path) == 0 {
return object
Expand Down Expand Up @@ -192,6 +194,9 @@ func get(path []string, object interface{}) interface{} {
if !fv.IsValid() {
return nil
}
if !fv.CanInterface() {
return nil
}
return get(path[1:], fv.Interface())
}
return nil
Expand Down