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

Make fake client actions use fully qualified resource #24398

Merged
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 @@ -55,6 +55,7 @@ func PackageForGroup(gv unversioned.GroupVersion, typeList []*types.Type, packag
},
outputPackage: outputPackagePath,
group: normalization.BeforeFirstDot(gv.Group),
version: gv.Version,
typeToMatch: t,
imports: generator.NewImportTracker(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type genFakeForType struct {
generator.DefaultGen
outputPackage string
group string
version string
typeToMatch *types.Type
imports namer.ImportTracker
}
Expand Down Expand Up @@ -78,16 +79,27 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
pkg := filepath.Base(t.Name.Package)
const pkgTestingCore = "k8s.io/kubernetes/pkg/client/testing/core"
namespaced := !(types.ExtractCommentTags("+", t.SecondClosestCommentLines)["nonNamespaced"] == "true")
canonicalGroup := g.group
if canonicalGroup == "core" {
canonicalGroup = ""
}
canonicalVersion := g.version
if canonicalVersion == "unversioned" {
canonicalVersion = ""
}
m := map[string]interface{}{
"type": t,
"package": pkg,
"Package": namer.IC(pkg),
"namespaced": namespaced,
"Group": namer.IC(g.group),
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/watch", Name: "Interface"}),
"apiDeleteOptions": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "DeleteOptions"}),
"apiListOptions": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "ListOptions"}),
"Everything": c.Universe.Function(types.Name{Package: "k8s.io/kubernetes/pkg/labels", Name: "Everything"}),
"type": t,
"package": pkg,
"Package": namer.IC(pkg),
"namespaced": namespaced,
"Group": namer.IC(g.group),
"group": canonicalGroup,
"version": canonicalVersion,
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/watch", Name: "Interface"}),
"apiDeleteOptions": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "DeleteOptions"}),
"apiListOptions": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "ListOptions"}),
"GroupVersionResource": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api/unversioned", Name: "GroupVersionResource"}),
"Everything": c.Universe.Function(types.Name{Package: "k8s.io/kubernetes/pkg/labels", Name: "Everything"}),

"NewRootListAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewRootListAction"}),
"NewListAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewListAction"}),
Expand Down Expand Up @@ -116,6 +128,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
}

