conversion.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1alpha1
  14. import (
  15. rbacv1alpha1 "k8s.io/api/rbac/v1alpha1"
  16. "k8s.io/apimachinery/pkg/conversion"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. api "k8s.io/kubernetes/pkg/apis/rbac"
  19. )
  20. // allAuthenticated matches k8s.io/apiserver/pkg/authentication/user.AllAuthenticated,
  21. // but we don't want an client library (which must include types), depending on a server library
  22. const allAuthenticated = "system:authenticated"
  23. func Convert_v1alpha1_Subject_To_rbac_Subject(in *rbacv1alpha1.Subject, out *api.Subject, s conversion.Scope) error {
  24. if err := autoConvert_v1alpha1_Subject_To_rbac_Subject(in, out, s); err != nil {
  25. return err
  26. }
  27. // specifically set the APIGroup for the three subjects recognized in v1alpha1
  28. switch {
  29. case in.Kind == rbacv1alpha1.ServiceAccountKind:
  30. out.APIGroup = ""
  31. case in.Kind == rbacv1alpha1.UserKind:
  32. out.APIGroup = GroupName
  33. case in.Kind == rbacv1alpha1.GroupKind:
  34. out.APIGroup = GroupName
  35. default:
  36. // For unrecognized kinds, use the group portion of the APIVersion if we can get it
  37. if gv, err := schema.ParseGroupVersion(in.APIVersion); err == nil {
  38. out.APIGroup = gv.Group
  39. }
  40. }
  41. // User * in v1alpha1 will only match all authenticated users
  42. // This is only for compatibility with old RBAC bindings
  43. // Special treatment for * should not be included in v1beta1
  44. if out.Kind == rbacv1alpha1.UserKind && out.APIGroup == GroupName && out.Name == "*" {
  45. out.Kind = rbacv1alpha1.GroupKind
  46. out.Name = allAuthenticated
  47. }
  48. return nil
  49. }
  50. func Convert_rbac_Subject_To_v1alpha1_Subject(in *api.Subject, out *rbacv1alpha1.Subject, s conversion.Scope) error {
  51. if err := autoConvert_rbac_Subject_To_v1alpha1_Subject(in, out, s); err != nil {
  52. return err
  53. }
  54. switch {
  55. case in.Kind == rbacv1alpha1.ServiceAccountKind && in.APIGroup == "":
  56. // Make service accounts v1
  57. out.APIVersion = "v1"
  58. case in.Kind == rbacv1alpha1.UserKind && in.APIGroup == GroupName:
  59. // users in the rbac API group get v1alpha
  60. out.APIVersion = SchemeGroupVersion.String()
  61. case in.Kind == rbacv1alpha1.GroupKind && in.APIGroup == GroupName:
  62. // groups in the rbac API group get v1alpha
  63. out.APIVersion = SchemeGroupVersion.String()
  64. default:
  65. // otherwise, they get an unspecified version of a group
  66. out.APIVersion = schema.GroupVersion{Group: in.APIGroup}.String()
  67. }
  68. return nil
  69. }