From 9002dfcd0a87db80f5c7f22b5a5b5671f3b3fcb5 Mon Sep 17 00:00:00 2001 From: David Eads Date: Wed, 23 Aug 2017 10:40:02 -0400 Subject: [PATCH 1/2] provide a default field selector for name and namespace --- .../k8s.io/apimachinery/pkg/runtime/conversion.go | 15 +++++++++++++++ .../src/k8s.io/apimachinery/pkg/runtime/scheme.go | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/conversion.go b/staging/src/k8s.io/apimachinery/pkg/runtime/conversion.go index 8eedffc9c34..afe4fab15e8 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/conversion.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/conversion.go @@ -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) { diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/scheme.go b/staging/src/k8s.io/apimachinery/pkg/runtime/scheme.go index ee78705b79c..c3d4b7f5f8e 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/scheme.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/scheme.go @@ -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) } From 9daf55e1735385d5b81b50ccd0d9a55522a026b8 Mon Sep 17 00:00:00 2001 From: David Eads Date: Wed, 23 Aug 2017 10:52:22 -0400 Subject: [PATCH 2/2] remove unnecessary field conversions --- federation/apis/core/v1/conversion.go | 20 ---------- federation/apis/federation/v1beta1/BUILD | 1 - .../apis/federation/v1beta1/conversion.go | 36 ----------------- .../apis/federation/v1beta1/register.go | 2 +- pkg/api/v1/conversion.go | 39 ------------------- pkg/apis/apps/v1beta1/conversion.go | 12 ------ pkg/apis/apps/v1beta2/conversion.go | 24 ------------ pkg/apis/certificates/v1beta1/BUILD | 1 - pkg/apis/certificates/v1beta1/conversion.go | 38 ------------------ pkg/apis/certificates/v1beta1/register.go | 2 +- pkg/apis/extensions/v1beta1/conversion.go | 18 --------- 11 files changed, 2 insertions(+), 191 deletions(-) delete mode 100644 federation/apis/federation/v1beta1/conversion.go delete mode 100644 pkg/apis/certificates/v1beta1/conversion.go diff --git a/federation/apis/core/v1/conversion.go b/federation/apis/core/v1/conversion.go index 8ce8f3a55ac..2e9ed1e7411 100644 --- a/federation/apis/core/v1/conversion.go +++ b/federation/apis/core/v1/conversion.go @@ -17,8 +17,6 @@ limitations under the License. package v1 import ( - "fmt" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/kubernetes/pkg/api/v1" ) @@ -59,24 +57,6 @@ func addConversionFuncs(scheme *runtime.Scheme) error { return err } - // Add field label conversions for kinds having selectable nothing but ObjectMeta fields. - for _, kind := range []string{ - "Service", - } { - err = scheme.AddFieldLabelConversionFunc("v1", kind, - func(label, value string) (string, string, error) { - switch label { - case "metadata.namespace", - "metadata.name": - return label, value, nil - default: - return "", "", fmt.Errorf("field label %q not supported for %q", label, kind) - } - }) - if err != nil { - return err - } - } if err := v1.AddFieldLabelConversionsForEvent(scheme); err != nil { return nil } diff --git a/federation/apis/federation/v1beta1/BUILD b/federation/apis/federation/v1beta1/BUILD index 9db2ea32cf7..a659903c83f 100644 --- a/federation/apis/federation/v1beta1/BUILD +++ b/federation/apis/federation/v1beta1/BUILD @@ -8,7 +8,6 @@ load( go_library( name = "go_default_library", srcs = [ - "conversion.go", "defaults.go", "doc.go", "generated.pb.go", diff --git a/federation/apis/federation/v1beta1/conversion.go b/federation/apis/federation/v1beta1/conversion.go deleted file mode 100644 index a6a8a867e8e..00000000000 --- a/federation/apis/federation/v1beta1/conversion.go +++ /dev/null @@ -1,36 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1beta1 - -import ( - "fmt" - - "k8s.io/apimachinery/pkg/runtime" -) - -func addConversionFuncs(scheme *runtime.Scheme) error { - return scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.String(), "Cluster", - func(label, value string) (string, string, error) { - switch label { - case "metadata.name": - return label, value, nil - default: - return "", "", fmt.Errorf("field label not supported: %s", label) - } - }, - ) -} diff --git a/federation/apis/federation/v1beta1/register.go b/federation/apis/federation/v1beta1/register.go index 10932175f58..b14f5b1b061 100644 --- a/federation/apis/federation/v1beta1/register.go +++ b/federation/apis/federation/v1beta1/register.go @@ -40,7 +40,7 @@ func init() { // We only register manually written functions here. The registration of the // generated functions takes place in the generated files. The separation // makes the code compile even when the generated files are missing. - localSchemeBuilder.Register(addKnownTypes, addDefaultingFuncs, addConversionFuncs) + localSchemeBuilder.Register(addKnownTypes, addDefaultingFuncs) } func addKnownTypes(scheme *runtime.Scheme) error { diff --git a/pkg/api/v1/conversion.go b/pkg/api/v1/conversion.go index 72b07cffd58..1768b2fbe74 100644 --- a/pkg/api/v1/conversion.go +++ b/pkg/api/v1/conversion.go @@ -154,32 +154,6 @@ func addConversionFuncs(scheme *runtime.Scheme) error { return err } - // Add field label conversions for kinds having selectable nothing but v1.ObjectMeta fields. - for _, k := range []string{ - "Endpoints", - "ResourceQuota", - "PersistentVolumeClaim", - "Service", - "ServiceAccount", - "ConfigMap", - } { - kind := k // don't close over range variables - err = scheme.AddFieldLabelConversionFunc("v1", kind, - func(label, value string) (string, string, error) { - switch label { - case "metadata.namespace", - "metadata.name": - return label, value, nil - default: - return "", "", fmt.Errorf("field label %q not supported for %q", label, kind) - } - }, - ) - if err != nil { - return err - } - } - // Add field conversion funcs. err = scheme.AddFieldLabelConversionFunc("v1", "Pod", func(label, value string) (string, string, error) { @@ -236,19 +210,6 @@ func addConversionFuncs(scheme *runtime.Scheme) error { if err != nil { return err } - err = scheme.AddFieldLabelConversionFunc("v1", "PersistentVolume", - func(label, value string) (string, string, error) { - switch label { - case "metadata.name": - return label, value, nil - default: - return "", "", fmt.Errorf("field label not supported: %s", label) - } - }, - ) - if err != nil { - return err - } if err := AddFieldLabelConversionsForEvent(scheme); err != nil { return err } diff --git a/pkg/apis/apps/v1beta1/conversion.go b/pkg/apis/apps/v1beta1/conversion.go index 600773d34c9..42d26b86d31 100644 --- a/pkg/apis/apps/v1beta1/conversion.go +++ b/pkg/apis/apps/v1beta1/conversion.go @@ -70,18 +70,6 @@ func addConversionFuncs(scheme *runtime.Scheme) error { if err != nil { return err } - err = scheme.AddFieldLabelConversionFunc("apps/v1beta1", "Deployment", - func(label, value string) (string, string, error) { - switch label { - case "metadata.name", "metadata.namespace": - return label, value, nil - default: - return "", "", fmt.Errorf("field label %q not supported for appsv1beta1.Deployment", label) - } - }) - if err != nil { - return err - } return nil } diff --git a/pkg/apis/apps/v1beta2/conversion.go b/pkg/apis/apps/v1beta2/conversion.go index 2e130eb2cd9..191c2eb56b4 100644 --- a/pkg/apis/apps/v1beta2/conversion.go +++ b/pkg/apis/apps/v1beta2/conversion.go @@ -85,30 +85,6 @@ func addConversionFuncs(scheme *runtime.Scheme) error { if err != nil { return err } - err = scheme.AddFieldLabelConversionFunc("apps/v1beta2", "Deployment", - func(label, value string) (string, string, error) { - switch label { - case "metadata.name", "metadata.namespace": - return label, value, nil - default: - return "", "", fmt.Errorf("field label %q not supported for appsv1beta2.Deployment", label) - } - }) - if err != nil { - return err - } - err = scheme.AddFieldLabelConversionFunc("apps/v1beta2", "ReplicaSet", - func(label, value string) (string, string, error) { - switch label { - case "metadata.name", "metadata.namespace": - return label, value, nil - default: - return "", "", fmt.Errorf("field label %q not supported for appsv1beta2.ReplicaSet", label) - } - }) - if err != nil { - return err - } return nil } diff --git a/pkg/apis/certificates/v1beta1/BUILD b/pkg/apis/certificates/v1beta1/BUILD index aa2d4eed068..879a2bc7182 100644 --- a/pkg/apis/certificates/v1beta1/BUILD +++ b/pkg/apis/certificates/v1beta1/BUILD @@ -8,7 +8,6 @@ load( go_library( name = "go_default_library", srcs = [ - "conversion.go", "defaults.go", "doc.go", "helpers.go", diff --git a/pkg/apis/certificates/v1beta1/conversion.go b/pkg/apis/certificates/v1beta1/conversion.go deleted file mode 100644 index b9cf0b0163a..00000000000 --- a/pkg/apis/certificates/v1beta1/conversion.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1beta1 - -import ( - "fmt" - - "k8s.io/apimachinery/pkg/runtime" -) - -func addConversionFuncs(scheme *runtime.Scheme) error { - // Add non-generated conversion functions here. Currently there are none. - - return scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.String(), "CertificateSigningRequest", - func(label, value string) (string, string, error) { - switch label { - case "metadata.name": - return label, value, nil - default: - return "", "", fmt.Errorf("field label not supported: %s", label) - } - }, - ) -} diff --git a/pkg/apis/certificates/v1beta1/register.go b/pkg/apis/certificates/v1beta1/register.go index e20ef9a3106..dbb0e801660 100644 --- a/pkg/apis/certificates/v1beta1/register.go +++ b/pkg/apis/certificates/v1beta1/register.go @@ -46,5 +46,5 @@ func init() { // We only register manually written functions here. The registration of the // generated functions takes place in the generated files. The separation // makes the code compile even when the generated files are missing. - localSchemeBuilder.Register(addConversionFuncs, addDefaultingFuncs) + localSchemeBuilder.Register(addDefaultingFuncs) } diff --git a/pkg/apis/extensions/v1beta1/conversion.go b/pkg/apis/extensions/v1beta1/conversion.go index 8e8c4c08cd9..0f221592241 100644 --- a/pkg/apis/extensions/v1beta1/conversion.go +++ b/pkg/apis/extensions/v1beta1/conversion.go @@ -65,24 +65,6 @@ func addConversionFuncs(scheme *runtime.Scheme) error { return err } - // Add field label conversions for kinds having selectable nothing but ObjectMeta fields. - for _, k := range []string{"DaemonSet", "Deployment", "Ingress"} { - kind := k // don't close over range variables - err = scheme.AddFieldLabelConversionFunc("extensions/v1beta1", kind, - func(label, value string) (string, string, error) { - switch label { - case "metadata.name", "metadata.namespace": - return label, value, nil - default: - return "", "", fmt.Errorf("field label %q not supported for %q", label, kind) - } - }, - ) - if err != nil { - return err - } - } - return nil }