k8s.io/kubernetes@v1.29.3/pkg/registry/rbac/clusterrolebinding/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 clusterrolebinding 18 19 import ( 20 "context" 21 22 rbacv1 "k8s.io/api/rbac/v1" 23 metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" 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 ClusterRoleBindings. 31 type Registry interface { 32 ListClusterRoleBindings(ctx context.Context, options *metainternalversion.ListOptions) (*rbacv1.ClusterRoleBindingList, error) 33 } 34 35 // storage puts strong typing around storage calls 36 type storage struct { 37 rest.Lister 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) ListClusterRoleBindings(ctx context.Context, options *metainternalversion.ListOptions) (*rbacv1.ClusterRoleBindingList, error) { 47 obj, err := s.List(ctx, options) 48 if err != nil { 49 return nil, err 50 } 51 52 ret := &rbacv1.ClusterRoleBindingList{} 53 if err := rbacv1helpers.Convert_rbac_ClusterRoleBindingList_To_v1_ClusterRoleBindingList(obj.(*rbac.ClusterRoleBindingList), 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 func (a AuthorizerAdapter) ListClusterRoleBindings() ([]*rbacv1.ClusterRoleBinding, error) { 65 list, err := a.Registry.ListClusterRoleBindings(genericapirequest.NewContext(), &metainternalversion.ListOptions{}) 66 if err != nil { 67 return nil, err 68 } 69 70 ret := []*rbacv1.ClusterRoleBinding{} 71 for i := range list.Items { 72 ret = append(ret, &list.Items[i]) 73 } 74 return ret, nil 75 }