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

Commit 478b0e2

Browse files
author
David Chung
authored
Instance + Flavor Plugin SPI Changes (#371)
Signed-off-by: David Chung <david.chung@docker.com>
1 parent c9b8799 commit 478b0e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1052
-391
lines changed

cmd/cli/flavor.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
log "github.com/Sirupsen/logrus"
1111
"github.com/docker/infrakit/pkg/discovery"
1212
"github.com/docker/infrakit/pkg/plugin"
13-
"github.com/docker/infrakit/pkg/plugin/group/types"
13+
group_types "github.com/docker/infrakit/pkg/plugin/group/types"
1414
flavor_plugin "github.com/docker/infrakit/pkg/rpc/flavor"
1515
"github.com/docker/infrakit/pkg/spi/flavor"
1616
"github.com/docker/infrakit/pkg/spi/instance"
17+
"github.com/docker/infrakit/pkg/types"
1718
"github.com/spf13/cobra"
1819
)
1920

@@ -65,13 +66,13 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
6566
"Group Size to use as the Allocation method")
6667
}
6768

68-
allocationMethodFromFlags := func() types.AllocationMethod {
69+
allocationMethodFromFlags := func() group_types.AllocationMethod {
6970
ids := []instance.LogicalID{}
7071
for _, id := range logicalIDs {
7172
ids = append(ids, instance.LogicalID(id))
7273
}
7374

74-
return types.AllocationMethod{
75+
return group_types.AllocationMethod{
7576
Size: groupSize,
7677
LogicalIDs: ids,
7778
}
@@ -94,7 +95,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
9495
os.Exit(1)
9596
}
9697

97-
return flavorPlugin.Validate(json.RawMessage(buff), allocationMethodFromFlags())
98+
return flavorPlugin.Validate(types.AnyBytes(buff), allocationMethodFromFlags())
9899
},
99100
}
100101
addAllocationMethodFlags(validate)
@@ -129,7 +130,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
129130
}
130131

131132
spec, err = flavorPlugin.Prepare(
132-
json.RawMessage(flavorProperties),
133+
types.AnyBytes(flavorProperties),
133134
spec,
134135
allocationMethodFromFlags())
135136
if err == nil {
@@ -187,7 +188,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
187188
desc.LogicalID = &logical
188189
}
189190

190-
healthy, err := flavorPlugin.Healthy(json.RawMessage(flavorProperties), desc)
191+
healthy, err := flavorPlugin.Healthy(types.AnyBytes(flavorProperties), desc)
191192
if err == nil {
192193
fmt.Printf("%v\n", healthy)
193194
}

cmd/cli/instance.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/infrakit/pkg/plugin"
1414
instance_plugin "github.com/docker/infrakit/pkg/rpc/instance"
1515
"github.com/docker/infrakit/pkg/spi/instance"
16+
"github.com/docker/infrakit/pkg/types"
1617
"github.com/spf13/cobra"
1718
)
1819

@@ -65,7 +66,7 @@ func instancePluginCommand(plugins func() discovery.Plugins) *cobra.Command {
6566
os.Exit(1)
6667
}
6768

68-
err = instancePlugin.Validate(json.RawMessage(buff))
69+
err = instancePlugin.Validate(types.AnyBytes(buff))
6970
if err == nil {
7071
fmt.Println("validate:ok")
7172
}

docs/plugins/flavor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Flavor plugin API
22