if !noMethods {
sw.Do(resource, m)
sw.Do(createTemplate, m)
sw.Do(updateTemplate, m)
// Generate the UpdateStatus method if the type has a status
Expand Down Expand Up @@ -154,11 +167,15 @@ type Fake$.type|publicPlural$ struct {
}
`

var resource = `
var $.type|allLowercasePlural$Resource = $.GroupVersionResource|raw${Group: "$.group$", Version: "$.version$", Resource: "$.type|allLowercasePlural$"}
`

var listTemplate = `
func (c *Fake$.type|publicPlural$) List(opts $.apiListOptions|raw$) (result *$.type|raw$List, err error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewListAction|raw$("$.type|allLowercasePlural$", c.ns, opts), &$.type|raw$List{})
$else$Invokes($.NewRootListAction|raw$("$.type|allLowercasePlural$", opts), &$.type|raw$List{})$end$
$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 obj == nil {
return nil, err
}
Expand All @@ -169,8 +186,8 @@ func (c *Fake$.type|publicPlural$) List(opts $.apiListOptions|raw$) (result *$.t
var listUsingOptionsTemplate = `
func (c *Fake$.type|publicPlural$) List(opts $.apiListOptions|raw$) (result *$.type|raw$List, err error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewListAction|raw$("$.type|allLowercasePlural$", c.ns, opts), &$.type|raw$List{})
$else$Invokes($.NewRootListAction|raw$("$.type|allLowercasePlural$", opts), &$.type|raw$List{})$end$
$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 obj == nil {
return nil, err
}
Expand All @@ -192,8 +209,8 @@ func (c *Fake$.type|publicPlural$) List(opts $.apiListOptions|raw$) (result *$.t
var getTemplate = `
func (c *Fake$.type|publicPlural$) Get(name string) (result *$.type|raw$, err error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewGetAction|raw$("$.type|allLowercasePlural$", c.ns, name), &$.type|raw${})
$else$Invokes($.NewRootGetAction|raw$("$.type|allLowercasePlural$", name), &$.type|raw${})$end$
$if .namespaced$Invokes($.NewGetAction|raw$($.type|allLowercasePlural$Resource, c.ns, name), &$.type|raw${})
$else$Invokes($.NewRootGetAction|raw$($.type|allLowercasePlural$Resource, name), &$.type|raw${})$end$
if obj == nil {
return nil, err
}
Expand All @@ -204,16 +221,16 @@ func (c *Fake$.type|publicPlural$) Get(name string) (result *$.type|raw$, err er
var deleteTemplate = `
func (c *Fake$.type|publicPlural$) Delete(name string, options *$.apiDeleteOptions|raw$) error {
_, err := c.Fake.
$if .namespaced$Invokes($.NewDeleteAction|raw$("$.type|allLowercasePlural$", c.ns, name), &$.type|raw${})
$else$Invokes($.NewRootDeleteAction|raw$("$.type|allLowercasePlural$", name), &$.type|raw${})$end$
$if .namespaced$Invokes($.NewDeleteAction|raw$($.type|allLowercasePlural$Resource, c.ns, name), &$.type|raw${})
$else$Invokes($.NewRootDeleteAction|raw$($.type|allLowercasePlural$Resource, name), &$.type|raw${})$end$
return err
}
`

var deleteCollectionTemplate = `
func (c *Fake$.type|publicPlural$) DeleteCollection(options *$.apiDeleteOptions|raw$, listOptions $.apiListOptions|raw$) error {
$if .namespaced$action := $.NewDeleteCollectionAction|raw$("$.type|allLowercasePlural$", c.ns, listOptions)
$else$action := $.NewRootDeleteCollectionAction|raw$("$.type|allLowercasePlural$", listOptions)
$if .namespaced$action := $.NewDeleteCollectionAction|raw$($.type|allLowercasePlural$Resource, c.ns, listOptions)
$else$action := $.NewRootDeleteCollectionAction|raw$($.type|allLowercasePlural$Resource, listOptions)
$end$
_, err := c.Fake.Invokes(action, &$.type|raw$List{})
return err
Expand All @@ -223,8 +240,8 @@ func (c *Fake$.type|publicPlural$) DeleteCollection(options *$.apiDeleteOptions|
var createTemplate = `
func (c *Fake$.type|publicPlural$) Create($.type|private$ *$.type|raw$) (result *$.type|raw$, err error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewCreateAction|raw$("$.type|allLowercasePlural$", c.ns, $.type|private$), &$.type|raw${})
$else$Invokes($.NewRootCreateAction|raw$("$.type|allLowercasePlural$", $.type|private$), &$.type|raw${})$end$
$if .namespaced$Invokes($.NewCreateAction|raw$($.type|allLowercasePlural$Resource, c.ns, $.type|private$), &$.type|raw${})
$else$Invokes($.NewRootCreateAction|raw$($.type|allLowercasePlural$Resource, $.type|private$), &$.type|raw${})$end$
if obj == nil {
return nil, err
}
Expand All @@ -235,8 +252,8 @@ func (c *Fake$.type|publicPlural$) Create($.type|private$ *$.type|raw$) (result
var updateTemplate = `
func (c *Fake$.type|publicPlural$) Update($.type|private$ *$.type|raw$) (result *$.type|raw$, err error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewUpdateAction|raw$("$.type|allLowercasePlural$", c.ns, $.type|private$), &$.type|raw${})
$else$Invokes($.NewRootUpdateAction|raw$("$.type|allLowercasePlural$", $.type|private$), &$.type|raw${})$end$
$if .namespaced$Invokes($.NewUpdateAction|raw$($.type|allLowercasePlural$Resource, c.ns, $.type|private$), &$.type|raw${})
$else$Invokes($.NewRootUpdateAction|raw$($.type|allLowercasePlural$Resource, $.type|private$), &$.type|raw${})$end$
if obj == nil {
return nil, err
}
Expand All @@ -247,8 +264,8 @@ func (c *Fake$.type|publicPlural$) Update($.type|private$ *$.type|raw$) (result
var updateStatusTemplate = `
func (c *Fake$.type|publicPlural$) UpdateStatus($.type|private$ *$.type|raw$) (*$.type|raw$, error) {
obj, err := c.Fake.
$if .namespaced$Invokes($.NewUpdateSubresourceAction|raw$("$.type|allLowercasePlural$", "status", c.ns, $.type|private$), &$.type|raw${})
$else$Invokes($.NewRootUpdateSubresourceAction|raw$("$.type|allLowercasePlural$", "status", $.type|private$), &$.type|raw${})$end$
$if .namespaced$Invokes($.NewUpdateSubresourceAction|raw$($.type|allLowercasePlural$Resource, "status", c.ns, $.type|private$), &$.type|raw${})
$else$Invokes($.NewRootUpdateSubresourceAction|raw$($.type|allLowercasePlural$Resource, "status", $.type|private$), &$.type|raw${})$end$
if obj == nil {
return nil, err
}
Expand All @@ -260,7 +277,7 @@ var watchTemplate = `
// Watch returns a $.watchInterface|raw$ that watches the requested $.type|privatePlural$.
func (c *Fake$.type|publicPlural$) Watch(opts $.apiListOptions|raw$) ($.watchInterface|raw$, error) {
return c.Fake.
$if .namespaced$InvokesWatch($.NewWatchAction|raw$("$.type|allLowercasePlural$", c.ns, opts))
$else$InvokesWatch($.NewRootWatchAction|raw$("$.type|allLowercasePlural$", opts))$end$
$if .namespaced$InvokesWatch($.NewWatchAction|raw$($.type|allLowercasePlural$Resource, c.ns, opts))
$else$InvokesWatch($.NewRootWatchAction|raw$($.type|allLowercasePlural$Resource, opts))$end$
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fake
import (
testgroup_k8s_io "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup.k8s.io"
api "k8s.io/kubernetes/pkg/api"
unversioned "k8s.io/kubernetes/pkg/api/unversioned"
core "k8s.io/kubernetes/pkg/client/testing/core"
labels "k8s.io/kubernetes/pkg/labels"
watch "k8s.io/kubernetes/pkg/watch"
Expand All @@ -30,9 +31,11 @@ type FakeTestTypes struct {
ns string
}

var testtypesResource = unversioned.GroupVersionResource{Group: "testgroup", Version: "", Resource: "testtypes"}

func (c *FakeTestTypes) Create(testType *testgroup_k8s_io.TestType) (result *testgroup_k8s_io.TestType, err error) {
obj, err := c.Fake.
Invokes(core.NewCreateAction("testtypes", c.ns, testType), &testgroup_k8s_io.TestType{})
Invokes(core.NewCreateAction(testtypesResource, c.ns, testType), &testgroup_k8s_io.TestType{})

if obj == nil {
return nil, err
Expand All @@ -42,7 +45,7 @@ func (c *FakeTestTypes) Create(testType *testgroup_k8s_io.TestType) (result *tes

func (c *FakeTestTypes) Update(testType *testgroup_k8s_io.TestType) (result *testgroup_k8s_io.TestType, err error) {
obj, err := c.Fake.
Invokes(core.NewUpdateAction("testtypes", c.ns, testType), &testgroup_k8s_io.TestType{})
Invokes(core.NewUpdateAction(testtypesResource, c.ns, testType), &testgroup_k8s_io.TestType{})

if obj == nil {
return nil, err
Expand All @@ -52,7 +55,7 @@ func (c *FakeTestTypes) Update(testType *testgroup_k8s_io.TestType) (result *tes

func (c *FakeTestTypes) UpdateStatus(testType *testgroup_k8s_io.TestType) (*testgroup_k8s_io.TestType, error) {
obj, err := c.Fake.
Invokes(core.NewUpdateSubresourceAction("testtypes", "status", c.ns, testType), &testgroup_k8s_io.TestType{})
Invokes(core.NewUpdateSubresourceAction(testtypesResource, "status", c.ns, testType), &testgroup_k8s_io.TestType{})

if obj == nil {
return nil, err
Expand All @@ -62,21 +65,21 @@ func (c *FakeTestTypes) UpdateStatus(testType *testgroup_k8s_io.TestType) (*test

func (c *FakeTestTypes) Delete(name string, options *api.DeleteOptions) error {
_, err := c.Fake.
Invokes(core.NewDeleteAction("testtypes", c.ns, name), &testgroup_k8s_io.TestType{})
Invokes(core.NewDeleteAction(testtypesResource, c.ns, name), &testgroup_k8s_io.TestType{})

return err
}

func (c *FakeTestTypes) DeleteCollection(options *api.DeleteOptions, listOptions api.ListOptions) error {
action := core.NewDeleteCollectionAction("testtypes", c.ns, listOptions)
action := core.NewDeleteCollectionAction(testtypesResource, c.ns, listOptions)

_, err := c.Fake.Invokes(action, &testgroup_k8s_io.TestTypeList{})
return err
}

func (c *FakeTestTypes) Get(name string) (result *testgroup_k8s_io.TestType, err error) {
obj, err := c.Fake.
Invokes(core.NewGetAction("testtypes", c.ns, name), &testgroup_k8s_io.TestType{})
Invokes(core.NewGetAction(testtypesResource, c.ns, name), &testgroup_k8s_io.TestType{})

if obj == nil {
return nil, err
Expand All @@ -86,7 +89,7 @@ func (c *FakeTestTypes) Get(name string) (result *testgroup_k8s_io.TestType, err

func (c *FakeTestTypes) List(opts api.ListOptions) (result *testgroup_k8s_io.TestTypeList, err error) {
obj, err := c.Fake.
Invokes(core.NewListAction("testtypes", c.ns, opts), &testgroup_k8s_io.TestTypeList{})
Invokes(core.NewListAction(testtypesResource, c.ns, opts), &testgroup_k8s_io.TestTypeList{})

if obj == nil {
return nil, err
Expand All @@ -108,6 +111,6 @@ func (c *FakeTestTypes) List(opts api.ListOptions) (result *testgroup_k8s_io.Tes
// Watch returns a watch.Interface that watches the requested testTypes.
func (c *FakeTestTypes) Watch(opts api.ListOptions) (watch.Interface, error) {
return c.Fake.
InvokesWatch(core.NewWatchAction("testtypes", c.ns, opts))
InvokesWatch(core.NewWatchAction(testtypesResource, c.ns, opts))

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package fake

import (
api "k8s.io/kubernetes/pkg/api"
unversioned "k8s.io/kubernetes/pkg/api/unversioned"
core "k8s.io/kubernetes/pkg/client/testing/core"
labels "k8s.io/kubernetes/pkg/labels"
watch "k8s.io/kubernetes/pkg/watch"
Expand All @@ -28,9 +29,11 @@ type FakeComponentStatuses struct {
Fake *FakeCore
}

var componentstatusesResource = unversioned.GroupVersionResource{Group: "", Version: "", Resource: "componentstatuses"}

func (c *FakeComponentStatuses) Create(componentStatus *api.ComponentStatus) (result *api.ComponentStatus, err error) {
obj, err := c.Fake.
Invokes(core.NewRootCreateAction("componentstatuses", componentStatus), &api.ComponentStatus{})
Invokes(core.NewRootCreateAction(componentstatusesResource, componentStatus), &api.ComponentStatus{})
if obj == nil {
return nil, err
}
Expand All @@ -39,7 +42,7 @@ func (c *FakeComponentStatuses) Create(componentStatus *api.ComponentStatus) (re

func (c *FakeComponentStatuses) Update(componentStatus *api.ComponentStatus) (result *api.ComponentStatus, err error) {
obj, err := c.Fake.
Invokes(core.NewRootUpdateAction("componentstatuses", componentStatus), &api.ComponentStatus{})
Invokes(core.NewRootUpdateAction(componentstatusesResource, componentStatus), &api.ComponentStatus{})
if obj == nil {
return nil, err
}
Expand All @@ -48,20 +51,20 @@ func (c *FakeComponentStatuses) Update(componentStatus *api.ComponentStatus) (re

func (c *FakeComponentStatuses) Delete(name string, options *api.DeleteOptions) error {
_, err := c.Fake.
Invokes(core.NewRootDeleteAction("componentstatuses", name), &api.ComponentStatus{})
Invokes(core.NewRootDeleteAction(componentstatusesResource, name), &api.ComponentStatus{})
return err
}

func (c *FakeComponentStatuses) DeleteCollection(options *api.DeleteOptions, listOptions api.ListOptions) error {
action := core.NewRootDeleteCollectionAction("componentstatuses", listOptions)
action := core.NewRootDeleteCollectionAction(componentstatusesResource, listOptions)

_, err := c.Fake.Invokes(action, &api.ComponentStatusList{})
return err
}

func (c *FakeComponentStatuses) Get(name string) (result *api.ComponentStatus, err error) {
obj, err := c.Fake.
Invokes(core.NewRootGetAction("componentstatuses", name), &api.ComponentStatus{})
Invokes(core.NewRootGetAction(componentstatusesResource, name), &api.ComponentStatus{})
if obj == nil {
return nil, err
}
Expand All @@ -70,7 +73,7 @@ func (c *FakeComponentStatuses) Get(name string) (result *api.ComponentStatus, e

func (c *FakeComponentStatuses) List(opts api.ListOptions) (result *api.ComponentStatusList, err error) {
obj, err := c.Fake.
Invokes(core.NewRootListAction("componentstatuses", opts), &api.ComponentStatusList{})
Invokes(core.NewRootListAction(componentstatusesResource, opts), &api.ComponentStatusList{})
if obj == nil {
return nil, err
}
Expand All @@ -91,5 +94,5 @@ func (c *FakeComponentStatuses) List(opts api.ListOptions) (result *api.Componen
// Watch returns a watch.Interface that watches the requested componentStatuses.
func (c *FakeComponentStatuses) Watch(opts api.ListOptions) (watch.Interface, error) {
return c.Fake.
InvokesWatch(core.NewRootWatchAction("componentstatuses", opts))
InvokesWatch(core.NewRootWatchAction(componentstatusesResource, opts))
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package fake

import (
api "k8s.io/kubernetes/pkg/api"
unversioned "k8s.io/kubernetes/pkg/api/unversioned"
core "k8s.io/kubernetes/pkg/client/testing/core"
labels "k8s.io/kubernetes/pkg/labels"
watch "k8s.io/kubernetes/pkg/watch"
Expand All @@ -29,9 +30,11 @@ type FakeConfigMaps struct {
ns string
}

var configmapsResource = unversioned.GroupVersionResource{Group: "", Version: "", Resource: "configmaps"}

func (c *FakeConfigMaps) Create(configMap *api.ConfigMap) (result *api.ConfigMap, err error) {
obj, err := c.Fake.
Invokes(core.NewCreateAction("configmaps", c.ns, configMap), &api.ConfigMap{})
Invokes(core.NewCreateAction(configmapsResource, c.ns, configMap), &api.ConfigMap{})

if obj == nil {
return nil, err
Expand All @@ -41,7 +44,7 @@ func (c *FakeConfigMaps) Create(configMap *api.ConfigMap) (result *api.ConfigMap

func (c *FakeConfigMaps) Update(configMap *api.ConfigMap) (result *api.ConfigMap, err error) {
obj, err := c.Fake.
Invokes(core.NewUpdateAction("configmaps", c.ns, configMap), &api.ConfigMap{})
Invokes(core.NewUpdateAction(configmapsResource, c.ns, configMap), &api.ConfigMap{})

if obj == nil {
return nil, err
Expand All @@ -51,21 +54,21 @@ func (c *FakeConfigMaps) Update(configMap *api.ConfigMap) (result *api.ConfigMap

func (c *FakeConfigMaps) Delete(name string, options *api.DeleteOptions) error {
_, err := c.Fake.
Invokes(core.NewDeleteAction("configmaps", c.ns, name), &api.ConfigMap{})
Invokes(core.NewDeleteAction(configmapsResource, c.ns, name), &api.ConfigMap{})

return err
}

func (c *FakeConfigMaps) DeleteCollection(options *api.DeleteOptions, listOptions api.ListOptions) error {
action := core.NewDeleteCollectionAction("configmaps", c.ns, listOptions)
action := core.NewDeleteCollectionAction(configmapsResource, c.ns, listOptions)

_, err := c.Fake.Invokes(action, &api.ConfigMapList{})
return err
}

func (c *FakeConfigMaps) Get(name string) (result *api.ConfigMap, err error) {
obj, err := c.Fake.
Invokes(core.NewGetAction("configmaps", c.ns, name), &api.ConfigMap{})
Invokes(core.NewGetAction(configmapsResource, c.ns, name), &api.ConfigMap{})

if obj == nil {
return nil, err
Expand All @@ -75,7 +78,7 @@ func (c *FakeConfigMaps) Get(name string) (result *api.ConfigMap, err error) {

func (c *FakeConfigMaps) List(opts api.ListOptions) (result *api.ConfigMapList, err error) {
obj, err := c.Fake.
Invokes(core.NewListAction("configmaps", c.ns, opts), &api.ConfigMapList{})
Invokes(core.NewListAction(configmapsResource, c.ns, opts), &api.ConfigMapList{})

if obj == nil {
return nil, err
Expand All @@ -97,6 +100,6 @@ func (c *FakeConfigMaps) List(opts api.ListOptions) (result *api.ConfigMapList,
// Watch returns a watch.Interface that watches the requested configMaps.
func (c *FakeConfigMaps) Watch(opts api.ListOptions) (watch.Interface, error) {
return c.Fake.
InvokesWatch(core.NewWatchAction("configmaps", c.ns, opts))
InvokesWatch(core.NewWatchAction(configmapsResource, c.ns, opts))

}