-
Notifications
You must be signed in to change notification settings - Fork 67
/
config.go
62 lines (51 loc) · 1.76 KB
/
config.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
package changedetector
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"github.com/armory/spinnaker-operator/pkg/apis/spinnaker/interfaces"
"github.com/go-logr/logr"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"
)
const SpinnakerConfigHashKey = "config"
const KustomizeHashKey = "kustomize"
type configChangeDetector struct {
log logr.Logger
evtRecorder record.EventRecorder
}
type configChangeDetectorGenerator struct{}
func (g *configChangeDetectorGenerator) NewChangeDetector(client client.Client, log logr.Logger, evtRecorder record.EventRecorder) (ChangeDetector, error) {
return &configChangeDetector{log: log, evtRecorder: evtRecorder}, nil
}
// IsSpinnakerUpToDate returns true if the Config has changed compared to the last recorded status hash
func (ch *configChangeDetector) IsSpinnakerUpToDate(ctx context.Context, spinSvc interfaces.SpinnakerService) (bool, error) {
upd, err := ch.isUpToDate(spinSvc.GetSpinnakerConfig(), SpinnakerConfigHashKey, spinSvc)
if err != nil {
return false, err
}
kUpd, err := ch.isUpToDate(spinSvc.GetKustomization(), KustomizeHashKey, spinSvc)
return upd && kUpd, err
}
func (ch *configChangeDetector) isUpToDate(config interface{}, hashKey string, spinSvc interfaces.SpinnakerService) (bool, error) {
h, err := ch.getHash(config)
if err != nil {
return false, err
}
st := spinSvc.GetStatus()
prior := st.UpdateHashIfNotExist(hashKey, h, time.Now(), true)
return h == prior.Hash, nil
}
func (ch *configChangeDetector) getHash(config interface{}) (string, error) {
data, err := json.Marshal(config)
if err != nil {
return "", err
}
m := md5.Sum(data)
return hex.EncodeToString(m[:]), nil
}
func (ch *configChangeDetector) AlwaysRun() bool {
return true
}