mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
provide a default field selector for name and namespace
This commit is contained in:
parent
6a4203eb4b
commit
9002dfcd0a
@ -19,6 +19,7 @@ limitations under the License.
|
|||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -26,6 +27,20 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/conversion"
|
"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
|
// 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.
|
// 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) {
|
func JSONKeyMapper(key string, sourceTag, destTag reflect.StructTag) (string, string) {
|
||||||
|
@ -454,11 +454,11 @@ func (s *Scheme) Convert(in, out interface{}, context interface{}) error {
|
|||||||
// versioned representation to an unversioned one.
|
// versioned representation to an unversioned one.
|
||||||
func (s *Scheme) ConvertFieldLabel(version, kind, label, value string) (string, string, error) {
|
func (s *Scheme) ConvertFieldLabel(version, kind, label, value string) (string, string, error) {
|
||||||
if s.fieldLabelConversionFuncs[version] == nil {
|
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]
|
conversionFunc, ok := s.fieldLabelConversionFuncs[version][kind]
|
||||||
if !ok {
|
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)
|
return conversionFunc(label, value)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user