k8s.io/kubernetes@v1.29.3/pkg/registry/rbac/role/registry.go (about) 1 /* 2 Copyright 2016 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 role 18 19 import ( 20 "context" 21 22 rbacv1 "k8s.io/api/rbac/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 genericapirequest "k8s.io/apiserver/pkg/endpoints/request" 25 "k8s.io/apiserver/pkg/registry/rest" 26 "k8s.io/kubernetes/pkg/apis/rbac" 27 rbacv1helpers "k8s.io/kubernetes/pkg/apis/rbac/v1" 28 ) 29 30 // Registry is an interface for things that know how to store Roles. 31 type Registry interface { 32 GetRole(ctx context.Context, name string, options *metav1.GetOptions) (*rbacv1.Role, error) 33 } 34 35 // storage puts strong typing around storage calls 36 type storage struct { 37 rest.Getter 38 } 39 40 // NewRegistry returns a new Registry interface for the given Storage. Any mismatched 41 // types will panic. 42 func NewRegistry(s rest.StandardStorage) Registry { 43 return &storage{s} 44 } 45 46 func (s *storage) GetRole(ctx context.Context, name string, options *metav1.GetOptions) (*rbacv1.Role, error) { 47 obj, err := s.Get(ctx, name, options) 48 if err != nil { 49 return nil, err 50 } 51 52 ret := &rbacv1.Role{} 53 if err := rbacv1helpers.Convert_rbac_Role_To_v1_Role(obj.(*rbac.Role), ret, nil); err != nil { 54 return nil, err 55 } 56 return ret, nil 57 } 58 59 // AuthorizerAdapter adapts the registry to the authorizer interface 60 type AuthorizerAdapter struct { 61 Registry Registry 62 } 63 64 // GetRole returns the corresponding Role by name in specified namespace 65 func (a AuthorizerAdapter) GetRole(namespace, name string) (*rbacv1.Role, error) { 66 return a.Registry.GetRole(genericapirequest.WithNamespace(genericapirequest.NewContext(), namespace), name, &metav1.GetOptions{}) 67 }