Skip to content

Commit

Permalink
MGMT-14838: Fix code after upgrade
Browse files Browse the repository at this point in the history
https://issues.redhat.com/browse/MGMT-14838
There were a few changes to controller-runtime such as
the Cache BuilderWithOptions function and Source.Kind
being removed.
  • Loading branch information
CrystalChun committed Sep 6, 2023
1 parent 745e2b4 commit b1a8556
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
13 changes: 6 additions & 7 deletions controllers/agentmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

const (
Expand Down Expand Up @@ -634,7 +633,7 @@ func getAddresses(foundAgent *aiv1beta1.Agent) []clusterv1.MachineAddress {
return machineAddresses
}

func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object) []reconcile.Request {
func (r *AgentMachineReconciler) mapMachineToAgentMachine(ctx context.Context, machine client.Object) []reconcile.Request {
log := r.Log.WithFields(
logrus.Fields{
"machine": machine.GetName(),
Expand All @@ -646,7 +645,7 @@ func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object)
opts := &client.ListOptions{
Namespace: machine.GetNamespace(),
}
if err := r.List(context.Background(), amList, opts); err != nil {
if err := r.List(ctx, amList, opts); err != nil {
log.Debugf("failed to list agent machines")
return []reconcile.Request{}
}
Expand All @@ -671,7 +670,7 @@ func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object)

// SetupWithManager sets up the controller with the Manager.
func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespace string) error {
mapAgentToAgentMachine := func(agent client.Object) []reconcile.Request {
mapAgentToAgentMachine := func(ctx context.Context, agent client.Object) []reconcile.Request {
log := r.Log.WithFields(
logrus.Fields{
"agent": agent.GetName(),
Expand All @@ -688,7 +687,7 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa
Namespace: namespace,
}

if err := r.List(context.Background(), amList, opts); err != nil {
if err := r.List(ctx, amList, opts); err != nil {
log.WithError(err).Error("failed to list agent machines")
return []reconcile.Request{}
}
Expand All @@ -710,7 +709,7 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa

return ctrl.NewControllerManagedBy(mgr).
For(&capiproviderv1alpha1.AgentMachine{}).
Watches(&source.Kind{Type: &aiv1beta1.Agent{}}, handler.EnqueueRequestsFromMapFunc(mapAgentToAgentMachine)).
Watches(&source.Kind{Type: &clusterv1.Machine{}}, handler.EnqueueRequestsFromMapFunc(r.mapMachineToAgentMachine)).
Watches(&aiv1beta1.Agent{}, handler.EnqueueRequestsFromMapFunc(mapAgentToAgentMachine)).
Watches(&clusterv1.Machine{}, handler.EnqueueRequestsFromMapFunc(r.mapMachineToAgentMachine)).
Complete(r)
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func main() {
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "7605f49b.agent-install.openshift.io",
NewCache: cache.BuilderWithOptions(cache.Options{
DefaultSelector: cache.ObjectSelector{Field: fields.OneTermEqualSelector("metadata.namespace", watchNamespace)},
SelectorsByObject: cache.SelectorsByObject{
Cache: cache.Options{
DefaultFieldSelector: fields.OneTermEqualSelector("metadata.namespace", watchNamespace),
ByObject: map[client.Object]cache.ByObject{
&aiv1beta1.Agent{}: {Field: fields.OneTermEqualSelector("metadata.namespace", agentsNamespace)},
},
}),
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit b1a8556

Please sign in to comment.