Skip to content

Commit

Permalink
Address PR review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranzke committed Jul 20, 2022
1 parent 210d9c0 commit 804b367
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 3 deletions.
97 changes: 97 additions & 0 deletions pkg/api/indexer/core_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package indexer_test

import (
"context"

. "github.com/gardener/gardener/pkg/api/indexer"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Core", func() {
var indexer *fakeFieldIndexer

BeforeEach(func() {
indexer = &fakeFieldIndexer{}
})

DescribeTable("#AddProjectNamespace",
func(obj client.Object, matcher gomegatypes.GomegaMatcher) {
Expect(AddProjectNamespace(context.TODO(), indexer)).To(Succeed())

Expect(indexer.obj).To(Equal(&gardencorev1beta1.Project{}))
Expect(indexer.field).To(Equal("spec.namespace"))
Expect(indexer.extractValue).NotTo(BeNil())
Expect(indexer.extractValue(obj)).To(matcher)
},

Entry("no Project", &corev1.Secret{}, ConsistOf("")),
Entry("Project w/o namespace", &gardencorev1beta1.Project{}, ConsistOf("")),
Entry("Project w/ namespace", &gardencorev1beta1.Project{Spec: gardencorev1beta1.ProjectSpec{Namespace: pointer.String("namespace")}}, ConsistOf("namespace")),
)

DescribeTable("#AddShootSeedName",
func(obj client.Object, matcher gomegatypes.GomegaMatcher) {
Expect(AddShootSeedName(context.TODO(), indexer)).To(Succeed())

Expect(indexer.obj).To(Equal(&gardencorev1beta1.Shoot{}))
Expect(indexer.field).To(Equal("spec.seedName"))
Expect(indexer.extractValue).NotTo(BeNil())
Expect(indexer.extractValue(obj)).To(matcher)
},

Entry("no Shoot", &corev1.Secret{}, ConsistOf("")),
Entry("Shoot w/o seedName", &gardencorev1beta1.Shoot{}, ConsistOf("")),
Entry("Shoot w/ seedName", &gardencorev1beta1.Shoot{Spec: gardencorev1beta1.ShootSpec{SeedName: pointer.String("seed")}}, ConsistOf("seed")),
)

DescribeTable("#AddBackupBucketSeedName",
func(obj client.Object, matcher gomegatypes.GomegaMatcher) {
Expect(AddBackupBucketSeedName(context.TODO(), indexer)).To(Succeed())

Expect(indexer.obj).To(Equal(&gardencorev1beta1.BackupBucket{}))
Expect(indexer.field).To(Equal("spec.seedName"))
Expect(indexer.extractValue).NotTo(BeNil())
Expect(indexer.extractValue(obj)).To(matcher)
},

Entry("no BackupBucket", &corev1.Secret{}, ConsistOf("")),
Entry("BackupBucket w/o seedName", &gardencorev1beta1.BackupBucket{}, ConsistOf("")),
Entry("BackupBucket w/ seedName", &gardencorev1beta1.BackupBucket{Spec: gardencorev1beta1.BackupBucketSpec{SeedName: pointer.String("seed")}}, ConsistOf("seed")),
)

DescribeTable("#AddControllerInstallationSeedRefName",
func(obj client.Object, matcher gomegatypes.GomegaMatcher) {
Expect(AddControllerInstallationSeedRefName(context.TODO(), indexer)).To(Succeed())

Expect(indexer.obj).To(Equal(&gardencorev1beta1.ControllerInstallation{}))
Expect(indexer.field).To(Equal("spec.seedRef.name"))
Expect(indexer.extractValue).NotTo(BeNil())
Expect(indexer.extractValue(obj)).To(matcher)
},

Entry("no ControllerInstallation", &corev1.Secret{}, ConsistOf("")),
Entry("ControllerInstallation w/o seedRef", &gardencorev1beta1.ControllerInstallation{}, ConsistOf("")),
Entry("ControllerInstallation w/ seedRef", &gardencorev1beta1.ControllerInstallation{Spec: gardencorev1beta1.ControllerInstallationSpec{SeedRef: corev1.ObjectReference{Name: "seed"}}}, ConsistOf("seed")),
)
})
42 changes: 42 additions & 0 deletions pkg/api/indexer/indexer_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package indexer_test

import (
"context"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func TestIndexer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "API Indexer Suite")
}

