-
Notifications
You must be signed in to change notification settings - Fork 71
/
changedetector.go
74 lines (65 loc) · 2.31 KB
/
changedetector.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
63
64
65
66
67
68
69
70
71
72
73
74
package changedetector
import (
"context"
"fmt"
"github.com/armory/spinnaker-operator/pkg/apis/spinnaker/interfaces"
"github.com/go-logr/logr"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type ChangeDetector interface {
IsSpinnakerUpToDate(ctx context.Context, svc interfaces.SpinnakerService) (bool, error)
AlwaysRun() bool
}
type DetectorGenerator interface {
NewChangeDetector(client client.Client, log logr.Logger, evtRecorder record.EventRecorder, scheme *runtime.Scheme) (ChangeDetector, error)
}
type compositeChangeDetector struct {
changeDetectors []ChangeDetector
log logr.Logger
evtRecorder record.EventRecorder
}
type CompositeChangeDetectorGenerator struct {
Generators []DetectorGenerator
}
func (g *CompositeChangeDetectorGenerator) NewChangeDetector(client client.Client, log logr.Logger, evtRecorder record.EventRecorder, scheme *runtime.Scheme) (ChangeDetector, error) {
changeDetectors := make([]ChangeDetector, 0)
for _, generator := range g.Generators {
ch, err := generator.NewChangeDetector(client, log, evtRecorder, scheme)
if err != nil {
return nil, err
}
changeDetectors = append(changeDetectors, ch)
}
return &compositeChangeDetector{
changeDetectors: changeDetectors,
log: log,
evtRecorder: evtRecorder,
}, nil
}
// IsSpinnakerUpToDate returns true if all children change detectors return true
func (ch *compositeChangeDetector) IsSpinnakerUpToDate(ctx context.Context, svc interfaces.SpinnakerService) (bool, error) {
rLogger := ch.log.WithValues("Service", svc.GetName())
isUpToDate := true
for _, changeDetector := range ch.changeDetectors {
// Don't run the change detector if we already know Spinnaker is not up to date
if !isUpToDate && !changeDetector.AlwaysRun() {
continue
}
upd, err := changeDetector.IsSpinnakerUpToDate(ctx, svc)
if err != nil {
return false, err
}
if !upd {
rLogger.Info(fmt.Sprintf("%T detected a change that needs to be reconciled", changeDetector))
ch.evtRecorder.Eventf(svc, v1.EventTypeNormal, "ConfigChanged", "%T detected a change that needs to be reconciled", changeDetector)
isUpToDate = false
}
}
return isUpToDate, nil
}
func (ch *compositeChangeDetector) AlwaysRun() bool {
return true
}