From d2b5ee37a54474bcfab954a1d91d87d384cacf9f Mon Sep 17 00:00:00 2001 From: miledxz Date: Tue, 14 Jan 2025 21:45:54 +0100 Subject: [PATCH 1/2] move ks client reader to separated directory --- .../framework/events/first_eventbatch_preparer.go | 14 +++----------- .../events/first_eventbatch_preparer_test.go | 6 +++--- internal/framework/kubernetes/client.go | 14 ++++++++++++++ .../kubernetesfakes}/fake_reader.go | 6 +++--- internal/mode/static/telemetry/collector_test.go | 6 +++--- 5 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 internal/framework/kubernetes/client.go rename internal/framework/{events/eventsfakes => kubernetes/kubernetesfakes}/fake_reader.go (97%) diff --git a/internal/framework/events/first_eventbatch_preparer.go b/internal/framework/events/first_eventbatch_preparer.go index b8bd48c470..f02382d238 100644 --- a/internal/framework/events/first_eventbatch_preparer.go +++ b/internal/framework/events/first_eventbatch_preparer.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" @@ -20,22 +21,13 @@ type FirstEventBatchPreparer interface { Prepare(ctx context.Context) (EventBatch, error) } -//counterfeiter:generate . Reader - -// Reader allows getting and listing resources from a cache. -// This interface is introduced for testing to mock the methods from -// sigs.k8s.io/controller-runtime/pkg/client.Reader. -type Reader interface { - client.Reader -} - // EachListItemFunc lists each item of a client.ObjectList. // It is from k8s.io/apimachinery/pkg/api/meta. type EachListItemFunc func(obj runtime.Object, fn func(runtime.Object) error) error // FirstEventBatchPreparerImpl is an implementation of FirstEventBatchPreparer. type FirstEventBatchPreparerImpl struct { - reader Reader + reader kubernetes.Reader eachListItem EachListItemFunc objects []client.Object objectLists []client.ObjectList @@ -48,7 +40,7 @@ type FirstEventBatchPreparerImpl struct { // For each list from objectLists, FirstEventBatchPreparerImpl will list the resources of the corresponding type from // the reader. func NewFirstEventBatchPreparerImpl( - reader Reader, + reader kubernetes.Reader, objects []client.Object, objectLists []client.ObjectList, ) *FirstEventBatchPreparerImpl { diff --git a/internal/framework/events/first_eventbatch_preparer_test.go b/internal/framework/events/first_eventbatch_preparer_test.go index 175818f62f..8828974cfd 100644 --- a/internal/framework/events/first_eventbatch_preparer_test.go +++ b/internal/framework/events/first_eventbatch_preparer_test.go @@ -17,19 +17,19 @@ import ( v1 "sigs.k8s.io/gateway-api/apis/v1" "github.com/nginx/nginx-gateway-fabric/internal/framework/events" - "github.com/nginx/nginx-gateway-fabric/internal/framework/events/eventsfakes" + "github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes/kubernetesfakes" ) var _ = Describe("FirstEventBatchPreparer", func() { var ( - fakeReader *eventsfakes.FakeReader + fakeReader *kubernetesfakes.FakeReader preparer *events.FirstEventBatchPreparerImpl ) const gcName = "my-class" BeforeEach(func() { - fakeReader = &eventsfakes.FakeReader{} + fakeReader = &kubernetesfakes.FakeReader{} preparer = events.NewFirstEventBatchPreparerImpl( fakeReader, []client.Object{&v1.GatewayClass{ObjectMeta: metav1.ObjectMeta{Name: gcName}}}, diff --git a/internal/framework/kubernetes/client.go b/internal/framework/kubernetes/client.go new file mode 100644 index 0000000000..e10df946aa --- /dev/null +++ b/internal/framework/kubernetes/client.go @@ -0,0 +1,14 @@ +package kubernetes + +import "sigs.k8s.io/controller-runtime/pkg/client" + +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate + +//counterfeiter:generate . Reader + +// Reader allows getting and listing resources from a cache. +// This interface is introduced for testing to mock the methods from +// sigs.k8s.io/controller-runtime/pkg/client.Reader. +type Reader interface { + client.Reader +} diff --git a/internal/framework/events/eventsfakes/fake_reader.go b/internal/framework/kubernetes/kubernetesfakes/fake_reader.go similarity index 97% rename from internal/framework/events/eventsfakes/fake_reader.go rename to internal/framework/kubernetes/kubernetesfakes/fake_reader.go index 48f1bec2ea..997bb91a19 100644 --- a/internal/framework/events/eventsfakes/fake_reader.go +++ b/internal/framework/kubernetes/kubernetesfakes/fake_reader.go @@ -1,11 +1,11 @@ // Code generated by counterfeiter. DO NOT EDIT. -package eventsfakes +package kubernetesfakes import ( "context" "sync" - "github.com/nginx/nginx-gateway-fabric/internal/framework/events" + "github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -194,4 +194,4 @@ func (fake *FakeReader) recordInvocation(key string, args []interface{}) { fake.invocations[key] = append(fake.invocations[key], args) } -var _ events.Reader = new(FakeReader) +var _ kubernetes.Reader = new(FakeReader) diff --git a/internal/mode/static/telemetry/collector_test.go b/internal/mode/static/telemetry/collector_test.go index 506bfa0c9e..24f6e4e973 100644 --- a/internal/mode/static/telemetry/collector_test.go +++ b/internal/mode/static/telemetry/collector_test.go @@ -18,8 +18,8 @@ import ( gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" ngfAPI "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1" - "github.com/nginx/nginx-gateway-fabric/internal/framework/events/eventsfakes" "github.com/nginx/nginx-gateway-fabric/internal/framework/kinds" + "github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes/kubernetesfakes" "github.com/nginx/nginx-gateway-fabric/internal/mode/static/config" "github.com/nginx/nginx-gateway-fabric/internal/mode/static/state/dataplane" "github.com/nginx/nginx-gateway-fabric/internal/mode/static/state/graph" @@ -73,7 +73,7 @@ func createGetCallsFunc(objects ...client.Object) getCallsFunc { var _ = Describe("Collector", Ordered, func() { var ( - k8sClientReader *eventsfakes.FakeReader + k8sClientReader *kubernetesfakes.FakeReader fakeGraphGetter *telemetryfakes.FakeGraphGetter fakeConfigurationGetter *telemetryfakes.FakeConfigurationGetter dataCollector telemetry.DataCollector @@ -178,7 +178,7 @@ var _ = Describe("Collector", Ordered, func() { SnippetsFiltersDirectivesCount: []int64{}, } - k8sClientReader = &eventsfakes.FakeReader{} + k8sClientReader = &kubernetesfakes.FakeReader{} fakeGraphGetter = &telemetryfakes.FakeGraphGetter{} fakeConfigurationGetter = &telemetryfakes.FakeConfigurationGetter{} From 2b5d13a97b1b353e3bea82ba40c84bc4eacc4cfb Mon Sep 17 00:00:00 2001 From: miledxz Date: Wed, 15 Jan 2025 20:33:08 +0100 Subject: [PATCH 2/2] lint update --- internal/framework/events/first_eventbatch_preparer.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/framework/events/first_eventbatch_preparer.go b/internal/framework/events/first_eventbatch_preparer.go index f02382d238..155f120b32 100644 --- a/internal/framework/events/first_eventbatch_preparer.go +++ b/internal/framework/events/first_eventbatch_preparer.go @@ -4,12 +4,13 @@ import ( "context" "fmt" - "github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/nginx/nginx-gateway-fabric/internal/framework/kubernetes" ) //counterfeiter:generate . FirstEventBatchPreparer