Skip to content

Commit 78afe65

Browse files
authored
Merge pull request #1085 from gianlucam76/registry
(bug) Use libsveltos cluster predicates
2 parents 3fa0cce + 5050f8e commit 78afe65

10 files changed

Lines changed: 21 additions & 705 deletions

controllers/clusterprofile_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/projectsveltos/addon-controller/pkg/scope"
4343
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
4444
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
45+
predicates "github.com/projectsveltos/libsveltos/lib/predicates"
4546
libsveltosset "github.com/projectsveltos/libsveltos/lib/set"
4647
)
4748

@@ -210,7 +211,7 @@ func (r *ClusterProfileReconciler) SetupWithManager(mgr ctrl.Manager) error {
210211
Watches(&libsveltosv1beta1.SveltosCluster{},
211212
handler.EnqueueRequestsFromMapFunc(r.requeueClusterProfileForSveltosCluster),
212213
builder.WithPredicates(
213-
SveltosClusterPredicates(mgr.GetLogger().WithValues("predicate", "sveltosclusterpredicate")),
214+
predicates.SveltosClusterPredicates(mgr.GetLogger().WithValues("predicate", "sveltosclusterpredicate")),
214215
),
215216
).
216217
Build(r)
@@ -231,7 +232,7 @@ func (r *ClusterProfileReconciler) WatchForCAPI(mgr ctrl.Manager, c controller.C
231232
mgr.GetCache(),
232233
&clusterv1.Cluster{},
233234
handler.TypedEnqueueRequestsFromMapFunc(r.requeueClusterProfileForCluster),
234-
ClusterPredicate{Logger: mgr.GetLogger().WithValues("predicate", "clusterpredicate")},
235+
predicates.ClusterPredicate{Logger: mgr.GetLogger().WithValues("predicate", "clusterpredicate")},
235236
)
236237

237238
// When cluster-api cluster changes, according to ClusterPredicates,
@@ -244,7 +245,7 @@ func (r *ClusterProfileReconciler) WatchForCAPI(mgr ctrl.Manager, c controller.C
244245
mgr.GetCache(),
245246
&clusterv1.Machine{},
246247
handler.TypedEnqueueRequestsFromMapFunc(r.requeueClusterProfileForMachine),
247-
MachinePredicate{Logger: mgr.GetLogger().WithValues("predicate", "machinepredicate")},
248+
predicates.MachinePredicate{Logger: mgr.GetLogger().WithValues("predicate", "machinepredicate")},
248249
)
249250

250251
// When cluster-api machine changes, according to ClusterPredicates,

controllers/clusterprofile_predicates.go

Lines changed: 0 additions & 270 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"reflect"
2121

2222
"github.com/go-logr/logr"
23-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2423
"sigs.k8s.io/controller-runtime/pkg/client"
2524
"sigs.k8s.io/controller-runtime/pkg/event"
2625
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -29,275 +28,6 @@ import (
2928
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
3029
)
3130

32-
type ClusterPredicate struct {
33-
Logger logr.Logger
34-
}
35-
36-
func (p ClusterPredicate) Create(obj event.TypedCreateEvent[*clusterv1.Cluster]) bool {
37-
cluster := obj.Object
38-
39-
log := p.Logger.WithValues("predicate", "createEvent",
40-
"namespace", cluster.Namespace,
41-
"cluster", cluster.Name,
42-
)
43-
44-
// Only need to trigger a reconcile if the Cluster.Spec.Paused is false
45-
if !cluster.Spec.Paused {
46-
log.V(logs.LogVerbose).Info(
47-
"Cluster is not paused. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.",
48-
)
49-
return true
50-
}
51-
log.V(logs.LogVerbose).Info(
52-
`Cluster did not match expected conditions. \
53-
Will not attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.`)
54-
return false
55-
}
56-
57-
func (p ClusterPredicate) Update(obj event.TypedUpdateEvent[*clusterv1.Cluster]) bool {
58-
newCluster := obj.ObjectNew
59-
oldCluster := obj.ObjectOld
60-
61-
log := p.Logger.WithValues("predicate", "updateEvent",
62-
"namespace", newCluster.Namespace,
63-
"cluster", newCluster.Name,
64-
)
65-
66-
if oldCluster == nil {
67-
log.V(logs.LogVerbose).Info("Old Cluster is nil. Reconcile (Cluster)Profiles/(Cluster)Set.")
68-
return true
69-
}
70-
71-
// a label change migth change which clusters match which clusterprofile
72-
if !reflect.DeepEqual(oldCluster.Labels, newCluster.Labels) {
73-
log.V(logs.LogVerbose).Info(
74-
"Cluster labels changed. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.",
75-
)
76-
return true
77-
}
78-
79-
// if sharding is used, cluster sharding annotation must be copied over ClusterSummary
80-
if !reflect.DeepEqual(oldCluster.Annotations, newCluster.Annotations) {
81-
log.V(logs.LogVerbose).Info(
82-
"Cluster annotations changed. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.",
83-
)
84-
return true
85-
}
86-
87-
// return true if Cluster.Spec.Paused has changed from true to false
88-
if oldCluster.Spec.Paused && !newCluster.Spec.Paused {
89-
log.V(logs.LogVerbose).Info(
90-
"Cluster was unpaused. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.")
91-
return true
92-
}
93-
94-
// return true if Cluster.Status.ControlPlaneReady has changed
95-
if oldCluster.Status.ControlPlaneReady != newCluster.Status.ControlPlaneReady {
96-
log.V(logs.LogVerbose).Info(
97-
"Cluster ControlPlaneReady changed. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.")
98-
return true
99-
}
100-
101-
if oldCluster.Status.Phase != string(clusterv1.ClusterPhaseDeleting) &&
102-
newCluster.Status.Phase == string(clusterv1.ClusterPhaseDeleting) {
103-
104-
log.V(logs.LogVerbose).Info(
105-
"Cluster is beng deleted. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.")
106-
return true
107-
}
108-
109-
// otherwise, return false
110-
log.V(logs.LogVerbose).Info(
111-
`Cluster did not match expected conditions. \
112-
Will not attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.`)
113-
return false
114-
}
115-
116-
func (p ClusterPredicate) Delete(obj event.TypedDeleteEvent[*clusterv1.Cluster]) bool {
117-
log := p.Logger.WithValues("predicate", "deleteEvent",
118-
"namespace", obj.Object.GetNamespace(),
119-
"cluster", obj.Object.GetName(),
120-
)
121-
log.V(logs.LogVerbose).Info(
122-
"Cluster deleted. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.")
123-
return true
124-
}
125-
126-
func (c ClusterPredicate) Generic(obj event.TypedGenericEvent[*clusterv1.Cluster]) bool {
127-
log := c.Logger.WithValues("predicate", "genericEvent",
128-
"namespace", obj.Object.GetNamespace(),
129-
"cluster", obj.Object.GetName(),
130-
)
131-
log.V(logs.LogVerbose).Info(
132-
`Cluster did not match expected conditions. \
133-
Will not attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.`)
134-
return false
135-
}
136-
137-
// SveltosClusterPredicates predicates for sveltos Cluster. ClusterProfileReconciler watches sveltos Cluster events
138-
// and react to those by reconciling itself based on following predicates
139-
func SveltosClusterPredicates(logger logr.Logger) predicate.Funcs {
140-
return predicate.Funcs{
141-
UpdateFunc: func(e event.UpdateEvent) bool {
142-
newCluster := e.ObjectNew.(*libsveltosv1beta1.SveltosCluster)
143-
oldCluster := e.ObjectOld.(*libsveltosv1beta1.SveltosCluster)
144-
log := logger.WithValues("predicate", "updateEvent",
145-
"namespace", newCluster.Namespace,
146-
"cluster", newCluster.Name,
147-
)
148-
149-
if oldCluster == nil {
150-
log.V(logs.LogVerbose).Info("Old Cluster is nil. Reconcile (Cluster)Profiles/(Cluster)Set.")
151-
return true
152-
}
153-
154-
// return true if Cluster.Spec.Paused has changed from true to false
155-
if oldCluster.Spec.Paused && !newCluster.Spec.Paused {
156-
log.V(logs.LogVerbose).Info(
157-
"Cluster was unpaused. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.")
158-
return true
159-
}
160-
161-
if oldCluster.Status.Ready != newCluster.Status.Ready {
162-
log.V(logs.LogVerbose).Info(
163-
"Cluster Status.Ready changed. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.")
164-
return true
165-
}
166-
167-
// a label change migth change which clusters match which clusterprofile
168-
if !reflect.DeepEqual(oldCluster.Labels, newCluster.Labels) {
169-
log.V(logs.LogVerbose).Info(
170-
"Cluster labels changed. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.",
171-
)
172-
return true
173-
}
174-
175-
// if sharding is used, cluster sharding annotation must be copied over ClusterSummary
176-
if !reflect.DeepEqual(oldCluster.Annotations, newCluster.Annotations) {
177-
log.V(logs.LogVerbose).Info(
178-
"Cluster annotations changed. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.",
179-
)
180-
return true
181-
}
182-
183-
// otherwise, return false
184-
log.V(logs.LogVerbose).Info(
185-
`Cluster did not match expected conditions. \
186-
Will not attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.`)
187-
return false
188-
},
189-
CreateFunc: func(e event.CreateEvent) bool {
190-
cluster := e.Object.(*libsveltosv1beta1.SveltosCluster)
191-
log := logger.WithValues("predicate", "createEvent",
192-
"namespace", cluster.Namespace,
193-
"cluster", cluster.Name,
194-
)
195-
196-
// Only need to trigger a reconcile if the Cluster.Spec.Paused is false
197-
if !cluster.Spec.Paused {
198-
log.V(logs.LogVerbose).Info(
199-
"Cluster is not paused. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.",
200-
)
201-
return true
202-
}
203-
log.V(logs.LogVerbose).Info(
204-
`Cluster did not match expected conditions. \
205-
Will not attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.`)
206-
return false
207-
},
208-
DeleteFunc: func(e event.DeleteEvent) bool {
209-
log := logger.WithValues("predicate", "deleteEvent",
210-
"namespace", e.Object.GetNamespace(),
211-
"cluster", e.Object.GetName(),
212-
)
213-
log.V(logs.LogVerbose).Info(
214-
"Cluster deleted. Will attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.")
215-
return true
216-
},
217-
GenericFunc: func(e event.GenericEvent) bool {
218-
log := logger.WithValues("predicate", "genericEvent",
219-
"namespace", e.Object.GetNamespace(),
220-
"cluster", e.Object.GetName(),
221-
)
222-
log.V(logs.LogVerbose).Info(
223-
`Cluster did not match expected conditions. \
224-
Will not attempt to reconcile associated (Cluster)Profiles/(Cluster)Set.`)
225-
return false
226-
},
227-
}
228-
}
229-
230-
type MachinePredicate struct {
231-
Logger logr.Logger
232-
}
233-
234-
func (p MachinePredicate) Create(obj event.TypedCreateEvent[*clusterv1.Machine]) bool {
235-
machine := obj.Object
236-
log := p.Logger.WithValues("predicate", "createEvent",
237-
"namespace", machine.Namespace,
238-
"machine", machine.Name,
239-
)
240-
241-
// Only need to trigger a reconcile if the Machine.Status.Phase is Running
242-
if machine.Status.GetTypedPhase() == clusterv1.MachinePhaseRunning {
243-
return true
244-
}
245-
246-
log.V(logs.LogVerbose).Info(
247-
"Machine did not match expected conditions. Will not attempt to reconcile associated ClusterProfiles.")
248-
return false
249-
}
250-
251-
func (p MachinePredicate) Update(obj event.TypedUpdateEvent[*clusterv1.Machine]) bool {
252-
newMachine := obj.ObjectNew
253-
oldMachine := obj.ObjectOld
254-
log := p.Logger.WithValues("predicate", "updateEvent",
255-
"namespace", newMachine.Namespace,
256-
"machine", newMachine.Name,
257-
)
258-
259-
if newMachine.Status.GetTypedPhase() != clusterv1.MachinePhaseRunning {
260-
return false
261-
}
262-
263-
if oldMachine == nil {
264-
log.V(logs.LogVerbose).Info("Old Machine is nil. Reconcile ClusterProfile")
265-
return true
266-
}
267-
268-
// return true if Machine.Status.Phase has changed from not running to running
269-
if oldMachine.Status.GetTypedPhase() != newMachine.Status.GetTypedPhase() {
270-
log.V(logs.LogVerbose).Info(
271-
"Machine was not in Running Phase. Will attempt to reconcile associated ClusterProfiles.")
272-
return true
273-
}
274-
275-
// otherwise, return false
276-
log.V(logs.LogVerbose).Info(
277-
"Machine did not match expected conditions. Will not attempt to reconcile associated ClusterProfiles.")
278-
return false
279-
}
280-
281-
func (p MachinePredicate) Delete(obj event.TypedDeleteEvent[*clusterv1.Machine]) bool {
282-
log := p.Logger.WithValues("predicate", "deleteEvent",
283-
"namespace", obj.Object.GetNamespace(),
284-
"machine", obj.Object.GetName(),
285-
)
286-
log.V(logs.LogVerbose).Info(
287-
"Machine did not match expected conditions. Will not attempt to reconcile associated ClusterProfiles.")
288-
return false
289-
}
290-
291-
func (p MachinePredicate) Generic(obj event.TypedGenericEvent[*clusterv1.Machine]) bool {
292-
log := p.Logger.WithValues("predicate", "genericEvent",
293-
"namespace", obj.Object.GetNamespace(),
294-
"machine", obj.Object.GetName(),
295-
)
296-
log.V(logs.LogVerbose).Info(
297-
"Machine did not match expected conditions. Will not attempt to reconcile associated ClusterProfiles.")
298-
return false
299-
}
300-
30131
// SetPredicates predicates for ClusterSet/Set. ClusterProfileReconciler watches ClusterSet/Set events
30232
// and react to those by reconciling itself based on following predicates
30333
func SetPredicates(logger logr.Logger) predicate.Funcs {

0 commit comments

Comments
 (0)