Skip to content

Commit 02e0217

Browse files
committed
update route field selectors
1 parent 0c5698a commit 02e0217

File tree

9 files changed

+168
-98
lines changed

9 files changed

+168
-98
lines changed

hack/import-restrictions.json

+21-30
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,15 @@
6060
],
6161
"allowedImportPackageRoots": [
6262
"vendor/k8s.io/apimachinery",
63-
"vendor/github.com/gogo/protobuf"
63+
"vendor/github.com/gogo/protobuf",
64+
"github.com/openshift/origin/pkg/api/apihelpers"
6465
],
6566
"allowedImportPackages": [
6667
"vendor/github.com/google/gofuzz",
6768
"vendor/k8s.io/kubernetes/pkg/api",
6869
"vendor/k8s.io/kubernetes/pkg/api/v1",
6970
"vendor/k8s.io/kubernetes/pkg/api/validation",
7071
"vendor/k8s.io/kubernetes/pkg/apis/rbac",
71-
"github.com/openshift/origin/pkg/api/apihelpers/apitesting",
72-
"github.com/openshift/origin/pkg/api/apihelpers",
7372
"github.com/openshift/origin/pkg/api/install",
7473
"vendor/k8s.io/kubernetes/pkg/api/helper",
7574
"github.com/openshift/origin/pkg/user/apis/user/validation",
@@ -86,13 +85,12 @@
8685
],
8786
"allowedImportPackageRoots": [
8887
"vendor/k8s.io/apimachinery",
89-
"vendor/github.com/gogo/protobuf"
88+
"vendor/github.com/gogo/protobuf",
89+
"github.com/openshift/origin/pkg/api/apihelpers"
9090
],
9191
"allowedImportPackages": [
9292
"vendor/k8s.io/kubernetes/pkg/api",
9393
"vendor/k8s.io/kubernetes/pkg/api/v1",
94-
"github.com/openshift/origin/pkg/api/apihelpers/apitesting",
95-
"github.com/openshift/origin/pkg/api/apihelpers",
9694
"github.com/openshift/origin/pkg/api/install",
9795
"github.com/openshift/origin/pkg/util/namer",
9896
"github.com/openshift/origin/pkg/build/util",
@@ -131,7 +129,8 @@
131129
],
132130
"allowedImportPackageRoots": [
133131
"vendor/k8s.io/apimachinery",
134-
"vendor/github.com/gogo/protobuf"
132+
"vendor/github.com/gogo/protobuf",
133+
"github.com/openshift/origin/pkg/api/apihelpers"
135134
],
136135
"allowedImportPackages": [
137136
"vendor/k8s.io/kubernetes/pkg/api",
@@ -144,8 +143,6 @@
144143
"vendor/github.com/docker/distribution/manifest/schema1",
145144
"vendor/github.com/docker/distribution/manifest/schema2",
146145
"vendor/github.com/blang/semver",
147-
"github.com/openshift/origin/pkg/api/apihelpers",
148-
"github.com/openshift/origin/pkg/api/apihelpers/apitesting",
149146
"github.com/openshift/origin/pkg/image/apis/image/install",
150147
"github.com/openshift/origin/pkg/image/reference"
151148
]
@@ -158,12 +155,10 @@
158155
],
159156
"allowedImportPackageRoots": [
160157
"vendor/k8s.io/apimachinery",
161-
"vendor/github.com/gogo/protobuf"
162-
],
163-
"allowedImportPackages": [
164-
"github.com/openshift/origin/pkg/api/apihelpers/apitesting",
158+
"vendor/github.com/gogo/protobuf",
165159
"github.com/openshift/origin/pkg/api/apihelpers"
166-
]
160+
],
161+
"allowedImportPackages": []
167162
},
168163

169164
{
@@ -173,13 +168,12 @@
173168
],
174169
"allowedImportPackageRoots": [
175170
"vendor/k8s.io/apimachinery",
176-
"vendor/github.com/gogo/protobuf"
171+
"vendor/github.com/gogo/protobuf",
172+
"github.com/openshift/origin/pkg/api/apihelpers"
177173
],
178174
"allowedImportPackages": [
179175
"vendor/k8s.io/kubernetes/pkg/api",
180176
"vendor/k8s.io/kubernetes/pkg/api/v1",
181-
"github.com/openshift/origin/pkg/api/apihelpers/apitesting",
182-
"github.com/openshift/origin/pkg/api/apihelpers",
183177
"vendor/k8s.io/kubernetes/pkg/registry/core/namespace"
184178
]
185179
},
@@ -208,14 +202,12 @@
208202
],
209203
"allowedImportPackageRoots": [
210204
"vendor/k8s.io/apimachinery",
211-
"vendor/github.com/gogo/protobuf"
205+
"vendor/github.com/gogo/protobuf",
206+
"github.com/openshift/origin/pkg/api/apihelpers"
212207
],
213208
"allowedImportPackages": [
214209
"vendor/k8s.io/kubernetes/pkg/api",
215-
"vendor/k8s.io/kubernetes/pkg/api/v1",
216-
"github.com/openshift/origin/pkg/api/apihelpers",
217-
"github.com/openshift/origin/pkg/api/apihelpers/apitesting",
218-
"github.com/openshift/origin/pkg/api/install"
210+
"vendor/k8s.io/kubernetes/pkg/api/v1"
219211
]
220212
},
221213

