Skip to content

Commit

Permalink
Drop NewClientWithFieldSelectorSupport function in favor of control…
Browse files Browse the repository at this point in the history
…ler-runtime `WithIndex` function

kubernetes-sigs/controller-runtime#2025
  • Loading branch information
shafeeqes committed Dec 20, 2022
1 parent 3e569e8 commit 1c599c1
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 150 deletions.
44 changes: 26 additions & 18 deletions pkg/api/indexer/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,35 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// AddProjectNamespace adds an index for core.ProjectNamespace to the given indexer.
func AddProjectNamespace(ctx context.Context, indexer client.FieldIndexer) error {
if err := indexer.IndexField(ctx, &gardencorev1beta1.Project{}, core.ProjectNamespace, func(obj client.Object) []string {
var (
ProjectNamespaceIndexerFunc = func(obj client.Object) []string {
project, ok := obj.(*gardencorev1beta1.Project)
if !ok {
return []string{""}
}
return []string{pointer.StringDeref(project.Spec.Namespace, "")}
}); err != nil {
}

BackupBucketSeedNameIndexerFunc = func(obj client.Object) []string {
backupBucket, ok := obj.(*gardencorev1beta1.BackupBucket)
if !ok {
return []string{""}
}
return []string{pointer.StringDeref(backupBucket.Spec.SeedName, "")}
}

ControllerInstallationSeedRefNameIndexerFunc = func(obj client.Object) []string {
controllerInstallation, ok := obj.(*gardencorev1beta1.ControllerInstallation)
if !ok {
return []string{""}
}
return []string{controllerInstallation.Spec.SeedRef.Name}
}
)

// AddProjectNamespace adds an index for core.ProjectNamespace to the given indexer.
func AddProjectNamespace(ctx context.Context, indexer client.FieldIndexer) error {
if err := indexer.IndexField(ctx, &gardencorev1beta1.Project{}, core.ProjectNamespace, ProjectNamespaceIndexerFunc); err != nil {
return fmt.Errorf("failed to add indexer for %s to Project Informer: %w", core.ProjectNamespace, err)
}
return nil
Expand Down Expand Up @@ -69,13 +89,7 @@ func AddShootStatusSeedName(ctx context.Context, indexer client.FieldIndexer) er

// AddBackupBucketSeedName adds an index for core.BackupBucketSeedName to the given indexer.
func AddBackupBucketSeedName(ctx context.Context, indexer client.FieldIndexer) error {
if err := indexer.IndexField(ctx, &gardencorev1beta1.BackupBucket{}, core.BackupBucketSeedName, func(obj client.Object) []string {
backupBucket, ok := obj.(*gardencorev1beta1.BackupBucket)
if !ok {
return []string{""}
}
return []string{pointer.StringDeref(backupBucket.Spec.SeedName, "")}
}); err != nil {
if err := indexer.IndexField(ctx, &gardencorev1beta1.BackupBucket{}, core.BackupBucketSeedName, BackupBucketSeedNameIndexerFunc); err != nil {
return fmt.Errorf("failed to add indexer for %s to BackupBucket Informer: %w", core.BackupBucketSeedName, err)
}
return nil
Expand Down Expand Up @@ -111,13 +125,7 @@ func AddBackupEntryBucketName(ctx context.Context, indexer client.FieldIndexer)

// AddControllerInstallationSeedRefName adds an index for core.ControllerInstallationSeedRefName to the given indexer.
func AddControllerInstallationSeedRefName(ctx context.Context, indexer client.FieldIndexer) error {
if err := indexer.IndexField(ctx, &gardencorev1beta1.ControllerInstallation{}, core.SeedRefName, func(obj client.Object) []string {
controllerInstallation, ok := obj.(*gardencorev1beta1.ControllerInstallation)
if !ok {
return []string{""}
}
return []string{controllerInstallation.Spec.SeedRef.Name}
}); err != nil {
if err := indexer.IndexField(ctx, &gardencorev1beta1.ControllerInstallation{}, core.SeedRefName, ControllerInstallationSeedRefNameIndexerFunc); err != nil {
return fmt.Errorf("failed to add indexer for %s to ControllerInstallation Informer: %w", core.SeedRefName, err)
}
return nil
Expand Down
16 changes: 9 additions & 7 deletions pkg/api/indexer/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var BastionShootNameIndexerFunc = func(obj client.Object) []string {
bastion, ok := obj.(*operationsv1alpha1.Bastion)
if !ok {
return []string{""}
}
return []string{bastion.Spec.ShootRef.Name}
}

// AddBastionShootName adds an index for operations.BastionShootName to the given indexer.
func AddBastionShootName(ctx context.Context, indexer client.FieldIndexer) error {
if err := indexer.IndexField(ctx, &operationsv1alpha1.Bastion{}, operations.BastionShootName, func(obj client.Object) []string {
bastion, ok := obj.(*operationsv1alpha1.Bastion)
if !ok {
return []string{""}
}
return []string{bastion.Spec.ShootRef.Name}
}); err != nil {
if err := indexer.IndexField(ctx, &operationsv1alpha1.Bastion{}, operations.BastionShootName, BastionShootNameIndexerFunc); err != nil {
return fmt.Errorf("failed to add indexer for %s to Bastion Informer: %w", operations.BastionShootName, err)
}
return nil
Expand Down
22 changes: 12 additions & 10 deletions pkg/api/indexer/seedmanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var ManagedSeedShootNameIndexerFunc = func(obj client.Object) []string {
managedSeed, ok := obj.(*seedmanagementv1alpha1.ManagedSeed)
if !ok {
return []string{""}
}
if managedSeed.Spec.Shoot == nil {
return []string{""}
}
return []string{managedSeed.Spec.Shoot.Name}
}

// AddManagedSeedShootName adds an index for seedmanagement.ManagedSeedShootName to the given indexer.
func AddManagedSeedShootName(ctx context.Context, indexer client.FieldIndexer) error {
if err := indexer.IndexField(ctx, &seedmanagementv1alpha1.ManagedSeed{}, seedmanagement.ManagedSeedShootName, func(obj client.Object) []string {
managedSeed, ok := obj.(*seedmanagementv1alpha1.ManagedSeed)
if !ok {
return []string{""}
}
if managedSeed.Spec.Shoot == nil {
return []string{""}
}
return []string{managedSeed.Spec.Shoot.Name}
}); err != nil {
if err := indexer.IndexField(ctx, &seedmanagementv1alpha1.ManagedSeed{}, seedmanagement.ManagedSeedShootName, ManagedSeedShootNameIndexerFunc); err != nil {
return fmt.Errorf("failed to add indexer for %s to ManagedSeed Informer: %w", seedmanagement.ManagedSeedShootName, err)
}
return nil
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllermanager/controller/bastion/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package bastion_test
import (
"context"

"github.com/gardener/gardener/pkg/api/indexer"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/apis/operations"
operationsv1alpha1 "github.com/gardener/gardener/pkg/apis/operations/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
. "github.com/gardener/gardener/pkg/controllermanager/controller/bastion"
bastionstrategy "github.com/gardener/gardener/pkg/registry/operations/bastion"
"github.com/gardener/gardener/pkg/utils/test"

"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -152,10 +152,10 @@ var _ = Describe("Add", func() {

BeforeEach(func() {
log = logr.Discard()
fakeClient = test.NewClientWithFieldSelectorSupport(
fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).Build(),
bastionstrategy.ToSelectableFields,
)
fakeClient = fakeclient.NewClientBuilder().
WithScheme(kubernetes.GardenScheme).
WithIndex(&operationsv1alpha1.Bastion{}, operations.BastionShootName, indexer.BastionShootNameIndexerFunc).
Build()
})

It("should do nothing if the object is no shoot", func() {
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllermanager/controller/project/activity/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"context"
"time"

"github.com/gardener/gardener/pkg/api/indexer"
"github.com/gardener/gardener/pkg/apis/core"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/client/kubernetes"
. "github.com/gardener/gardener/pkg/controllermanager/controller/project/activity"
projectstrategy "github.com/gardener/gardener/pkg/registry/core/project"
"github.com/gardener/gardener/pkg/utils/test"

"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -172,10 +172,10 @@ var _ = Describe("Add", func() {

BeforeEach(func() {
log = logr.Discard()
fakeClient = test.NewClientWithFieldSelectorSupport(
fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).Build(),
projectstrategy.ToSelectableFields,
)
fakeClient = fakeclient.NewClientBuilder().
WithScheme(kubernetes.GardenScheme).
WithIndex(&gardencorev1beta1.Project{}, core.ProjectNamespace, indexer.ProjectNamespaceIndexerFunc).
Build()

project = &gardencorev1beta1.Project{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"context"
"time"

"github.com/gardener/gardener/pkg/api/indexer"
"github.com/gardener/gardener/pkg/apis/core"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/controllermanager/apis/config"
. "github.com/gardener/gardener/pkg/controllermanager/controller/seed/backupbucketscheck"
backupbucketstrategy "github.com/gardener/gardener/pkg/registry/core/backupbucket"
"github.com/gardener/gardener/pkg/utils/test"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -73,10 +73,11 @@ var _ = Describe("Reconciler", func() {

fakeClock = testclock.NewFakeClock(time.Now().Round(time.Second))

c = test.NewClientWithFieldSelectorSupport(
fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).WithObjects(seed).Build(),
backupbucketstrategy.ToSelectableFields,
)
c = fakeclient.NewClientBuilder().
WithScheme(kubernetes.GardenScheme).
WithObjects(seed).
WithIndex(&gardencorev1beta1.BackupBucket{}, core.BackupBucketSeedName, indexer.BackupBucketSeedNameIndexerFunc).
Build()

conf = config.SeedBackupBucketsCheckControllerConfiguration{
SyncPeriod: &metav1.Duration{Duration: syncPeriod},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"time"

"github.com/gardener/gardener/pkg/api/indexer"
"github.com/gardener/gardener/pkg/apis/core"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/controllermanager/apis/config"
Expand Down Expand Up @@ -64,7 +66,11 @@ var _ = Describe("Reconciler", func() {

fakeClock = testclock.NewFakeClock(time.Now().Round(time.Second))

c = fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).WithObjects(seed).Build()
c = fakeclient.NewClientBuilder().
WithScheme(kubernetes.GardenScheme).
WithObjects(seed).
WithIndex(&gardencorev1beta1.ControllerInstallation{}, core.SeedRefName, indexer.ControllerInstallationSeedRefNameIndexerFunc).
Build()
conf := config.SeedExtensionsCheckControllerConfiguration{
SyncPeriod: &metav1.Duration{Duration: syncPeriodDuration},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,17 @@ var _ = Describe("#SNI", func() {
// TODO(mvladev): can't directly import the istio apis due to dependency issues.
s.AddKnownTypeWithName(schema.FromAPIVersionAndKind("networking.istio.io/v1beta1", "VirtualServiceList"), &unstructured.UnstructuredList{})
s.AddKnownTypeWithName(schema.FromAPIVersionAndKind("networking.istio.io/v1beta1", "VirtualService"), &unstructured.Unstructured{})
c = fake.NewClientBuilder().WithScheme(s).Build()
virtualServiceObj := &unstructured.Unstructured{}
virtualServiceObj.SetGroupVersionKind(schema.FromAPIVersionAndKind("networking.istio.io/v1beta1", "VirtualService"))

c = fake.NewClientBuilder().
WithScheme(s).
WithIndex(
virtualServiceObj, "metadata.name", func(o client.Object) []string {
return []string{o.GetName()}
},
).
Build()
})

It("returns true when exists", func() {
Expand Down
8 changes: 7 additions & 1 deletion pkg/utils/kubernetes/managedseed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/gardener/gardener/pkg/api/indexer"
"github.com/gardener/gardener/pkg/apis/seedmanagement"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
mockclient "github.com/gardener/gardener/pkg/mock/controller-runtime/client"
Expand Down Expand Up @@ -66,7 +68,11 @@ var _ = Describe("managedseed", func() {
)

BeforeEach(func() {
fakeClient = fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).WithObjects(managedSeed).Build()
fakeClient = fakeclient.NewClientBuilder().
WithScheme(kubernetes.GardenScheme).
WithObjects(managedSeed).
WithIndex(&seedmanagementv1alpha1.ManagedSeed{}, seedmanagement.ManagedSeedShootName, indexer.ManagedSeedShootNameIndexerFunc).
Build()
})

It("should return the ManagedSeed for the given shoot namespace and name, if it exists", func() {
Expand Down
94 changes: 0 additions & 94 deletions pkg/utils/test/client.go

This file was deleted.

0 comments on commit 1c599c1

Please sign in to comment.