type fakeFieldIndexer struct {
obj client.Object
field string
extractValue client.IndexerFunc
}

func (f *fakeFieldIndexer) IndexField(_ context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
f.obj = obj
f.field = field
f.extractValue = extractValue
return nil
}
51 changes: 51 additions & 0 deletions pkg/api/indexer/operations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package indexer_test

import (
"context"

. "github.com/gardener/gardener/pkg/api/indexer"
operationsv1alpha1 "github.com/gardener/gardener/pkg/apis/operations/v1alpha1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Operations", func() {
var indexer *fakeFieldIndexer

BeforeEach(func() {
indexer = &fakeFieldIndexer{}
})

DescribeTable("#AddBastionShootName",
func(obj client.Object, matcher gomegatypes.GomegaMatcher) {
Expect(AddBastionShootName(context.TODO(), indexer)).To(Succeed())

Expect(indexer.obj).To(Equal(&operationsv1alpha1.Bastion{}))
Expect(indexer.field).To(Equal("spec.shootRef.name"))
Expect(indexer.extractValue).NotTo(BeNil())
Expect(indexer.extractValue(obj)).To(matcher)
},

Entry("no Bastion", &corev1.Secret{}, ConsistOf("")),
Entry("Bastion w/o shootRef", &operationsv1alpha1.Bastion{}, ConsistOf("")),
Entry("Bastion w/ shootRef", &operationsv1alpha1.Bastion{Spec: operationsv1alpha1.BastionSpec{ShootRef: corev1.LocalObjectReference{Name: "shoot"}}}, ConsistOf("shoot")),
)
})
51 changes: 51 additions & 0 deletions pkg/api/indexer/seedmanagement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package indexer_test

import (
"context"

. "github.com/gardener/gardener/pkg/api/indexer"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("SeedManagement", func() {
var indexer *fakeFieldIndexer

BeforeEach(func() {
indexer = &fakeFieldIndexer{}
})

DescribeTable("#AddManagedSeedShootName",
func(obj client.Object, matcher gomegatypes.GomegaMatcher) {
Expect(AddManagedSeedShootName(context.TODO(), indexer)).To(Succeed())

Expect(indexer.obj).To(Equal(&seedmanagementv1alpha1.ManagedSeed{}))
Expect(indexer.field).To(Equal("spec.shoot.name"))
Expect(indexer.extractValue).NotTo(BeNil())
Expect(indexer.extractValue(obj)).To(matcher)
},

Entry("no ManagedSeed", &corev1.Secret{}, ConsistOf("")),
Entry("ManagedSeed w/o shoot", &seedmanagementv1alpha1.ManagedSeed{}, ConsistOf("")),
Entry("ManagedSeed w/ shoot", &seedmanagementv1alpha1.ManagedSeed{Spec: seedmanagementv1alpha1.ManagedSeedSpec{Shoot: &seedmanagementv1alpha1.Shoot{Name: "shoot"}}}, ConsistOf("shoot")),
)
})
6 changes: 3 additions & 3 deletions pkg/controllerutils/mapper/enqueue_mapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (f MapFunc) Map(ctx context.Context, log logr.Logger, reader client.Reader,

// EnqueueRequestsFrom is similar to controller-runtime's mapper.EnqueueRequestsFromMapFunc.
// Instead of taking only a MapFunc it also allows passing a Mapper interface. Also, it allows customizing the
// behaviour on UpdateEvents.
// For UpdateEvents, the given UpdateBehaviour decides if only the old, only the new or both objects should be mapped
// behavior on UpdateEvents.
// For UpdateEvents, the given UpdateBehavior decides if only the old, only the new or both objects should be mapped
// and enqueued.
func EnqueueRequestsFrom(m Mapper, updateBehavior UpdateBehavior, log logr.Logger) handler.EventHandler {
return &enqueueRequestsFromMapFunc{
Expand All @@ -67,7 +67,7 @@ func EnqueueRequestsFrom(m Mapper, updateBehavior UpdateBehavior, log logr.Logge
type enqueueRequestsFromMapFunc struct {
// mapper transforms the argument into a slice of keys to be reconciled
mapper Mapper
// updateBehaviour decides which object(s) to map and enqueue on updates
// updateBehavior decides which object(s) to map and enqueue on updates
updateBehavior UpdateBehavior

ctx context.Context
Expand Down

0 comments on commit 804b367

Please sign in to comment.