3-
<!-- SOURCE-CHECKSUM pkg/spi/flavor/* 921b81c90c2abc7aec298333e1e1cf9c039afca5 -->
3+
<!-- SOURCE-CHECKSUM pkg/spi/flavor/* 76aecf22acb4af73f36857bb2fad861896ae0b62 -->
44

55
## API
66

docs/plugins/instance.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Instance plugin API
22

3-
<!-- SOURCE-CHECKSUM pkg/spi/instance/* 8fc5d1832d0d96d01d8d76ea1137230790fe51fe338393f886f528c3824986fc97cb27410aefd8e2 -->
3+
<!-- SOURCE-CHECKSUM pkg/spi/instance/* befb47292c2f062637d65e9777d88c322856baf650feb6b527cd3a4242f86d3482b656246051d2ca -->
44

55
## API
66

@@ -80,6 +80,35 @@ Parameters:
8080
Fields:
8181
- `OK`: Whether the operation succeeded.
8282

83+
### Method `Instance.Label`
84+
Labels an instance. The plugin should add or update the labels given.
85+
86+
#### Request
87+
```json
88+
{
89+
"Instance": "instance_id",
90+
"Labels" : {
91+
"label1" : "value1",
92+
"label2" : "value2",
93+
"label3" : "value3"
94+
}
95+
}
96+
```
97+
98+
Parameters:
99+
- `Instance`: An [instance ID](types.md#instance-id).
100+
- `Labels`: A [map](types.md#instance-tags) of labels or instance tags.
101+
102+
#### Response
103+
```json
104+
{
105+
"OK": true
106+
}
107+
```
108+
109+
Fields:
110+
- `OK`: Whether the operation succeeded.
111+
83112
### Method `Instance.DescribeInstances`
84113
Fetches details about Instances.
85114

docs/plugins/types.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@ Instance grouping and metadata.
7171
### Logical ID
7272
A possibly-reused logical identifier for an Instance.
7373

74+
### Labels
75+
A map or dictionary of key-value pairs that label / tag the instance. Same as [Instance Tags](#instance-tags)

examples/flavor/combo/flavor.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"errors"
5+
"strings"
6+
67
"github.com/docker/infrakit/pkg/plugin/group"
7-
"github.com/docker/infrakit/pkg/plugin/group/types"
8+
group_types "github.com/docker/infrakit/pkg/plugin/group/types"
89
"github.com/docker/infrakit/pkg/spi/flavor"
910
"github.com/docker/infrakit/pkg/spi/instance"
10-
"strings"
11+
"github.com/docker/infrakit/pkg/types"
1112
)
1213

1314
// Spec is the model of the plugin Properties.
1415
type Spec struct {
15-
Flavors []types.FlavorPlugin
16+
Flavors []group_types.FlavorPlugin
1617
}
1718

1819
// NewPlugin creates a Flavor Combo plugin that chains multiple flavors in a sequence. Each flavor
@@ -24,18 +25,18 @@ type flavorCombo struct {
2425
flavorPlugins group.FlavorPluginLookup
2526
}
2627

27-
func (f flavorCombo) Validate(flavorProperties json.RawMessage, allocation types.AllocationMethod) error {
28+
func (f flavorCombo) Validate(flavorProperties *types.Any, allocation group_types.AllocationMethod) error {
2829
s := Spec{}
29-
return json.Unmarshal(flavorProperties, &s)
30+
return flavorProperties.Decode(&s)
3031
}
3132

32-
func (f flavorCombo) Healthy(flavorProperties json.RawMessage, inst instance.Description) (flavor.Health, error) {
33+
func (f flavorCombo) Healthy(flavorProperties *types.Any, inst instance.Description) (flavor.Health, error) {
3334
// The overall health of the flavor combination is taken as the 'lowest common demoninator' of the configured
3435
// flavors. Only flavor.Healthy is reported if all flavors report flavor.Healthy. flavor.Unhealthy or
3536
// flavor.UnknownHealth is returned as soon as any Flavor reports that value.
3637

3738
s := Spec{}
38-
if err := json.Unmarshal(flavorProperties, &s); err != nil {
39+
if err := flavorProperties.Decode(s); err != nil {
3940
return flavor.Unknown, err
4041
}
4142

@@ -45,7 +46,7 @@ func (f flavorCombo) Healthy(flavorProperties json.RawMessage, inst instance.Des
4546
return flavor.Unknown, err
4647
}
4748

48-
health, err := plugin.Healthy(types.RawMessage(pluginSpec.Properties), inst)
49+
health, err := plugin.Healthy(pluginSpec.Properties, inst)
4950
if err != nil || health != flavor.Healthy {
5051
return health, err
5152
}
@@ -54,12 +55,12 @@ func (f flavorCombo) Healthy(flavorProperties json.RawMessage, inst instance.Des
5455
return flavor.Healthy, nil
5556
}
5657

57-
func (f flavorCombo) Drain(flavorProperties json.RawMessage, inst instance.Description) error {
58+
func (f flavorCombo) Drain(flavorProperties *types.Any, inst instance.Description) error {
5859
// Draining is attempted on all flavors regardless of errors encountered. All errors encountered are combined
5960
// and returned.
6061

6162
s := Spec{}
62-
if err := json.Unmarshal(flavorProperties, &s); err != nil {
63+
if err := flavorProperties.Decode(&s); err != nil {
6364
return err
6465
}
6566

@@ -71,7 +72,7 @@ func (f flavorCombo) Drain(flavorProperties json.RawMessage, inst instance.Descr
7172
errs = append(errs, err.Error())
7273
}
7374

74-
if err := plugin.Drain(types.RawMessage(pluginSpec.Properties), inst); err != nil {
75+
if err := plugin.Drain(pluginSpec.Properties, inst); err != nil {
7576
errs = append(errs, err.Error())
7677
}
7778
}
@@ -133,13 +134,12 @@ func mergeSpecs(initial instance.Spec, specs []instance.Spec) (instance.Spec, er
133134
return result, nil
134135
}
135136

136-
func (f flavorCombo) Prepare(
137-
flavor json.RawMessage,
137+
func (f flavorCombo) Prepare(flavor *types.Any,
138138
inst instance.Spec,
139-
allocation types.AllocationMethod) (instance.Spec, error) {
139+
allocation group_types.AllocationMethod) (instance.Spec, error) {
140140

141141
combo := Spec{}
142-
err := json.Unmarshal(flavor, &combo)
142+
err := flavor.Decode(&combo)
143143
if err != nil {
144144
return inst, err
145145
}
@@ -154,7 +154,7 @@ func (f flavorCombo) Prepare(
154154
return inst, err
155155
}
156156

157-
flavorOutput, err := plugin.Prepare(types.RawMessage(pluginSpec.Properties), clone, allocation)
157+
flavorOutput, err := plugin.Prepare(pluginSpec.Properties, clone, allocation)
158158
if err != nil {
159159
return inst, err
160160
}

examples/flavor/combo/flavor_test.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"errors"
65
"testing"
76

87
mock_flavor "github.com/docker/infrakit/pkg/mock/spi/flavor"
98
"github.com/docker/infrakit/pkg/plugin"
109
"github.com/docker/infrakit/pkg/plugin/group"
11-
"github.com/docker/infrakit/pkg/plugin/group/types"
10+
group_types "github.com/docker/infrakit/pkg/plugin/group/types"
1211
"github.com/docker/infrakit/pkg/spi/flavor"
1312
"github.com/docker/infrakit/pkg/spi/instance"
13+
"github.com/docker/infrakit/pkg/types"
1414
"github.com/golang/mock/gomock"
1515
"github.com/stretchr/testify/require"
1616
)
1717

18-
func jsonPtr(v string) *json.RawMessage {
19-
j := json.RawMessage(v)
20-
return &j
21-
}
22-
2318
func logicalID(v string) *instance.LogicalID {
2419
id := instance.LogicalID(v)
2520
return &id
2621
}
2722

2823
var inst = instance.Spec{
29-
Properties: jsonPtr("{}"),
24+
Properties: types.AnyString("{}"),
3025
Tags: map[string]string{},
3126
Init: "",
3227
LogicalID: logicalID("id"),
@@ -54,7 +49,7 @@ func TestMergeBehavior(t *testing.T) {
5449

5550
combo := NewPlugin(pluginLookup(plugins))
5651

57-
flavorProperties := json.RawMessage(`{
52+
flavorProperties := types.AnyString(`{
5853
"Flavors": [
5954
{
6055
"Plugin": "a",
@@ -67,25 +62,25 @@ func TestMergeBehavior(t *testing.T) {
6762
]
6863
}`)
6964

70-
allocation := types.AllocationMethod{Size: 1}
65+
allocation := group_types.AllocationMethod{Size: 1}
7166

72-
a.EXPECT().Prepare(json.RawMessage(`{"a": "1"}`), inst, allocation).Return(instance.Spec{
67+
a.EXPECT().Prepare(types.AnyString(`{"a": "1"}`), inst, allocation).Return(instance.Spec{
7368
Properties: inst.Properties,
7469
Tags: map[string]string{"a": "1", "c": "4"},
7570
Init: "init data a",
7671
LogicalID: inst.LogicalID,
7772
Attachments: []instance.Attachment{{ID: "a", Type: "nic"}},
7873
}, nil)
7974

80-
b.EXPECT().Prepare(json.RawMessage(`{"b": "2"}`), inst, allocation).Return(instance.Spec{
75+
b.EXPECT().Prepare(types.AnyString(`{"b": "2"}`), inst, allocation).Return(instance.Spec{
8176
Properties: inst.Properties,
8277
Tags: map[string]string{"b": "2", "c": "5"},
8378
Init: "init data b",
8479
LogicalID: inst.LogicalID,
8580
Attachments: []instance.Attachment{{ID: "b", Type: "gpu"}},
8681
}, nil)
8782

88-
result, err := combo.Prepare(flavorProperties, inst, types.AllocationMethod{Size: 1})
83+
result, err := combo.Prepare(flavorProperties, inst, group_types.AllocationMethod{Size: 1})
8984
require.NoError(t, err)
9085

9186
expected := instance.Spec{
@@ -102,7 +97,7 @@ func TestMergeNoLogicalID(t *testing.T) {
10297
// Tests regression of a bug where a zero value was returned for the LogicalID despite nil being passed.
10398

10499
inst = instance.Spec{
105-
Properties: jsonPtr("{}"),
100+
Properties: types.AnyString("{}"),
106101
Tags: map[string]string{},
107102
Init: "",
108103
Attachments: []instance.Attachment{{ID: "att1", Type: "nic"}},
@@ -118,7 +113,7 @@ func TestMergeNoLogicalID(t *testing.T) {
118113

119114
combo := NewPlugin(pluginLookup(plugins))
120115

121-
flavorProperties := json.RawMessage(`{
116+
flavorProperties := types.AnyString(`{
122117
"Flavors": [
123118
{
124119
"Plugin": "a",
@@ -131,25 +126,25 @@ func TestMergeNoLogicalID(t *testing.T) {
131126
]
132127
}`)
133128

134-
allocation := types.AllocationMethod{Size: 1}
129+
allocation := group_types.AllocationMethod{Size: 1}
135130

136-
a.EXPECT().Prepare(json.RawMessage(`{"a": "1"}`), inst, allocation).Return(instance.Spec{
131+
a.EXPECT().Prepare(types.AnyString(`{"a": "1"}`), inst, allocation).Return(instance.Spec{
137132
Properties: inst.Properties,
138133
Tags: map[string]string{"a": "1", "c": "4"},
139134
Init: "init data a",
140135
LogicalID: inst.LogicalID,
141136
Attachments: []instance.Attachment{{ID: "a", Type: "nic"}},
142137
}, nil)
143138

144-
b.EXPECT().Prepare(json.RawMessage(`{"b": "2"}`), inst, allocation).Return(instance.Spec{
139+
b.EXPECT().Prepare(types.AnyString(`{"b": "2"}`), inst, allocation).Return(instance.Spec{
145140
Properties: inst.Properties,
146141
Tags: map[string]string{"b": "2", "c": "5"},
147142
Init: "init data b",
148143
LogicalID: inst.LogicalID,
149144
Attachments: []instance.Attachment{{ID: "b", Type: "gpu"}},
150145
}, nil)
151146

152-
result, err := combo.Prepare(flavorProperties, inst, types.AllocationMethod{Size: 1})
147+
result, err := combo.Prepare(flavorProperties, inst, group_types.AllocationMethod{Size: 1})
153148
require.NoError(t, err)
154149

155150
expected := instance.Spec{

0 commit comments

Comments
 (0)