1
0
mirror of https://github.com/rancher/os.git synced 2025-07-01 09:11:48 +00:00

Merge pull request #297 from deniseschannon/WIP

Fixes creating Hash
This commit is contained in:
Darren Shepherd 2015-05-11 18:02:25 -07:00
commit 4e94e92c9d

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"reflect"
"sort" "sort"
"strings" "strings"
@ -47,8 +48,62 @@ func getHash(containerCfg *config.ContainerConfig) (string, error) {
w.Write([]byte(containerCfg.Id)) w.Write([]byte(containerCfg.Id))
w.Write([]byte(containerCfg.Cmd)) w.Write([]byte(containerCfg.Cmd))
if containerCfg.Service != nil { if containerCfg.Service != nil {
//TODO: properly hash //Get values of Service through reflection
w.Write([]byte(fmt.Sprintf("%v", containerCfg.Service))) val := reflect.ValueOf(containerCfg.Service).Elem()
//Create slice to sort the keys in Service Config, which allow constant hash ordering
var serviceKeys []string
//Create a data structure of map of values keyed by a string
unsortedKeyValue := make(map[string]interface{})
//Get all keys and values in Service Configuration
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
keyField := val.Type().Field(i)
serviceKeys = append(serviceKeys, keyField.Name)
unsortedKeyValue[keyField.Name] = valueField.Interface()
}
//Sort serviceKeys alphabetically
sort.Strings(serviceKeys)
//Go through keys and write hash
for i := 0; i < len(serviceKeys); i++ {
serviceValue := unsortedKeyValue[serviceKeys[i]]
sliceKeys := []string{}
switch s := serviceValue.(type) {
default:
w.Write([]byte(fmt.Sprintf("%v", serviceValue)))
case *project.SliceorMap:
for lkey := range s.MapParts() {
if lkey != "io.rancher.os.hash" {
sliceKeys = append(sliceKeys, lkey)
}
}
sort.Strings(sliceKeys)
for j := 0; j < len(sliceKeys); j++ {
w.Write([]byte(fmt.Sprintf("%s=%v", sliceKeys[j], s.MapParts()[sliceKeys[j]])))
}
case *project.Stringorslice:
sliceKeys = s.Slice()
sort.Strings(sliceKeys)
for j := 0; j < len(sliceKeys); j++ {
w.Write([]byte(fmt.Sprintf("%s", sliceKeys[j])))
}
case []string:
sliceKeys = s
sort.Strings(sliceKeys)
for j := 0; j < len(sliceKeys); j++ {
w.Write([]byte(fmt.Sprintf("%s", sliceKeys[j])))
}
}
}
} }
if w.Err != nil { if w.Err != nil {