autoscale.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2015 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 kubectl
  14. import (
  15. "fmt"
  16. "strconv"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/apis/autoscaling"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. )
  21. type HorizontalPodAutoscalerV1Beta1 struct{}
  22. func (HorizontalPodAutoscalerV1Beta1) ParamNames() []GeneratorParam {
  23. return []GeneratorParam{
  24. {"default-name", true},
  25. {"name", false},
  26. {"scaleRef-kind", false},
  27. {"scaleRef-name", false},
  28. {"scaleRef-apiVersion", false},
  29. {"scaleRef-subresource", false},
  30. {"min", false},
  31. {"max", true},
  32. {"cpu-percent", false},
  33. }
  34. }
  35. func (HorizontalPodAutoscalerV1Beta1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  36. return generateHPA(genericParams)
  37. }
  38. type HorizontalPodAutoscalerV1 struct{}
  39. func (HorizontalPodAutoscalerV1) ParamNames() []GeneratorParam {
  40. return []GeneratorParam{
  41. {"default-name", true},
  42. {"name", false},
  43. {"scaleRef-kind", false},
  44. {"scaleRef-name", false},
  45. {"scaleRef-apiVersion", false},
  46. {"min", false},
  47. {"max", true},
  48. {"cpu-percent", false},
  49. }
  50. }
  51. func (HorizontalPodAutoscalerV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  52. return generateHPA(genericParams)
  53. }
  54. func generateHPA(genericParams map[string]interface{}) (runtime.Object, error) {
  55. params := map[string]string{}
  56. for key, value := range genericParams {
  57. strVal, isString := value.(string)
  58. if !isString {
  59. return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
  60. }
  61. params[key] = strVal
  62. }
  63. name, found := params["name"]
  64. if !found || len(name) == 0 {
  65. name, found = params["default-name"]
  66. if !found || len(name) == 0 {
  67. return nil, fmt.Errorf("'name' is a required parameter.")
  68. }
  69. }
  70. minString, found := params["min"]
  71. min := -1
  72. var err error
  73. if found {
  74. if min, err = strconv.Atoi(minString); err != nil {
  75. return nil, err
  76. }
  77. }
  78. maxString, found := params["max"]
  79. if !found {
  80. return nil, fmt.Errorf("'max' is a required parameter.")
  81. }
  82. max, err := strconv.Atoi(maxString)
  83. if err != nil {
  84. return nil, err
  85. }
  86. cpuString, found := params["cpu-percent"]
  87. cpu := -1
  88. if found {
  89. if cpu, err = strconv.Atoi(cpuString); err != nil {
  90. return nil, err
  91. }
  92. }
  93. scaler := autoscaling.HorizontalPodAutoscaler{
  94. ObjectMeta: api.ObjectMeta{
  95. Name: name,
  96. },
  97. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  98. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  99. Kind: params["scaleRef-kind"],
  100. Name: params["scaleRef-name"],
  101. APIVersion: params["scaleRef-apiVersion"],
  102. },
  103. MaxReplicas: int32(max),
  104. },
  105. }
  106. if min > 0 {
  107. v := int32(min)
  108. scaler.Spec.MinReplicas = &v
  109. }
  110. if cpu >= 0 {
  111. c := int32(cpu)
  112. scaler.Spec.TargetCPUUtilizationPercentage = &c
  113. }
  114. return &scaler, nil
  115. }