Commit bbb94e42 authored by Chao Xu's avatar Chao Xu

remove registry from testing/fixture.go; update client-gen to not use

registry in the generated clients
parent ca520e34
......@@ -123,12 +123,11 @@ func PackageForClientset(customArgs clientgenargs.Args, fakeClientsetPackage str
DefaultGen: generator.DefaultGen{
OptionalName: "register",
},
InputPackages: customArgs.GroupVersionToInputPath,
OutputPackage: fakeClientsetPackage,
Groups: customArgs.Groups,
ImportTracker: generator.NewImportTracker(),
PrivateScheme: true,
CreateRegistry: true, // needed to know about root resources and for RESTMapper
InputPackages: customArgs.GroupVersionToInputPath,
OutputPackage: fakeClientsetPackage,
Groups: customArgs.Groups,
ImportTracker: generator.NewImportTracker(),
PrivateScheme: true,
},
}
return generators
......
......@@ -108,7 +108,7 @@ var common = `
// without applying any validations and/or defaults. It shouldn't be considered a replacement
// for a real clientset and is mostly useful in simple unit tests.
func NewSimpleClientset(objects ...runtime.Object) *Clientset {
o := testing.NewObjectTracker(registry, scheme, codecs.UniversalDecoder())
o := testing.NewObjectTracker(scheme, codecs.UniversalDecoder())
for _, obj := range objects {
if err := o.Add(obj); err != nil {
panic(err)
......@@ -116,7 +116,7 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {
}
fakePtr := testing.Fake{}
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o, registry.RESTMapper()))
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
fakePtr.AddWatchReactor("*", testing.DefaultWatchReactor(watch.NewFake(), nil))
......
......@@ -120,6 +120,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
"GetOptions": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "GetOptions"}),
"Everything": c.Universe.Function(types.Name{Package: "k8s.io/apimachinery/pkg/labels", Name: "Everything"}),
"GroupVersionResource": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime/schema", Name: "GroupVersionResource"}),
"GroupVersionKind": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime/schema", Name: "GroupVersionKind"}),
"PatchType": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/types", Name: "PatchType"}),
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/watch", Name: "Interface"}),
......@@ -156,6 +157,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
if !noMethods {
sw.Do(resource, m)
sw.Do(kind, m)
sw.Do(createTemplate, m)
sw.Do(updateTemplate, m)
// Generate the UpdateStatus method if the type has a status
......@@ -198,11 +200,15 @@ var resource = `
var $.type|allLowercasePlural$Resource = $.GroupVersionResource|raw${Group: "$.groupName$", Version: "$.version$", Resource: "$.type|allLowercasePlural$"}
`
var kind = `
var $.type|allLowercasePlural$Kind = $.GroupVersionKind|raw${Group: "$.groupName$", Version: "$.version$", Kind: "$.type|public$"}
`
var listTemplate = `
func (c *Fake$.type|publicPlural$) List(opts $.ListOptions|raw$) (result *$.type|raw$List, err error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewListAction|raw$($.type|allLowercasePlural$Resource, c.ns, opts), &$.type|raw$List{})
$else$Invokes($.NewRootListAction|raw$($.type|allLowercasePlural$Resource, opts), &$.type|raw$List{})$end$
$if .namespaced$Invokes($.NewListAction|raw$($.type|allLowercasePlural$Resource, $.type|allLowercasePlural$Kind, c.ns, opts), &$.type|raw$List{})
$else$Invokes($.NewRootListAction|raw$($.type|allLowercasePlural$Resource, $.type|allLowercasePlural$Kind, opts), &$.type|raw$List{})$end$
if obj == nil {
return nil, err
}
......@@ -213,8 +219,8 @@ func (c *Fake$.type|publicPlural$) List(opts $.ListOptions|raw$) (result *$.type
var listUsingOptionsTemplate = `
func (c *Fake$.type|publicPlural$) List(opts $.ListOptions|raw$) (result *$.type|raw$List, err error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewListAction|raw$($.type|allLowercasePlural$Resource, c.ns, opts), &$.type|raw$List{})
$else$Invokes($.NewRootListAction|raw$($.type|allLowercasePlural$Resource, opts), &$.type|raw$List{})$end$
$if .namespaced$Invokes($.NewListAction|raw$($.type|allLowercasePlural$Resource, $.type|allLowercasePlural$Kind, c.ns, opts), &$.type|raw$List{})
$else$Invokes($.NewRootListAction|raw$($.type|allLowercasePlural$Resource, $.type|allLowercasePlural$Kind, opts), &$.type|raw$List{})$end$
if obj == nil {
return nil, err
}
......
......@@ -67,9 +67,9 @@ func (c *FakeEvents) PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.
// Search returns a list of events matching the specified object.
func (c *FakeEvents) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
action := core.NewRootListAction(eventsResource, metav1.ListOptions{})
action := core.NewRootListAction(eventsResource, eventsKind, metav1.ListOptions{})
if c.ns != "" {
action = core.NewListAction(eventsResource, c.ns, metav1.ListOptions{})
action = core.NewListAction(eventsResource, eventsKind, c.ns, metav1.ListOptions{})
}
obj, err := c.Fake.Invokes(action, &v1.EventList{})
if obj == nil {
......
......@@ -67,9 +67,9 @@ func (c *FakeEvents) PatchWithEventNamespace(event *api.Event, data []byte) (*ap
// Search returns a list of events matching the specified object.
func (c *FakeEvents) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*api.EventList, error) {
action := core.NewRootListAction(eventsResource, metav1.ListOptions{})
action := core.NewRootListAction(eventsResource, eventsKind, metav1.ListOptions{})
if c.ns != "" {
action = core.NewListAction(eventsResource, c.ns, metav1.ListOptions{})
action = core.NewListAction(eventsResource, eventsKind, c.ns, metav1.ListOptions{})
}
obj, err := c.Fake.Invokes(action, &api.EventList{})
if obj == nil {
......
......@@ -19,6 +19,7 @@ package util
import (
"reflect"
"testing"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
......@@ -31,7 +32,6 @@ import (
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"time"
)
type fakeClientAccessFactory struct {
......@@ -52,6 +52,7 @@ func newFakeClientAccessFactory(objs []runtime.Object) *fakeClientAccessFactory
var (
podsResource = schema.GroupVersionResource{Resource: "pods"}
podsKind = schema.GroupVersionKind{Kind: "Pod"}
)
func TestLogsForObject(t *testing.T) {
......@@ -82,7 +83,7 @@ func TestLogsForObject(t *testing.T) {
},
pods: []runtime.Object{testPod()},
actions: []testclient.Action{
testclient.NewListAction(podsResource, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
getLogsAction("test", nil),
},
},
......@@ -96,7 +97,7 @@ func TestLogsForObject(t *testing.T) {
},
pods: []runtime.Object{testPod()},
actions: []testclient.Action{
testclient.NewListAction(podsResource, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
getLogsAction("test", nil),
},
},
......@@ -110,7 +111,7 @@ func TestLogsForObject(t *testing.T) {
},
pods: []runtime.Object{testPod()},
actions: []testclient.Action{
testclient.NewListAction(podsResource, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
getLogsAction("test", nil),
},
},
......@@ -124,7 +125,7 @@ func TestLogsForObject(t *testing.T) {
},
pods: []runtime.Object{testPod()},
actions: []testclient.Action{
testclient.NewListAction(podsResource, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
getLogsAction("test", nil),
},
},
......@@ -138,7 +139,7 @@ func TestLogsForObject(t *testing.T) {
},
pods: []runtime.Object{testPod()},
actions: []testclient.Action{
testclient.NewListAction(podsResource, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
getLogsAction("test", nil),
},
},
......
......@@ -47,20 +47,22 @@ func NewGetAction(resource schema.GroupVersionResource, namespace, name string)
return action
}
func NewRootListAction(resource schema.GroupVersionResource, opts interface{}) ListActionImpl {
func NewRootListAction(resource schema.GroupVersionResource, kind schema.GroupVersionKind, opts interface{}) ListActionImpl {
action := ListActionImpl{}
action.Verb = "list"
action.Resource = resource
action.Kind = kind
labelSelector, fieldSelector, _ := ExtractFromListOptions(opts)
action.ListRestrictions = ListRestrictions{labelSelector, fieldSelector}
return action
}
func NewListAction(resource schema.GroupVersionResource, namespace string, opts interface{}) ListActionImpl {
func NewListAction(resource schema.GroupVersionResource, kind schema.GroupVersionKind, namespace string, opts interface{}) ListActionImpl {
action := ListActionImpl{}
action.Verb = "list"
action.Resource = resource
action.Kind = kind
action.Namespace = namespace
labelSelector, fieldSelector, _ := ExtractFromListOptions(opts)
action.ListRestrictions = ListRestrictions{labelSelector, fieldSelector}
......@@ -375,9 +377,14 @@ func (a GetActionImpl) GetName() string {
type ListActionImpl struct {
ActionImpl
Kind schema.GroupVersionKind
ListRestrictions ListRestrictions
}
func (a ListActionImpl) GetKind() schema.GroupVersionKind {
return a.Kind
}
func (a ListActionImpl) GetListRestrictions() ListRestrictions {
return a.ListRestrictions
}
......
......@@ -33,7 +33,7 @@ import (
// without applying any validations and/or defaults. It shouldn't be considered a replacement
// for a real clientset and is mostly useful in simple unit tests.
func NewSimpleClientset(objects ...runtime.Object) *Clientset {
o := testing.NewObjectTracker(api.Registry, api.Scheme, api.Codecs.UniversalDecoder())
o := testing.NewObjectTracker(api.Scheme, api.Codecs.UniversalDecoder())
for _, obj := range objects {
if err := o.Add(obj); err != nil {
panic(err)
......@@ -41,7 +41,7 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {
}
fakePtr := testing.Fake{}
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o, api.Registry.RESTMapper()))
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
fakePtr.AddWatchReactor("*", testing.DefaultWatchReactor(watch.NewFake(), nil))
......
......@@ -32,6 +32,8 @@ type FakeNodeMetricses struct {
var nodemetricsesResource = schema.GroupVersionResource{Group: "metrics", Version: "v1alpha1", Resource: "nodemetricses"}
var nodemetricsesKind = schema.GroupVersionKind{Group: "metrics", Version: "v1alpha1", Kind: "NodeMetrics"}
func (c *FakeNodeMetricses) Get(name string, options v1.GetOptions) (result *v1alpha1.NodeMetrics, err error) {
obj, err := c.Fake.
Invokes(testing.NewRootGetAction(nodemetricsesResource, name), &v1alpha1.NodeMetrics{})
......@@ -43,7 +45,7 @@ func (c *FakeNodeMetricses) Get(name string, options v1.GetOptions) (result *v1a
func (c *FakeNodeMetricses) List(opts v1.ListOptions) (result *v1alpha1.NodeMetricsList, err error) {
obj, err := c.Fake.
Invokes(testing.NewRootListAction(nodemetricsesResource, opts), &v1alpha1.NodeMetricsList{})
Invokes(testing.NewRootListAction(nodemetricsesResource, nodemetricsesKind, opts), &v1alpha1.NodeMetricsList{})
if obj == nil {
return nil, err
}
......
......@@ -33,6 +33,8 @@ type FakePodMetricses struct {
var podmetricsesResource = schema.GroupVersionResource{Group: "metrics", Version: "v1alpha1", Resource: "podmetricses"}
var podmetricsesKind = schema.GroupVersionKind{Group: "metrics", Version: "v1alpha1", Kind: "PodMetrics"}
func (c *FakePodMetricses) Get(name string, options v1.GetOptions) (result *v1alpha1.PodMetrics, err error) {
obj, err := c.Fake.
Invokes(testing.NewGetAction(podmetricsesResource, c.ns, name), &v1alpha1.PodMetrics{})
......@@ -45,7 +47,7 @@ func (c *FakePodMetricses) Get(name string, options v1.GetOptions) (result *v1al
func (c *FakePodMetricses) List(opts v1.ListOptions) (result *v1alpha1.PodMetricsList, err error) {
obj, err := c.Fake.
Invokes(testing.NewListAction(podmetricsesResource, c.ns, opts), &v1alpha1.PodMetricsList{})
Invokes(testing.NewListAction(podmetricsesResource, podmetricsesKind, c.ns, opts), &v1alpha1.PodMetricsList{})
if obj == nil {
return nil, err
......
......@@ -19,9 +19,9 @@ package fake
import (
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/testing"
"k8s.io/metrics/pkg/apis/custom_metrics/v1alpha1"
cmclient "k8s.io/metrics/pkg/client/custom_metrics"
......@@ -52,13 +52,12 @@ func (i GetForActionImpl) GetSubresource() string {
}
func NewGetForAction(groupKind schema.GroupKind, namespace, name string, metricName string, labelSelector labels.Selector) GetForActionImpl {
mapping, err := api.Registry.RESTMapper().RESTMapping(groupKind)
if err != nil {
panic(fmt.Sprintf("unable to get REST mapping for groupKind %s while building GetFor action: %v", groupKind.String, err))
}
// the version doesn't matter
gvk := groupKind.WithVersion("")
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
groupResourceForKind := schema.GroupResource{
Group: mapping.GroupVersionKind.Group,
Resource: mapping.Resource,
Group: gvr.Group,
Resource: gvr.Resource,
}
resource := schema.GroupResource{
Group: v1alpha1.SchemeGroupVersion.Group,
......@@ -72,13 +71,12 @@ func NewGetForAction(groupKind schema.GroupKind, namespace, name string, metricN
}
func NewRootGetForAction(groupKind schema.GroupKind, name string, metricName string, labelSelector labels.Selector) GetForActionImpl {
mapping, err := api.Registry.RESTMapper().RESTMapping(groupKind)
if err != nil {
panic(fmt.Sprintf("unable to get REST mapping for groupKind %s while building GetFor action: %v", groupKind.String, err))
}
// the version doesn't matter
gvk := groupKind.WithVersion("")
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
groupResourceForKind := schema.GroupResource{
Group: mapping.GroupVersionKind.Group,
Resource: mapping.Resource,
Group: gvr.Group,
Resource: gvr.Resource,
}
resource := schema.GroupResource{
Group: v1alpha1.SchemeGroupVersion.Group,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment