Skip to content

Commit 4b03d42

Browse files
Updates InfraMachine watch_filters
1 parent 2a0af92 commit 4b03d42

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

pkg/controllers/machinesync/machine_sync_controller.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const (
8181
mapiNamespace string = "openshift-machine-api"
8282
capiInfraCommonFinalizerSuffix string = ".cluster.x-k8s.io"
8383

84+
requeueInfraProgress = 1 * time.Second
85+
8486
messageSuccessfullySynchronizedCAPItoMAPI = "Successfully synchronized CAPI Machine to MAPI"
8587
messageSuccessfullySynchronizedMAPItoCAPI = "Successfully synchronized MAPI Machine to CAPI"
8688
progressingToSynchronizeMAPItoCAPI = "Progressing to synchronize MAPI Machine to CAPI"
@@ -134,7 +136,7 @@ type MachineSyncReconciler struct {
134136
MAPINamespace string
135137
}
136138

137-
// SetupWithManager sets the CoreClusterReconciler controller up with the given manager.
139+
// SetupWithManager sets the MachineSyncReconciler controller up with the given manager.
138140
func (r *MachineSyncReconciler) SetupWithManager(mgr ctrl.Manager) error {
139141
infraMachine, _, err := controllers.InitInfraMachineAndInfraClusterFromProvider(r.Platform)
140142
if err != nil {
@@ -161,7 +163,7 @@ func (r *MachineSyncReconciler) SetupWithManager(mgr ctrl.Manager) error {
161163
).
162164
Watches(
163165
infraMachine,
164-
handler.EnqueueRequestsFromMapFunc(util.RewriteNamespace(r.MAPINamespace)),
166+
handler.EnqueueRequestsFromMapFunc(util.ResolveCAPIMachineFromInfraMachine(r.MAPINamespace)),
165167
builder.WithPredicates(util.FilterNamespace(r.CAPINamespace)),
166168
).
167169
Complete(r); err != nil {
@@ -182,8 +184,8 @@ func (r *MachineSyncReconciler) SetupWithManager(mgr ctrl.Manager) error {
182184
func (r *MachineSyncReconciler) Reconcile(ctx context.Context, req reconcile.Request) (ctrl.Result, error) {
183185
logger := log.FromContext(ctx, "namespace", req.Namespace, "name", req.Name)
184186

185-
logger.V(1).Info("Reconciling machine")
186-
defer logger.V(1).Info("Finished reconciling machine")
187+
logger.Info("Reconciling machine")
188+
defer logger.Info("Finished reconciling machine")
187189

188190
var mapiMachineNotFound, capiMachineNotFound bool
189191

@@ -531,6 +533,8 @@ func (r *MachineSyncReconciler) reconcileMAPIMachinetoCAPIMachine(ctx context.Co
531533
return result, fmt.Errorf("unable to ensure Cluster API machine: %w", err)
532534
}
533535

536+
logger.Info("log 7")
537+
534538
newCAPIInfraMachine.SetOwnerReferences([]metav1.OwnerReference{{
535539
APIVersion: clusterv1.GroupVersion.String(),
536540
Kind: machineKind,
@@ -540,13 +544,13 @@ func (r *MachineSyncReconciler) reconcileMAPIMachinetoCAPIMachine(ctx context.Co
540544
BlockOwnerDeletion: ptr.To(true),
541545
}})
542546

543-
result, syncronizationIsProgressing, err := r.createOrUpdateCAPIInfraMachine(ctx, mapiMachine, infraMachine, newCAPIInfraMachine)
547+
result, synchronizationIsProgressing, err := r.createOrUpdateCAPIInfraMachine(ctx, mapiMachine, infraMachine, newCAPIInfraMachine)
544548
if err != nil {
545549
return result, fmt.Errorf("unable to ensure Cluster API infra machine: %w", err)
546550
}
547551

548-
if syncronizationIsProgressing {
549-
return ctrl.Result{RequeueAfter: time.Second * 1}, r.applySynchronizedConditionWithPatch(ctx, mapiMachine, corev1.ConditionUnknown,
552+
if synchronizationIsProgressing {
553+
return ctrl.Result{RequeueAfter: requeueInfraProgress}, r.applySynchronizedConditionWithPatch(ctx, mapiMachine, corev1.ConditionUnknown,
550554
reasonProgressingToCreateCAPIInfraMachine, progressingToSynchronizeMAPItoCAPI, nil)
551555
}
552556

@@ -1011,7 +1015,9 @@ func (r *MachineSyncReconciler) fetchCAPIInfraResources(ctx context.Context, cap
10111015
}
10121016

10131017
//nolint:funlen,gocognit,cyclop
1014-
func (r *MachineSyncReconciler) reconcileMAPItoCAPIMachineDeletion(ctx context.Context, mapiMachine *machinev1beta1.Machine, capiMachine *clusterv1.Machine, infraMachine client.Object) (bool, error) {
1018+
func (r *MachineSyncReconciler) reconcileMAPItoCAPIMachineDeletion(ctx context.Context, mapiMachine *machinev1beta1.Machine, capiMachine *clusterv1.Machine, infraMachine client.Object) (shouldReqeue bool, err error) {
1019+
logger := log.FromContext(ctx)
1020+
10151021
if mapiMachine.DeletionTimestamp.IsZero() {
10161022
if capiMachine == nil || capiMachine.DeletionTimestamp.IsZero() {
10171023
// Neither MAPI authoritative machine nor its CAPI non-authoritative machine mirror
@@ -1029,8 +1035,6 @@ func (r *MachineSyncReconciler) reconcileMAPItoCAPIMachineDeletion(ctx context.C
10291035
return true, nil
10301036
}
10311037

1032-
logger := log.FromContext(ctx)
1033-
10341038
if capiMachine == nil && util.IsNilObject(infraMachine) {
10351039
logger.Info("Cluster API machine and infra machine do not exist, removing corresponding Machine API machine sync finalizer")
10361040
// We don't have a capi machine or infra resouorces to clean up we can
@@ -1228,9 +1232,7 @@ func (r *MachineSyncReconciler) reconcileCAPItoMAPIMachineDeletion(ctx context.C
12281232

12291233
// ensureSyncFinalizer ensures the sync finalizer is present across the mapi
12301234
// machine, capi machine and capi infra machine.
1231-
func (r *MachineSyncReconciler) ensureSyncFinalizer(ctx context.Context, mapiMachine *machinev1beta1.Machine, capiMachine *clusterv1.Machine, infraMachine client.Object) (bool, error) {
1232-
var shouldRequeue bool
1233-
1235+
func (r *MachineSyncReconciler) ensureSyncFinalizer(ctx context.Context, mapiMachine *machinev1beta1.Machine, capiMachine *clusterv1.Machine, infraMachine client.Object) (shouldRequeue bool, err error) {
12341236
var errors []error
12351237

12361238
if mapiMachine != nil {

pkg/controllers/machinesync/machine_sync_controller_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ var _ = Describe("With a running MachineSync Reconciler", func() {
191191

192192
mgr, err = ctrl.NewManager(controllerCfg, ctrl.Options{
193193
Scheme: testScheme,
194+
Logger: GinkgoLogr,
194195
Controller: config.Controller{
195196
SkipNameValidation: ptr.To(true),
196197
},

pkg/controllers/machinesync/suite_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"k8s.io/client-go/kubernetes/scheme"
3434
"k8s.io/client-go/rest"
3535
"k8s.io/klog/v2"
36-
"k8s.io/klog/v2/textlogger"
36+
ctrl "sigs.k8s.io/controller-runtime"
3737

3838
"sigs.k8s.io/controller-runtime/pkg/client"
3939
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
@@ -64,7 +64,8 @@ func TestAPIs(t *testing.T) {
6464
var _ = BeforeSuite(func() {
6565
klog.SetOutput(GinkgoWriter)
6666

67-
logf.SetLogger(textlogger.NewLogger(textlogger.NewConfig()))
67+
logf.SetLogger(GinkgoLogr)
68+
ctrl.SetLogger(GinkgoLogr)
6869

6970
By("bootstrapping test environment")
7071
var err error

pkg/util/watch_filters.go

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"fmt"
2222

2323
"github.com/openshift/cluster-capi-operator/pkg/controllers"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
2425
"k8s.io/klog/v2"
26+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2527
"sigs.k8s.io/controller-runtime/pkg/client"
2628
"sigs.k8s.io/controller-runtime/pkg/predicate"
2729
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -37,10 +39,10 @@ const machineSetKind = "MachineSet"
3739
func RewriteNamespace(namespace string) func(context.Context, client.Object) []reconcile.Request {
3840
return func(ctx context.Context, obj client.Object) []reconcile.Request {
3941
klog.V(4).Info(
40-
"reconcile triggered by object",
41-
"objectType", fmt.Sprintf("%T", obj),
42-
"namespace", obj.GetNamespace(),
43-
"name", obj.GetName(),
42+
"reconcile triggered by ",
43+
"objectType: ", fmt.Sprintf("%T", obj),
44+
"namespace: ", obj.GetNamespace(),
45+
"name: ", obj.GetName(),
4446
)
4547

4648
return []reconcile.Request{{
@@ -56,10 +58,10 @@ func RewriteNamespace(namespace string) func(context.Context, client.Object) []r
5658
func ResolveCAPIMachineSetFromInfraMachineTemplate(namespace string) func(context.Context, client.Object) []reconcile.Request {
5759
return func(ctx context.Context, obj client.Object) []reconcile.Request {
5860
klog.V(4).Info(
59-
"reconcile triggered by object",
60-
"objectType", fmt.Sprintf("%T", obj),
61-
"namespace", obj.GetNamespace(),
62-
"name", obj.GetName(),
61+
"reconcile triggered by ",
62+
"objectType: ", fmt.Sprintf("%T", obj),
63+
"namespace: ", obj.GetNamespace(),
64+
"name: ", obj.GetName(),
6365
)
6466

6567
objLabels := obj.GetLabels()
@@ -79,10 +81,47 @@ func ResolveCAPIMachineSetFromInfraMachineTemplate(namespace string) func(contex
7981
}
8082
}
8183

84+
// ResolveCAPIMachineFromInfraMachine resolves a CAPI Machine from an InfraMachine. It takes client.Object,
85+
// and uses owner references to determine the owning CAPI machine. If one is found, it returns a reconcile.Request
86+
// for the corresponding MAPI Machine in the MAPI namespace to trigger reconciliation of the mirror MAPI Machine.
87+
func ResolveCAPIMachineFromInfraMachine(namespace string) func(context.Context, client.Object) []reconcile.Request {
88+
return func(ctx context.Context, obj client.Object) []reconcile.Request {
89+
klog.V(4).Info(
90+
"reconcile triggered by ",
91+
"objectType: ", fmt.Sprintf("%T", obj),
92+
"namespace: ", obj.GetNamespace(),
93+
"name: ", obj.GetName(),
94+
)
95+
96+
requests := []reconcile.Request{}
97+
98+
for _, ref := range obj.GetOwnerReferences() {
99+
gv, err := schema.ParseGroupVersion(ref.APIVersion)
100+
if err != nil {
101+
klog.Info("Failed to parse GroupVersion", "name", obj.GetName(), "APIVersion", ref.APIVersion)
102+
}
103+
104+
if ref.Kind == "Machine" && gv.Group == clusterv1.GroupVersion.Group {
105+
requests = append(requests, reconcile.Request{
106+
NamespacedName: client.ObjectKey{Namespace: namespace, Name: ref.Name},
107+
})
108+
}
109+
}
110+
111+
return requests
112+
}
113+
}
114+
82115
// FilterNamespace filters a client.Object request, ensuring they are in the
83116
// namespace provided.
84117
func FilterNamespace(namespace string) predicate.Predicate {
85118
return predicate.NewPredicateFuncs(func(obj client.Object) bool {
119+
klog.V(4).Info(
120+
"reconcile triggered by ",
121+
"objectType: ", fmt.Sprintf("%T", obj),
122+
"namespace: ", obj.GetNamespace(),
123+
"name: ", obj.GetName(),
124+
)
86125
// Check namespace is as expected
87126
return obj.GetNamespace() == namespace
88127
})

0 commit comments

Comments
 (0)