Repository: kubernetes/minikube (go)

Issue:
`network` doesn't use the default `IsPrivate` function provided by `net` pkg
### What Happened?

From go 1.17, `IsPrivate` was added to the net pkg. So we don't need `isSubnetPrivate` function (See discussion in #15517)

While trying to migrate to `IsPrivate`, it was discovered that [TestFreeSubnet](https://github.com/kubernetes/minikube/blob/master/pkg/network/network_test.go#L51) does not restore the mocks after each test case which has an effect on the subsequent tests giving incorrect test results and coverage.

For eg: [NonPrivateSubnet](https://github.com/kubernetes/minikube/blob/master/pkg/network/network_test.go#L51) test ends up using the mocked `inspect` function from the [FirstSubnetIPV6NetworkFound](https://github.com/kubernetes/minikube/blob/master/pkg/network/network_test.go#L86) test



### Attach the log file

NA

### Operating System

None

### Driver

N/A

Pull request description:
Use `IsPrivate` function provided by `net` pkg
Fixes #15544

Removed `isSubnetPrivate` function and started using the default `IsPrivate` function provided by the `net` pkg. Updated test cases and added restoration of mocked functions to avoid side effects on subsequent tests.


Patch:
diff --git a/pkg/network/network.go b/pkg/network/network.go
index f7772183d..f0a35ed1a 100644
--- a/pkg/network/network.go
+++ b/pkg/network/network.go
@@ -28,28 +28,7 @@ import (
 
 const defaultReservationPeriod = 1 * time.Minute
 
-var (
-	reservedSubnets = sync.Map{}
-
-	// valid private network subnets (RFC1918)
-	privateSubnets = []net.IPNet{
-		// 10.0.0.0/8
-		{
-			IP:   []byte{10, 0, 0, 0},
-			Mask: []byte{255, 0, 0, 0},
-		},
-		// 172.16.0.0/12
-		{
-			IP:   []byte{172, 16, 0, 0},
-			Mask: []byte{255, 240, 0, 0},
-		},
-		// 192.168.0.0/16
-		{
-			IP:   []byte{192, 168, 0, 0},
-			Mask: []byte{255, 255, 0, 0},
-		},
-	}
-)
+var reservedSubnets = sync.Map{}
 
 // reservation of free private subnet is held for defined reservation period from createdAt time.
 type reservation struct {
@@ -66,6 +45,7 @@ type Parameters struct {
 	ClientMin string // second IP address
 	ClientMax string // last IP address before broadcast
 	Broadcast string // last IP address
+	IsPrivate bool   // whether the IP is private or not
 	Interface
 }
 
@@ -158,6 +138,7 @@ var inspect = func(addr string) (*Parameters, error) {
 	n.Netmask = net.IP(network.Mask).String() // dotted-decimal format ('a.b.c.d')
 	n.Prefix, _ = network.Mask.Size()
 	n.CIDR = network.String()
+	n.IsPrivate = network.IP.IsPrivate()
 
 	networkIP := binary.BigEndian.Uint32(network.IP)                      // IP address of network
 	networkMask := binary.BigEndian.Uint32(network.Mask)                  // network mask
@@ -205,16 +186,6 @@ var isSubnetTaken = func(subnet string) (bool, error) {
 	return false, nil
 }
 
-// isSubnetPrivate returns if subnet is private network.
-func isSubnetPrivate(subnet string) bool {
-	for _, ipnet := range privateSubnets {
-		if ipnet.Contains(net.ParseIP(subnet)) {
-			return true
-		}
-	}
-	return false
-}
-
 // IsUser returns if network is user.
 func IsUser(network string) bool {
 	return network == "user"
@@ -229,7 +200,7 @@ func FreeSubnet(startSubnet string, step, tries int) (*Parameters, error) {
 			return nil, err
 		}
 		subnet := n.IP
-		if isSubnetPrivate(subnet) {
+		if n.IsPrivate {
 			taken, err := isSubnetTaken(subnet)
 			if err != nil {
 				return nil, err


Test patch:
diff --git a/pkg/network/network_test.go b/pkg/network/network_test.go
index f2d995d6c..5390bf5f3 100644
--- a/pkg/network/network_test.go
+++ b/pkg/network/network_test.go
@@ -22,32 +22,6 @@ import (
 	"time"
 )
 
-func TestIsSubnetPrivate(t *testing.T) {
-	tests := []struct {
-		subnet   string
-		expected bool
-	}{
-		{"9.255.255.255", false},
-		{"10.0.0.0", true},
-		{"10.255.255.255", true},
-		{"11.0.0.0", false},
-		{"172.15.255.255", false},
-		{"172.16.0.0", true},
-		{"172.31.255.255", true},
-		{"172.32.0.0", false},
-		{"192.167.255.255", false},
-		{"192.168.0.0", true},
-		{"192.168.255.255", true},
-		{"192.169.0.0", false},
-	}
-	for _, test := range tests {
-		got := isSubnetPrivate(test.subnet)
-		if got != test.expected {
-			t.Errorf("isSubnetPrivate(%q) = %t; expected = %t", test.subnet, got, test.expected)
-		}
-	}
-}
-
 func TestFreeSubnet(t *testing.T) {
 	reserveSubnet = func(subnet string, period time.Duration) bool { return true }
 
@@ -65,6 +39,11 @@ func TestFreeSubnet(t *testing.T) {
 
 	t.Run("FirstSubnetTaken", func(t *testing.T) {
 		count := 0
+		originalIsSubnetTaken := isSubnetTaken
+		defer func() {
+			isSubnetTaken = originalIsSubnetTaken
+		}()
+
 		isSubnetTaken = func(subnet string) (bool, error) {
 			count++
 			return count == 1, nil
@@ -83,11 +62,17 @@ func TestFreeSubnet(t *testing.T) {
 
 	t.Run("FirstSubnetIPV6NetworkFound", func(t *testing.T) {
 		count := 0
+		originalInspect := inspect
+		defer func() {
+			inspect = originalInspect
+		}()
+
 		inspect = func(addr string) (*Parameters, error) {
 			count++
-			p := &Parameters{IP: addr}
+			p := &Parameters{IP: addr, IsPrivate: true}
 			if count == 1 {
 				p.IP = "0.0.0.0"
+				p.IsPrivate = false
 			}
 			return p, nil
 		}