code review

This commit is contained in:
Donny Xia
2020-08-05 13:31:05 -07:00
parent 740ec39dd8
commit fc70e3181f
2 changed files with 10 additions and 46 deletions

View File

@@ -63,57 +63,21 @@ func HashRNode(node *yaml.RNode) (string, error) {
kind := kindNode.YNode().Value kind := kindNode.YNode().Value
// calculate hash for different kinds // calculate hash for different kinds
encoded := ""
switch kind { switch kind {
case "ConfigMap": case "ConfigMap":
return configMapHash(node) encoded, err = encodeConfigMap(node)
case "Secret": case "Secret":
return secretHash(node) encoded, err = encodeSecret(node)
default: default:
return defaultHash(node) var encodedBytes []byte
encodedBytes, err = json.Marshal(node.YNode())
encoded = string(encodedBytes)
} }
}
// configMapHash returns a hash of the ConfigMap.
// The Data, Kind, and Name are taken into account.
func configMapHash(node *yaml.RNode) (string, error) {
encoded, err := encodeConfigMap(node)
if err != nil { if err != nil {
return "", err return "", err
} }
h, err := Encode(Hash(encoded)) return Encode(Hash(encoded))
if err != nil {
return "", err
}
return h, nil
}
// SecretHash returns a hash of the Secret.
// The Data, Kind, Name, and Type are taken into account.
func secretHash(node *yaml.RNode) (string, error) {
encoded, err := encodeSecret(node)
if err != nil {
return "", err
}
h, err := Encode(Hash(encoded))
if err != nil {
return "", err
}
return h, nil
}
// unstructuredHash creates a hash for an arbitrary type.
// All fields of the object are taken into account when generating the hash.
// This is a fallback for when a specalised hash for the type is unavailable.
func defaultHash(node *yaml.RNode) (string, error) {
encoded, err := json.Marshal(node.YNode())
if err != nil {
return "", err
}
h, err := Encode(Hash(string(encoded)))
if err != nil {
return "", err
}
return h, nil
} }
func getNodeValues(node *yaml.RNode, paths []string) (map[string]interface{}, error) { func getNodeValues(node *yaml.RNode, paths []string) (map[string]interface{}, error) {

View File

@@ -99,7 +99,7 @@ binaryData:
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
h, err := configMapHash(node) h, err := HashRNode(node)
if SkipRest(t, c.desc, err, c.err) { if SkipRest(t, c.desc, err, c.err) {
continue continue
} }
@@ -160,7 +160,7 @@ data:
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
h, err := secretHash(node) h, err := HashRNode(node)
if SkipRest(t, c.desc, err, c.err) { if SkipRest(t, c.desc, err, c.err) {
continue continue
} }
@@ -197,7 +197,7 @@ spec:
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
h, err := defaultHash(node) h, err := HashRNode(node)
if SkipRest(t, c.desc, err, c.err) { if SkipRest(t, c.desc, err, c.err) {
continue continue
} }