mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-26 15:12:06 +00:00
client-go metadata: plumb context
Kubernetes-commit: cb4ee9334b744404e670ea2c28a3093a3cd67844
This commit is contained in:
parent
b173caf598
commit
5883a775b1
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package fake
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@ -214,7 +215,7 @@ func (c *metadataResourceClient) UpdateStatus(obj *metav1.PartialObjectMetadata,
|
||||
}
|
||||
|
||||
// Delete records the object deletion and processes it via the reactor.
|
||||
func (c *metadataResourceClient) Delete(name string, opts *metav1.DeleteOptions, subresources ...string) error {
|
||||
func (c *metadataResourceClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions, subresources ...string) error {
|
||||
var err error
|
||||
switch {
|
||||
case len(c.namespace) == 0 && len(subresources) == 0:
|
||||
@ -238,7 +239,7 @@ func (c *metadataResourceClient) Delete(name string, opts *metav1.DeleteOptions,
|
||||
}
|
||||
|
||||
// DeleteCollection records the object collection deletion and processes it via the reactor.
|
||||
func (c *metadataResourceClient) DeleteCollection(opts *metav1.DeleteOptions, listOptions metav1.ListOptions) error {
|
||||
func (c *metadataResourceClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOptions metav1.ListOptions) error {
|
||||
var err error
|
||||
switch {
|
||||
case len(c.namespace) == 0:
|
||||
@ -255,7 +256,7 @@ func (c *metadataResourceClient) DeleteCollection(opts *metav1.DeleteOptions, li
|
||||
}
|
||||
|
||||
// Get records the object retrieval and processes it via the reactor.
|
||||
func (c *metadataResourceClient) Get(name string, opts metav1.GetOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
func (c *metadataResourceClient) Get(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
var uncastRet runtime.Object
|
||||
var err error
|
||||
switch {
|
||||
@ -290,7 +291,7 @@ func (c *metadataResourceClient) Get(name string, opts metav1.GetOptions, subres
|
||||
}
|
||||
|
||||
// List records the object deletion and processes it via the reactor.
|
||||
func (c *metadataResourceClient) List(opts metav1.ListOptions) (*metav1.PartialObjectMetadataList, error) {
|
||||
func (c *metadataResourceClient) List(ctx context.Context, opts metav1.ListOptions) (*metav1.PartialObjectMetadataList, error) {
|
||||
var obj runtime.Object
|
||||
var err error
|
||||
switch {
|
||||
@ -337,7 +338,7 @@ func (c *metadataResourceClient) List(opts metav1.ListOptions) (*metav1.PartialO
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (c *metadataResourceClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
|
||||
func (c *metadataResourceClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
|
||||
switch {
|
||||
case len(c.namespace) == 0:
|
||||
return c.client.Fake.
|
||||
@ -353,7 +354,7 @@ func (c *metadataResourceClient) Watch(opts metav1.ListOptions) (watch.Interface
|
||||
}
|
||||
|
||||
// Patch records the object patch and processes it via the reactor.
|
||||
func (c *metadataResourceClient) Patch(name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
func (c *metadataResourceClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
var uncastRet runtime.Object
|
||||
var err error
|
||||
switch {
|
||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package fake
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
@ -72,7 +73,7 @@ func TestList(t *testing.T) {
|
||||
newPartialObjectMetadata("group/version", "TheKind", "ns-foo", "name-baz"),
|
||||
newPartialObjectMetadata("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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -100,7 +101,7 @@ func (tc *patchTestCase) runner(t *testing.T) {
|
||||
client := NewSimpleMetadataClient(scheme, tc.object)
|
||||
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 {
|
||||
t.Error(err)
|
||||
|
@ -17,6 +17,8 @@ limitations under the License.
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@ -32,12 +34,12 @@ type Interface interface {
|
||||
// ResourceInterface contains the set of methods that may be invoked on objects by their metadata.
|
||||
// Update is not supported by the server, but Patch can be used for the actions Update would handle.
|
||||
type ResourceInterface interface {
|
||||
Delete(name string, options *metav1.DeleteOptions, subresources ...string) error
|
||||
DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error
|
||||
Get(name string, options metav1.GetOptions, subresources ...string) (*metav1.PartialObjectMetadata, error)
|
||||
List(opts metav1.ListOptions) (*metav1.PartialObjectMetadataList, error)
|
||||
Watch(opts metav1.ListOptions) (watch.Interface, error)
|
||||
Patch(name string, pt types.PatchType, data []byte, options metav1.PatchOptions, subresources ...string) (*metav1.PartialObjectMetadata, error)
|
||||
Delete(ctx context.Context, name string, options metav1.DeleteOptions, subresources ...string) error
|
||||
DeleteCollection(ctx context.Context, options metav1.DeleteOptions, listOptions metav1.ListOptions) error
|
||||
Get(ctx context.Context, name string, options metav1.GetOptions, subresources ...string) (*metav1.PartialObjectMetadata, error)
|
||||
List(ctx context.Context, opts metav1.ListOptions) (*metav1.PartialObjectMetadataList, error)
|
||||
Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
|
||||
Patch(ctx context.Context, name string, pt types.PatchType, data []byte, options metav1.PatchOptions, subresources ...string) (*metav1.PartialObjectMetadata, error)
|
||||
}
|
||||
|
||||
// Getter handles both namespaced and non-namespaced resource types consistently.
|
||||
|
@ -120,14 +120,11 @@ func (c *client) Namespace(ns string) ResourceInterface {
|
||||
}
|
||||
|
||||
// Delete removes the provided resource from the server.
|
||||
func (c *client) Delete(name string, opts *metav1.DeleteOptions, subresources ...string) error {
|
||||
func (c *client) Delete(ctx context.Context, name string, opts metav1.DeleteOptions, subresources ...string) error {
|
||||
if len(name) == 0 {
|
||||
return fmt.Errorf("name is required")
|
||||
}
|
||||
if opts == nil {
|
||||
opts = &metav1.DeleteOptions{}
|
||||
}
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@ -136,16 +133,13 @@ func (c *client) Delete(name string, opts *metav1.DeleteOptions, subresources ..
|
||||
Delete().
|
||||
AbsPath(append(c.makeURLSegments(name), subresources...)...).
|
||||
Body(deleteOptionsByte).
|
||||
Do(context.TODO())
|
||||
Do(ctx)
|
||||
return result.Error()
|
||||
}
|
||||
|
||||
// DeleteCollection triggers deletion of all resources in the specified scope (namespace or cluster).
|
||||
func (c *client) DeleteCollection(opts *metav1.DeleteOptions, listOptions metav1.ListOptions) error {
|
||||
if opts == nil {
|
||||
opts = &metav1.DeleteOptions{}
|
||||
}
|
||||
deleteOptionsByte, err := runtime.Encode(deleteOptionsCodec.LegacyCodec(schema.GroupVersion{Version: "v1"}), opts)
|
||||
func (c *client) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOptions metav1.ListOptions) error {
|
||||
deleteOptionsByte, err := runtime.Encode(deleteOptionsCodec.LegacyCodec(schema.GroupVersion{Version: "v1"}), &opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -155,19 +149,19 @@ func (c *client) DeleteCollection(opts *metav1.DeleteOptions, listOptions metav1
|
||||
AbsPath(c.makeURLSegments("")...).
|
||||
Body(deleteOptionsByte).
|
||||
SpecificallyVersionedParams(&listOptions, dynamicParameterCodec, versionV1).
|
||||
Do(context.TODO())
|
||||
Do(ctx)
|
||||
return result.Error()
|
||||
}
|
||||
|
||||
// Get returns the resource with name from the specified scope (namespace or cluster).
|
||||
func (c *client) Get(name string, opts metav1.GetOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
func (c *client) Get(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
if len(name) == 0 {
|
||||
return nil, fmt.Errorf("name is required")
|
||||
}
|
||||
result := c.client.client.Get().AbsPath(append(c.makeURLSegments(name), subresources...)...).
|
||||
SetHeader("Accept", "application/vnd.kubernetes.protobuf;as=PartialObjectMetadata;g=meta.k8s.io;v=v1,application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1,application/json").
|
||||
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
|
||||
Do(context.TODO())
|
||||
Do(ctx)
|
||||
if err := result.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -199,11 +193,11 @@ func (c *client) Get(name string, opts metav1.GetOptions, subresources ...string
|
||||
}
|
||||
|
||||
// List returns all resources within the specified scope (namespace or cluster).
|
||||
func (c *client) List(opts metav1.ListOptions) (*metav1.PartialObjectMetadataList, error) {
|
||||
func (c *client) List(ctx context.Context, opts metav1.ListOptions) (*metav1.PartialObjectMetadataList, error) {
|
||||
result := c.client.client.Get().AbsPath(c.makeURLSegments("")...).
|
||||
SetHeader("Accept", "application/vnd.kubernetes.protobuf;as=PartialObjectMetadataList;g=meta.k8s.io;v=v1,application/json;as=PartialObjectMetadataList;g=meta.k8s.io;v=v1,application/json").
|
||||
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
|
||||
Do(context.TODO())
|
||||
Do(ctx)
|
||||
if err := result.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -232,7 +226,7 @@ func (c *client) List(opts metav1.ListOptions) (*metav1.PartialObjectMetadataLis
|
||||
}
|
||||
|
||||
// Watch finds all changes to the resources in the specified scope (namespace or cluster).
|
||||
func (c *client) Watch(opts metav1.ListOptions) (watch.Interface, error) {
|
||||
func (c *client) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
|
||||
var timeout time.Duration
|
||||
if opts.TimeoutSeconds != nil {
|
||||
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
|
||||
@ -243,11 +237,11 @@ func (c *client) Watch(opts metav1.ListOptions) (watch.Interface, error) {
|
||||
SetHeader("Accept", "application/vnd.kubernetes.protobuf;as=PartialObjectMetadata;g=meta.k8s.io;v=v1,application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1,application/json").
|
||||
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
|
||||
Timeout(timeout).
|
||||
Watch(context.TODO())
|
||||
Watch(ctx)
|
||||
}
|
||||
|
||||
// Patch modifies the named resource in the specified scope (namespace or cluster).
|
||||
func (c *client) Patch(name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
func (c *client) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*metav1.PartialObjectMetadata, error) {
|
||||
if len(name) == 0 {
|
||||
return nil, fmt.Errorf("name is required")
|
||||
}
|
||||
@ -257,7 +251,7 @@ func (c *client) Patch(name string, pt types.PatchType, data []byte, opts metav1
|
||||
Body(data).
|
||||
SetHeader("Accept", "application/vnd.kubernetes.protobuf;as=PartialObjectMetadata;g=meta.k8s.io;v=v1,application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1,application/json").
|
||||
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
|
||||
Do(context.TODO())
|
||||
Do(ctx)
|
||||
if err := result.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@ -72,7 +73,7 @@ func TestClient(t *testing.T) {
|
||||
})
|
||||
},
|
||||
want: func(t *testing.T, client *Client) {
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get("name", metav1.GetOptions{})
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -120,7 +121,7 @@ func TestClient(t *testing.T) {
|
||||
})
|
||||
},
|
||||
want: func(t *testing.T, client *Client) {
|
||||
objs, err := client.Resource(gvr).Namespace("ns").List(metav1.ListOptions{})
|
||||
objs, err := client.Resource(gvr).Namespace("ns").List(context.TODO(), metav1.ListOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -162,7 +163,7 @@ func TestClient(t *testing.T) {
|
||||
})
|
||||
},
|
||||
want: func(t *testing.T, client *Client) {
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get("name", metav1.GetOptions{})
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
|
||||
if err == nil || !runtime.IsMissingKind(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -191,7 +192,7 @@ func TestClient(t *testing.T) {
|
||||
})
|
||||
},
|
||||
want: func(t *testing.T, client *Client) {
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get("name", metav1.GetOptions{})
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
|
||||
if err == nil || !runtime.IsMissingVersion(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -219,7 +220,7 @@ func TestClient(t *testing.T) {
|
||||
})
|
||||
},
|
||||
want: func(t *testing.T, client *Client) {
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get("name", metav1.GetOptions{})
|
||||
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
|
||||
if err == nil || !strings.Contains(err.Error(), "object does not appear to match the ObjectMeta schema") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package metadatainformer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -124,13 +125,13 @@ func NewFilteredMetadataInformer(client metadata.Interface, gvr schema.GroupVers
|
||||
if tweakListOptions != nil {
|
||||
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) {
|
||||
if tweakListOptions != nil {
|
||||
tweakListOptions(&options)
|
||||
}
|
||||
return client.Resource(gvr).Namespace(namespace).Watch(options)
|
||||
return client.Resource(gvr).Namespace(namespace).Watch(context.TODO(), options)
|
||||
},
|
||||
},
|
||||
&metav1.PartialObjectMetadata{},
|
||||
|
@ -103,7 +103,7 @@ func TestMetadataSharedInformerFactory(t *testing.T) {
|
||||
gvr: schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "deployments"},
|
||||
existingObj: newPartialObjectMetadata("extensions/v1beta1", "Deployment", "ns-foo", "name-foo"),
|
||||
trigger: func(gvr schema.GroupVersionResource, ns string, fakeClient *fake.FakeMetadataClient, testObject *metav1.PartialObjectMetadata) *metav1.PartialObjectMetadata {
|
||||
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 {
|
||||
t.Error(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user