Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 3188e12

Browse files
author
Michal Minar
committed
Add missing field label conversions
Added missing field labels conversion functions for kinds: - ConfigMap - DaemonSet - Deployment - Endpoints - Event - Ingress - Job - PersistentVolume - PersistentVolumeClaim - ResourceQuota - Secret - Service Completed conversion functions of few other kinds: - Namespace - ServiceAccount Added tests suite for the above. Signed-off-by: Michal Minar <miminar@redhat.com>
1 parent 36dc6ea commit 3188e12

File tree

26 files changed

+656
-35
lines changed

26 files changed

+656
-35
lines changed

pkg/api/testing/conversion.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testing
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/kubernetes/pkg/api"
23+
"k8s.io/kubernetes/pkg/labels"
24+
)
25+
26+
// TestSelectableFieldLabelConversions verifies that given resource have field
27+
// label conversion defined for each its selectable field.
28+
// fields contains selectable fields of the resource.
29+
// labelMap maps deprecated labels to their canonical names.
30+
func TestSelectableFieldLabelConversionsOfKind(t *testing.T, apiVersion string, kind string, fields labels.Set, labelMap map[string]string) {
31+
badFieldLabels := []string{
32+
"name",
33+
".name",
34+
"bad",
35+
"metadata",
36+
"foo.bar",
37+
}
38+
39+
value := "value"
40+
41+
if len(fields) == 0 {
42+
t.Logf("no selectable fields for kind %q, skipping", kind)
43+
}
44+
for label := range fields {
45+
if label == "name" {
46+
t.Logf("FIXME: \"name\" is deprecated by \"metadata.name\", it should be removed from selectable fields of kind=%s", kind)
47+
continue
48+
}
49+
newLabel, newValue, err := api.Scheme.ConvertFieldLabel(apiVersion, kind, label, value)
50+
if err != nil {
51+
t.Errorf("kind=%s label=%s: got unexpected error: %v", kind, label, err)
52+
} else {
53+
expectedLabel := label
54+
if l, exists := labelMap[label]; exists {
55+
expectedLabel = l
56+
}
57+
if newLabel != expectedLabel {
58+
t.Errorf("kind=%s label=%s: got unexpected label name (%q != %q)", kind, label, newLabel, expectedLabel)
59+
}
60+
if newValue != value {
61+
t.Errorf("kind=%s label=%s: got unexpected new value (%q != %q)", kind, label, newValue, value)
62+
}
63+
}
64+
}
65+
66+
for _, label := range badFieldLabels {
67+
_, _, err := api.Scheme.ConvertFieldLabel(apiVersion, kind, label, "value")
68+
if err == nil {
69+
t.Errorf("kind=%s label=%s: got unexpected non-error", kind, label)
70+
}
71+
}
72+
}

pkg/api/v1/conversion.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ func addConversionFuncs(scheme *runtime.Scheme) {
5252
panic(err)
5353
}
5454

