make dynamic client slightly easier to use

This commit is contained in:
David Eads 2018-04-27 11:29:05 -04:00
parent 03c5f298f3
commit 5ff923c7f9
10 changed files with 28 additions and 21 deletions

View File

@ -342,7 +342,7 @@ func (d *namespacedResourcesDeleter) deleteCollection(gvr schema.GroupVersionRes
// namespace itself.
background := metav1.DeletePropagationBackground
opts := &metav1.DeleteOptions{PropagationPolicy: &background}
err := d.dynamicClient.NamespacedResource(gvr, namespace).DeleteCollection(opts, metav1.ListOptions{})
err := d.dynamicClient.Resource(gvr).Namespace(namespace).DeleteCollection(opts, metav1.ListOptions{})
if err == nil {
return true, nil
@ -378,7 +378,7 @@ func (d *namespacedResourcesDeleter) listCollection(gvr schema.GroupVersionResou
return nil, false, nil
}
unstructuredList, err := d.dynamicClient.NamespacedResource(gvr, namespace).List(metav1.ListOptions{IncludeUninitialized: true})
unstructuredList, err := d.dynamicClient.Resource(gvr).Namespace(namespace).List(metav1.ListOptions{IncludeUninitialized: true})
if err == nil {
return unstructuredList, true, nil
}
@ -412,7 +412,7 @@ func (d *namespacedResourcesDeleter) deleteEachItem(gvr schema.GroupVersionResou
for _, item := range unstructuredList.Items {
background := metav1.DeletePropagationBackground
opts := &metav1.DeleteOptions{PropagationPolicy: &background}
if err = d.dynamicClient.NamespacedResource(gvr, namespace).Delete(item.GetName(), opts); err != nil && !errors.IsNotFound(err) && !errors.IsMethodNotSupported(err) {
if err = d.dynamicClient.Resource(gvr).Namespace(namespace).Delete(item.GetName(), opts); err != nil && !errors.IsNotFound(err) && !errors.IsMethodNotSupported(err) {
return err
}
}

View File

@ -72,9 +72,9 @@ func NewNamespacedCustomResourceClient(ns string, client dynamic.DynamicInterfac
gvr := schema.GroupVersionResource{Group: crd.Spec.Group, Version: crd.Spec.Version, Resource: crd.Spec.Names.Plural}
if crd.Spec.Scope != apiextensionsv1beta1.ClusterScoped {
return client.NamespacedResource(gvr, ns)
return client.Resource(gvr).Namespace(ns)
}
return client.ClusterResource(gvr)
return client.Resource(gvr)
}
func NewNamespacedCustomResourceStatusClient(ns string, client dynamic.DynamicInterface, crd *apiextensionsv1beta1.CustomResourceDefinition) dynamic.DynamicResourceInterface {

View File

@ -217,9 +217,9 @@ func checkForWatchCachePrimed(crd *apiextensionsv1beta1.CustomResourceDefinition
gvr := schema.GroupVersionResource{Group: crd.Spec.Group, Version: crd.Spec.Version, Resource: crd.Spec.Names.Plural}
var resourceClient dynamic.DynamicResourceInterface
if crd.Spec.Scope != apiextensionsv1beta1.ClusterScoped {
resourceClient = dynamicClientSet.NamespacedResource(gvr, ns)
resourceClient = dynamicClientSet.Resource(gvr).Namespace(ns)
} else {
resourceClient = dynamicClientSet.ClusterResource(gvr)
resourceClient = dynamicClientSet.Resource(gvr)
}
initialList, err := resourceClient.List(metav1.ListOptions{})

View File

@ -32,8 +32,7 @@ import (
)
type DynamicInterface interface {
ClusterResource(resource schema.GroupVersionResource) DynamicResourceInterface
NamespacedResource(resource schema.GroupVersionResource, namespace string) DynamicResourceInterface
Resource(resource schema.GroupVersionResource) NamespaceableDynamicResourceInterface
// Deprecated, this isn't how we want to do it
ClusterSubresource(resource schema.GroupVersionResource, subresource string) DynamicResourceInterface
@ -53,6 +52,11 @@ type DynamicResourceInterface interface {
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (*unstructured.Unstructured, error)
}
type NamespaceableDynamicResourceInterface interface {
Namespace(string) DynamicResourceInterface
DynamicResourceInterface
}
type dynamicClient struct {
client *rest.RESTClient
}
@ -86,12 +90,9 @@ type dynamicResourceClient struct {
subresource string
}
func (c *dynamicClient) ClusterResource(resource schema.GroupVersionResource) DynamicResourceInterface {
func (c *dynamicClient) Resource(resource schema.GroupVersionResource) NamespaceableDynamicResourceInterface {
return &dynamicResourceClient{client: c, resource: resource}
}
func (c *dynamicClient) NamespacedResource(resource schema.GroupVersionResource, namespace string) DynamicResourceInterface {
return &dynamicResourceClient{client: c, resource: resource, namespace: namespace}
}
func (c *dynamicClient) ClusterSubresource(resource schema.GroupVersionResource, subresource string) DynamicResourceInterface {
return &dynamicResourceClient{client: c, resource: resource, subresource: subresource}
@ -100,6 +101,12 @@ func (c *dynamicClient) NamespacedSubresource(resource schema.GroupVersionResour
return &dynamicResourceClient{client: c, resource: resource, namespace: namespace, subresource: subresource}
}
func (c *dynamicResourceClient) Namespace(ns string) DynamicResourceInterface {
ret := *c
ret.namespace = ns
return &ret
}
func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
if len(c.subresource) > 0 {
return nil, fmt.Errorf("create not supported for subresources")

View File

@ -391,7 +391,7 @@ func TestSampleAPIServer(f *framework.Framework, image string) {
if !ok {
framework.Failf("could not find group version resource for dynamic client and wardle/flunders.")
}
dynamicClient := f.DynamicClient.NamespacedResource(gvr, namespace)
dynamicClient := f.DynamicClient.Resource(gvr).Namespace(namespace)
// kubectl create -f flunders-1.yaml
// Request Body: {"apiVersion":"wardle.k8s.io/v1alpha1","kind":"Flunder","metadata":{"labels":{"sample-label":"true"},"name":"test-flunder","namespace":"default"}}

View File

@ -158,9 +158,9 @@ func newNamespacedCustomResourceClient(ns string, client dynamic.DynamicInterfac
gvr := schema.GroupVersionResource{Group: crd.Spec.Group, Version: crd.Spec.Version, Resource: crd.Spec.Names.Plural}
if crd.Spec.Scope != apiextensionsv1beta1.ClusterScoped {
return client.NamespacedResource(gvr, ns)
return client.Resource(gvr).Namespace(ns)
} else {
return client.ClusterResource(gvr)
return client.Resource(gvr)
}
}

View File

@ -924,7 +924,7 @@ var _ = SIGDescribe("Garbage collector", func() {
// Get a client for the custom resource.
gvr := schema.GroupVersionResource{Group: definition.Spec.Group, Version: definition.Spec.Version, Resource: definition.Spec.Names.Plural}
resourceClient := f.DynamicClient.ClusterResource(gvr)
resourceClient := f.DynamicClient.Resource(gvr)
apiVersion := definition.Spec.Group + "/" + definition.Spec.Version

View File

@ -83,7 +83,7 @@ func CreateTestCRD(f *Framework) (*TestCrd, error) {
}
gvr := schema.GroupVersionResource{Group: crd.Spec.Group, Version: crd.Spec.Version, Resource: crd.Spec.Names.Plural}
resourceClient := dynamicClient.NamespacedResource(gvr, f.Namespace.Name)
resourceClient := dynamicClient.Resource(gvr).Namespace(f.Namespace.Name)
testcrd.ApiExtensionClient = apiExtensionClient
testcrd.Crd = crd

View File

@ -508,7 +508,7 @@ func SkipUnlessServerVersionGTE(v *utilversion.Version, c discovery.ServerVersio
}
func SkipIfMissingResource(dynamicClient dynamic.DynamicInterface, gvr schema.GroupVersionResource, namespace string) {
resourceClient := dynamicClient.NamespacedResource(gvr, namespace)
resourceClient := dynamicClient.Resource(gvr).Namespace(namespace)
_, err := resourceClient.List(metav1.ListOptions{})
if err != nil {
// not all resources support list, so we ignore those
@ -1258,7 +1258,7 @@ func hasRemainingContent(c clientset.Interface, dynamicClient dynamic.DynamicInt
// dump how many of resource type is on the server in a log.
for gvr := range groupVersionResources {
// get a client for this group version...
dynamicClient := dynamicClient.NamespacedResource(gvr, namespace)
dynamicClient := dynamicClient.Resource(gvr).Namespace(namespace)
if err != nil {
// not all resource types support list, so some errors here are normal depending on the resource type.
Logf("namespace: %s, unable to get client - gvr: %v, error: %v", namespace, gvr, err)

View File

@ -183,7 +183,7 @@ func createRandomCustomResourceDefinition(
// Get a client for the custom resource.
gvr := schema.GroupVersionResource{Group: definition.Spec.Group, Version: definition.Spec.Version, Resource: definition.Spec.Names.Plural}
resourceClient := dynamicClient.NamespacedResource(gvr, namespace)
resourceClient := dynamicClient.Resource(gvr).Namespace(namespace)
return definition, resourceClient
}