provide a default field selector for name and namespace

This commit is contained in:
David Eads 2017-08-23 10:40:02 -04:00
parent 6a4203eb4b
commit 9002dfcd0a
2 changed files with 17 additions and 2 deletions

View File

@ -19,6 +19,7 @@ limitations under the License.
package runtime
import (
"fmt"
"reflect"
"strconv"
"strings"
@ -26,6 +27,20 @@ import (
"k8s.io/apimachinery/pkg/conversion"
)
// DefaultFieldSelectorConversion auto-accepts metav1 values for name and namespace.
// A cluster scoped resource specifying namespace empty works fine and specifying a particular
// namespace will return no results, as expected.
func DefaultMetaV1FieldSelectorConversion(label, value string) (string, string, error) {
switch label {
case "metadata.name":
return label, value, nil
case "metadata.namespace":
return label, value, nil
default:
return "", "", fmt.Errorf("%q is not a known field selector: only %q, %q", label, "metadata.name", "metadata.namespace")
}
}
// JSONKeyMapper uses the struct tags on a conversion to determine the key value for
// the other side. Use when mapping from a map[string]* to a struct or vice versa.
func JSONKeyMapper(key string, sourceTag, destTag reflect.StructTag) (string, string) {

View File

@ -454,11 +454,11 @@ func (s *Scheme) Convert(in, out interface{}, context interface{}) error {
// versioned representation to an unversioned one.
func (s *Scheme) ConvertFieldLabel(version, kind, label, value string) (string, string, error) {
if s.fieldLabelConversionFuncs[version] == nil {
return "", "", fmt.Errorf("No field label conversion function found for version: %s", version)
return DefaultMetaV1FieldSelectorConversion(label, value)
}
conversionFunc, ok := s.fieldLabelConversionFuncs[version][kind]
if !ok {
return "", "", fmt.Errorf("No field label conversion function found for version %s and kind %s", version, kind)
return DefaultMetaV1FieldSelectorConversion(label, value)
}
return conversionFunc(label, value)
}