client-go dynamic context

Kubernetes-commit: 6fa54d715fbc992afb483e71d450d5dbd66990eb
This commit is contained in:
Jordan Liggitt 2020-03-06 01:27:11 -05:00 committed by Kubernetes Publisher
parent 67a40b9a1a
commit 1faf9e8d03
8 changed files with 63 additions and 56 deletions

View File

@ -18,6 +18,7 @@ package dynamic
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -134,7 +135,7 @@ func TestList(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
got, err := cl.Resource(resource).Namespace(tc.namespace).List(metav1.ListOptions{}) got, err := cl.Resource(resource).Namespace(tc.namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil { if err != nil {
t.Errorf("unexpected error when listing %q: %v", tc.name, err) t.Errorf("unexpected error when listing %q: %v", tc.name, err)
continue continue
@ -209,7 +210,7 @@ func TestGet(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
got, err := cl.Resource(resource).Namespace(tc.namespace).Get(tc.name, metav1.GetOptions{}, tc.subresource...) got, err := cl.Resource(resource).Namespace(tc.namespace).Get(context.TODO(), tc.name, metav1.GetOptions{}, tc.subresource...)
if err != nil { if err != nil {
t.Errorf("unexpected error when getting %q: %v", tc.name, err) t.Errorf("unexpected error when getting %q: %v", tc.name, err)
continue continue
@ -283,7 +284,7 @@ func TestDelete(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
err = cl.Resource(resource).Namespace(tc.namespace).Delete(tc.name, tc.deleteOptions, tc.subresource...) err = cl.Resource(resource).Namespace(tc.namespace).Delete(context.TODO(), tc.name, tc.deleteOptions, tc.subresource...)
if err != nil { if err != nil {
t.Errorf("unexpected error when deleting %q: %v", tc.name, err) t.Errorf("unexpected error when deleting %q: %v", tc.name, err)
continue continue
@ -331,7 +332,7 @@ func TestDeleteCollection(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
err = cl.Resource(resource).Namespace(tc.namespace).DeleteCollection(metav1.DeleteOptions{}, metav1.ListOptions{}) err = cl.Resource(resource).Namespace(tc.namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})
if err != nil { if err != nil {
t.Errorf("unexpected error when deleting collection %q: %v", tc.name, err) t.Errorf("unexpected error when deleting collection %q: %v", tc.name, err)
continue continue
@ -404,7 +405,7 @@ func TestCreate(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
got, err := cl.Resource(resource).Namespace(tc.namespace).Create(tc.obj, metav1.CreateOptions{}, tc.subresource...) got, err := cl.Resource(resource).Namespace(tc.namespace).Create(context.TODO(), tc.obj, metav1.CreateOptions{}, tc.subresource...)
if err != nil { if err != nil {
t.Errorf("unexpected error when creating %q: %v", tc.name, err) t.Errorf("unexpected error when creating %q: %v", tc.name, err)
continue continue
@ -481,7 +482,7 @@ func TestUpdate(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
got, err := cl.Resource(resource).Namespace(tc.namespace).Update(tc.obj, metav1.UpdateOptions{}, tc.subresource...) got, err := cl.Resource(resource).Namespace(tc.namespace).Update(context.TODO(), tc.obj, metav1.UpdateOptions{}, tc.subresource...)
if err != nil { if err != nil {
t.Errorf("unexpected error when updating %q: %v", tc.name, err) t.Errorf("unexpected error when updating %q: %v", tc.name, err)
continue continue
@ -550,7 +551,7 @@ func TestWatch(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
watcher, err := cl.Resource(resource).Namespace(tc.namespace).Watch(metav1.ListOptions{}) watcher, err := cl.Resource(resource).Namespace(tc.namespace).Watch(context.TODO(), metav1.ListOptions{})
if err != nil { if err != nil {
t.Errorf("unexpected error when watching %q: %v", tc.name, err) t.Errorf("unexpected error when watching %q: %v", tc.name, err)
continue continue
@ -640,7 +641,7 @@ func TestPatch(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
got, err := cl.Resource(resource).Namespace(tc.namespace).Patch(tc.name, types.StrategicMergePatchType, tc.patch, metav1.PatchOptions{}, tc.subresource...) got, err := cl.Resource(resource).Namespace(tc.namespace).Patch(context.TODO(), tc.name, types.StrategicMergePatchType, tc.patch, metav1.PatchOptions{}, tc.subresource...)
if err != nil { if err != nil {
t.Errorf("unexpected error when patching %q: %v", tc.name, err) t.Errorf("unexpected error when patching %q: %v", tc.name, err)
continue continue

View File

@ -17,6 +17,7 @@ limitations under the License.
package dynamicinformer package dynamicinformer
import ( import (
"context"
"sync" "sync"
"time" "time"
@ -125,13 +126,13 @@ func NewFilteredDynamicInformer(client dynamic.Interface, gvr schema.GroupVersio
if tweakListOptions != nil { if tweakListOptions != nil {
tweakListOptions(&options) tweakListOptions(&options)
} }
return client.Resource(gvr).Namespace(namespace).List(options) return client.Resource(gvr).Namespace(namespace).List(context.TODO(), options)
}, },
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
if tweakListOptions != nil { if tweakListOptions != nil {
tweakListOptions(&options) tweakListOptions(&options)
} }
return client.Resource(gvr).Namespace(namespace).Watch(options) return client.Resource(gvr).Namespace(namespace).Watch(context.TODO(), options)
}, },
}, },
&unstructured.Unstructured{}, &unstructured.Unstructured{},

View File

@ -48,7 +48,7 @@ func TestDynamicSharedInformerFactory(t *testing.T) {
gvr: schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "deployments"}, gvr: schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "deployments"},
trigger: func(gvr schema.GroupVersionResource, ns string, fakeClient *fake.FakeDynamicClient, _ *unstructured.Unstructured) *unstructured.Unstructured { trigger: func(gvr schema.GroupVersionResource, ns string, fakeClient *fake.FakeDynamicClient, _ *unstructured.Unstructured) *unstructured.Unstructured {
testObject := newUnstructured("extensions/v1beta1", "Deployment", "ns-foo", "name-foo") testObject := newUnstructured("extensions/v1beta1", "Deployment", "ns-foo", "name-foo")
createdObj, err := fakeClient.Resource(gvr).Namespace(ns).Create(testObject, metav1.CreateOptions{}) createdObj, err := fakeClient.Resource(gvr).Namespace(ns).Create(context.TODO(), testObject, metav1.CreateOptions{})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -71,7 +71,7 @@ func TestDynamicSharedInformerFactory(t *testing.T) {
existingObj: newUnstructured("extensions/v1beta1", "Deployment", "ns-foo", "name-foo"), existingObj: newUnstructured("extensions/v1beta1", "Deployment", "ns-foo", "name-foo"),
trigger: func(gvr schema.GroupVersionResource, ns string, fakeClient *fake.FakeDynamicClient, testObject *unstructured.Unstructured) *unstructured.Unstructured { trigger: func(gvr schema.GroupVersionResource, ns string, fakeClient *fake.FakeDynamicClient, testObject *unstructured.Unstructured) *unstructured.Unstructured {
testObject.Object["spec"] = "updatedName" testObject.Object["spec"] = "updatedName"
updatedObj, err := fakeClient.Resource(gvr).Namespace(ns).Update(testObject, metav1.UpdateOptions{}) updatedObj, err := fakeClient.Resource(gvr).Namespace(ns).Update(context.TODO(), testObject, metav1.UpdateOptions{})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -93,7 +93,7 @@ func TestDynamicSharedInformerFactory(t *testing.T) {
gvr: schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "deployments"}, gvr: schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "deployments"},
existingObj: newUnstructured("extensions/v1beta1", "Deployment", "ns-foo", "name-foo"), existingObj: newUnstructured("extensions/v1beta1", "Deployment", "ns-foo", "name-foo"),
trigger: func(gvr schema.GroupVersionResource, ns string, fakeClient *fake.FakeDynamicClient, testObject *unstructured.Unstructured) *unstructured.Unstructured { trigger: func(gvr schema.GroupVersionResource, ns string, fakeClient *fake.FakeDynamicClient, testObject *unstructured.Unstructured) *unstructured.Unstructured {
err := fakeClient.Resource(gvr).Namespace(ns).Delete(testObject.GetName(), metav1.DeleteOptions{}) err := fakeClient.Resource(gvr).Namespace(ns).Delete(context.TODO(), testObject.GetName(), metav1.DeleteOptions{})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -17,6 +17,7 @@ limitations under the License.
package fake package fake
import ( import (
"context"
"strings" "strings"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
@ -86,7 +87,7 @@ func (c *dynamicResourceClient) Namespace(ns string) dynamic.ResourceInterface {
return &ret return &ret
} }
func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Create(ctx context.Context, obj *unstructured.Unstructured, opts metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) {
var uncastRet runtime.Object var uncastRet runtime.Object
var err error var err error
switch { switch {
@ -132,7 +133,7 @@ func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts meta
return ret, err return ret, err
} }
func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) {
var uncastRet runtime.Object var uncastRet runtime.Object
var err error var err error
switch { switch {
@ -168,7 +169,7 @@ func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts meta
return ret, err return ret, err
} }
func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) UpdateStatus(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*unstructured.Unstructured, error) {
var uncastRet runtime.Object var uncastRet runtime.Object
var err error var err error
switch { switch {
@ -196,7 +197,7 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt
return ret, err return ret, err
} }
func (c *dynamicResourceClient) Delete(name string, opts metav1.DeleteOptions, subresources ...string) error { func (c *dynamicResourceClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions, subresources ...string) error {
var err error var err error
switch { switch {
case len(c.namespace) == 0 && len(subresources) == 0: case len(c.namespace) == 0 && len(subresources) == 0:
@ -219,7 +220,7 @@ func (c *dynamicResourceClient) Delete(name string, opts metav1.DeleteOptions, s
return err return err
} }
func (c *dynamicResourceClient) DeleteCollection(opts metav1.DeleteOptions, listOptions metav1.ListOptions) error { func (c *dynamicResourceClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOptions metav1.ListOptions) error {
var err error var err error
switch { switch {
case len(c.namespace) == 0: case len(c.namespace) == 0:
@ -235,7 +236,7 @@ func (c *dynamicResourceClient) DeleteCollection(opts metav1.DeleteOptions, list
return err return err
} }
func (c *dynamicResourceClient) Get(name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Get(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
var uncastRet runtime.Object var uncastRet runtime.Object
var err error var err error
switch { switch {
@ -270,7 +271,7 @@ func (c *dynamicResourceClient) Get(name string, opts metav1.GetOptions, subreso
return ret, err return ret, err
} }
func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.UnstructuredList, error) { func (c *dynamicResourceClient) List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
var obj runtime.Object var obj runtime.Object
var err error var err error
switch { switch {
@ -317,7 +318,7 @@ func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.Uns
return list, nil return list, nil
} }
func (c *dynamicResourceClient) Watch(opts metav1.ListOptions) (watch.Interface, error) { func (c *dynamicResourceClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
switch { switch {
case len(c.namespace) == 0: case len(c.namespace) == 0:
return c.client.Fake. return c.client.Fake.
@ -333,7 +334,7 @@ func (c *dynamicResourceClient) Watch(opts metav1.ListOptions) (watch.Interface,
} }
// TODO: opts are currently ignored. // TODO: opts are currently ignored.
func (c *dynamicResourceClient) Patch(name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) {
var uncastRet runtime.Object var uncastRet runtime.Object
var err error var err error
switch { switch {

View File

@ -17,6 +17,7 @@ limitations under the License.
package fake package fake
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
@ -68,7 +69,7 @@ func TestList(t *testing.T) {
newUnstructured("group/version", "TheKind", "ns-foo", "name-baz"), newUnstructured("group/version", "TheKind", "ns-foo", "name-baz"),
newUnstructured("group2/version", "TheKind", "ns-foo", "name2-baz"), newUnstructured("group2/version", "TheKind", "ns-foo", "name2-baz"),
) )
listFirst, err := client.Resource(schema.GroupVersionResource{Group: "group", Version: "version", Resource: "thekinds"}).List(metav1.ListOptions{}) listFirst, err := client.Resource(schema.GroupVersionResource{Group: "group", Version: "version", Resource: "thekinds"}).List(context.TODO(), metav1.ListOptions{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -96,7 +97,7 @@ func (tc *patchTestCase) runner(t *testing.T) {
client := NewSimpleDynamicClient(runtime.NewScheme(), tc.object) client := NewSimpleDynamicClient(runtime.NewScheme(), tc.object)
resourceInterface := client.Resource(schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: testResource}).Namespace(testNamespace) resourceInterface := client.Resource(schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: testResource}).Namespace(testNamespace)
got, recErr := resourceInterface.Patch(testName, tc.patchType, tc.patchBytes, metav1.PatchOptions{}) got, recErr := resourceInterface.Patch(context.TODO(), testName, tc.patchType, tc.patchBytes, metav1.PatchOptions{})
if err := tc.verifyErr(recErr); err != nil { if err := tc.verifyErr(recErr); err != nil {
t.Error(err) t.Error(err)

View File

@ -17,6 +17,8 @@ limitations under the License.
package dynamic package dynamic
import ( import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
@ -29,15 +31,15 @@ type Interface interface {
} }
type ResourceInterface interface { type ResourceInterface interface {
Create(obj *unstructured.Unstructured, options metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) Create(ctx context.Context, obj *unstructured.Unstructured, options metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error)
Update(obj *unstructured.Unstructured, options metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) Update(ctx context.Context, obj *unstructured.Unstructured, options metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error)
UpdateStatus(obj *unstructured.Unstructured, options metav1.UpdateOptions) (*unstructured.Unstructured, error) UpdateStatus(ctx context.Context, obj *unstructured.Unstructured, options metav1.UpdateOptions) (*unstructured.Unstructured, error)
Delete(name string, options metav1.DeleteOptions, subresources ...string) error Delete(ctx context.Context, name string, options metav1.DeleteOptions, subresources ...string) error
DeleteCollection(options metav1.DeleteOptions, listOptions metav1.ListOptions) error DeleteCollection(ctx context.Context, options metav1.DeleteOptions, listOptions metav1.ListOptions) error
Get(name string, options metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) Get(ctx context.Context, name string, options metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error)
List(opts metav1.ListOptions) (*unstructured.UnstructuredList, error) List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error)
Watch(opts metav1.ListOptions) (watch.Interface, error) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, options metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, options metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error)
} }
type NamespaceableResourceInterface interface { type NamespaceableResourceInterface interface {

View File

@ -90,7 +90,7 @@ func (c *dynamicResourceClient) Namespace(ns string) ResourceInterface {
return &ret return &ret
} }
func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Create(ctx context.Context, obj *unstructured.Unstructured, opts metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) {
outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj) outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
if err != nil { if err != nil {
return nil, err return nil, err
@ -112,7 +112,7 @@ func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts meta
AbsPath(append(c.makeURLSegments(name), subresources...)...). AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(outBytes). Body(outBytes).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1). SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Do(context.TODO()) Do(ctx)
if err := result.Error(); err != nil { if err := result.Error(); err != nil {
return nil, err return nil, err
} }
@ -128,7 +128,7 @@ func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts meta
return uncastObj.(*unstructured.Unstructured), nil return uncastObj.(*unstructured.Unstructured), nil
} }
func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) {
accessor, err := meta.Accessor(obj) accessor, err := meta.Accessor(obj)
if err != nil { if err != nil {
return nil, err return nil, err
@ -147,7 +147,7 @@ func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts meta
AbsPath(append(c.makeURLSegments(name), subresources...)...). AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(outBytes). Body(outBytes).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1). SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Do(context.TODO()) Do(ctx)
if err := result.Error(); err != nil { if err := result.Error(); err != nil {
return nil, err return nil, err
} }
@ -163,7 +163,7 @@ func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts meta
return uncastObj.(*unstructured.Unstructured), nil return uncastObj.(*unstructured.Unstructured), nil
} }
func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) UpdateStatus(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*unstructured.Unstructured, error) {
accessor, err := meta.Accessor(obj) accessor, err := meta.Accessor(obj)
if err != nil { if err != nil {
return nil, err return nil, err
@ -183,7 +183,7 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt
AbsPath(append(c.makeURLSegments(name), "status")...). AbsPath(append(c.makeURLSegments(name), "status")...).
Body(outBytes). Body(outBytes).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1). SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Do(context.TODO()) Do(ctx)
if err := result.Error(); err != nil { if err := result.Error(); err != nil {
return nil, err return nil, err
} }
@ -199,7 +199,7 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt
return uncastObj.(*unstructured.Unstructured), nil return uncastObj.(*unstructured.Unstructured), nil
} }
func (c *dynamicResourceClient) Delete(name string, opts metav1.DeleteOptions, subresources ...string) error { func (c *dynamicResourceClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions, subresources ...string) error {
if len(name) == 0 { if len(name) == 0 {
return fmt.Errorf("name is required") return fmt.Errorf("name is required")
} }
@ -212,11 +212,11 @@ func (c *dynamicResourceClient) Delete(name string, opts metav1.DeleteOptions, s
Delete(). Delete().
AbsPath(append(c.makeURLSegments(name), subresources...)...). AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(deleteOptionsByte). Body(deleteOptionsByte).
Do(context.TODO()) Do(ctx)
return result.Error() return result.Error()
} }
func (c *dynamicResourceClient) DeleteCollection(opts metav1.DeleteOptions, listOptions metav1.ListOptions) error { func (c *dynamicResourceClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOptions metav1.ListOptions) error {
deleteOptionsByte, err := runtime.Encode(deleteOptionsCodec.LegacyCodec(schema.GroupVersion{Version: "v1"}), &opts) deleteOptionsByte, err := runtime.Encode(deleteOptionsCodec.LegacyCodec(schema.GroupVersion{Version: "v1"}), &opts)
if err != nil { if err != nil {
return err return err
@ -227,15 +227,15 @@ func (c *dynamicResourceClient) DeleteCollection(opts metav1.DeleteOptions, list
AbsPath(c.makeURLSegments("")...). AbsPath(c.makeURLSegments("")...).
Body(deleteOptionsByte). Body(deleteOptionsByte).
SpecificallyVersionedParams(&listOptions, dynamicParameterCodec, versionV1). SpecificallyVersionedParams(&listOptions, dynamicParameterCodec, versionV1).
Do(context.TODO()) Do(ctx)
return result.Error() return result.Error()
} }
func (c *dynamicResourceClient) Get(name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Get(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
if len(name) == 0 { if len(name) == 0 {
return nil, fmt.Errorf("name is required") return nil, fmt.Errorf("name is required")
} }
result := c.client.client.Get().AbsPath(append(c.makeURLSegments(name), subresources...)...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(context.TODO()) result := c.client.client.Get().AbsPath(append(c.makeURLSegments(name), subresources...)...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(ctx)
if err := result.Error(); err != nil { if err := result.Error(); err != nil {
return nil, err return nil, err
} }
@ -250,8 +250,8 @@ func (c *dynamicResourceClient) Get(name string, opts metav1.GetOptions, subreso
return uncastObj.(*unstructured.Unstructured), nil return uncastObj.(*unstructured.Unstructured), nil
} }
func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.UnstructuredList, error) { func (c *dynamicResourceClient) List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
result := c.client.client.Get().AbsPath(c.makeURLSegments("")...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(context.TODO()) result := c.client.client.Get().AbsPath(c.makeURLSegments("")...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(ctx)
if err := result.Error(); err != nil { if err := result.Error(); err != nil {
return nil, err return nil, err
} }
@ -274,14 +274,14 @@ func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.Uns
return list, nil return list, nil
} }
func (c *dynamicResourceClient) Watch(opts metav1.ListOptions) (watch.Interface, error) { func (c *dynamicResourceClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
opts.Watch = true opts.Watch = true
return c.client.client.Get().AbsPath(c.makeURLSegments("")...). return c.client.client.Get().AbsPath(c.makeURLSegments("")...).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1). SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Watch(context.TODO()) Watch(ctx)
} }
func (c *dynamicResourceClient) Patch(name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) { func (c *dynamicResourceClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) {
if len(name) == 0 { if len(name) == 0 {
return nil, fmt.Errorf("name is required") return nil, fmt.Errorf("name is required")
} }
@ -290,7 +290,7 @@ func (c *dynamicResourceClient) Patch(name string, pt types.PatchType, data []by
AbsPath(append(c.makeURLSegments(name), subresources...)...). AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(data). Body(data).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1). SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Do(context.TODO()) Do(ctx)
if err := result.Error(); err != nil { if err := result.Error(); err != nil {
return nil, err return nil, err
} }

View File

@ -19,6 +19,7 @@ package main
import ( import (
"bufio" "bufio"
"context"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -108,7 +109,7 @@ func main() {
// Create Deployment // Create Deployment
fmt.Println("Creating deployment...") fmt.Println("Creating deployment...")
result, err := client.Resource(deploymentRes).Namespace(namespace).Create(deployment, metav1.CreateOptions{}) result, err := client.Resource(deploymentRes).Namespace(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -133,7 +134,7 @@ func main() {
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error { retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
// Retrieve the latest version of Deployment before attempting update // Retrieve the latest version of Deployment before attempting update
// RetryOnConflict uses exponential backoff to avoid exhausting the apiserver // RetryOnConflict uses exponential backoff to avoid exhausting the apiserver
result, getErr := client.Resource(deploymentRes).Namespace(namespace).Get("demo-deployment", metav1.GetOptions{}) result, getErr := client.Resource(deploymentRes).Namespace(namespace).Get(context.TODO(), "demo-deployment", metav1.GetOptions{})
if getErr != nil { if getErr != nil {
panic(fmt.Errorf("failed to get latest version of Deployment: %v", getErr)) panic(fmt.Errorf("failed to get latest version of Deployment: %v", getErr))
} }
@ -157,7 +158,7 @@ func main() {
panic(err) panic(err)
} }
_, updateErr := client.Resource(deploymentRes).Namespace(namespace).Update(result, metav1.UpdateOptions{}) _, updateErr := client.Resource(deploymentRes).Namespace(namespace).Update(context.TODO(), result, metav1.UpdateOptions{})
return updateErr return updateErr
}) })
if retryErr != nil { if retryErr != nil {
@ -168,7 +169,7 @@ func main() {
// List Deployments // List Deployments
prompt() prompt()
fmt.Printf("Listing deployments in namespace %q:\n", apiv1.NamespaceDefault) fmt.Printf("Listing deployments in namespace %q:\n", apiv1.NamespaceDefault)
list, err := client.Resource(deploymentRes).Namespace(namespace).List(metav1.ListOptions{}) list, err := client.Resource(deploymentRes).Namespace(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -188,7 +189,7 @@ func main() {
deleteOptions := metav1.DeleteOptions{ deleteOptions := metav1.DeleteOptions{
PropagationPolicy: &deletePolicy, PropagationPolicy: &deletePolicy,
} }
if err := client.Resource(deploymentRes).Namespace(namespace).Delete("demo-deployment", deleteOptions); err != nil { if err := client.Resource(deploymentRes).Namespace(namespace).Delete(context.TODO(), "demo-deployment", deleteOptions); err != nil {
panic(err) panic(err)
} }