fuzzer.go 4.6 KB

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