-
Notifications
You must be signed in to change notification settings - Fork 37
/
cf_toleration.go
83 lines (70 loc) · 2.35 KB
/
cf_toleration.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
75
76
77
78
79
80
81
82
83
package cf
import (
"reflect"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop/context"
"github.com/Apicurio/apicurio-registry-operator/controllers/svc/resources"
ar "github.com/Apicurio/apicurio-registry-operator/api/v1"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)
var _ loop.ControlFunction = &TolerationCF{}
type TolerationCF struct {
ctx context.LoopContext
svcResourceCache resources.ResourceCache
deploymentEntry resources.ResourceCacheEntry
deploymentEntryExists bool
existingTolerations []corev1.Toleration
targetTolerations []corev1.Toleration
}
func NewTolerationCF(ctx context.LoopContext) loop.ControlFunction {
return &TolerationCF{
ctx: ctx,
svcResourceCache: ctx.GetResourceCache(),
deploymentEntry: nil,
deploymentEntryExists: false,
existingTolerations: nil,
targetTolerations: nil,
}
}
func (this *TolerationCF) Describe() string {
return "TolerationCF"
}
func (this *TolerationCF) Sense() {
// Observation #1
// Get the cached deployment
this.deploymentEntry, this.deploymentEntryExists = this.svcResourceCache.Get(resources.RC_KEY_DEPLOYMENT)
if this.deploymentEntryExists {
// Observation #2
// Get the existing tolerations
this.existingTolerations = this.deploymentEntry.GetValue().(*apps.Deployment).Spec.Template.Spec.Tolerations
// Observation #3
// Get the target tolerations
if specEntry, exists := this.svcResourceCache.Get(resources.RC_KEY_SPEC); exists {
this.targetTolerations = specEntry.GetValue().(*ar.ApicurioRegistry).Spec.Deployment.Tolerations
}
}
}
func (this *TolerationCF) Compare() bool {
// Condition #1
// Deployment exists
// Condition #2
// Target toleration exists
// Condition #3
// Existing tolerations are different from target tolerations
return this.deploymentEntryExists &&
!reflect.DeepEqual(this.existingTolerations, this.targetTolerations)
}
func (this *TolerationCF) Respond() {
// Response #1
// Patch the resource
this.deploymentEntry.ApplyPatch(func(value interface{}) interface{} {
deployment := value.(*apps.Deployment).DeepCopy()
deployment.Spec.Template.Spec.Tolerations = this.targetTolerations
return deployment
})
}
func (this *TolerationCF) Cleanup() bool {
// No cleanup
return true
}