role_interfaces.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2017 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 reconciliation
  14. import (
  15. rbacv1 "k8s.io/api/rbac/v1"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
  19. rbacv1client "k8s.io/client-go/kubernetes/typed/rbac/v1"
  20. )
  21. // +k8s:deepcopy-gen=true
  22. // +k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/pkg/registry/rbac/reconciliation.RuleOwner
  23. // +k8s:deepcopy-gen:nonpointer-interfaces=true
  24. type RoleRuleOwner struct {
  25. Role *rbacv1.Role
  26. }
  27. func (o RoleRuleOwner) GetObject() runtime.Object {
  28. return o.Role
  29. }
  30. func (o RoleRuleOwner) GetNamespace() string {
  31. return o.Role.Namespace
  32. }
  33. func (o RoleRuleOwner) GetName() string {
  34. return o.Role.Name
  35. }
  36. func (o RoleRuleOwner) GetLabels() map[string]string {
  37. return o.Role.Labels
  38. }
  39. func (o RoleRuleOwner) SetLabels(in map[string]string) {
  40. o.Role.Labels = in
  41. }
  42. func (o RoleRuleOwner) GetAnnotations() map[string]string {
  43. return o.Role.Annotations
  44. }
  45. func (o RoleRuleOwner) SetAnnotations(in map[string]string) {
  46. o.Role.Annotations = in
  47. }
  48. func (o RoleRuleOwner) GetRules() []rbacv1.PolicyRule {
  49. return o.Role.Rules
  50. }
  51. func (o RoleRuleOwner) SetRules(in []rbacv1.PolicyRule) {
  52. o.Role.Rules = in
  53. }
  54. func (o RoleRuleOwner) GetAggregationRule() *rbacv1.AggregationRule {
  55. return nil
  56. }
  57. func (o RoleRuleOwner) SetAggregationRule(in *rbacv1.AggregationRule) {
  58. }
  59. type RoleModifier struct {
  60. Client rbacv1client.RolesGetter
  61. NamespaceClient corev1client.NamespaceInterface
  62. }
  63. func (c RoleModifier) Get(namespace, name string) (RuleOwner, error) {
  64. ret, err := c.Client.Roles(namespace).Get(name, metav1.GetOptions{})
  65. if err != nil {
  66. return nil, err
  67. }
  68. return RoleRuleOwner{Role: ret}, err
  69. }
  70. func (c RoleModifier) Create(in RuleOwner) (RuleOwner, error) {
  71. if err := tryEnsureNamespace(c.NamespaceClient, in.GetNamespace()); err != nil {
  72. return nil, err
  73. }
  74. ret, err := c.Client.Roles(in.GetNamespace()).Create(in.(RoleRuleOwner).Role)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return RoleRuleOwner{Role: ret}, err
  79. }
  80. func (c RoleModifier) Update(in RuleOwner) (RuleOwner, error) {
  81. ret, err := c.Client.Roles(in.GetNamespace()).Update(in.(RoleRuleOwner).Role)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return RoleRuleOwner{Role: ret}, err
  86. }