/
predicate_func.go
204 lines (180 loc) · 5.74 KB
/
predicate_func.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
// Licensed under the Apache License 2.0
package placementrule
import (
"fmt"
"reflect"
"strings"
"github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/pkg/config"
operatorconfig "github.com/stolostron/multicluster-observability-operator/operators/pkg/config"
appsv1 "k8s.io/api/apps/v1"
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
func getClusterPreds() predicate.Funcs {
createFunc := func(e event.CreateEvent) bool {
log.Info("CreateFunc", "managedCluster", e.Object.GetName())
//ACM 8509: Special case for local-cluster, we do not react changes to
//local-cluster as it is expected to be always present in the managed cluster list
//whether hubSelfManagement is enabled or not
if e.Object.GetName() == "local-cluster" {
return false
}
if isAutomaticAddonInstallationDisabled(e.Object) {
return false
}
if e.Object.GetName() != "local-cluster" {
updateManagedClusterList(e.Object)
}
updateManagedClusterImageRegistry(e.Object)
return true
}
updateFunc := func(e event.UpdateEvent) bool {
log.Info("UpdateFunc", "managedCluster", e.ObjectNew.GetName())
if e.ObjectNew.GetName() == "local-cluster" {
return false
}
if e.ObjectNew.GetResourceVersion() == e.ObjectOld.GetResourceVersion() {
return false
}
if isAutomaticAddonInstallationDisabled(e.ObjectNew) {
return false
}
if e.ObjectNew.GetDeletionTimestamp() != nil {
log.Info("managedcluster is in terminating state", "managedCluster", e.ObjectNew.GetName())
managedClusterListMutex.Lock()
delete(managedClusterList, e.ObjectNew.GetName())
managedClusterListMutex.Unlock()
managedClusterImageRegistryMutex.Lock()
delete(managedClusterImageRegistry, e.ObjectNew.GetName())
managedClusterImageRegistryMutex.Unlock()
} else {
updateManagedClusterList(e.ObjectNew)
updateManagedClusterImageRegistry(e.ObjectNew)
}
return true
}
deleteFunc := func(e event.DeleteEvent) bool {
log.Info("DeleteFunc", "managedCluster", e.Object.GetName())
if e.Object.GetName() == "local-cluster" {
return false
}
if isAutomaticAddonInstallationDisabled(e.Object) {
return false
}
managedClusterListMutex.Lock()
delete(managedClusterList, e.Object.GetName())
managedClusterListMutex.Unlock()
managedClusterImageRegistryMutex.Lock()
delete(managedClusterImageRegistry, e.Object.GetName())
managedClusterImageRegistryMutex.Unlock()
return true
}
return predicate.Funcs{
CreateFunc: createFunc,
UpdateFunc: updateFunc,
DeleteFunc: deleteFunc,
}
}
func GetAddOnDeploymentConfigPredicates() predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
newObj := e.ObjectNew.(*addonv1alpha1.AddOnDeploymentConfig)
oldObj := e.ObjectOld.(*addonv1alpha1.AddOnDeploymentConfig)
if reflect.DeepEqual(newObj.Spec, oldObj.Spec) {
return false
}
log.Info("AddonDeploymentConfig is updated", e.ObjectNew.GetName(), "name", e.ObjectNew.GetNamespace(), "namespace")
return true
},
DeleteFunc: func(e event.DeleteEvent) bool {
return true
},
}
}
func getHubEndpointOperatorPredicates() predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectNew.GetNamespace() == config.GetDefaultNamespace() && e.ObjectNew.GetName() == config.HubEndpointOperatorName &&
!reflect.DeepEqual(e.ObjectNew.(*appsv1.Deployment).Spec.Template.Spec,
e.ObjectOld.(*appsv1.Deployment).Spec.Template.Spec) {
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
if operatorconfig.IsMCOTerminating {
log.Info("MCO is terminating, skip reconcile for hub endpoint operator")
return false
}
if e.Object.GetNamespace() == config.GetDefaultNamespace() && e.Object.GetName() == config.HubEndpointOperatorName {
return true
}
return false
},
}
}
//nolint:unparam
func getPred(name string, namespace string,
create bool, update bool, delete bool) predicate.Funcs {
createFunc := func(e event.CreateEvent) bool {
return false
}
updateFunc := func(e event.UpdateEvent) bool {
return false
}
deleteFunc := func(e event.DeleteEvent) bool {
return false
}
if create {
createFunc = func(e event.CreateEvent) bool {
if e.Object.GetName() == name && (e.Object.GetNamespace() == namespace) {
return true
}
return false
}
}
if update {
updateFunc = func(e event.UpdateEvent) bool {
if e.ObjectNew.GetName() == name && (e.ObjectNew.GetNamespace() == namespace) &&
e.ObjectNew.GetResourceVersion() != e.ObjectOld.GetResourceVersion() {
// also check objectNew string in case Kind is empty
if strings.HasPrefix(fmt.Sprint(e.ObjectNew), "&Deployment") ||
e.ObjectNew.GetObjectKind().GroupVersionKind().Kind == "Deployment" {
if !reflect.DeepEqual(e.ObjectNew.(*appsv1.Deployment).Spec.Template.Spec,
e.ObjectOld.(*appsv1.Deployment).Spec.Template.Spec) {
return true
}
} else {
return true
}
}
return false
}
}
if delete {
deleteFunc = func(e event.DeleteEvent) bool {
if operatorconfig.IsMCOTerminating {
log.Info("MCO is terminating, skip reconcile for placementrule controller", "name", name, "namespace", namespace)
return false
}
if e.Object.GetName() == name && (e.Object.GetNamespace() == namespace) {
return true
}
return false
}
}
return predicate.Funcs{
CreateFunc: createFunc,
UpdateFunc: updateFunc,
DeleteFunc: deleteFunc,
}
}