forked from rancher/rancher-compose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
153 lines (126 loc) · 3.8 KB
/
hash.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
package digest
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"reflect"
"sort"
"github.com/docker/libcompose/utils"
"github.com/rancher/go-rancher/v2"
rUtils "github.com/rancher/rancher-compose/utils"
)
const (
ServiceHashKey = "io.rancher.service.hash"
)
var (
ignoreKeys = []string{
ServiceHashKey,
"links",
"scale",
"selector_container",
"selector_link",
"stack_id",
}
)
type ServiceHash struct {
Service string `json:"service,omitempty" yaml:"service,omitempty"`
LaunchConfig string `json:"launch_config,omitempty" yaml:"launch_config,omitempty"`
SecondaryLaunchConfigs map[string]string `json:"secondary_launch_configs,omitempty" yaml:"secondary_launch_configs,omitempty"`
}
func (s ServiceHash) Equals(hash ServiceHash) bool {
return s.Service == hash.Service &&
s.LaunchConfig == hash.LaunchConfig &&
// The check for 0 handles when one of the maps is nil and the other is empty
(len(s.SecondaryLaunchConfigs) == 0 && len(hash.SecondaryLaunchConfigs) == 0 ||
reflect.DeepEqual(s.SecondaryLaunchConfigs, hash.SecondaryLaunchConfigs))
}
func toString(obj interface{}) string {
if obj == nil {
return ""
}
return fmt.Sprintf("%v", obj)
}
func LookupHash(service *client.Service) (ServiceHash, bool) {
ret := ServiceHash{
SecondaryLaunchConfigs: map[string]string{},
}
ret.Service = toString(service.Metadata[ServiceHashKey])
ret.LaunchConfig = toString(service.LaunchConfig.Labels[ServiceHashKey])
for _, rawSecondaryLaunchConfig := range service.SecondaryLaunchConfigs {
var secondaryLaunchConfig client.SecondaryLaunchConfig
if err := utils.Convert(rawSecondaryLaunchConfig, &secondaryLaunchConfig); err != nil {
return ret, false
}
ret.SecondaryLaunchConfigs[secondaryLaunchConfig.Name] = toString(secondaryLaunchConfig.Labels[ServiceHashKey])
}
return ret, ret.Service != ""
}
func CreateServiceHash(rancherService interface{}, launchConfig *client.LaunchConfig, secondaryLaunchConfigs []client.SecondaryLaunchConfig) (ServiceHash, error) {
var err error
result := ServiceHash{}
if err != nil {
return result, err
}
result.Service, err = hashObj(rancherService)
if err != nil {
return result, err
}
result.LaunchConfig, err = hashObj(launchConfig)
if err != nil {
return result, err
}
for _, secondaryLaunchConfig := range secondaryLaunchConfigs {
hash, err := hashObj(secondaryLaunchConfig)
if err != nil {
return result, err
}
if result.SecondaryLaunchConfigs == nil {
result.SecondaryLaunchConfigs = map[string]string{}
}
result.SecondaryLaunchConfigs[secondaryLaunchConfig.Name] = hash
}
return result, nil
}
func toSortedStringMap(data map[interface{}]interface{}) ([]string, map[string]interface{}) {
keys := []string{}
ret := map[string]interface{}{}
for k, v := range data {
str := fmt.Sprintf("%v", k)
keys = append(keys, str)
ret[str] = v
}
sort.Strings(keys)
return keys, ret
}
func writeNullTerminatedValue(writer io.Writer, value interface{}) {
switch s := value.(type) {
case map[interface{}]interface{}:
writeNativeMap(writer, s)
case []interface{}:
for _, sliceValue := range s {
writeNullTerminatedValue(writer, sliceValue)
}
default:
io.WriteString(writer, fmt.Sprintf("%v", value))
writer.Write([]byte{0})
}
}
func hashObj(obj interface{}) (string, error) {
hash := sha1.New()
mapObj := map[interface{}]interface{}{}
if err := utils.Convert(obj, &mapObj); err != nil {
return "", err
}
writeNativeMap(hash, mapObj)
return hex.EncodeToString(hash.Sum(nil)), nil
}
func writeNativeMap(writer io.Writer, rawData map[interface{}]interface{}) {
keys, data := toSortedStringMap(rawData)
for _, key := range keys {
if !rUtils.Contains(ignoreKeys, key) {
writeNullTerminatedValue(writer, key)
writeNullTerminatedValue(writer, data[key])
}
}
}