This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
/
testplugin.go
176 lines (144 loc) · 3.83 KB
/
testplugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package group
import (
"errors"
"fmt"
"sync"
group_types "github.com/docker/infrakit/pkg/plugin/group/types"
"github.com/docker/infrakit/pkg/plugin/group/util"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
)
// newTestInstancePlugin creates a new instance plugin for use in testing and development.
func newTestInstancePlugin(seedInstances ...instance.Spec) *testplugin {
plugin := testplugin{idPrefix: util.RandomAlphaNumericString(4), instances: map[instance.ID]instance.Spec{}}
for _, inst := range seedInstances {
plugin.addInstance(inst)
}
return &plugin
}
type testplugin struct {
lock sync.Mutex
idPrefix string
nextID int
instances map[instance.ID]instance.Spec
}
func (d *testplugin) instancesCopy() map[instance.ID]instance.Spec {
d.lock.Lock()
defer d.lock.Unlock()
instances := map[instance.ID]instance.Spec{}
for k, v := range d.instances {
instances[k] = v
}
return instances
}
func (d *testplugin) Validate(req *types.Any) error {
return nil
}
func (d *testplugin) addInstance(spec instance.Spec) instance.ID {
d.lock.Lock()
defer d.lock.Unlock()
d.nextID++
id := instance.ID(fmt.Sprintf("%s-%d", d.idPrefix, d.nextID))
d.instances[id] = spec
return id
}
func (d *testplugin) Provision(spec instance.Spec) (*instance.ID, error) {
id := d.addInstance(spec)
return &id, nil
}
func (d *testplugin) Label(id instance.ID, labels map[string]string) error {
return nil
}
func (d *testplugin) Destroy(id instance.ID) error {
d.lock.Lock()
defer d.lock.Unlock()
_, exists := d.instances[id]
if !exists {
return errors.New("Instance does not exist")
}
delete(d.instances, id)
return nil
}
func (d *testplugin) DescribeInstances(tags map[string]string) ([]instance.Description, error) {
d.lock.Lock()
defer d.lock.Unlock()
desc := []instance.Description{}
for id, inst := range d.instances {
allMatched := true
for searchKey, searchValue := range tags {
tagValue, has := inst.Tags[searchKey]
if !has || searchValue != tagValue {
allMatched = false
}
}
if allMatched {
desc = append(desc, instance.Description{
ID: id,
LogicalID: inst.LogicalID,
Tags: inst.Tags,
})
}
}
return desc, nil
}
const (
typeMinion = "minion"
typeLeader = "leader"
)
type testFlavor struct {
healthy func(flavorProperties *types.Any, inst instance.Description) (flavor.Health, error)
drain func(flavorProperties *types.Any, inst instance.Description) error
}
type flavorSchema struct {
Type string
Init string
Tags map[string]string
}
func (t testFlavor) Validate(flavorProperties *types.Any, allocation group_types.AllocationMethod) error {
s := flavorSchema{}
err := flavorProperties.Decode(&s)
if err != nil {
return err
}
switch s.Type {
case typeMinion:
if len(allocation.LogicalIDs) > 0 {
return errors.New("Minion Groups must be scaled with Size, not LogicalIDs")
}
return nil
case typeLeader:
if allocation.Size > 0 {
return errors.New("Leader Groups must be scaled with LogicalIDs, not Size")
}
return nil
default:
return errors.New("Unrecognized node type")
}
}
func (t testFlavor) Prepare(flavorProperties *types.Any,
spec instance.Spec,
allocation group_types.AllocationMethod) (instance.Spec, error) {
s := flavorSchema{}
err := flavorProperties.Decode(&s)
if err != nil {
return spec, err
}
spec.Init = s.Init
for k, v := range s.Tags {
spec.Tags[k] = v
}
return spec, nil
}
func (t testFlavor) Healthy(flavorProperties *types.Any, inst instance.Description) (flavor.Health, error) {
if t.healthy != nil {
return t.healthy(flavorProperties, inst)
}
return flavor.Healthy, nil
}
func (t testFlavor) Drain(flavorProperties *types.Any, inst instance.Description) error {
if t.drain != nil {
return t.drain(flavorProperties, inst)
}
return nil
}