role_interfaces.go 2.8 KB

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