Skip to content

Commit bee5e03

Browse files
authored
Merge pull request kubernetes#126333 from aroradaman/master
kube-proxy: internal config: fuzz cidr values for unit tests
2 parents b4dcbbe + 5359098 commit bee5e03

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pkg/proxy/apis/config/fuzzer/fuzzer.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package fuzzer
1818

1919
import (
2020
"fmt"
21+
"net/netip"
2122
"time"
2223

2324
"github.com/google/gofuzz"
@@ -28,13 +29,63 @@ import (
2829
"k8s.io/utils/ptr"
2930
)
3031

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+
3181
// Funcs returns the fuzzer functions for the kube-proxy apis.
3282
func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
3383
return []interface{}{
3484
func(obj *kubeproxyconfig.KubeProxyConfiguration, c fuzz.Continue) {
3585
c.FuzzNoCustom(obj)
3686
obj.BindAddress = fmt.Sprintf("%d.%d.%d.%d", c.Intn(256), c.Intn(256), c.Intn(256), c.Intn(256))
3787
obj.ClientConnection.ContentType = c.RandString()
88+
obj.DetectLocal.ClusterCIDRs = getRandomDualStackCIDR(c)
3889
obj.Linux.Conntrack.MaxPerCore = ptr.To(c.Int31())
3990
obj.Linux.Conntrack.Min = ptr.To(c.Int31())
4091
obj.Linux.Conntrack.TCPCloseWaitTimeout = &metav1.Duration{Duration: time.Duration(c.Int63()) * time.Hour}

0 commit comments

Comments
 (0)