k8s.io/kubernetes@v1.29.3/pkg/apis/autoscaling/fuzzer/fuzzer.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package fuzzer 18 19 import ( 20 fuzz "github.com/google/gofuzz" 21 22 "k8s.io/apimachinery/pkg/api/resource" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" 25 "k8s.io/kubernetes/pkg/apis/autoscaling" 26 api "k8s.io/kubernetes/pkg/apis/core" 27 "k8s.io/utils/pointer" 28 ) 29 30 // Funcs returns the fuzzer functions for the autoscaling api group. 31 var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { 32 return []interface{}{ 33 func(s *autoscaling.ScaleStatus, c fuzz.Continue) { 34 c.FuzzNoCustom(s) // fuzz self without calling this function again 35 36 // ensure we have a valid selector 37 metaSelector := &metav1.LabelSelector{} 38 c.Fuzz(metaSelector) 39 labelSelector, _ := metav1.LabelSelectorAsSelector(metaSelector) 40 s.Selector = labelSelector.String() 41 }, 42 func(s *autoscaling.HorizontalPodAutoscalerSpec, c fuzz.Continue) { 43 c.FuzzNoCustom(s) // fuzz self without calling this function again 44 s.MinReplicas = pointer.Int32(c.Rand.Int31()) 45 46 randomQuantity := func() resource.Quantity { 47 var q resource.Quantity 48 c.Fuzz(&q) 49 // precalc the string for benchmarking purposes 50 _ = q.String() 51 return q 52 } 53 54 var podMetricID autoscaling.MetricIdentifier 55 var objMetricID autoscaling.MetricIdentifier 56 c.Fuzz(&podMetricID) 57 c.Fuzz(&objMetricID) 58 59 targetUtilization := int32(c.RandUint64()) 60 averageValue := randomQuantity() 61 s.Metrics = []autoscaling.MetricSpec{ 62 { 63 Type: autoscaling.PodsMetricSourceType, 64 Pods: &autoscaling.PodsMetricSource{ 65 Metric: podMetricID, 66 Target: autoscaling.MetricTarget{ 67 Type: autoscaling.AverageValueMetricType, 68 AverageValue: &averageValue, 69 }, 70 }, 71 }, 72 { 73 Type: autoscaling.ObjectMetricSourceType, 74 Object: &autoscaling.ObjectMetricSource{ 75 Metric: objMetricID, 76 Target: autoscaling.MetricTarget{ 77 Type: autoscaling.ValueMetricType, 78 Value: &averageValue, 79 }, 80 }, 81 }, 82 { 83 Type: autoscaling.ResourceMetricSourceType, 84 Resource: &autoscaling.ResourceMetricSource{ 85 Name: api.ResourceCPU, 86 Target: autoscaling.MetricTarget{ 87 Type: autoscaling.UtilizationMetricType, 88 AverageUtilization: &targetUtilization, 89 }, 90 }, 91 }, 92 } 93 stabilizationWindow := int32(c.RandUint64()) 94 maxPolicy := autoscaling.MaxPolicySelect 95 minPolicy := autoscaling.MinPolicySelect 96 s.Behavior = &autoscaling.HorizontalPodAutoscalerBehavior{ 97 ScaleUp: &autoscaling.HPAScalingRules{ 98 StabilizationWindowSeconds: &stabilizationWindow, 99 SelectPolicy: &maxPolicy, 100 Policies: []autoscaling.HPAScalingPolicy{ 101 { 102 Type: autoscaling.PodsScalingPolicy, 103 Value: int32(c.RandUint64()), 104 PeriodSeconds: int32(c.RandUint64()), 105 }, 106 { 107 Type: autoscaling.PercentScalingPolicy, 108 Value: int32(c.RandUint64()), 109 PeriodSeconds: int32(c.RandUint64()), 110 }, 111 }, 112 }, 113 ScaleDown: &autoscaling.HPAScalingRules{ 114 StabilizationWindowSeconds: &stabilizationWindow, 115 SelectPolicy: &minPolicy, 116 Policies: []autoscaling.HPAScalingPolicy{ 117 { 118 Type: autoscaling.PodsScalingPolicy, 119 Value: int32(c.RandUint64()), 120 PeriodSeconds: int32(c.RandUint64()), 121 }, 122 { 123 Type: autoscaling.PercentScalingPolicy, 124 Value: int32(c.RandUint64()), 125 PeriodSeconds: int32(c.RandUint64()), 126 }, 127 }, 128 }, 129 } 130 }, 131 func(s *autoscaling.HorizontalPodAutoscalerStatus, c fuzz.Continue) { 132 c.FuzzNoCustom(s) // fuzz self without calling this function again 133 randomQuantity := func() resource.Quantity { 134 var q resource.Quantity 135 c.Fuzz(&q) 136 // precalc the string for benchmarking purposes 137 _ = q.String() 138 return q 139 } 140 averageValue := randomQuantity() 141 currentUtilization := int32(c.RandUint64()) 142 s.CurrentMetrics = []autoscaling.MetricStatus{ 143 { 144 Type: autoscaling.PodsMetricSourceType, 145 Pods: &autoscaling.PodsMetricStatus{ 146 Metric: autoscaling.MetricIdentifier{ 147 Name: c.RandString(), 148 }, 149 Current: autoscaling.MetricValueStatus{ 150 AverageValue: &averageValue, 151 }, 152 }, 153 }, 154 { 155 Type: autoscaling.ResourceMetricSourceType, 156 Resource: &autoscaling.ResourceMetricStatus{ 157 Name: api.ResourceCPU, 158 Current: autoscaling.MetricValueStatus{ 159 AverageUtilization: ¤tUtilization, 160 AverageValue: &averageValue, 161 }, 162 }, 163 }, 164 } 165 }, 166 } 167 }