...

Source file src/k8s.io/kubernetes/pkg/registry/rbac/clusterrole/registry.go

Documentation: k8s.io/kubernetes/pkg/registry/rbac/clusterrole

     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 clusterrole
    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 ClusterRoles.
    31  type Registry interface {
    32  	GetClusterRole(ctx context.Context, name string, options *metav1.GetOptions) (*rbacv1.ClusterRole, 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) GetClusterRole(ctx context.Context, name string, options *metav1.GetOptions) (*rbacv1.ClusterRole, error) {
    47  	obj, err := s.Get(ctx, name, options)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	ret := &rbacv1.ClusterRole{}
    53  	if err := rbacv1helpers.Convert_rbac_ClusterRole_To_v1_ClusterRole(obj.(*rbac.ClusterRole), 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  // GetClusterRole returns the corresponding ClusterRole by name
    65  func (a AuthorizerAdapter) GetClusterRole(name string) (*rbacv1.ClusterRole, error) {
    66  	return a.Registry.GetClusterRole(genericapirequest.NewContext(), name, &metav1.GetOptions{})
    67  }
    68  

View as plain text