fuzzer.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright 2017 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 fuzzer
  14. import (
  15. fuzz "github.com/google/gofuzz"
  16. "k8s.io/apimachinery/pkg/api/resource"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
  19. "k8s.io/kubernetes/pkg/apis/autoscaling"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. )
  22. // Funcs returns the fuzzer functions for the autoscaling api group.
  23. var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} {
  24. return []interface{}{
  25. func(s *autoscaling.ScaleStatus, c fuzz.Continue) {
  26. c.FuzzNoCustom(s) // fuzz self without calling this function again
  27. // ensure we have a valid selector
  28. metaSelector := &metav1.LabelSelector{}
  29. c.Fuzz(metaSelector)
  30. labelSelector, _ := metav1.LabelSelectorAsSelector(metaSelector)
  31. s.Selector = labelSelector.String()
  32. },
  33. func(s *autoscaling.HorizontalPodAutoscalerSpec, c fuzz.Continue) {
  34. c.FuzzNoCustom(s) // fuzz self without calling this function again
  35. minReplicas := int32(c.Rand.Int31())
  36. s.MinReplicas = &minReplicas
  37. randomQuantity := func() resource.Quantity {
  38. var q resource.Quantity
  39. c.Fuzz(&q)
  40. // precalc the string for benchmarking purposes
  41. _ = q.String()
  42. return q
  43. }
  44. var podMetricID autoscaling.MetricIdentifier
  45. var objMetricID autoscaling.MetricIdentifier
  46. c.Fuzz(&podMetricID)
  47. c.Fuzz(&objMetricID)
  48. targetUtilization := int32(c.RandUint64())
  49. averageValue := randomQuantity()
  50. s.Metrics = []autoscaling.MetricSpec{
  51. {
  52. Type: autoscaling.PodsMetricSourceType,
  53. Pods: &autoscaling.PodsMetricSource{
  54. Metric: podMetricID,
  55. Target: autoscaling.MetricTarget{
  56. Type: autoscaling.AverageValueMetricType,
  57. AverageValue: &averageValue,
  58. },
  59. },
  60. },
  61. {
  62. Type: autoscaling.ObjectMetricSourceType,
  63. Object: &autoscaling.ObjectMetricSource{
  64. Metric: objMetricID,
  65. Target: autoscaling.MetricTarget{
  66. Type: autoscaling.ValueMetricType,
  67. Value: &averageValue,
  68. },
  69. },
  70. },
  71. {
  72. Type: autoscaling.ResourceMetricSourceType,
  73. Resource: &autoscaling.ResourceMetricSource{
  74. Name: api.ResourceCPU,
  75. Target: autoscaling.MetricTarget{
  76. Type: autoscaling.UtilizationMetricType,
  77. AverageUtilization: &targetUtilization,
  78. },
  79. },
  80. },
  81. }
  82. stabilizationWindow := int32(c.RandUint64())
  83. maxPolicy := autoscaling.MaxPolicySelect
  84. minPolicy := autoscaling.MinPolicySelect
  85. s.Behavior = &autoscaling.HorizontalPodAutoscalerBehavior{
  86. ScaleUp: &autoscaling.HPAScalingRules{
  87. StabilizationWindowSeconds: &stabilizationWindow,
  88. SelectPolicy: &maxPolicy,
  89. Policies: []autoscaling.HPAScalingPolicy{
  90. {
  91. Type: autoscaling.PodsScalingPolicy,
  92. Value: int32(c.RandUint64()),
  93. PeriodSeconds: int32(c.RandUint64()),
  94. },
  95. {
  96. Type: autoscaling.PercentScalingPolicy,
  97. Value: int32(c.RandUint64()),
  98. PeriodSeconds: int32(c.RandUint64()),
  99. },
  100. },
  101. },
  102. ScaleDown: &autoscaling.HPAScalingRules{
  103. StabilizationWindowSeconds: &stabilizationWindow,
  104. SelectPolicy: &minPolicy,
  105. Policies: []autoscaling.HPAScalingPolicy{
  106. {
  107. Type: autoscaling.PodsScalingPolicy,
  108. Value: int32(c.RandUint64()),
  109. PeriodSeconds: int32(c.RandUint64()),
  110. },
  111. {
  112. Type: autoscaling.PercentScalingPolicy,
  113. Value: int32(c.RandUint64()),
  114. PeriodSeconds: int32(c.RandUint64()),
  115. },
  116. },
  117. },
  118. }
  119. },
  120. func(s *autoscaling.HorizontalPodAutoscalerStatus, c fuzz.Continue) {
  121. c.FuzzNoCustom(s) // fuzz self without calling this function again
  122. randomQuantity := func() resource.Quantity {
  123. var q resource.Quantity
  124. c.Fuzz(&q)
  125. // precalc the string for benchmarking purposes
  126. _ = q.String()
  127. return q
  128. }
  129. averageValue := randomQuantity()
  130. currentUtilization := int32(c.RandUint64())
  131. s.CurrentMetrics = []autoscaling.MetricStatus{
  132. {
  133. Type: autoscaling.PodsMetricSourceType,
  134. Pods: &autoscaling.PodsMetricStatus{
  135. Metric: autoscaling.MetricIdentifier{
  136. Name: c.RandString(),
  137. },
  138. Current: autoscaling.MetricValueStatus{
  139. AverageValue: &averageValue,
  140. },
  141. },
  142. },
  143. {
  144. Type: autoscaling.ResourceMetricSourceType,
  145. Resource: &autoscaling.ResourceMetricStatus{
  146. Name: api.ResourceCPU,
  147. Current: autoscaling.MetricValueStatus{
  148. AverageUtilization: &currentUtilization,
  149. AverageValue: &averageValue,
  150. },
  151. },
  152. },
  153. }
  154. },
  155. }
  156. }