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 1b5d830
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 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)
}
6 changes: 3 additions & 3 deletions controllers/agentmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func newAgentMachine(name, namespace string, spec capiproviderv1alpha1.AgentMach
},
Status: clusterv1.MachineStatus{},
}
machine.ObjectMeta.Labels[clusterv1.ClusterLabelName] = cluster.Name
machine.ObjectMeta.Labels[clusterv1.ClusterNameLabel] = cluster.Name
Expect(c.Create(ctx, &machine)).To(BeNil())

machineOwnerRef := metav1.OwnerReference{APIVersion: "cluster.x-k8s.io/v1beta1", Kind: "Machine", Name: machine.Name}
Expand Down Expand Up @@ -616,7 +616,7 @@ var _ = Describe("mapMachineToAgentMachine", func() {
agentMachine, machine := newAgentMachine("agentMachine-1", testNamespace, capiproviderv1alpha1.AgentMachineSpec{}, ctx, c)
Expect(c.Create(ctx, agentMachine)).To(Succeed())

requests := amr.mapMachineToAgentMachine(machine)
requests := amr.mapMachineToAgentMachine(ctx, machine)
Expect(len(requests)).To(Equal(1))

agentMachineKey := types.NamespacedName{
Expand All @@ -637,6 +637,6 @@ var _ = Describe("mapMachineToAgentMachine", func() {
machine := clusterv1.Machine{}
Expect(c.Get(ctx, key, &machine)).To(Succeed())

Expect(amr.mapMachineToAgentMachine(&machine)).To(BeEmpty())
Expect(amr.mapMachineToAgentMachine(ctx, &machine)).To(BeEmpty())
})
})
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 1b5d830

Please sign in to comment.