55+
// Add field label conversions for kinds having selectable nothing but ObjectMeta fields.
56+
for _, kind := range []string{
57+
"Endpoints",
58+
"ResourceQuota",
59+
"PersistentVolumeClaim",
60+
"Service",
61+
"ServiceAccount",
62+
} {
63+
err = api.Scheme.AddFieldLabelConversionFunc("v1", kind,
64+
func(label, value string) (string, string, error) {
65+
switch label {
66+
case "metadata.namespace",
67+
"metadata.name":
68+
return label, value, nil
69+
default:
70+
return "", "", fmt.Errorf("field label %q not supported for %q", label, kind)
71+
}
72+
})
73+
if err != nil {
74+
// If one of the conversion functions is malformed, detect it immediately.
75+
panic(err)
76+
}
77+
}
78+
5579
// Add field conversion funcs.
5680
err = api.Scheme.AddFieldLabelConversionFunc("v1", "Pod",
5781
func(label, value string) (string, string, error) {
@@ -143,40 +167,10 @@ func addConversionFuncs(scheme *runtime.Scheme) {
143167
// If one of the conversion functions is malformed, detect it immediately.
144168
panic(err)
145169
}
146-
err = api.Scheme.AddFieldLabelConversionFunc("v1", "Secret",
147-
func(label, value string) (string, string, error) {
148-
switch label {
149-
case "type",
150-
"metadata.namespace",
151-
"metadata.name":
152-
return label, value, nil
153-
default:
154-
return "", "", fmt.Errorf("field label not supported: %s", label)
155-
}
156-
})
157-
if err != nil {
158-
// If one of the conversion functions is malformed, detect it immediately.
159-
panic(err)
160-
}
161-
err = api.Scheme.AddFieldLabelConversionFunc("v1", "ServiceAccount",
170+
err = api.Scheme.AddFieldLabelConversionFunc("v1", "PersistentVolume",
162171
func(label, value string) (string, string, error) {
163172
switch label {
164-
case "metadata.name",
165-
"metadata.namespace":
166-
return label, value, nil
167-
default:
168-
return "", "", fmt.Errorf("field label not supported: %s", label)
169-
}
170-
})
171-
if err != nil {
172-
// If one of the conversion functions is malformed, detect it immediately.
173-
panic(err)
174-
}
175-
err = api.Scheme.AddFieldLabelConversionFunc("v1", "Endpoints",
176-
func(label, value string) (string, string, error) {
177-
switch label {
178-
case "metadata.namespace",
179-
"metadata.name":
173+
case "metadata.name":
180174
return label, value, nil
181175
default:
182176
return "", "", fmt.Errorf("field label not supported: %s", label)
@@ -186,10 +180,11 @@ func addConversionFuncs(scheme *runtime.Scheme) {
186180
// If one of the conversion functions is malformed, detect it immediately.
187181
panic(err)
188182
}
189-
err = api.Scheme.AddFieldLabelConversionFunc("v1", "Service",
183+
err = api.Scheme.AddFieldLabelConversionFunc("v1", "Secret",
190184
func(label, value string) (string, string, error) {
191185
switch label {
192-
case "metadata.namespace",
186+
case "type",
187+
"metadata.namespace",
193188
"metadata.name":
194189
return label, value, nil
195190
default:

pkg/apis/extensions/v1beta1/conversion.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"fmt"
2021
"reflect"
2122

2223
"k8s.io/kubernetes/pkg/api"
@@ -43,6 +44,37 @@ func addConversionFuncs(scheme *runtime.Scheme) {
4344
// If one of the conversion functions is malformed, detect it immediately.
4445
panic(err)
4546
}
47+
48+
// Add field label conversions for kinds having selectable nothing but ObjectMeta fields.
49+
for _, kind := range []string{"ConfigMap", "DaemonSet", "Deployment", "Ingress"} {
50+
err = api.Scheme.AddFieldLabelConversionFunc("extensions/v1beta1", kind,
51+
func(label, value string) (string, string, error) {
52+
switch label {
53+
case "metadata.name", "metadata.namespace":
54+
return label, value, nil
55+
default:
56+
return "", "", fmt.Errorf("field label %q not supported for %q", label, kind)
57+
}
58+
})
59+
if err != nil {
60+
// If one of the conversion functions is malformed, detect it immediately.
61+
panic(err)
62+
}
63+
}
64+
65+
err = api.Scheme.AddFieldLabelConversionFunc("extensions/v1beta1", "Job",
66+
func(label, value string) (string, string, error) {
67+
switch label {
68+
case "metadata.name", "metadata.namespace", "status.successful":
69+
return label, value, nil
70+
default:
71+
return "", "", fmt.Errorf("field label not supported: %s", label)
72+
}
73+
})
74+
if err != nil {
75+
// If one of the conversion functions is malformed, detect it immediately.
76+
panic(err)
77+
}
4678
}
4779

4880
// The following two PodSpec conversions functions where copied from pkg/api/conversion.go

pkg/registry/configmap/strategy_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import (
2020
"testing"
2121

2222
"k8s.io/kubernetes/pkg/api"
23+
"k8s.io/kubernetes/pkg/api/testapi"
24+
apitesting "k8s.io/kubernetes/pkg/api/testing"
2325
"k8s.io/kubernetes/pkg/apis/extensions"
26+
"k8s.io/kubernetes/pkg/labels"
2427
)
2528

2629
func TestConfigMapStrategy(t *testing.T) {
@@ -67,3 +70,12 @@ func TestConfigMapStrategy(t *testing.T) {
6770
t.Errorf("Expected a validation error")
6871
}
6972
}
73+
74+
func TestSelectableFieldLabelConversions(t *testing.T) {
75+
apitesting.TestSelectableFieldLabelConversionsOfKind(t,
76+
testapi.Extensions.GroupVersion().String(),
77+
"ConfigMap",
78+
labels.Set(ConfigMapToSelectableFields(&extensions.ConfigMap{})),
79+
nil,
80+
)
81+
}

pkg/registry/controller/strategy_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"testing"
2121

2222
"k8s.io/kubernetes/pkg/api"
23+
"k8s.io/kubernetes/pkg/api/testapi"
24+
apitesting "k8s.io/kubernetes/pkg/api/testing"
25+
"k8s.io/kubernetes/pkg/labels"
2326
)
2427

2528
func TestControllerStrategy(t *testing.T) {
@@ -138,3 +141,12 @@ func TestControllerStatusStrategy(t *testing.T) {
138141
t.Errorf("Unexpected error %v", errs)
139142
}
140143
}
144+
145+
func TestSelectableFieldLabelConversions(t *testing.T) {
146+
apitesting.TestSelectableFieldLabelConversionsOfKind(t,
147+
testapi.Default.GroupVersion().String(),
148+
"ReplicationController",
149+
labels.Set(ControllerToSelectableFields(&api.ReplicationController{})),
150+
nil,
151+
)
152+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package daemonset
18+
19+
import (
20+
"testing"
21+
22+
_ "k8s.io/kubernetes/pkg/api"
23+
"k8s.io/kubernetes/pkg/api/testapi"
24+
apitesting "k8s.io/kubernetes/pkg/api/testing"
25+
"k8s.io/kubernetes/pkg/apis/extensions"
26+
"k8s.io/kubernetes/pkg/labels"
27+
)
28+
29+
func TestSelectableFieldLabelConversions(t *testing.T) {
30+
apitesting.TestSelectableFieldLabelConversionsOfKind(t,
31+
testapi.Extensions.GroupVersion().String(),
32+
"DaemonSet",
33+
labels.Set(DaemonSetToSelectableFields(&extensions.DaemonSet{})),
34+
nil,
35+
)
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package deployment
18+
19+
import (
20+
"testing"
21+
22+
_ "k8s.io/kubernetes/pkg/api"
23+
"k8s.io/kubernetes/pkg/api/testapi"
24+
apitesting "k8s.io/kubernetes/pkg/api/testing"
25+
"k8s.io/kubernetes/pkg/apis/extensions"
26+
"k8s.io/kubernetes/pkg/labels"
27+
)
28+
29+
func TestSelectableFieldLabelConversions(t *testing.T) {
30+
apitesting.TestSelectableFieldLabelConversionsOfKind(t,
31+
testapi.Extensions.GroupVersion().String(),
32+
"Deployment",
33+
labels.Set(DeploymentToSelectableFields(&extensions.Deployment{})),
34+
nil,
35+
)
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2014 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package endpoint
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/kubernetes/pkg/api"
23+
"k8s.io/kubernetes/pkg/api/testapi"
24+
apitesting "k8s.io/kubernetes/pkg/api/testing"
25+
"k8s.io/kubernetes/pkg/labels"
26+
)
27+
28+
func TestSelectableFieldLabelConversions(t *testing.T) {
29+
_, fieldsSet, err := EndpointsAttributes(&api.Endpoints{})
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
apitesting.TestSelectableFieldLabelConversionsOfKind(t,
34+
testapi.Default.GroupVersion().String(),
35+
"Endpoints",
36+
labels.Set(fieldsSet),
37+
nil,
38+
)
39+
}

pkg/registry/event/strategy_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"k8s.io/kubernetes/pkg/api"
2424
"k8s.io/kubernetes/pkg/api/testapi"
25+
apitesting "k8s.io/kubernetes/pkg/api/testing"
2526
"k8s.io/kubernetes/pkg/fields"
2627
"k8s.io/kubernetes/pkg/labels"
2728
"k8s.io/kubernetes/pkg/util"
@@ -84,3 +85,16 @@ func TestGetAttrs(t *testing.T) {
8485
t.Errorf("diff: %s", util.ObjectDiff(e, a))
8586
}
8687
}
88+
89+
func TestSelectableFieldLabelConversions(t *testing.T) {
90+
_, fset, err := getAttrs(&api.Event{})
91+
if err != nil {
92+
t.Fatalf("Unexpected error %v", err)
93+
}
94+
apitesting.TestSelectableFieldLabelConversionsOfKind(t,
95+
testapi.Default.GroupVersion().String(),
96+
"Event",
97+
labels.Set(fset),
98+
nil,
99+
)
100+
}

0 commit comments

Comments
 (0)