utils/client.go
func ClientForResource(client dynamic.Interface, mapper meta.RESTMapper, obj runtime.Object, defNs string) (dynamic.ResourceInterface, error) {
	gvk := obj.GetObjectKind().GroupVersionKind()

	mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
	if err != nil {
		return nil, err
	}

	rc := client.Resource(mapping.Resource)

	switch mapping.Scope.Name() {
	case meta.RESTScopeNameRoot:
		return rc, nil
	case meta.RESTScopeNameNamespace:
		meta, err := meta.Accessor(obj)
		if err != nil {
			return nil, err
		}
		namespace := meta.GetNamespace()
		if namespace == "" {
			namespace = defNs
		}
		return rc.Namespace(namespace), nil
	default:
		return nil, fmt.Errorf("unexpected resource scope %q", mapping.Scope)
	}
}
func ClientForResource(client dynamic.Interface, mapper meta.RESTMapper, obj runtime.Object, defNs string) (dynamic.ResourceInterface, error) {
	gvk := obj.GetObjectKind().GroupVersionKind()

	mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
	if err != nil {
		return nil, err
	}

	rc := client.Resource(mapping.Resource)

	switch mapping.Scope.Name() {
	case meta.RESTScopeNameRoot:
		return rc, nil
	case meta.RESTScopeNameNamespace:
		meta, err := meta.Accessor(obj)
		if err != nil {
			return nil, err
		}
		namespace := meta.GetNamespace()
		if namespace == "" {
			namespace = defNs
		}
		return rc.Namespace(namespace), nil
	default:
		return nil, fmt.Errorf("unexpected resource scope %q", mapping.Scope)
	}
}
func ClientForResource ( client dynamic . Interface , mapper meta . RESTMapper , obj runtime . Object , defNs string ) ( dynamic . ResourceInterface , error ) { gvk := obj . GetObjectKind ( ) . GroupVersionKind ( ) mapping , err := mapper . RESTMapping ( gvk . GroupKind ( ) , gvk . Version ) if err != nil { return nil , err } rc := client . Resource ( mapping . Resource ) switch mapping . Scope . Name ( ) { case meta . RESTScopeNameRoot : return rc , nil case meta . RESTScopeNameNamespace : meta , err := meta . Accessor ( obj ) if err != nil { return nil , err } namespace := meta . GetNamespace ( ) if namespace == " " { namespace = defNs } return rc . Namespace ( namespace ) , nil default : return nil , fmt . Errorf ( " " , mapping . Scope ) } }
// ClientForResource returns the ResourceClient for a given object
ClientForResource returns the ResourceClient for a given object