From 06af67c120c0335bcf9290f25b0e6c1a19185431 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Sat, 9 Sep 2023 15:39:13 -0400 Subject: [PATCH] Track selected workloads When a ServiceBinding uses a selector to match workloads, we now track changes to those workload resource to reprocess the controller. Signed-off-by: Scott Andrews --- controllers/client.go | 4 ++ controllers/servicebinding_controller_test.go | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/controllers/client.go b/controllers/client.go index 1d7a9847..e30ad6e5 100644 --- a/controllers/client.go +++ b/controllers/client.go @@ -34,3 +34,7 @@ type trackingClient struct { func (c *trackingClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { return c.Config.TrackAndGet(ctx, key, obj, opts...) } + +func (c *trackingClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { + return c.Config.TrackAndList(ctx, list, opts...) +} diff --git a/controllers/servicebinding_controller_test.go b/controllers/servicebinding_controller_test.go index e67566d8..5e344f72 100644 --- a/controllers/servicebinding_controller_test.go +++ b/controllers/servicebinding_controller_test.go @@ -26,12 +26,14 @@ import ( diemetav1 "dies.dev/apis/meta/v1" "github.com/vmware-labs/reconciler-runtime/reconcilers" rtesting "github.com/vmware-labs/reconciler-runtime/testing" + "github.com/vmware-labs/reconciler-runtime/tracker" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apierrs "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" @@ -692,6 +694,19 @@ func TestResolveWorkload(t *testing.T) { workload2.DieReleaseUnstructured(), }, }, + ExpectTracks: []rtesting.TrackRequest{ + { + Tracker: types.NamespacedName{Namespace: serviceBinding.GetNamespace(), Name: serviceBinding.GetName()}, + TrackedReference: tracker.Reference{ + APIGroup: "apps", + Kind: "Deployment", + Namespace: serviceBinding.GetNamespace(), + Selector: labels.SelectorFromSet(labels.Set{ + "app": "my", + }), + }, + }, + }, }, "resolve selected workload not found": { GivenObjects: []client.Object{}, @@ -709,6 +724,19 @@ func TestResolveWorkload(t *testing.T) { ExpectStashedValues: map[reconcilers.StashKey]interface{}{ controllers.WorkloadsStashKey: []runtime.Object{}, }, + ExpectTracks: []rtesting.TrackRequest{ + { + Tracker: types.NamespacedName{Namespace: serviceBinding.GetNamespace(), Name: serviceBinding.GetName()}, + TrackedReference: tracker.Reference{ + APIGroup: "apps", + Kind: "Deployment", + Namespace: serviceBinding.GetNamespace(), + Selector: labels.SelectorFromSet(labels.Set{ + "app": "my", + }), + }, + }, + }, }, "resolve selected workload forbidden": { Resource: serviceBinding. @@ -756,6 +784,19 @@ func TestResolveWorkload(t *testing.T) { ) }). DieReleasePtr(), + ExpectTracks: []rtesting.TrackRequest{ + { + Tracker: types.NamespacedName{Namespace: serviceBinding.GetNamespace(), Name: serviceBinding.GetName()}, + TrackedReference: tracker.Reference{ + APIGroup: "apps", + Kind: "Deployment", + Namespace: serviceBinding.GetNamespace(), + Selector: labels.SelectorFromSet(labels.Set{ + "app": "my", + }), + }, + }, + }, }, }