From 15c6082c45afb2d942ee6d1c0c30e2fe5e304616 Mon Sep 17 00:00:00 2001 From: Yachika <36668209+YachikaRalhan@users.noreply.github.com> Date: Mon, 25 Sep 2023 17:14:53 +0530 Subject: [PATCH] fix(): cluster filtering in predicates (#282) * fix(): cluster filtering in predicates Signed-off-by: YachikaRalhan * fix(): fixed test cases Signed-off-by: YachikaRalhan --------- Signed-off-by: YachikaRalhan --- pkg/hub/controllers/vpnkeyrotation/reconciler.go | 8 ++++---- .../vpnkeyrotation/vpnrotation_ctrl_test.go | 4 ++++ .../vpnkeyrotation/vpnrotation_suite_test.go | 14 ++++++++++++++ pkg/hub/manager/manager.go | 13 +++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/hub/controllers/vpnkeyrotation/reconciler.go b/pkg/hub/controllers/vpnkeyrotation/reconciler.go index d2469b043..701e7f5d3 100644 --- a/pkg/hub/controllers/vpnkeyrotation/reconciler.go +++ b/pkg/hub/controllers/vpnkeyrotation/reconciler.go @@ -412,9 +412,7 @@ func (r *Reconciler) syncCurrentRotationState(ctx context.Context, } } if len(syncedRotationState) != len(vpnKeyRotation.Status.CurrentRotationState) || len(keysToDeleteFromStatus) > 0 { - log.Info("syncing current rotation state for the gateways", - "from", vpnKeyRotation.Status.CurrentRotationState, - "to", syncedRotationState) + // Merge the new syncedRotationState with the existing state for gw, obj := range syncedRotationState { currentRotationState[gw] = obj @@ -425,7 +423,9 @@ func (r *Reconciler) syncCurrentRotationState(ctx context.Context, delete(currentRotationState, key) } } - + log.Info("syncing current rotation state for the gateways", + "from", vpnKeyRotation.Status.CurrentRotationState, + "to", currentRotationState) vpnKeyRotation.Status.CurrentRotationState = currentRotationState requeue = true return r.Status().Update(ctx, vpnKeyRotation) diff --git a/pkg/hub/controllers/vpnkeyrotation/vpnrotation_ctrl_test.go b/pkg/hub/controllers/vpnkeyrotation/vpnrotation_ctrl_test.go index 8b349dd40..febd85290 100644 --- a/pkg/hub/controllers/vpnkeyrotation/vpnrotation_ctrl_test.go +++ b/pkg/hub/controllers/vpnkeyrotation/vpnrotation_ctrl_test.go @@ -75,6 +75,7 @@ var _ = Describe("Hub VPN Key Rotation", func() { ClusterGatewayMapping: map[string][]string{ CLUSTER_NAME: {gws[0]}, }, + Clusters: []string{ClusterName}, CertificateCreationTime: &metav1.Time{Time: time.Now()}, }, } @@ -177,6 +178,7 @@ var _ = Describe("Hub VPN Key Rotation", func() { ClusterGatewayMapping: map[string][]string{ CLUSTER_NAME: gws, }, + Clusters: []string{ClusterName}, CertificateCreationTime: &metav1.Time{Time: time.Now()}, CertificateExpiryTime: &metav1.Time{Time: time.Now().AddDate(0, 0, 30)}, RotationInterval: 30, @@ -515,6 +517,7 @@ var _ = Describe("Hub VPN Key Rotation", func() { ClusterGatewayMapping: map[string][]string{ CLUSTER_NAME: {gws[0]}, }, + Clusters: []string{ClusterName}, CertificateCreationTime: &metav1.Time{Time: time.Now()}, CertificateExpiryTime: &metav1.Time{Time: time.Now().AddDate(0, 0, 30)}, RotationInterval: 30, @@ -742,6 +745,7 @@ var _ = Describe("Hub VPN Key Rotation", func() { ClusterGatewayMapping: map[string][]string{ "worker-1": gws, }, + Clusters: []string{ClusterName}, CertificateCreationTime: &metav1.Time{Time: time.Now()}, }, } diff --git a/pkg/hub/controllers/vpnkeyrotation/vpnrotation_suite_test.go b/pkg/hub/controllers/vpnkeyrotation/vpnrotation_suite_test.go index ad9d2017b..987030fc3 100644 --- a/pkg/hub/controllers/vpnkeyrotation/vpnrotation_suite_test.go +++ b/pkg/hub/controllers/vpnkeyrotation/vpnrotation_suite_test.go @@ -18,6 +18,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/controller-runtime/pkg/predicate" hubv1alpha1 "github.com/kubeslice/apis/pkg/controller/v1alpha1" spokev1alpha1 "github.com/kubeslice/apis/pkg/worker/v1alpha1" @@ -152,6 +153,9 @@ var _ = BeforeSuite(func() { err = builder. ControllerManagedBy(k8sManager). For(&hubv1alpha1.VpnKeyRotation{}). + WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool { + return shouldProcessVpnKeyRotation(object) + })). Complete(rotationReconciler) if err != nil { os.Exit(1) @@ -203,3 +207,13 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).NotTo(HaveOccurred()) }) + +func shouldProcessVpnKeyRotation(object client.Object) bool { + vpn := object.(*hubv1alpha1.VpnKeyRotation) + for _, v := range vpn.Spec.Clusters { + if v == ClusterName { + return true + } + } + return false +} diff --git a/pkg/hub/manager/manager.go b/pkg/hub/manager/manager.go index 1eea368dc..e6f15e90e 100644 --- a/pkg/hub/manager/manager.go +++ b/pkg/hub/manager/manager.go @@ -214,6 +214,9 @@ func Start(meshClient client.Client, hubClient client.Client, ctx context.Contex ControllerManagedBy(mgr). For(&hubv1alpha1.VpnKeyRotation{}). WithEventFilter(predicate.GenerationChangedPredicate{}). + WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool { + return shouldProcessVpnKeyRotation(object) + })). Complete(vpnKeyRotationReconciler) if err != nil { log.Error(err, "could not create vpn key rotation controller") @@ -225,3 +228,13 @@ func Start(meshClient client.Client, hubClient client.Client, ctx context.Contex os.Exit(1) } } + +func shouldProcessVpnKeyRotation(object client.Object) bool { + vpn := object.(*hubv1alpha1.VpnKeyRotation) + for _, v := range vpn.Spec.Clusters { + if v == ClusterName { + return true + } + } + return false +}