@@ -238,12 +230,12 @@
238230
],
239231
"allowedImportPackageRoots": [
240232
"vendor/k8s.io/apimachinery",
241-
"vendor/github.com/gogo/protobuf"
233+
"vendor/github.com/gogo/protobuf",
234+
"github.com/openshift/origin/pkg/api/apihelpers"
242235
],
243236
"allowedImportPackages": [
244237
"vendor/k8s.io/kubernetes/pkg/api",
245-
"vendor/k8s.io/kubernetes/pkg/api/v1",
246-
"github.com/openshift/origin/pkg/api/apihelpers"
238+
"vendor/k8s.io/kubernetes/pkg/api/v1"
247239
]
248240
},
249241

@@ -254,19 +246,18 @@
254246
],
255247
"allowedImportPackageRoots": [
256248
"vendor/k8s.io/apimachinery",
257-
"vendor/github.com/gogo/protobuf"
249+
"vendor/github.com/gogo/protobuf",
250+
"github.com/openshift/origin/pkg/api/apihelpers"
258251
],
259252
"allowedImportPackages": [
260253
"vendor/k8s.io/kubernetes/pkg/api",
261-
"vendor/k8s.io/kubernetes/pkg/api/v1",
262-
"github.com/openshift/origin/pkg/api/apihelpers/apitesting",
263-
"github.com/openshift/origin/pkg/api/apihelpers"
254+
"vendor/k8s.io/kubernetes/pkg/api/v1"
264255
]
265256
},
266257

267258

