This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
/
types.go
102 lines (87 loc) · 2.76 KB
/
types.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
package types
import (
"crypto/sha1"
"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"
"github.com/docker/infrakit/pkg/types"
)
// Spec is the configuration schema for the plugin, provided in group.Spec.Properties
type Spec struct {
Instance InstancePlugin
Flavor FlavorPlugin
Allocation AllocationMethod
}
// AllocationMethod defines the type of allocation and supervision needed by a flavor's Group.
type AllocationMethod struct {
Size uint
LogicalIDs []instance.LogicalID
}
// InstancePlugin is the structure that describes an instance plugin.
type InstancePlugin struct {
Plugin plugin.Name
Properties *types.Any // this will be the Spec of the plugin
}
// FlavorPlugin describes the flavor configuration
type FlavorPlugin struct {
Plugin plugin.Name
Properties *types.Any // this will be the Spec of the plugin
}
// ParseProperties parses the group plugin properties JSON document in a group configuration.
func ParseProperties(config group.Spec) (Spec, error) {
parsed := Spec{}
if config.Properties != nil {
if err := config.Properties.Decode(&parsed); err != nil {
return parsed, fmt.Errorf("Invalid properties: %s", err)
}
}
return parsed, nil
}
// UnparseProperties composes group.spec from id and props
func UnparseProperties(id string, props Spec) (group.Spec, error) {
unparsed := group.Spec{ID: group.ID(id)}
any, err := types.AnyValue(props)
if err != nil {
return unparsed, err
}
unparsed.Properties = any
return unparsed, nil
}
// MustParse can be wrapped over ParseProperties to panic if parsing fails.
func MustParse(s Spec, e error) Spec {
if e != nil {
panic(e)
}
return s
}
func stableFormat(v interface{}) []byte {
// Marshal the JSON to ensure stable key ordering. This allows structurally-identical JSON to yield the same
// hash even if the fields are reordered.
unstable, err := json.Marshal(v)
if err != nil {
panic(err)
}
props := map[string]interface{}{}
err = json.Unmarshal(unstable, &props)
if err != nil {
panic(err)
}
stable, err := json.MarshalIndent(props, " ", " ") // sorts the fields
if err != nil {
panic(err)
}
return stable
}
// InstanceHash computes a stable hash of the document in InstancePluginProperties.
func (c Spec) InstanceHash() string {
// TODO(wfarner): This does not consider changes made by plugins that are not represented by user
// configuration changes, such as if a plugin is updated. We may be able to address this by resolving plugin
// names to a versioned plugin identifier.
hasher := sha1.New()
hasher.Write(stableFormat(c.Instance))
hasher.Write(stableFormat(c.Flavor))
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}