fed_rolebinding.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package k8s
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. )
  19. const (
  20. RBACAPIGroup = "rbac.authorization.k8s.io"
  21. )
  22. type FedRoleBindingCreateOpt struct {
  23. FedNamespaceResourceCreateOptions
  24. RoleRef RoleRef `json:"roleRef"`
  25. Subject Subject `help:"Subject is role bind subject, e.g: User=jane"`
  26. }
  27. func (o *FedRoleBindingCreateOpt) ToInput() (*FedRoleBindingCreateInput, error) {
  28. input := &FedRoleBindingCreateInput{
  29. FedNamespaceResourceCreateOptions: o.FedNamespaceResourceCreateOptions,
  30. }
  31. if err := validateRoleRef(&o.RoleRef); err != nil {
  32. return nil, err
  33. }
  34. if err := validateSubject(&o.Subject); err != nil {
  35. return nil, err
  36. }
  37. input.Spec.Template.RoleRef = o.RoleRef
  38. subs := []Subject{o.Subject}
  39. input.Spec.Template.Subjects = subs
  40. return input, nil
  41. }
  42. func (o *FedRoleBindingCreateOpt) Params() (jsonutils.JSONObject, error) {
  43. input, err := o.ToInput()
  44. if err != nil {
  45. return nil, err
  46. }
  47. return input.JSON(input), nil
  48. }
  49. func validateRoleRef(ref *RoleRef) error {
  50. if ref.APIGroup == "" {
  51. ref.APIGroup = RBACAPIGroup
  52. }
  53. if ref.Kind == "" {
  54. return errors.Errorf("roleRef kind must specified")
  55. }
  56. if ref.Name == "" {
  57. return errors.Errorf("roleRef name must specified")
  58. }
  59. return nil
  60. }
  61. func validateSubject(sub *Subject) error {
  62. if sub.APIGroup == "" {
  63. sub.APIGroup = RBACAPIGroup
  64. }
  65. if sub.Kind == "" {
  66. return errors.Errorf("subject kind must specified")
  67. }
  68. if sub.Name == "" {
  69. return errors.Errorf("subject name must specified")
  70. }
  71. return nil
  72. }
  73. type RoleRef struct {
  74. Kind string `help:"Role kind" choices:"ClusterRole|Role" json:"kind"`
  75. Name string `help:"Name is the name of role" json:"name"`
  76. APIGroup string `json:"apiGroup"`
  77. }
  78. type FedRoleBindingCreateInput struct {
  79. FedNamespaceResourceCreateOptions
  80. Spec FedRoleBindingSpec `json:"spec"`
  81. }
  82. type FedRoleBindingSpec struct {
  83. Template RoleBindingTemplate `json:"template"`
  84. }
  85. type RoleBindingTemplate struct {
  86. RoleRef RoleRef `json:"roleRef"`
  87. Subjects []Subject `json:"subjects"`
  88. }
  89. type Subject struct {
  90. Kind string `json:"kind"`
  91. APIGroup string `json:"apiGroup"`
  92. Name string `json:"name"`
  93. Namespace string `json:"namespace"`
  94. }