268259
{
269-
"checkedPackages": [
260+
"checkedPackageRoots": [
270261
"github.com/openshift/origin/pkg/api/apihelpers"
271262
],
272263
"allowedImportPackageRoots": [

pkg/api/apihelpers/apitesting/fields.go

+51
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package apitesting
33
import (
44
"testing"
55

6+
"k8s.io/apimachinery/pkg/fields"
67
"k8s.io/apimachinery/pkg/runtime"
8+
"k8s.io/apimachinery/pkg/runtime/schema"
79
)
810

911
func TestFieldLabelConversions(t *testing.T, scheme *runtime.Scheme, version, kind string, expectedLabels map[string]string, customLabels ...string) {
@@ -20,3 +22,52 @@ func TestFieldLabelConversions(t *testing.T, scheme *runtime.Scheme, version, ki
2022
}
2123
}
2224
}
25+
26+
// FieldKeyCheck gathers information to check if the field key conversions are working correctly. It takes many parameters
27+
// in an attempt to reflect reality
28+
type FieldKeyCheck struct {
29+
SchemeBuilder runtime.SchemeBuilder
30+
Kind schema.GroupVersionKind
31+
AllowedExternalFieldKeys []string
32+
FieldKeyEvaluatorFn FieldKeyEvaluator
33+
}
34+
35+
func (f FieldKeyCheck) Check(t *testing.T) {
36+
scheme := runtime.NewScheme()
37+
f.SchemeBuilder.AddToScheme(scheme)
38+
internalObj, err := scheme.New(f.Kind.GroupKind().WithVersion(runtime.APIVersionInternal))
39+
if err != nil {
40+
t.Errorf("unable to new up %v", f.Kind)
41+
}
42+
43+
for _, externalFieldKey := range f.AllowedExternalFieldKeys {
44+
internalFieldKey, _, err := scheme.ConvertFieldLabel(f.Kind.GroupVersion().String(), f.Kind.Kind, externalFieldKey, "")
45+
if err != nil {
46+
t.Errorf("illegal field conversion %q for %v", externalFieldKey, f.Kind)
47+
continue
48+
}
49+
50+
fieldSet := fields.Set{}
51+
if err := f.FieldKeyEvaluatorFn(internalObj, fieldSet); err != nil {
52+
t.Errorf("unable to valuate field keys for %v: %v", f.Kind, err)
53+
continue
54+
}
55+
56+
found := false
57+
for actualInternalFieldKey := range fieldSet {
58+
if internalFieldKey == actualInternalFieldKey {
59+
found = true
60+
break
61+
}
62+
}
63+
if !found {
64+
t.Errorf("%q converted to %q which has no internal field key match for %v", externalFieldKey, internalFieldKey, f.Kind)
65+
continue
66+
}
67+
68+
}
69+
70+
}
71+
72+
// FieldKeyEvaluator overlaps with the storage mutation func. We use this to confirm that the non-meta fields are actually being handled
73+
type FieldKeyEvaluator func(obj runtime.Object, fieldSet fields.Set) error

pkg/route/apis/route/fields.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package route
22

3-
import "k8s.io/apimachinery/pkg/fields"
3+
import (
4+
"fmt"
45

5-
// RouteToSelectableFields returns a label set that represents the object
6-
func RouteToSelectableFields(route *Route) fields.Set {
7-
return fields.Set{
8-
"metadata.name": route.Name,
9-
"metadata.namespace": route.Namespace,
10-
"spec.path": route.Spec.Path,
11-
"spec.host": route.Spec.Host,
12-
"spec.to.name": route.Spec.To.Name,
6+
"k8s.io/apimachinery/pkg/fields"
7+
runtime "k8s.io/apimachinery/pkg/runtime"
8+
)
9+
10+
func RouteFieldSelector(obj runtime.Object, fieldSet fields.Set) error {
11+
route, ok := obj.(*Route)
12+
if !ok {
13+
return fmt.Errorf("%T not a Route", obj)
1314
}
15+
fieldSet["spec.path"] = route.Spec.Path
16+
fieldSet["spec.host"] = route.Spec.Host
17+
fieldSet["spec.to.name"] = route.Spec.To.Name
18+
return nil
1419
}

pkg/route/apis/route/v1/conversion.go

+37-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,43 @@ package v1
22

33
import (
44
"k8s.io/apimachinery/pkg/runtime"
5-
6-
"github.com/openshift/origin/pkg/api/apihelpers"
7-
routeapi "github.com/openshift/origin/pkg/route/apis/route"
85
)
96

10-
func addConversionFuncs(scheme *runtime.Scheme) error {
11-
return scheme.AddFieldLabelConversionFunc("v1", "Route",
12-
apihelpers.GetFieldLabelConversionFunc(routeapi.RouteToSelectableFields(&routeapi.Route{}), nil),
13-
)
7+
func addLegacyFieldConversionFuncs(scheme *runtime.Scheme) error {
8+
if err := scheme.AddFieldLabelConversionFunc(LegacySchemeGroupVersion.String(), "Route", legacyRouteFieldSelectorConversionFunc); err != nil {
9+
return err
10+
}
11+
return nil
12+
}
13+
14+
func addFieldConversionFuncs(scheme *runtime.Scheme) error {
15+
if err := scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.String(), "Route", routeFieldSelectorConversionFunc); err != nil {
16+
return err
17+
}
18+
return nil
19+
}
20+
21+
// because field selectors can vary in support by version they are exposed under, we have one function for each
22+
// groupVersion we're registering for
23+
24+
func legacyRouteFieldSelectorConversionFunc(label, value string) (internalLabel, internalValue string, err error) {
25+
switch label {
26+
case "spec.path",
27+
"spec.host",
28+
"spec.to.name":
29+
return label, value, nil
30+
default:
31+
return runtime.DefaultMetaV1FieldSelectorConversion(label, value)
32+
}
33+
}
34+
35+
func routeFieldSelectorConversionFunc(label, value string) (internalLabel, internalValue string, err error) {
36+
switch label {
37+
case "spec.path",
38+
"spec.host",
39+
"spec.to.name":
40+
return label, value, nil
41+
default:
42+
return runtime.DefaultMetaV1FieldSelectorConversion(label, value)
43+
}
1444
}

pkg/route/apis/route/v1/conversion_test.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@ import (
66
"k8s.io/apimachinery/pkg/runtime"
77

88
"github.com/openshift/origin/pkg/api/apihelpers/apitesting"
9-
routeapi "github.com/openshift/origin/pkg/route/apis/route"
9+
"github.com/openshift/origin/pkg/route/apis/route"
1010
)
1111

1212
func TestFieldSelectorConversions(t *testing.T) {
13-
converter := runtime.NewScheme()
14-
LegacySchemeBuilder.AddToScheme(converter)
13+
apitesting.FieldKeyCheck{
14+
SchemeBuilder: []func(*runtime.Scheme) error{LegacySchemeBuilder.AddToScheme, route.LegacySchemeBuilder.AddToScheme},
15+
Kind: LegacySchemeGroupVersion.WithKind("Route"),
16+
// Ensure previously supported labels have conversions. DO NOT REMOVE THINGS FROM THIS LIST
17+
AllowedExternalFieldKeys: []string{"spec.host", "spec.path", "spec.to.name"},
18+
FieldKeyEvaluatorFn: route.RouteFieldSelector,
19+
}.Check(t)
1520

16-
apitesting.TestFieldLabelConversions(t, converter, "v1", "Route",
17-
// Ensure all currently returned labels are supported
18-
routeapi.RouteToSelectableFields(&routeapi.Route{}),
21+
apitesting.FieldKeyCheck{
22+
SchemeBuilder: []func(*runtime.Scheme) error{SchemeBuilder.AddToScheme, route.SchemeBuilder.AddToScheme},
23+
Kind: SchemeGroupVersion.WithKind("Route"),
1924
// Ensure previously supported labels have conversions. DO NOT REMOVE THINGS FROM THIS LIST
20-
"spec.host", "spec.path", "spec.to.name",
21-
)
25+
AllowedExternalFieldKeys: []string{"spec.host", "spec.path", "spec.to.name"},
26+
FieldKeyEvaluatorFn: route.RouteFieldSelector,
27+
}.Check(t)
2228
}
2329

2430
func TestSupportingCamelConstants(t *testing.T) {

0 commit comments

Comments
 (0)