Skip to content

Commit

Permalink
fix(): cluster filtering in predicates (#282)
Browse files Browse the repository at this point in the history
* fix(): cluster filtering in predicates

Signed-off-by: YachikaRalhan <[email protected]>

* fix(): fixed test cases

Signed-off-by: YachikaRalhan <[email protected]>

---------

Signed-off-by: YachikaRalhan <[email protected]>
  • Loading branch information
YachikaRalhan authored Sep 25, 2023
1 parent 57957ec commit 15c6082
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/hub/controllers/vpnkeyrotation/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/hub/controllers/vpnkeyrotation/vpnrotation_ctrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
},
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()},
},
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/hub/controllers/vpnkeyrotation/vpnrotation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions pkg/hub/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}

0 comments on commit 15c6082

Please sign in to comment.