clusterrolebinding.go 5.2 KB

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