pdb.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. policy "k8s.io/api/policy/v1beta1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/util/intstr"
  20. "k8s.io/kubernetes/pkg/kubectl/generate"
  21. )
  22. // PodDisruptionBudgetV1Generator supports stable generation of a pod disruption budget.
  23. type PodDisruptionBudgetV1Generator struct {
  24. Name string
  25. MinAvailable string
  26. Selector string
  27. }
  28. // Ensure it supports the generator pattern that uses parameters specified during construction.
  29. var _ generate.StructuredGenerator = &PodDisruptionBudgetV1Generator{}
  30. func (PodDisruptionBudgetV1Generator) ParamNames() []generate.GeneratorParam {
  31. return []generate.GeneratorParam{
  32. {Name: "name", Required: true},
  33. {Name: "min-available", Required: false},
  34. {Name: "selector", Required: true},
  35. }
  36. }
  37. func (s PodDisruptionBudgetV1Generator) Generate(params map[string]interface{}) (runtime.Object, error) {
  38. err := generate.ValidateParams(s.ParamNames(), params)
  39. if err != nil {
  40. return nil, err
  41. }
  42. name, isString := params["name"].(string)
  43. if !isString {
  44. return nil, fmt.Errorf("expected string, found %T for 'name'", params["name"])
  45. }
  46. minAvailable, isString := params["min-available"].(string)
  47. if !isString {
  48. return nil, fmt.Errorf("expected string, found %T for 'min-available'", params["min-available"])
  49. }
  50. selector, isString := params["selector"].(string)
  51. if !isString {
  52. return nil, fmt.Errorf("expected string, found %T for 'selector'", params["selector"])
  53. }
  54. delegate := &PodDisruptionBudgetV1Generator{Name: name, MinAvailable: minAvailable, Selector: selector}
  55. return delegate.StructuredGenerate()
  56. }
  57. // StructuredGenerate outputs a pod disruption budget object using the configured fields.
  58. func (s *PodDisruptionBudgetV1Generator) StructuredGenerate() (runtime.Object, error) {
  59. if len(s.MinAvailable) == 0 {
  60. // defaulting behavior seen in Kubernetes 1.6 and below.
  61. s.MinAvailable = "1"
  62. }
  63. if err := s.validate(); err != nil {
  64. return nil, err
  65. }
  66. selector, err := metav1.ParseToLabelSelector(s.Selector)
  67. if err != nil {
  68. return nil, err
  69. }
  70. minAvailable := intstr.Parse(s.MinAvailable)
  71. return &policy.PodDisruptionBudget{
  72. ObjectMeta: metav1.ObjectMeta{
  73. Name: s.Name,
  74. },
  75. Spec: policy.PodDisruptionBudgetSpec{
  76. MinAvailable: &minAvailable,
  77. Selector: selector,
  78. },
  79. }, nil
  80. }
  81. // validate validates required fields are set to support structured generation.
  82. func (s *PodDisruptionBudgetV1Generator) validate() error {
  83. if len(s.Name) == 0 {
  84. return fmt.Errorf("name must be specified")
  85. }
  86. if len(s.Selector) == 0 {
  87. return fmt.Errorf("a selector must be specified")
  88. }
  89. if len(s.MinAvailable) == 0 {
  90. return fmt.Errorf("the minimum number of available pods required must be specified")
  91. }
  92. return nil
  93. }
  94. // PodDisruptionBudgetV2Generator supports stable generation of a pod disruption budget.
  95. type PodDisruptionBudgetV2Generator struct {
  96. Name string
  97. MinAvailable string
  98. MaxUnavailable string
  99. Selector string
  100. }
  101. // Ensure it supports the generator pattern that uses parameters specified during construction.
  102. var _ generate.StructuredGenerator = &PodDisruptionBudgetV2Generator{}
  103. func (PodDisruptionBudgetV2Generator) ParamNames() []generate.GeneratorParam {
  104. return []generate.GeneratorParam{
  105. {Name: "name", Required: true},
  106. {Name: "min-available", Required: false},
  107. {Name: "max-unavailable", Required: false},
  108. {Name: "selector", Required: true},
  109. }
  110. }
  111. func (s PodDisruptionBudgetV2Generator) Generate(params map[string]interface{}) (runtime.Object, error) {
  112. err := generate.ValidateParams(s.ParamNames(), params)
  113. if err != nil {
  114. return nil, err
  115. }
  116. name, isString := params["name"].(string)
  117. if !isString {
  118. return nil, fmt.Errorf("expected string, found %T for 'name'", params["name"])
  119. }
  120. minAvailable, isString := params["min-available"].(string)
  121. if !isString {
  122. return nil, fmt.Errorf("expected string, found %T for 'min-available'", params["min-available"])
  123. }
  124. maxUnavailable, isString := params["max-unavailable"].(string)
  125. if !isString {
  126. return nil, fmt.Errorf("expected string, found %T for 'max-unavailable'", params["max-unavailable"])
  127. }
  128. selector, isString := params["selector"].(string)
  129. if !isString {
  130. return nil, fmt.Errorf("expected string, found %T for 'selector'", params["selector"])
  131. }
  132. delegate := &PodDisruptionBudgetV2Generator{Name: name, MinAvailable: minAvailable, MaxUnavailable: maxUnavailable, Selector: selector}
  133. return delegate.StructuredGenerate()
  134. }
  135. // StructuredGenerate outputs a pod disruption budget object using the configured fields.
  136. func (s *PodDisruptionBudgetV2Generator) StructuredGenerate() (runtime.Object, error) {
  137. if err := s.validate(); err != nil {
  138. return nil, err
  139. }
  140. selector, err := metav1.ParseToLabelSelector(s.Selector)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if len(s.MaxUnavailable) > 0 {
  145. maxUnavailable := intstr.Parse(s.MaxUnavailable)
  146. return &policy.PodDisruptionBudget{
  147. ObjectMeta: metav1.ObjectMeta{
  148. Name: s.Name,
  149. },
  150. Spec: policy.PodDisruptionBudgetSpec{
  151. MaxUnavailable: &maxUnavailable,
  152. Selector: selector,
  153. },
  154. }, nil
  155. }
  156. if len(s.MinAvailable) > 0 {
  157. minAvailable := intstr.Parse(s.MinAvailable)
  158. return &policy.PodDisruptionBudget{
  159. ObjectMeta: metav1.ObjectMeta{
  160. Name: s.Name,
  161. },
  162. Spec: policy.PodDisruptionBudgetSpec{
  163. MinAvailable: &minAvailable,
  164. Selector: selector,
  165. },
  166. }, nil
  167. }
  168. return nil, err
  169. }
  170. // validate validates required fields are set to support structured generation.
  171. func (s *PodDisruptionBudgetV2Generator) validate() error {
  172. if len(s.Name) == 0 {
  173. return fmt.Errorf("name must be specified")
  174. }
  175. if len(s.Selector) == 0 {
  176. return fmt.Errorf("a selector must be specified")
  177. }
  178. if len(s.MaxUnavailable) == 0 && len(s.MinAvailable) == 0 {
  179. return fmt.Errorf("one of min-available or max-unavailable must be specified")
  180. }
  181. if len(s.MaxUnavailable) > 0 && len(s.MinAvailable) > 0 {
  182. return fmt.Errorf("min-available and max-unavailable cannot be both specified")
  183. }
  184. return nil
  185. }