mirror of https://github.com/knative/pkg.git
pull out object reference helper function (#733)
This commit is contained in:
parent
38b92bcf01
commit
f9aa7d2d11
|
@ -19,6 +19,7 @@ package kmeta
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
@ -59,3 +60,16 @@ func DeletionHandlingAccessor(obj interface{}) (Accessor, error) {
|
|||
|
||||
return accessor, nil
|
||||
}
|
||||
|
||||
// ObjectReference returns an core/v1.ObjectReference for the given object
|
||||
func ObjectReference(obj Accessor) corev1.ObjectReference {
|
||||
gvk := obj.GroupVersionKind()
|
||||
apiVersion, kind := gvk.ToAPIVersionAndKind()
|
||||
|
||||
return corev1.ObjectReference{
|
||||
APIVersion: apiVersion,
|
||||
Kind: kind,
|
||||
Namespace: obj.GetNamespace(),
|
||||
Name: obj.GetName(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
. "knative.dev/pkg/testing"
|
||||
|
@ -74,3 +76,27 @@ func TestAccessor(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestObjectReference(t *testing.T) {
|
||||
r := &Resource{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "king",
|
||||
APIVersion: "some.api.version/v1",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "name",
|
||||
Namespace: "namespace",
|
||||
},
|
||||
}
|
||||
|
||||
want := corev1.ObjectReference{
|
||||
APIVersion: "some.api.version/v1",
|
||||
Kind: "king",
|
||||
Name: "name",
|
||||
Namespace: "namespace",
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(want, ObjectReference(r)); diff != "" {
|
||||
t.Errorf("Unexpected ObjectReference (-want, +got) %s", diff)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,17 +121,6 @@ func (i *impl) Track(ref corev1.ObjectReference, obj interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func objectReference(item kmeta.Accessor) corev1.ObjectReference {
|
||||
gvk := item.GroupVersionKind()
|
||||
apiVersion, kind := gvk.ToAPIVersionAndKind()
|
||||
return corev1.ObjectReference{
|
||||
APIVersion: apiVersion,
|
||||
Kind: kind,
|
||||
Namespace: item.GetNamespace(),
|
||||
Name: item.GetName(),
|
||||
}
|
||||
}
|
||||
|
||||
func isExpired(expiry time.Time) bool {
|
||||
return time.Now().After(expiry)
|
||||
}
|
||||
|
@ -144,7 +133,7 @@ func (i *impl) OnChanged(obj interface{}) {
|
|||
return
|
||||
}
|
||||
|
||||
or := objectReference(item)
|
||||
or := kmeta.ObjectReference(item)
|
||||
|
||||
// TODO(mattmoor): Consider locking the mapping (global) for a
|
||||
// smaller scope and leveraging a per-set lock to guard its access.
|
||||
|
|
|
@ -26,6 +26,7 @@ import (
|
|||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
||||
"knative.dev/pkg/kmeta"
|
||||
. "knative.dev/pkg/testing"
|
||||
)
|
||||
|
||||
|
@ -47,7 +48,7 @@ func TestHappyPaths(t *testing.T) {
|
|||
Name: "foo",
|
||||
},
|
||||
}
|
||||
objRef := objectReference(thing1)
|
||||
objRef := kmeta.ObjectReference(thing1)
|
||||
|
||||
thing2 := &Resource{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
|
|
Loading…
Reference in New Issue