Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of runtime.Object and meta.Accessor #3476

Merged
merged 4 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
networkingv1 "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -496,8 +495,8 @@ var _ = Describe("Actuator", func() {
})
})

func clientGet(result runtime.Object) interface{} {
return func(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
func clientGet(result client.Object) interface{} {
return func(ctx context.Context, key client.ObjectKey, obj client.Object) error {
switch obj.(type) {
case *corev1.Secret:
*obj.(*corev1.Secret) = *result.(*corev1.Secret)
Expand Down
7 changes: 4 additions & 3 deletions extensions/pkg/controller/csimigration/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
package csimigration

import (
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
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/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
)

// ClusterCSIMigrationControllerNotFinished is a predicate for an annotation on the cluster.
func ClusterCSIMigrationControllerNotFinished() predicate.Predicate {
f := func(obj runtime.Object) bool {
f := func(obj client.Object) bool {
if obj == nil {
return false
}
Expand Down
27 changes: 13 additions & 14 deletions extensions/pkg/controller/csimigration/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ package csimigration
import (
"context"

mockclient "github.com/gardener/gardener/pkg/mock/controller-runtime/client"

gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -31,12 +25,17 @@ import (
storagev1 "k8s.io/api/storage/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
mockclient "github.com/gardener/gardener/pkg/mock/controller-runtime/client"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
)

var _ = Describe("reconciler", func() {
Expand Down Expand Up @@ -91,7 +90,7 @@ var _ = Describe("reconciler", func() {
c.EXPECT().Patch(ctx, kubeControllerManagerDeployment, emptyPatch)
c.EXPECT().Patch(ctx, kubeSchedulerDeployment, emptyPatch)

c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj runtime.Object, _ ...client.UpdateOption) error {
c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
cluster, ok := obj.(*extensionsv1alpha1.Cluster)
Expect(ok).To(BeTrue())
Expect(cluster.Annotations).To(HaveKeyWithValue(AnnotationKeyNeedsComplete, "true"))
Expand All @@ -106,7 +105,7 @@ var _ = Describe("reconciler", func() {
It("should behave as expected for new clusters", func() {
c.EXPECT().Get(ctx, kutil.Key(cluster.Name, shoot.Name), gomock.AssignableToTypeOf(&extensionsv1alpha1.ControlPlane{})).Return(apierrors.NewNotFound(extensionsv1alpha1.Resource("controlplane"), shoot.Name))

c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj runtime.Object, _ ...client.UpdateOption) error {
c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
cluster, ok := obj.(*extensionsv1alpha1.Cluster)
Expect(ok).To(BeTrue())
Expect(cluster.Annotations).To(HaveKeyWithValue(AnnotationKeyNeedsComplete, "true"))
Expand Down Expand Up @@ -152,7 +151,7 @@ var _ = Describe("reconciler", func() {
return nil, shootClient, nil
}

shootClient.EXPECT().List(ctx, gomock.AssignableToTypeOf(&corev1.NodeList{})).DoAndReturn(func(_ context.Context, list runtime.Object, _ ...client.ListOption) error {
shootClient.EXPECT().List(ctx, gomock.AssignableToTypeOf(&corev1.NodeList{})).DoAndReturn(func(_ context.Context, list client.ObjectList, _ ...client.ListOption) error {
obj := &corev1.NodeList{
Items: []corev1.Node{
{
Expand Down Expand Up @@ -182,7 +181,7 @@ var _ = Describe("reconciler", func() {
return nil, shootClient, nil
}

shootClient.EXPECT().List(ctx, gomock.AssignableToTypeOf(&corev1.NodeList{})).DoAndReturn(func(_ context.Context, list runtime.Object, _ ...client.ListOption) error {
shootClient.EXPECT().List(ctx, gomock.AssignableToTypeOf(&corev1.NodeList{})).DoAndReturn(func(_ context.Context, list client.ObjectList, _ ...client.ListOption) error {
obj := &corev1.NodeList{
Items: []corev1.Node{
{
Expand All @@ -204,7 +203,7 @@ var _ = Describe("reconciler", func() {
},
Provisioner: storageClassProvisioner,
}
shootClient.EXPECT().List(ctx, gomock.AssignableToTypeOf(&storagev1.StorageClassList{})).DoAndReturn(func(_ context.Context, list runtime.Object, _ ...client.ListOption) error {
shootClient.EXPECT().List(ctx, gomock.AssignableToTypeOf(&storagev1.StorageClassList{})).DoAndReturn(func(_ context.Context, list client.ObjectList, _ ...client.ListOption) error {
obj := &storagev1.StorageClassList{
Items: []storagev1.StorageClass{*storageClass},
}
Expand All @@ -213,7 +212,7 @@ var _ = Describe("reconciler", func() {
})
shootClient.EXPECT().Delete(ctx, storageClass)

c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj runtime.Object, _ ...client.UpdateOption) error {
c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
cluster, ok := obj.(*extensionsv1alpha1.Cluster)
Expect(ok).To(BeTrue())
Expect(cluster.Annotations).To(HaveKeyWithValue(AnnotationKeyNeedsComplete, "true"))
Expand All @@ -225,7 +224,7 @@ var _ = Describe("reconciler", func() {
c.EXPECT().Patch(ctx, kubeControllerManagerDeployment, emptyPatch)
c.EXPECT().Patch(ctx, kubeSchedulerDeployment, emptyPatch)

c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj runtime.Object, _ ...client.UpdateOption) error {
c.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).DoAndReturn(func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
cluster, ok := obj.(*extensionsv1alpha1.Cluster)
Expect(ok).To(BeTrue())
Expect(cluster.Annotations).To(HaveKeyWithValue(AnnotationKeyNeedsComplete, "true"))
Expand Down
3 changes: 1 addition & 2 deletions extensions/pkg/controller/healthcheck/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"time"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -53,7 +52,7 @@ type GetExtensionObjectFunc = func() extensionsv1alpha1.Object
type GetExtensionObjectListFunc = func() client.ObjectList

// PreCheckFunc checks whether the health check shall be performed based on the given object and cluster.
type PreCheckFunc = func(runtime.Object, *extensionscontroller.Cluster) bool
type PreCheckFunc = func(client.Object, *extensionscontroller.Cluster) bool

// ConditionTypeToHealthCheck registers a HealthCheck for the given ConditionType. If the PreCheckFunc is not nil it will
// be executed with the given object before the health check if performed. Otherwise, the health check will always be
Expand Down
6 changes: 3 additions & 3 deletions extensions/pkg/controller/healthcheck/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type AddArgs struct {
// The Gardenlet reads the conditions on the extension Resource.
// Through this mechanism, the extension can contribute to the Shoot's HealthStatus.
registeredExtension *RegisteredExtension
// GetExtensionObjListFunc returns a list of the runtime.Object representation of the extension to register
// GetExtensionObjListFunc returns a client.ObjectList representation of the extension to register
GetExtensionObjListFunc GetExtensionObjectListFunc
}

Expand Down Expand Up @@ -85,8 +85,8 @@ type RegisteredExtension struct {
// the NewActuator reconciles a single extension with a specific type and writes conditions for each distinct healthConditionTypes.
// extensionType (e.g aws) defines the spec.type of the extension to watch
// kind defines the GroupVersionKind of the extension
// GetExtensionObjListFunc returns a list of the runtime.Object representation of the extension to register
// getExtensionObjFunc returns a runtime.Object representation of the extension to register
// GetExtensionObjListFunc returns a client.ObjectList representation of the extension to register
// getExtensionObjFunc returns a extensionsv1alpha1.Object representation of the extension to register
// mgr is the controller runtime manager
// opts contain config for the healthcheck controller
// custom predicates allow for fine-grained control which resources to watch
Expand Down
10 changes: 2 additions & 8 deletions extensions/pkg/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"reflect"

controllererror "github.com/gardener/gardener/extensions/pkg/controller/error"
"github.com/gardener/gardener/pkg/api/extensions"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
Expand Down Expand Up @@ -216,13 +215,8 @@ func RemoveAnnotation(ctx context.Context, c client.Client, obj client.Object, a
}

// IsMigrated checks if an extension object has been migrated
func IsMigrated(obj runtime.Object) bool {
acc, err := extensions.Accessor(obj)
if err != nil {
return false
}

lastOp := acc.GetExtensionStatus().GetLastOperation()
func IsMigrated(obj extensionsv1alpha1.Object) bool {
lastOp := obj.GetExtensionStatus().GetLastOperation()
return lastOp != nil &&
lastOp.Type == gardencorev1beta1.LastOperationTypeMigrate &&
lastOp.State == gardencorev1beta1.LastOperationStateSucceeded
Expand Down
2 changes: 1 addition & 1 deletion extensions/pkg/controller/worker/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func newMachineToObjectMapper(newObjListFunc func() client.ObjectList, predicate
return &machineToObjectMapper{newObjListFunc: newObjListFunc, predicates: predicates}
}

func getReconcileRequestsFromObjectList(objList runtime.Object, predicates []predicate.Predicate) []reconcile.Request {
func getReconcileRequestsFromObjectList(objList client.ObjectList, predicates []predicate.Predicate) []reconcile.Request {
var requests []reconcile.Request

utilruntime.HandleError(meta.EachListItem(objList, func(obj runtime.Object) error {
Expand Down
4 changes: 2 additions & 2 deletions extensions/pkg/controller/worker/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ package worker

import (
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// MachineStatusHasChanged is a predicate deciding whether the status of a Machine has been changed.
func MachineStatusHasChanged() predicate.Predicate {
statusHasChanged := func(oldObj runtime.Object, newObj runtime.Object) bool {
statusHasChanged := func(oldObj client.Object, newObj client.Object) bool {
oldMachine, ok := oldObj.(*machinev1alpha1.Machine)
if !ok {
return false
Expand Down
8 changes: 4 additions & 4 deletions extensions/pkg/predicate/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func HasOperationAnnotation() predicate.Predicate {

// LastOperationNotSuccessful is a predicate for unsuccessful last operations **only** for creation events.
func LastOperationNotSuccessful() predicate.Predicate {
operationNotSucceeded := func(obj runtime.Object) bool {
operationNotSucceeded := func(obj client.Object) bool {
acc, err := extensions.Accessor(obj)
if err != nil {
return false
Expand Down Expand Up @@ -191,7 +191,7 @@ func HasPurpose(purpose extensionsv1alpha1.Purpose) predicate.Predicate {

// ClusterShootProviderType is a predicate for the provider type of the shoot in the cluster resource.
func ClusterShootProviderType(decoder runtime.Decoder, providerType string) predicate.Predicate {
f := func(obj runtime.Object) bool {
f := func(obj client.Object) bool {
if obj == nil {
return false
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func ClusterShootProviderType(decoder runtime.Decoder, providerType string) pred

// GardenCoreProviderType is a predicate for the provider type of a `gardencore.Object` implementation.
func GardenCoreProviderType(providerType string) predicate.Predicate {
f := func(obj runtime.Object) bool {
f := func(obj client.Object) bool {
if obj == nil {
return false
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func GardenCoreProviderType(providerType string) predicate.Predicate {

// ClusterShootKubernetesVersionAtLeast is a predicate for the kubernetes version of the shoot in the cluster resource.
func ClusterShootKubernetesVersionAtLeast(decoder runtime.Decoder, kubernetesVersion string) predicate.Predicate {
f := func(obj runtime.Object) bool {
f := func(obj client.Object) bool {
if obj == nil {
return false
}
Expand Down
8 changes: 1 addition & 7 deletions extensions/pkg/webhook/cloudprovider/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
Expand Down Expand Up @@ -68,12 +67,7 @@ func (m *mutator) InjectScheme(scheme *runtime.Scheme) error {

// Mutate validates and if needed mutates the given object.
func (m *mutator) Mutate(ctx context.Context, new, old client.Object) error {
acc, err := meta.Accessor(new)
if err != nil {
return fmt.Errorf("could not create accessor during webhook: %v", err)
}

if acc.GetDeletionTimestamp() != nil {
if new.GetDeletionTimestamp() != nil {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ func checkOperatingSystemConfig(osc *extensionsv1alpha1.OperatingSystemConfig) {
Expect(cloudProvider.Content.Inline).To(Equal(&extensionsv1alpha1.FileContentInline{Data: cloudproviderconfEncoded, Encoding: encoding}))
}

func clientGet(result runtime.Object) interface{} {
return func(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
func clientGet(result client.Object) interface{} {
return func(ctx context.Context, key client.ObjectKey, obj client.Object) error {
switch obj.(type) {
case *extensionsv1alpha1.Cluster:
*obj.(*extensionsv1alpha1.Cluster) = *result.(*extensionsv1alpha1.Cluster)
Expand Down
9 changes: 4 additions & 5 deletions pkg/apis/extensions/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package v1alpha1

import (
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"

dnsv1alpha1 "github.com/gardener/external-dns-management/pkg/apis/dns/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"

gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
)

// Status is the status of an Object.
Expand Down Expand Up @@ -63,8 +63,7 @@ type Spec interface {

// Object is an extension object resource.
type Object interface {
metav1.Object
runtime.Object
client.Object

// GetExtensionSpec retrieves the object's spec.
GetExtensionSpec() Spec
Expand Down
3 changes: 1 addition & 2 deletions pkg/client/kubernetes/applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/scheme"
Expand All @@ -42,7 +41,7 @@ var (
configMapTypeMeta = metav1.TypeMeta{Kind: "ConfigMap", APIVersion: "v1"}
)

func mkManifest(objs ...runtime.Object) []byte {
func mkManifest(objs ...client.Object) []byte {
var out bytes.Buffer
for _, obj := range objs {
data, err := yaml.Marshal(obj)
Expand Down