k8s.io/kubernetes@v1.29.3/pkg/apis/apps/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 "fmt" 21 22 fuzz "github.com/google/gofuzz" 23 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" 26 "k8s.io/apimachinery/pkg/util/intstr" 27 "k8s.io/kubernetes/pkg/apis/apps" 28 ) 29 30 // Funcs returns the fuzzer functions for the apps api group. 31 var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { 32 return []interface{}{ 33 func(s *apps.StatefulSet, c fuzz.Continue) { 34 c.FuzzNoCustom(s) // fuzz self without calling this function again 35 36 // match defaulter 37 if len(s.Spec.PodManagementPolicy) == 0 { 38 s.Spec.PodManagementPolicy = apps.OrderedReadyPodManagement 39 } 40 if len(s.Spec.UpdateStrategy.Type) == 0 { 41 s.Spec.UpdateStrategy.Type = apps.RollingUpdateStatefulSetStrategyType 42 } 43 if s.Spec.PersistentVolumeClaimRetentionPolicy == nil { 44 s.Spec.PersistentVolumeClaimRetentionPolicy = &apps.StatefulSetPersistentVolumeClaimRetentionPolicy{} 45 } 46 if len(s.Spec.PersistentVolumeClaimRetentionPolicy.WhenDeleted) == 0 { 47 s.Spec.PersistentVolumeClaimRetentionPolicy.WhenDeleted = apps.RetainPersistentVolumeClaimRetentionPolicyType 48 } 49 if len(s.Spec.PersistentVolumeClaimRetentionPolicy.WhenScaled) == 0 { 50 s.Spec.PersistentVolumeClaimRetentionPolicy.WhenScaled = apps.RetainPersistentVolumeClaimRetentionPolicyType 51 } 52 if s.Spec.RevisionHistoryLimit == nil { 53 s.Spec.RevisionHistoryLimit = new(int32) 54 *s.Spec.RevisionHistoryLimit = 10 55 } 56 if s.Status.ObservedGeneration == nil { 57 s.Status.ObservedGeneration = new(int64) 58 } 59 if s.Status.CollisionCount == nil { 60 s.Status.CollisionCount = new(int32) 61 } 62 if s.Spec.Selector == nil { 63 s.Spec.Selector = &metav1.LabelSelector{MatchLabels: s.Spec.Template.Labels} 64 } 65 if len(s.Labels) == 0 { 66 s.Labels = s.Spec.Template.Labels 67 } 68 }, 69 func(j *apps.Deployment, c fuzz.Continue) { 70 c.FuzzNoCustom(j) 71 72 // match defaulting 73 if j.Spec.Selector == nil { 74 j.Spec.Selector = &metav1.LabelSelector{MatchLabels: j.Spec.Template.Labels} 75 } 76 if len(j.Labels) == 0 { 77 j.Labels = j.Spec.Template.Labels 78 } 79 }, 80 func(j *apps.DeploymentSpec, c fuzz.Continue) { 81 c.FuzzNoCustom(j) // fuzz self without calling this function again 82 rhl := int32(c.Rand.Int31()) 83 pds := int32(c.Rand.Int31()) 84 j.RevisionHistoryLimit = &rhl 85 j.ProgressDeadlineSeconds = &pds 86 }, 87 func(j *apps.DeploymentStrategy, c fuzz.Continue) { 88 c.FuzzNoCustom(j) // fuzz self without calling this function again 89 // Ensure that strategyType is one of valid values. 90 strategyTypes := []apps.DeploymentStrategyType{apps.RecreateDeploymentStrategyType, apps.RollingUpdateDeploymentStrategyType} 91 j.Type = strategyTypes[c.Rand.Intn(len(strategyTypes))] 92 if j.Type != apps.RollingUpdateDeploymentStrategyType { 93 j.RollingUpdate = nil 94 } else { 95 rollingUpdate := apps.RollingUpdateDeployment{} 96 if c.RandBool() { 97 rollingUpdate.MaxUnavailable = intstr.FromInt32(c.Rand.Int31()) 98 rollingUpdate.MaxSurge = intstr.FromInt32(c.Rand.Int31()) 99 } else { 100 rollingUpdate.MaxSurge = intstr.FromString(fmt.Sprintf("%d%%", c.Rand.Int31())) 101 } 102 j.RollingUpdate = &rollingUpdate 103 } 104 }, 105 func(j *apps.DaemonSet, c fuzz.Continue) { 106 c.FuzzNoCustom(j) 107 108 // match defaulter 109 j.Spec.Template.Generation = 0 110 if len(j.ObjectMeta.Labels) == 0 { 111 j.ObjectMeta.Labels = j.Spec.Template.ObjectMeta.Labels 112 } 113 }, 114 func(j *apps.DaemonSetSpec, c fuzz.Continue) { 115 c.FuzzNoCustom(j) // fuzz self without calling this function again 116 rhl := int32(c.Rand.Int31()) 117 j.RevisionHistoryLimit = &rhl 118 }, 119 func(j *apps.DaemonSetUpdateStrategy, c fuzz.Continue) { 120 c.FuzzNoCustom(j) // fuzz self without calling this function again 121 // Ensure that strategyType is one of valid values. 122 strategyTypes := []apps.DaemonSetUpdateStrategyType{apps.RollingUpdateDaemonSetStrategyType, apps.OnDeleteDaemonSetStrategyType} 123 j.Type = strategyTypes[c.Rand.Intn(len(strategyTypes))] 124 if j.Type != apps.RollingUpdateDaemonSetStrategyType { 125 j.RollingUpdate = nil 126 } else { 127 rollingUpdate := apps.RollingUpdateDaemonSet{} 128 if c.RandBool() { 129 if c.RandBool() { 130 rollingUpdate.MaxUnavailable = intstr.FromInt32(c.Rand.Int31()) 131 rollingUpdate.MaxSurge = intstr.FromInt32(c.Rand.Int31()) 132 } else { 133 rollingUpdate.MaxSurge = intstr.FromString(fmt.Sprintf("%d%%", c.Rand.Int31())) 134 } 135 } 136 137 j.RollingUpdate = &rollingUpdate 138 } 139 }, 140 func(j *apps.ReplicaSet, c fuzz.Continue) { 141 c.FuzzNoCustom(j) 142 143 // match defaulter 144 if j.Spec.Selector == nil { 145 j.Spec.Selector = &metav1.LabelSelector{MatchLabels: j.Spec.Template.Labels} 146 } 147 if len(j.Labels) == 0 { 148 j.Labels = j.Spec.Template.Labels 149 } 150 }, 151 } 152 }