Skip to content

Commit f8b45a1

Browse files
authored
Merge pull request kubernetes#84464 from wojtek-t/remove_conversion_funcs_1
Migrate couple manual conversions to the new AddConversionFunc() way
2 parents 6170296 + fc2332d commit f8b45a1

File tree

12 files changed

+118
-92
lines changed

12 files changed

+118
-92
lines changed

hack/.golint_failures

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmd/cloud-controller-manager/app/apis/config/v1alpha1
22
cmd/kube-apiserver/app
33
cmd/kubeadm/app/apis/kubeadm/v1beta1
44
cmd/kubeadm/app/apis/kubeadm/v1beta2
5+
pkg/apis/abac/v0
6+
pkg/apis/abac/v1beta1
57
pkg/apis/admission
68
pkg/apis/admissionregistration/v1
79
pkg/apis/admissionregistration/v1beta1

pkg/api/testing/serialization_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ func ConvertV1ReplicaSetToAPIReplicationController(in *appsv1.ReplicaSet, out *a
8080
}
8181

8282
func TestSetControllerConversion(t *testing.T) {
83-
if err := legacyscheme.Scheme.AddConversionFuncs(ConvertV1ReplicaSetToAPIReplicationController); err != nil {
83+
s := legacyscheme.Scheme
84+
if err := s.AddConversionFunc((*appsv1.ReplicaSet)(nil), (*api.ReplicationController)(nil), func(a, b interface{}, scope conversion.Scope) error {
85+
return ConvertV1ReplicaSetToAPIReplicationController(a.(*appsv1.ReplicaSet), b.(*api.ReplicationController), scope)
86+
}); err != nil {
8487
t.Fatal(err)
8588
}
8689

pkg/apis/abac/v0/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go_library(
1313
"doc.go",
1414
"register.go",
1515
"types.go",
16+
"zz_generated.conversion.go",
1617
"zz_generated.deepcopy.go",
1718
],
1819
importpath = "k8s.io/kubernetes/pkg/apis/abac/v0",
@@ -22,6 +23,7 @@ go_library(
2223
"//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library",
2324
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
2425
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
26+
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
2527
],
2628
)
2729

pkg/apis/abac/v0/conversion.go

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,45 @@ package v0
1818

1919
import (
2020
"k8s.io/apimachinery/pkg/conversion"
21-
"k8s.io/apimachinery/pkg/runtime"
22-
api "k8s.io/kubernetes/pkg/apis/abac"
21+
"k8s.io/kubernetes/pkg/apis/abac"
2322
)
2423

2524
// allAuthenticated matches k8s.io/apiserver/pkg/authentication/user.AllAuthenticated,
2625
// but we don't want a client library (which must include types), depending on a server library
2726
const allAuthenticated = "system:authenticated"
2827

29-
func addConversionFuncs(scheme *runtime.Scheme) error {
30-
return scheme.AddConversionFuncs(
31-
func(in *Policy, out *api.Policy, s conversion.Scope) error {
32-
// Begin by copying all fields
33-
out.Spec.User = in.User
34-
out.Spec.Group = in.Group
35-
out.Spec.Namespace = in.Namespace
36-
out.Spec.Resource = in.Resource
37-
out.Spec.Readonly = in.Readonly
38-
39-
// In v0, unspecified user and group matches all authenticated subjects
40-
if len(in.User) == 0 && len(in.Group) == 0 {
41-
out.Spec.Group = allAuthenticated
42-
}
43-
// In v0, user or group of * matches all authenticated subjects
44-
if in.User == "*" || in.Group == "*" {
45-
out.Spec.Group = allAuthenticated
46-
out.Spec.User = ""
47-
}
48-
49-
// In v0, leaving namespace empty matches all namespaces
50-
if len(in.Namespace) == 0 {
51-
out.Spec.Namespace = "*"
52-
}
53-
// In v0, leaving resource empty matches all resources
54-
if len(in.Resource) == 0 {
55-
out.Spec.Resource = "*"
56-
}
57-
// Any rule in v0 should match all API groups
58-
out.Spec.APIGroup = "*"
59-
60-
// In v0, leaving namespace and resource blank allows non-resource paths
61-
if len(in.Namespace) == 0 && len(in.Resource) == 0 {
62-
out.Spec.NonResourcePath = "*"
63-
}
64-
65-
return nil
66-
},
67-
)
28+
func Convert_v0_Policy_To_abac_Policy(in *Policy, out *abac.Policy, s conversion.Scope) error {
29+
out.Spec.User = in.User
30+
out.Spec.Group = in.Group
31+
out.Spec.Namespace = in.Namespace
32+
out.Spec.Resource = in.Resource
33+
out.Spec.Readonly = in.Readonly
34+
35+
// In v0, unspecified user and group matches all authenticated subjects
36+
if len(in.User) == 0 && len(in.Group) == 0 {
37+
out.Spec.Group = allAuthenticated
38+
}
39+
// In v0, user or group of * matches all authenticated subjects
40+
if in.User == "*" || in.Group == "*" {
41+
out.Spec.Group = allAuthenticated
42+
out.Spec.User = ""
43+
}
44+
45+
// In v0, leaving namespace empty matches all namespaces
46+
if len(in.Namespace) == 0 {
47+
out.Spec.Namespace = "*"
48+
}
49+
// In v0, leaving resource empty matches all resources
50+
if len(in.Resource) == 0 {
51+
out.Spec.Resource = "*"
52+
}
53+
// Any rule in v0 should match all API groups
54+
out.Spec.APIGroup = "*"
55+
56+
// In v0, leaving namespace and resource blank allows non-resource paths
57+
if len(in.Namespace) == 0 && len(in.Resource) == 0 {
58+
out.Spec.NonResourcePath = "*"
59+
}
60+
61+
return nil
6862
}

pkg/apis/abac/v0/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// +k8s:conversion-gen=false
1718
// +k8s:deepcopy-gen=package
1819

1920
// +groupName=abac.authorization.kubernetes.io

pkg/apis/abac/v0/register.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v0
1919
import (
2020
"k8s.io/apimachinery/pkg/runtime"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
22+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2223
"k8s.io/kubernetes/pkg/apis/abac"
2324
)
2425

@@ -30,14 +31,9 @@ var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v0"}
3031

3132
func init() {
3233
// TODO: Delete this init function, abac should not have its own scheme.
33-
if err := addKnownTypes(abac.Scheme); err != nil {
34-
// Programmer error.
35-
panic(err)
36-
}
37-
if err := addConversionFuncs(abac.Scheme); err != nil {
38-
// Programmer error.
39-
panic(err)
40-
}
34+
utilruntime.Must(addKnownTypes(abac.Scheme))
35+
36+
utilruntime.Must(RegisterConversions(abac.Scheme))
4137
}
4238

4339
var (
@@ -56,7 +52,7 @@ func init() {
5652
// We only register manually written functions here. The registration of the
5753
// generated functions takes place in the generated files. The separation
5854
// makes the code compile even when the generated files are missing.
59-
localSchemeBuilder.Register(addKnownTypes, addConversionFuncs)
55+
localSchemeBuilder.Register(addKnownTypes)
6056
}
6157

6258
func addKnownTypes(scheme *runtime.Scheme) error {

pkg/apis/abac/v0/zz_generated.conversion.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/abac/v1beta1/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ go_library(
2424
"//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library",
2525
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
2626
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
27+
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
2728
],
2829
)
2930

pkg/apis/abac/v1beta1/conversion.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,23 @@ package v1beta1
1818

1919
import (
2020
"k8s.io/apimachinery/pkg/conversion"
21-
"k8s.io/apimachinery/pkg/runtime"
22-
api "k8s.io/kubernetes/pkg/apis/abac"
21+
"k8s.io/kubernetes/pkg/apis/abac"
2322
)
2423

2524
// allAuthenticated matches k8s.io/apiserver/pkg/authentication/user.AllAuthenticated,
2625
// but we don't want an client library (which must include types), depending on a server library
2726
const allAuthenticated = "system:authenticated"
2827

29-
func addConversionFuncs(scheme *runtime.Scheme) error {
30-
return scheme.AddConversionFuncs(
31-
func(in *Policy, out *api.Policy, s conversion.Scope) error {
32-
// Begin by copying all fields
33-
if err := autoConvert_v1beta1_Policy_To_abac_Policy(in, out, s); err != nil {
34-
return err
35-
}
36-
37-
// In v1beta1, * user or group maps to all authenticated subjects
38-
if in.Spec.User == "*" || in.Spec.Group == "*" {
39-
out.Spec.Group = allAuthenticated
40-
out.Spec.User = ""
41-
}
42-
43-
return nil
44-
},
45-
)
28+
func Convert_v1beta1_Policy_To_abac_Policy(in *Policy, out *abac.Policy, s conversion.Scope) error {
29+
if err := autoConvert_v1beta1_Policy_To_abac_Policy(in, out, s); err != nil {
30+
return err
31+
}
32+
33+
// In v1beta1, * user or group maps to all authenticated subjects
34+
if in.Spec.User == "*" || in.Spec.Group == "*" {
35+
out.Spec.Group = allAuthenticated
36+
out.Spec.User = ""
37+
}
38+
39+
return nil
4640
}

pkg/apis/abac/v1beta1/register.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1beta1
1919
import (
2020
"k8s.io/apimachinery/pkg/runtime"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
22+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2223
"k8s.io/kubernetes/pkg/apis/abac"
2324
)
2425

@@ -29,15 +30,10 @@ const GroupName = "abac.authorization.kubernetes.io"
2930
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1beta1"}
3031

3132
func init() {
32-
// TODO: delete this, abac should not have its own scheme.
33-
if err := addKnownTypes(abac.Scheme); err != nil {
34-
// Programmer error.
35-
panic(err)
36-
}
37-
if err := addConversionFuncs(abac.Scheme); err != nil {
38-
// Programmer error.
39-
panic(err)
40-
}
33+
// TODO: Delete this init function, abac should not have its own scheme.
34+
utilruntime.Must(addKnownTypes(abac.Scheme))
35+
36+
utilruntime.Must(RegisterConversions(abac.Scheme))
4137
}
4238

4339
var (
@@ -56,7 +52,7 @@ func init() {
5652
// We only register manually written functions here. The registration of the
5753
// generated functions takes place in the generated files. The separation
5854
// makes the code compile even when the generated files are missing.
59-
localSchemeBuilder.Register(addKnownTypes, addConversionFuncs, RegisterDefaults)
55+
localSchemeBuilder.Register(addKnownTypes, RegisterDefaults)
6056
}
6157

6258
func addKnownTypes(scheme *runtime.Scheme) error {

0 commit comments

Comments
 (0)