Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): cluster filtering in predicates #282

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}