mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 15:37:24 +00:00
Merge pull request #27293 from caesarxuchao/add-patch-to-clientset
Automatic merge from submit-queue [client-gen]Add Patch to clientset * add the Patch() method to the clientset. * I have to rename the existing Patch() method of `Event` to PatchWithEventNamespace() to avoid overriding. * some minor changes to the fake Patch action. cc @Random-Liu since he asked for the method @kubernetes/sig-api-machinery ref #26580 ```release-note Add the Patch method to the generated clientset. ```
This commit is contained in:
@@ -115,6 +115,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
|
||||
"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"}),
|
||||
"PatchType": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "PatchType"}),
|
||||
"Everything": c.Universe.Function(types.Name{Package: "k8s.io/kubernetes/pkg/labels", Name: "Everything"}),
|
||||
|
||||
"NewRootListAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewRootListAction"}),
|
||||
@@ -133,6 +134,8 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
|
||||
"NewWatchAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewWatchAction"}),
|
||||
"NewUpdateSubresourceAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewUpdateSubresourceAction"}),
|
||||
"NewRootUpdateSubresourceAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewRootUpdateSubresourceAction"}),
|
||||
"NewRootPatchAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewRootPatchAction"}),
|
||||
"NewPatchAction": c.Universe.Function(types.Name{Package: pkgTestingCore, Name: "NewPatchAction"}),
|
||||
}
|
||||
|
||||
noMethods := types.ExtractCommentTags("+", t.SecondClosestCommentLines)["noMethods"] == "true"
|
||||
@@ -160,7 +163,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
|
||||
sw.Do(listTemplate, m)
|
||||
}
|
||||
sw.Do(watchTemplate, m)
|
||||
|
||||
sw.Do(patchTemplate, m)
|
||||
}
|
||||
|
||||
return sw.Error()
|
||||
@@ -297,3 +300,16 @@ func (c *Fake$.type|publicPlural$) Watch(opts $.apiListOptions|raw$) ($.watchInt
|
||||
$else$InvokesWatch($.NewRootWatchAction|raw$($.type|allLowercasePlural$Resource, opts))$end$
|
||||
}
|
||||
`
|
||||
|
||||
var patchTemplate = `
|
||||
// Patch applies the patch and returns the patched $.type|private$.
|
||||
func (c *Fake$.type|publicPlural$) Patch(name string, pt $.PatchType|raw$, data []byte) (result *$.type|raw$, err error) {
|
||||
obj, err := c.Fake.
|
||||
$if .namespaced$Invokes($.NewPatchAction|raw$($.type|allLowercasePlural$Resource, c.ns, name, data), &$.type|raw${})
|
||||
$else$Invokes($.NewRootPatchAction|raw$($.type|allLowercasePlural$Resource, name, data), &$.type|raw${})$end$
|
||||
if obj == nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj.(*$.type|raw$), err
|
||||
}
|
||||
`
|
||||
|
||||
@@ -76,6 +76,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
|
||||
"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"}),
|
||||
"apiParameterCodec": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "ParameterCodec"}),
|
||||
"PatchType": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "PatchType"}),
|
||||
"namespaced": namespaced,
|
||||
}
|
||||
|
||||
@@ -118,6 +119,7 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
|
||||
sw.Do(getTemplate, m)
|
||||
sw.Do(listTemplate, m)
|
||||
sw.Do(watchTemplate, m)
|
||||
sw.Do(patchTemplate, m)
|
||||
}
|
||||
|
||||
return sw.Error()
|
||||
@@ -158,7 +160,8 @@ var interfaceTemplate3 = `
|
||||
DeleteCollection(options *$.apiDeleteOptions|raw$, listOptions $.apiListOptions|raw$) error
|
||||
Get(name string) (*$.type|raw$, error)
|
||||
List(opts $.apiListOptions|raw$) (*$.type|raw$List, error)
|
||||
Watch(opts $.apiListOptions|raw$) ($.watchInterface|raw$, error)`
|
||||
Watch(opts $.apiListOptions|raw$) ($.watchInterface|raw$, error)
|
||||
Patch(name string, pt $.PatchType|raw$, data []byte) (result *$.type|raw$, err error)`
|
||||
|
||||
var interfaceTemplate4 = `
|
||||
$.type|public$Expansion
|
||||
@@ -309,3 +312,18 @@ func (c *$.type|privatePlural$) Watch(opts $.apiListOptions|raw$) ($.watchInterf
|
||||
Watch()
|
||||
}
|
||||
`
|
||||
|
||||
var patchTemplate = `
|
||||
// Patch applies the patch and returns the patched $.type|private$.
|
||||
func (c *$.type|privatePlural$) Patch(name string, pt $.PatchType|raw$, data []byte) (result *$.type|raw$, err error) {
|
||||
result = &$.type|raw${}
|
||||
err = c.client.Patch(pt).
|
||||
$if .namespaced$Namespace(c.ns).$end$
|
||||
Resource("$.type|allLowercasePlural$").
|
||||
Name(name).
|
||||
Body(data).
|
||||
Do().
|
||||
Into(result)
|
||||
return
|
||||
}
|
||||
`
|
||||
|
||||
@@ -114,3 +114,14 @@ func (c *FakeTestTypes) Watch(opts api.ListOptions) (watch.Interface, error) {
|
||||
InvokesWatch(core.NewWatchAction(testtypesResource, c.ns, opts))
|
||||
|
||||
}
|
||||
|
||||
// Patch applies the patch and returns the patched testType.
|
||||
func (c *FakeTestTypes) Patch(name string, pt api.PatchType, data []byte) (result *testgroup_k8s_io.TestType, err error) {
|
||||
obj, err := c.Fake.
|
||||
Invokes(core.NewPatchAction(testtypesResource, c.ns, name, data), &testgroup_k8s_io.TestType{})
|
||||
|
||||
if obj == nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj.(*testgroup_k8s_io.TestType), err
|
||||
}
|
||||
|
||||
@@ -245,3 +245,25 @@ func TestExpansionInterface(t *testing.T) {
|
||||
t.Errorf("expansion failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchTestType(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
requestTestType := &testgroup.TestType{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
ResourceVersion: "1",
|
||||
Labels: map[string]string{
|
||||
"foo": "bar",
|
||||
"name": "baz",
|
||||
},
|
||||
},
|
||||
}
|
||||
c := DecoratedSimpleClient{
|
||||
simpleClient: simple.Client{
|
||||
Request: simple.Request{Method: "PATCH", Path: testHelper.ResourcePath("testtypes", ns, "foo"), Query: simple.BuildQueryValues(nil)},
|
||||
Response: simple.Response{StatusCode: http.StatusOK, Body: requestTestType},
|
||||
},
|
||||
}
|
||||
receivedTestType, err := c.Setup(t).TestTypes(ns).Patch(requestTestType.Name, api.StrategicMergePatchType, []byte{})
|
||||
c.simpleClient.Validate(t, receivedTestType, err)
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ type TestTypeInterface interface {
|
||||
Get(name string) (*testgroup_k8s_io.TestType, error)
|
||||
List(opts api.ListOptions) (*testgroup_k8s_io.TestTypeList, error)
|
||||
Watch(opts api.ListOptions) (watch.Interface, error)
|
||||
Patch(name string, pt api.PatchType, data []byte) (result *testgroup_k8s_io.TestType, err error)
|
||||
TestTypeExpansion
|
||||
}
|
||||
|
||||
@@ -148,3 +149,16 @@ func (c *testTypes) Watch(opts api.ListOptions) (watch.Interface, error) {
|
||||
VersionedParams(&opts, api.ParameterCodec).
|
||||
Watch()
|
||||
}
|
||||
|
||||
// Patch applies the patch and returns the patched testType.
|
||||
func (c *testTypes) Patch(name string, pt api.PatchType, data []byte) (result *testgroup_k8s_io.TestType, err error) {
|
||||
result = &testgroup_k8s_io.TestType{}
|
||||
err = c.client.Patch(pt).
|
||||
Namespace(c.ns).
|
||||
Resource("testtypes").
|
||||
Name(name).
|
||||
Body(data).
|
||||
Do().
|
||||
Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user