@@ -121,12 +121,20 @@ type lppItem struct {
121121 cache.ObjectName
122122}
123123
124+ type lcItem struct {
125+ cache.ObjectName
126+ }
127+
124128func (ctl * controller ) OnAdd (obj any , isInInitialList bool ) {
125129 switch typed := obj .(type ) {
126130 case * fmav1alpha1.LauncherPopulationPolicy :
127131 ctl .enqueueLogger .V (5 ).Info ("Enqueuing LauncherPopulationPolicy reference due to notification of add" , "name" , typed .Name )
128132 item := lppItem {cache .MetaObjectToName (typed )}
129133 ctl .Queue .Add (item )
134+ case * fmav1alpha1.LauncherConfig :
135+ ctl .enqueueLogger .V (5 ).Info ("Enqueuing LauncherConfig reference due to notification of add" , "name" , typed .Name )
136+ item := lcItem {cache .MetaObjectToName (typed )}
137+ ctl .Queue .Add (item )
130138 default :
131139 ctl .enqueueLogger .V (5 ).Info ("Notified of add of type of ignored object" , "type" , fmt .Sprintf ("%T" , obj ))
132140 return
@@ -139,6 +147,10 @@ func (ctl *controller) OnUpdate(prev, obj any) {
139147 ctl .enqueueLogger .V (5 ).Info ("Enqueuing LauncherPopulationPolicy reference due to notification of update" , "name" , typed .Name )
140148 item := lppItem {cache .MetaObjectToName (typed )}
141149 ctl .Queue .Add (item )
150+ case * fmav1alpha1.LauncherConfig :
151+ ctl .enqueueLogger .V (5 ).Info ("Enqueuing LauncherConfig reference due to notification of update" , "name" , typed .Name )
152+ item := lcItem {cache .MetaObjectToName (typed )}
153+ ctl .Queue .Add (item )
142154 default :
143155 ctl .enqueueLogger .V (5 ).Info ("Notified of update of type of ignored object" , "type" , fmt .Sprintf ("%T" , obj ))
144156 return
@@ -154,7 +166,10 @@ func (ctl *controller) OnDelete(obj any) {
154166 ctl .enqueueLogger .V (5 ).Info ("Enqueuing LauncherPopulationPolicy reference due to notification of delete" , "name" , typed .Name )
155167 item := lppItem {cache .MetaObjectToName (typed )}
156168 ctl .Queue .Add (item )
157-
169+ case * fmav1alpha1.LauncherConfig :
170+ ctl .enqueueLogger .V (5 ).Info ("Enqueuing LauncherConfig reference due to notification of delete" , "name" , typed .Name )
171+ item := lcItem {cache .MetaObjectToName (typed )}
172+ ctl .Queue .Add (item )
158173 default :
159174 ctl .enqueueLogger .V (5 ).Info ("Notified of delete of type of ignored object" , "type" , fmt .Sprintf ("%T" , obj ))
160175 return
@@ -235,6 +250,63 @@ func (item lppItem) process(ctx context.Context, ctl *controller) (error, bool)
235250 return nil , false
236251}
237252
253+ func (item lcItem ) process (ctx context.Context , ctl * controller ) (error , bool ) {
254+ logger := klog .FromContext (ctx )
255+
256+ // Get the LauncherConfig
257+ lc , err := ctl .lcLister .LauncherConfigs (ctl .namespace ).Get (item .Name )
258+ if err != nil {
259+ if apierrors .IsNotFound (err ) {
260+ logger .Info ("LauncherConfig no longer exists, skipping reconciliation" , "name" , item .Name )
261+ return nil , false
262+ }
263+ logger .Error (err , "Failed to get LauncherConfig" , "name" , item .Name )
264+ return err , true
265+ }
266+
267+ // Get all LauncherPopulationPolicies that reference this LauncherConfig
268+ policies , err := ctl .lppLister .List (labels .Everything ())
269+ if err != nil {
270+ logger .Error (err , "Failed to list LauncherPopulationPolicies" )
271+ return err , true
272+ }
273+
274+ // Build desired state for this LauncherConfig across all nodes
275+ desired := make (map [NodeLauncherKey ]int32 )
276+ for _ , lpp := range policies {
277+ nodes , err := ctl .getMatchingNodes (ctx , lpp .Spec .EnhancedNodeSelector )
278+ if err != nil {
279+ logger .Error (err , "Failed to get matching nodes for policy" , "policy" , lpp .Name )
280+ continue
281+ }
282+
283+ for _ , countRule := range lpp .Spec .CountForLauncher {
284+ if countRule .LauncherConfigName != lc .Name {
285+ continue
286+ }
287+ for _ , node := range nodes {
288+ key := NodeLauncherKey {
289+ NodeName : node .Name ,
290+ LauncherConfigName : lc .Name ,
291+ }
292+ // Take the maximum count if multiple rules apply
293+ if current , exists := desired [key ]; ! exists || countRule .LauncherCount > current {
294+ desired [key ] = countRule .LauncherCount
295+ }
296+ }
297+ }
298+ }
299+
300+ // Reconcile launchers for this LauncherConfig
301+ if err := ctl .reconcileAllLaunchers (ctx , desired ); err != nil {
302+ logger .Error (err , "Failed to reconcile launchers for LauncherConfig" , "name" , lc .Name )
303+ return err , true
304+ }
305+
306+ logger .Info ("Successfully reconciled launchers for LauncherConfig" , "name" , lc .Name )
307+ return nil , false
308+ }
309+
238310// getMatchingNodes returns nodes that match the EnhancedNodeSelector
239311func (ctl * controller ) getMatchingNodes (ctx context.Context , selector fmav1alpha1.EnhancedNodeSelector ) ([]corev1.Node , error ) {
240312 // Use label selector to filter nodes
0 commit comments