Skip to content

Commit c4ae7dd

Browse files
committed
⚠️ client.ObjectKeyFromObject now uses client.Client
This change removes the need to get an accessor from the runtime object, because the object interface already has all the information needed. It also removes the need to return an error, which makes it a little easier to use. Signed-off-by: Vince Prignano <vincepri@vmware.com>
1 parent 32e94b6 commit c4ae7dd

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

pkg/client/interfaces.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@ import (
3030
type ObjectKey = types.NamespacedName
3131

3232
// ObjectKeyFromObject returns the ObjectKey given a runtime.Object
33-
func ObjectKeyFromObject(obj runtime.Object) (ObjectKey, error) {
34-
accessor, err := meta.Accessor(obj)
35-
if err != nil {
36-
return ObjectKey{}, err
37-
}
38-
return ObjectKey{Namespace: accessor.GetNamespace(), Name: accessor.GetName()}, nil
33+
func ObjectKeyFromObject(obj Object) ObjectKey {
34+
return ObjectKey{Namespace: obj.GetNamespace(), Name: obj.GetName()}
3935
}
4036

4137
// Patch is a patch that can be applied to a Kubernetes object.

pkg/controller/controllerutil/controllerutil.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,7 @@ const ( // They should complete the sentence "Deployment default/foo has been ..
195195
//
196196
// It returns the executed operation and an error.
197197
func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error) {
198-
key, err := client.ObjectKeyFromObject(obj)
199-
if err != nil {
200-
return OperationResultNone, err
201-
}
202-
198+
key := client.ObjectKeyFromObject(obj)
203199
if err := c.Get(ctx, key, obj); err != nil {
204200
if !errors.IsNotFound(err) {
205201
return OperationResultNone, err
@@ -236,11 +232,7 @@ func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f M
236232
//
237233
// It returns the executed operation and an error.
238234
func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, f MutateFn) (OperationResult, error) {
239-
key, err := client.ObjectKeyFromObject(obj)
240-
if err != nil {
241-
return OperationResultNone, err
242-
}
243-
235+
key := client.ObjectKeyFromObject(obj)
244236
if err := c.Get(ctx, key, obj); err != nil {
245237
if !errors.IsNotFound(err) {
246238
return OperationResultNone, err
@@ -331,11 +323,11 @@ func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, f Mu
331323
}
332324

333325
// mutate wraps a MutateFn and applies validation to its result
334-
func mutate(f MutateFn, key client.ObjectKey, obj runtime.Object) error {
326+
func mutate(f MutateFn, key client.ObjectKey, obj client.Object) error {
335327
if err := f(); err != nil {
336328
return err
337329
}
338-
if newKey, err := client.ObjectKeyFromObject(obj); err != nil || key != newKey {
330+
if newKey := client.ObjectKeyFromObject(obj); key != newKey {
339331
return fmt.Errorf("MutateFn cannot mutate object name and/or object namespace")
340332
}
341333
return nil

0 commit comments

Comments
 (0)