k8s.io/kubernetes@v1.29.3/pkg/apis/rbac/v1alpha1/conversion.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 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 v1alpha1 18 19 import ( 20 rbacv1alpha1 "k8s.io/api/rbac/v1alpha1" 21 "k8s.io/apimachinery/pkg/conversion" 22 "k8s.io/apimachinery/pkg/runtime/schema" 23 api "k8s.io/kubernetes/pkg/apis/rbac" 24 ) 25 26 // allAuthenticated matches k8s.io/apiserver/pkg/authentication/user.AllAuthenticated, 27 // but we don't want an client library (which must include types), depending on a server library 28 const allAuthenticated = "system:authenticated" 29 30 func Convert_v1alpha1_Subject_To_rbac_Subject(in *rbacv1alpha1.Subject, out *api.Subject, s conversion.Scope) error { 31 if err := autoConvert_v1alpha1_Subject_To_rbac_Subject(in, out, s); err != nil { 32 return err 33 } 34 35 // specifically set the APIGroup for the three subjects recognized in v1alpha1 36 switch { 37 case in.Kind == rbacv1alpha1.ServiceAccountKind: 38 out.APIGroup = "" 39 case in.Kind == rbacv1alpha1.UserKind: 40 out.APIGroup = GroupName 41 case in.Kind == rbacv1alpha1.GroupKind: 42 out.APIGroup = GroupName 43 default: 44 // For unrecognized kinds, use the group portion of the APIVersion if we can get it 45 if gv, err := schema.ParseGroupVersion(in.APIVersion); err == nil { 46 out.APIGroup = gv.Group 47 } 48 } 49 50 // User * in v1alpha1 will only match all authenticated users 51 // This is only for compatibility with old RBAC bindings 52 // Special treatment for * should not be included in v1beta1 53 if out.Kind == rbacv1alpha1.UserKind && out.APIGroup == GroupName && out.Name == "*" { 54 out.Kind = rbacv1alpha1.GroupKind 55 out.Name = allAuthenticated 56 } 57 58 return nil 59 } 60 61 func Convert_rbac_Subject_To_v1alpha1_Subject(in *api.Subject, out *rbacv1alpha1.Subject, s conversion.Scope) error { 62 if err := autoConvert_rbac_Subject_To_v1alpha1_Subject(in, out, s); err != nil { 63 return err 64 } 65 66 switch { 67 case in.Kind == rbacv1alpha1.ServiceAccountKind && in.APIGroup == "": 68 // Make service accounts v1 69 out.APIVersion = "v1" 70 case in.Kind == rbacv1alpha1.UserKind && in.APIGroup == GroupName: 71 // users in the rbac API group get v1alpha 72 out.APIVersion = SchemeGroupVersion.String() 73 case in.Kind == rbacv1alpha1.GroupKind && in.APIGroup == GroupName: 74 // groups in the rbac API group get v1alpha 75 out.APIVersion = SchemeGroupVersion.String() 76 default: 77 // otherwise, they get an unspecified version of a group 78 out.APIVersion = schema.GroupVersion{Group: in.APIGroup}.String() 79 } 80 81 return nil 82 }