-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper.go
42 lines (40 loc) · 1.41 KB
/
helper.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
package hashing
import (
"encoding/json"
"fmt"
)
// function to get the relevant information out of k8s object that is needed to compute hash
func GetFilteredK8sObjectForHashing(object interface{}) (map[string]interface{}, error) {
// By marshaling/unmarshaling the object via JSON we're copying it into a
// map, which is easier to manipulate in generic way than structs.
if b, err := json.Marshal(object); err != nil {
return nil, fmt.Errorf("error while encoding %+v into JSON: %s", object, err)
} else {
var v map[string]interface{}
if err := json.Unmarshal(b, &v); err != nil {
return nil, fmt.Errorf("error while decoding JSON %s into an object: %s", v, err)
} else {
// We need only kind/version/spec and labels excluding the label with the
// current hash value while calculating the hash. Also Kubernetes adds
// its own info into `metadata` which we need to ignore.
meta := v["metadata"].(map[string]interface{})
labels := meta["labels"]
if labels != nil {
delete(labels.(map[string]interface{}), "yelp.com/operator_config_hash")
} else {
labels = make(map[string]interface{})
}
m := map[string]interface{}{
"kind": v["kind"],
"apiVersion": v["apiVersion"],
"spec": v["spec"],
"metadata": map[string]interface{}{
"name": meta["name"],
"namespace": meta["namespace"],
"labels": labels,
},
}
return m, nil
}
}
}