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 Jan 9, 2023
1 parent bec73af commit 75c2875
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 197 deletions.
100 changes: 54 additions & 46 deletions extensions/pkg/util/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"testing"

"github.com/gardener/gardener/extensions/pkg/util/index"
secretutil "github.com/gardener/gardener/extensions/pkg/util/secret"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"

Expand All @@ -26,6 +27,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

Expand All @@ -41,7 +43,14 @@ var _ = Describe("Secret", func() {

var (
scheme *runtime.Scheme
client client.Client

secret *corev1.Secret
secretBinding *gardencorev1beta1.SecretBinding
shoot *gardencorev1beta1.Shoot
)

JustBeforeEach(func() {
secret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret",
Expand Down Expand Up @@ -70,77 +79,76 @@ var _ = Describe("Secret", func() {
SecretBindingName: secretBinding.Name,
},
}
)

BeforeEach(func() {
scheme = runtime.NewScheme()
Expect(corev1.AddToScheme(scheme)).NotTo(HaveOccurred())
Expect(gardencorev1beta1.AddToScheme(scheme)).NotTo(HaveOccurred())

client = fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(
secret,
secretBinding,
shoot,
).
WithIndex(&gardencorev1beta1.SecretBinding{}, index.SecretRefNamespaceField, index.SecretRefNamespaceIndexerFunc).
WithIndex(&gardencorev1beta1.Shoot{}, index.SecretBindingNameField, index.SecretBindingNameIndexerFunc).
Build()
})

It("should return false when the Secret is not used", func() {
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(

secret,
secretBinding,
).Build()
client := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(
secret,
secretBinding,
).
WithIndex(&gardencorev1beta1.SecretBinding{}, index.SecretRefNamespaceField, index.SecretRefNamespaceIndexerFunc).
WithIndex(&gardencorev1beta1.Shoot{}, index.SecretBindingNameField, index.SecretBindingNameIndexerFunc).
Build()

isUsed, err := secretutil.IsSecretInUseByShoot(context.TODO(), client, secret, "gcp")
Expect(isUsed).To(BeFalse())
Expect(err).NotTo(HaveOccurred())
})

It("should return false when the Secret is in use but the provider does not match", func() {
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(
secret,
secretBinding,
shoot,
).Build()

isUsed, err := secretutil.IsSecretInUseByShoot(context.TODO(), client, secret, "other")
Expect(isUsed).To(BeFalse())
Expect(err).NotTo(HaveOccurred())
})

It("should return true when the Secret is in use by Shoot with the given provider", func() {
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(
secret,
secretBinding,
shoot,
).Build()

isUsed, err := secretutil.IsSecretInUseByShoot(context.TODO(), client, secret, "gcp")
Expect(isUsed).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
})

It("should return true when the Secret is in use by Shoot from another namespace", func() {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret",
Namespace: "another-namespace",
},
}
secretBinding := &gardencorev1beta1.SecretBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "secretbinding",
Namespace: namespace,
},
SecretRef: corev1.SecretReference{
Name: secret.Name,
Namespace: secret.Namespace,
},
}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(
secret,
secretBinding,
shoot,
).Build()

isUsed, err := secretutil.IsSecretInUseByShoot(context.TODO(), client, secret, "gcp")
Expect(isUsed).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
Context("when the Secret is in use by Shoot from another namespace", func() {
BeforeEach(func() {
secret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret",
Namespace: "another-namespace",
},
}
secretBinding = &gardencorev1beta1.SecretBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "secretbinding",
Namespace: namespace,
},
SecretRef: corev1.SecretReference{
Name: secret.Name,
Namespace: secret.Namespace,
},
}
})

It("should return true", func() {
isUsed, err := secretutil.IsSecretInUseByShoot(context.TODO(), client, secret, "gcp")
Expect(isUsed).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
})
})
})
})
47 changes: 29 additions & 18 deletions pkg/api/indexer/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,38 @@ 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 extracts the .spec.namespace field of a Project.
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 extracts the .spec.seedName field of a BackupBucket.
BackupBucketSeedNameIndexerFunc = func(obj client.Object) []string {
backupBucket, ok := obj.(*gardencorev1beta1.BackupBucket)
if !ok {
return []string{""}
}
return []string{pointer.StringDeref(backupBucket.Spec.SeedName, "")}
}

// ControllerInstallationSeedRefNameIndexerFunc extracts the .spec.seedRef.name field of a ControllerInstallation.
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 +92,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 +128,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
17 changes: 10 additions & 7 deletions pkg/api/indexer/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// BastionShootNameIndexerFunc extracts the .spec.shootRef.name field of a Bastion.
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
23 changes: 13 additions & 10 deletions pkg/api/indexer/seedmanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ManagedSeedShootNameIndexerFunc extracts the .spec.shoot.name field of a ManagedSeed.
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
Loading

0 comments on commit 75c2875

Please sign in to comment.