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 0159eef commit 9c1ef49
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 131 deletions.
28 changes: 17 additions & 11 deletions pkg/api/indexer/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,27 @@ 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, "")}
}
)

// 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 +81,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
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
11 changes: 5 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,9 @@ 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
11 changes: 5 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,9 @@ 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,9 @@ 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 @@ -289,7 +289,14 @@ 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
94 changes: 0 additions & 94 deletions pkg/utils/test/client.go

This file was deleted.

0 comments on commit 9c1ef49

Please sign in to comment.