forked from docker-archive/deploykit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflavor.go
66 lines (53 loc) · 1.53 KB
/
flavor.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
package vanilla
import (
"encoding/json"
"strings"
"github.com/docker/infrakit/spi/flavor"
"github.com/docker/infrakit/spi/instance"
)
// Spec is the model of the Properties section of the top level group spec.
type Spec struct {
flavor.AllocationMethod
// UserData
UserData []string
// Labels
Labels map[string]string
}
// NewPlugin creates a Flavor plugin that doesn't do very much. It assumes instances are
// identical (cattles) but can assume specific identities (via the LogicalIDs). The
// instances here are treated identically because we have constant UserData that applies
// to all instances
func NewPlugin() flavor.Plugin {
return vanillaFlavor(0)
}
type vanillaFlavor int
func (f vanillaFlavor) Validate(flavorProperties json.RawMessage) (flavor.AllocationMethod, error) {
s := Spec{}
err := json.Unmarshal(flavorProperties, &s)
return s.AllocationMethod, err
}
func (f vanillaFlavor) Healthy(inst instance.Description) (bool, error) {
return true, nil
}
func (f vanillaFlavor) Prepare(flavor json.RawMessage, instance instance.Spec) (instance.Spec, error) {
s := Spec{}
err := json.Unmarshal(flavor, &s)
if err != nil {
return instance, err
}
// Merge UserData into Init
lines := []string{}
if instance.Init != "" {
lines = append(lines, instance.Init)
}
lines = append(lines, s.UserData...)
instance.Init = strings.Join(lines, "\n")
// Add user Labels as tags
for k, v := range s.Labels {
if instance.Tags == nil {
instance.Tags = map[string]string{}
}
instance.Tags[k] = v
}
return instance, nil
}