@@ -340,7 +340,7 @@ func (r *AgentMachineReconciler) findAgent(ctx context.Context, log logrus.Field
340
340
341
341
// Find an agent that is unbound and whose validations pass
342
342
for i := 0 ; i < len (agents .Items ) && foundAgent == nil ; i ++ {
343
- if isValidAgent (& agents .Items [i ], agentMachine ) {
343
+ if isValidAgent (& agents .Items [i ]) {
344
344
foundAgent = & agents .Items [i ]
345
345
log .Infof ("Found agent to associate with AgentMachine: %s/%s" , foundAgent .Namespace , foundAgent .Name )
346
346
err = r .updateFoundAgent (ctx , log , agentMachine , foundAgent , clusterDeploymentRef , machineConfigPool , ignitionTokenSecretRef )
@@ -511,7 +511,7 @@ func (r *AgentMachineReconciler) processBootstrapDataSecret(ctx context.Context,
511
511
return machineConfigPool , ignitionTokenSecretRef , nil
512
512
}
513
513
514
- func isValidAgent (agent * aiv1beta1.Agent , agentMachine * capiproviderv1. AgentMachine ) bool {
514
+ func isValidAgent (agent * aiv1beta1.Agent ) bool {
515
515
if ! agent .Spec .Approved {
516
516
return false
517
517
}
@@ -675,31 +675,33 @@ func (r *AgentMachineReconciler) mapMachineToAgentMachine(ctx context.Context, m
675
675
return []reconcile.Request {}
676
676
}
677
677
678
- // SetupWithManager sets up the controller with the Manager.
679
- func (r * AgentMachineReconciler ) SetupWithManager (mgr ctrl.Manager , agentNamespace string ) error {
680
- mapAgentToAgentMachine := func (ctx context.Context , agent client.Object ) []reconcile.Request {
681
- log := r .Log .WithFields (
682
- logrus.Fields {
683
- "agent" : agent .GetName (),
684
- "agent_namespace" : agent .GetNamespace (),
685
- })
678
+ func (r * AgentMachineReconciler ) mapAgentToAgentMachine (ctx context.Context , a client.Object ) []reconcile.Request {
679
+ log := r .Log .WithFields (
680
+ logrus.Fields {
681
+ "agent" : a .GetName (),
682
+ "agent_namespace" : a .GetNamespace (),
683
+ })
686
684
687
- namespace , ok := agent .GetAnnotations ()[AgentMachineRefNamespace ]
688
- if ! ok {
689
- return make ([]reconcile.Request , 0 )
690
- }
685
+ amList := & capiproviderv1.AgentMachineList {}
686
+ opts := & client.ListOptions {}
687
+ mappedAgent := false
691
688
692
- amList := & capiproviderv1.AgentMachineList {}
693
- opts := & client.ListOptions {
694
- Namespace : namespace ,
695
- }
689
+ // The annotation will only be present on an Agent that's mapped to an AgentMachine
690
+ namespace , ok := a .GetAnnotations ()[AgentMachineRefNamespace ]
691
+ if ok {
692
+ opts .Namespace = namespace
693
+ mappedAgent = true
694
+ }
696
695
697
- if err := r .List (ctx , amList , opts ); err != nil {
698
- log .WithError (err ).Error ("failed to list agent machines" )
699
- return []reconcile.Request {}
700
- }
696
+ if err := r .List (ctx , amList , opts ); err != nil {
697
+ log .WithError (err ).Error ("failed to list agent machines" )
698
+ return []reconcile.Request {}
699
+ }
701
700
702
- reply := make ([]reconcile.Request , 0 , len (amList .Items ))
701
+ reply := make ([]reconcile.Request , 0 , len (amList .Items ))
702
+ if mappedAgent {
703
+ // If the Agent is mapped to an AgentMachine, return only that AgentMachine
704
+ agent := a
703
705
for _ , agentMachine := range amList .Items {
704
706
if agentMachine .Status .AgentRef != nil &&
705
707
agentMachine .Status .AgentRef .Namespace == agent .GetNamespace () &&
@@ -711,12 +713,34 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa
711
713
break
712
714
}
713
715
}
714
- return reply
716
+ } else {
717
+ // If the Agent isn't mapped to an AgentMachine and it's "valid" for use, return any AgentMachines
718
+ // that don't have an Agent mapped to them yet
719
+ agent := & aiv1beta1.Agent {}
720
+ if err := r .Get (ctx , types.NamespacedName {Name : a .GetName (), Namespace : a .GetNamespace ()}, agent ); err != nil {
721
+ return []reconcile.Request {}
722
+ }
723
+ if ! isValidAgent (agent ) {
724
+ return []reconcile.Request {}
725
+ }
726
+ for _ , agentMachine := range amList .Items {
727
+ if agentMachine .Status .AgentRef == nil {
728
+ reply = append (reply , reconcile.Request {NamespacedName : types.NamespacedName {
729
+ Namespace : agentMachine .Namespace ,
730
+ Name : agentMachine .Name ,
731
+ }})
732
+ }
733
+ }
715
734
}
716
735
736
+ return reply
737
+ }
738
+
739
+ // SetupWithManager sets up the controller with the Manager.
740
+ func (r * AgentMachineReconciler ) SetupWithManager (mgr ctrl.Manager , agentNamespace string ) error {
717
741
return ctrl .NewControllerManagedBy (mgr ).
718
742
For (& capiproviderv1.AgentMachine {}).
719
- Watches (& aiv1beta1.Agent {}, handler .EnqueueRequestsFromMapFunc (mapAgentToAgentMachine )).
743
+ Watches (& aiv1beta1.Agent {}, handler .EnqueueRequestsFromMapFunc (r . mapAgentToAgentMachine )).
720
744
Watches (& clusterv1.Machine {}, handler .EnqueueRequestsFromMapFunc (r .mapMachineToAgentMachine )).
721
745
Complete (r )
722
746
}
0 commit comments