@@ -18,6 +18,7 @@ package fuzzer
18
18
19
19
import (
20
20
"fmt"
21
+ "net/netip"
21
22
"time"
22
23
23
24
"github.com/google/gofuzz"
@@ -28,13 +29,63 @@ import (
28
29
"k8s.io/utils/ptr"
29
30
)
30
31
32
+ // generateRandomIP is copied from pkg/apis/networking/fuzzer/fuzzer.go
33
+ func generateRandomIP (is6 bool , c fuzz.Continue ) string {
34
+ n := 4
35
+ if is6 {
36
+ n = 16
37
+ }
38
+ bytes := make ([]byte , n )
39
+ for i := 0 ; i < n ; i ++ {
40
+ bytes [i ] = uint8 (c .Rand .Intn (255 ))
41
+ }
42
+
43
+ ip , ok := netip .AddrFromSlice (bytes )
44
+ if ok {
45
+ return ip .String ()
46
+ }
47
+ // this should not happen
48
+ panic (fmt .Sprintf ("invalid IP %v" , bytes ))
49
+ }
50
+
51
+ // generateRandomCIDR is copied from pkg/apis/networking/fuzzer/fuzzer.go
52
+ func generateRandomCIDR (is6 bool , c fuzz.Continue ) string {
53
+ ip , err := netip .ParseAddr (generateRandomIP (is6 , c ))
54
+ if err != nil {
55
+ // generateRandomIP already panics if returns a not valid ip
56
+ panic (err )
57
+ }
58
+
59
+ n := 32
60
+ if is6 {
61
+ n = 128
62
+ }
63
+
64
+ bits := c .Rand .Intn (n )
65
+ prefix := netip .PrefixFrom (ip , bits )
66
+ return prefix .Masked ().String ()
67
+ }
68
+
69
+ // getRandomDualStackCIDR returns a random dual-stack CIDR.
70
+ func getRandomDualStackCIDR (c fuzz.Continue ) []string {
71
+ cidrIPv4 := generateRandomCIDR (false , c )
72
+ cidrIPv6 := generateRandomCIDR (true , c )
73
+
74
+ cidrs := []string {cidrIPv4 , cidrIPv6 }
75
+ if c .RandBool () {
76
+ cidrs = []string {cidrIPv6 , cidrIPv4 }
77
+ }
78
+ return cidrs [:1 + c .Intn (2 )]
79
+ }
80
+
31
81
// Funcs returns the fuzzer functions for the kube-proxy apis.
32
82
func Funcs (codecs runtimeserializer.CodecFactory ) []interface {} {
33
83
return []interface {}{
34
84
func (obj * kubeproxyconfig.KubeProxyConfiguration , c fuzz.Continue ) {
35
85
c .FuzzNoCustom (obj )
36
86
obj .BindAddress = fmt .Sprintf ("%d.%d.%d.%d" , c .Intn (256 ), c .Intn (256 ), c .Intn (256 ), c .Intn (256 ))
37
87
obj .ClientConnection .ContentType = c .RandString ()
88
+ obj .DetectLocal .ClusterCIDRs = getRandomDualStackCIDR (c )
38
89
obj .Linux .Conntrack .MaxPerCore = ptr .To (c .Int31 ())
39
90
obj .Linux .Conntrack .Min = ptr .To (c .Int31 ())
40
91
obj .Linux .Conntrack .TCPCloseWaitTimeout = & metav1.Duration {Duration : time .Duration (c .Int63 ()) * time .Hour }
0 commit comments