rolebinding.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 versioned
  14. import (
  15. "fmt"
  16. "strings"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. "k8s.io/kubernetes/pkg/kubectl/generate"
  21. )
  22. // RoleBindingGeneratorV1 supports stable generation of a roleBinding.
  23. type RoleBindingGeneratorV1 struct {
  24. // Name of roleBinding (required)
  25. Name string
  26. // ClusterRole for the roleBinding
  27. ClusterRole string
  28. // Role for the roleBinding
  29. Role string
  30. // Users to derive the roleBinding from (optional)
  31. Users []string
  32. // Groups to derive the roleBinding from (optional)
  33. Groups []string
  34. // ServiceAccounts to derive the roleBinding from in namespace:name format(optional)
  35. ServiceAccounts []string
  36. }
  37. // Ensure it supports the generator pattern that uses parameter injection.
  38. var _ generate.Generator = &RoleBindingGeneratorV1{}
  39. // Ensure it supports the generator pattern that uses parameters specified during construction.
  40. var _ generate.StructuredGenerator = &RoleBindingGeneratorV1{}
  41. // Generate returns a roleBinding using the specified parameters.
  42. func (s RoleBindingGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  43. err := generate.ValidateParams(s.ParamNames(), genericParams)
  44. if err != nil {
  45. return nil, err
  46. }
  47. delegate := &RoleBindingGeneratorV1{}
  48. userStrings, found := genericParams["user"]
  49. if found {
  50. fromFileArray, isArray := userStrings.([]string)
  51. if !isArray {
  52. return nil, fmt.Errorf("expected []string, found :%v", userStrings)
  53. }
  54. delegate.Users = fromFileArray
  55. delete(genericParams, "user")
  56. }
  57. groupStrings, found := genericParams["group"]
  58. if found {
  59. fromLiteralArray, isArray := groupStrings.([]string)
  60. if !isArray {
  61. return nil, fmt.Errorf("expected []string, found :%v", groupStrings)
  62. }
  63. delegate.Groups = fromLiteralArray
  64. delete(genericParams, "group")
  65. }
  66. saStrings, found := genericParams["serviceaccount"]
  67. if found {
  68. fromLiteralArray, isArray := saStrings.([]string)
  69. if !isArray {
  70. return nil, fmt.Errorf("expected []string, found :%v", saStrings)
  71. }
  72. delegate.ServiceAccounts = fromLiteralArray
  73. delete(genericParams, "serviceaccount")
  74. }
  75. params := map[string]string{}
  76. for key, value := range genericParams {
  77. strVal, isString := value.(string)
  78. if !isString {
  79. return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
  80. }
  81. params[key] = strVal
  82. }
  83. delegate.Name = params["name"]
  84. delegate.ClusterRole = params["clusterrole"]
  85. delegate.Role = params["role"]
  86. return delegate.StructuredGenerate()
  87. }
  88. // ParamNames returns the set of supported input parameters when using the parameter injection generator pattern.
  89. func (s RoleBindingGeneratorV1) ParamNames() []generate.GeneratorParam {
  90. return []generate.GeneratorParam{
  91. {Name: "name", Required: true},
  92. {Name: "clusterrole", Required: false},
  93. {Name: "role", Required: false},
  94. {Name: "user", Required: false},
  95. {Name: "group", Required: false},
  96. {Name: "serviceaccount", Required: false},
  97. }
  98. }
  99. // StructuredGenerate outputs a roleBinding object using the configured fields.
  100. func (s RoleBindingGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  101. if err := s.validate(); err != nil {
  102. return nil, err
  103. }
  104. roleBinding := &rbacv1.RoleBinding{}
  105. roleBinding.Name = s.Name
  106. switch {
  107. case len(s.Role) > 0:
  108. roleBinding.RoleRef = rbacv1.RoleRef{
  109. APIGroup: rbacv1.GroupName,
  110. Kind: "Role",
  111. Name: s.Role,
  112. }
  113. case len(s.ClusterRole) > 0:
  114. roleBinding.RoleRef = rbacv1.RoleRef{
  115. APIGroup: rbacv1.GroupName,
  116. Kind: "ClusterRole",
  117. Name: s.ClusterRole,
  118. }
  119. }
  120. for _, user := range sets.NewString(s.Users...).List() {
  121. roleBinding.Subjects = append(roleBinding.Subjects, rbacv1.Subject{
  122. Kind: rbacv1.UserKind,
  123. APIGroup: rbacv1.GroupName,
  124. Name: user,
  125. })
  126. }
  127. for _, group := range sets.NewString(s.Groups...).List() {
  128. roleBinding.Subjects = append(roleBinding.Subjects, rbacv1.Subject{
  129. Kind: rbacv1.GroupKind,
  130. APIGroup: rbacv1.GroupName,
  131. Name: group,
  132. })
  133. }
  134. for _, sa := range sets.NewString(s.ServiceAccounts...).List() {
  135. tokens := strings.Split(sa, ":")
  136. if len(tokens) != 2 || tokens[1] == "" {
  137. return nil, fmt.Errorf("serviceaccount must be <namespace>:<name>")
  138. }
  139. roleBinding.Subjects = append(roleBinding.Subjects, rbacv1.Subject{
  140. Kind: rbacv1.ServiceAccountKind,
  141. APIGroup: "",
  142. Namespace: tokens[0],
  143. Name: tokens[1],
  144. })
  145. }
  146. return roleBinding, nil
  147. }
  148. // validate validates required fields are set to support structured generation.
  149. func (s RoleBindingGeneratorV1) validate() error {
  150. if len(s.Name) == 0 {
  151. return fmt.Errorf("name must be specified")
  152. }
  153. if (len(s.ClusterRole) == 0) == (len(s.Role) == 0) {
  154. return fmt.Errorf("exactly one of clusterrole or role must be specified")
  155. }
  156. return nil
  157. }