registry.go 2.0 KB

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