/
comparison.go
61 lines (52 loc) · 1.8 KB
/
comparison.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
package comparison
import (
"bytes"
"crypto/md5" // #nosec
"encoding/hex"
"encoding/json"
"fmt"
"io"
apps "k8s.io/api/apps/v1beta1"
kanaryv1alpha1 "github.com/amadeusitgroup/kanary/pkg/apis/kanary/v1alpha1"
)
// IsUpToDateDeployment returns true if the Deployment is up to date with the KanaryDeployment deployment template.
func IsUpToDateDeployment(kd *kanaryv1alpha1.KanaryDeployment, dep *apps.Deployment) bool {
hash, err := GenerateMD5DeploymentSpec(&kd.Spec.Template.Spec)
if err != nil {
return false
}
return CompareDeploymentMD5Hash(hash, dep)
}
// CompareDeploymentMD5Hash used to compare a md5 hash with the one setted in Deployment annotation
func CompareDeploymentMD5Hash(hash string, dep *apps.Deployment) bool {
if val, ok := dep.Annotations[string(kanaryv1alpha1.MD5KanaryDeploymentAnnotationKey)]; ok && val == hash {
return true
}
return false
}
// GenerateMD5DeploymentSpec used to generate the DeploymentSpec MD5 hash
func GenerateMD5DeploymentSpec(spec *apps.DeploymentSpec) (string, error) {
b, err := json.Marshal(spec)
if err != nil {
return "", err
}
/* #nosec */
hash := md5.New()
_, err = io.Copy(hash, bytes.NewReader(b))
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
// SetMD5DeploymentSpecAnnotation used to set the md5 annotation key/value from the KanaryDeployement.Spec.Template.Spec
func SetMD5DeploymentSpecAnnotation(kd *kanaryv1alpha1.KanaryDeployment, dep *apps.Deployment) (string, error) {
md5Spec, err := GenerateMD5DeploymentSpec(&kd.Spec.Template.Spec)
if err != nil {
return "", fmt.Errorf("unable to generates the JobSpec MD5, %v", err)
}
if dep.Annotations == nil {
dep.SetAnnotations(map[string]string{})
}
dep.Annotations[string(kanaryv1alpha1.MD5KanaryDeploymentAnnotationKey)] = md5Spec
return md5Spec, nil
}