Repository: lima-vm/lima

Issue:
Creating a new bridged network results in an error.
### Description

When trying to use the command `limactl network create` to create a bridged network, it results in a error
`failed to parse CIDR "/24": invalid CIDR address: /24 `

I'm assuming the error is here because you cannot add gateway / IP ranges as they are managed outside. https://github.com/lima-vm/lima/blob/bd955e4d4c680984fd3e61bd3d32d35558ee1773/cmd/limactl/network.go#L177-L207 

Discussion:
@nirs: @dharsanb can you show the full command that fails? @dharsanb: Sorry,
Should've posted the full error
```bash
# Command
limactl network create inf --mode=bridged --interface en8
# Error
## FATA[0000] failed to parse CIDR "/24": invalid CIDR address: /24
```

Solution patch:
diff --git a/cmd/limactl/network.go b/cmd/limactl/network.go
index 6a782134..492237c4 100644
--- a/cmd/limactl/network.go
+++ b/cmd/limactl/network.go
@@ -182,6 +182,8 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
 		if intf == "" {
 			return fmt.Errorf("network mode %q requires specifying interface", mode)
 		}
+		yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"interface":%q}`, name, mode, intf)
+		return networkApplyYQ(yq)
 	default:
 		if gateway == "" {
 			return fmt.Errorf("network mode %q requires specifying gateway", mode)
@@ -189,27 +191,25 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
 		if intf != "" {
 			return fmt.Errorf("network mode %q does not support specifying interface", mode)
 		}
-	}
+		if !strings.Contains(gateway, "/") {
+			gateway += "/24"
+		}
+		gwIP, gwMask, err := net.ParseCIDR(gateway)
+		if err != nil {
+			return fmt.Errorf("failed to parse CIDR %q: %w", gateway, err)
+		}
+		if gwIP.IsUnspecified() || gwIP.IsLoopback() {
+			return fmt.Errorf("invalid IP address: %v", gwIP)
+		}
+		gwMaskStr := "255.255.255.0"
+		if gwMask != nil {
+			gwMaskStr = net.IP(gwMask.Mask).String()
+		}
+		// TODO: check IP range collision
 
-	if !strings.Contains(gateway, "/") {
-		gateway += "/24"
-	}
-	gwIP, gwMask, err := net.ParseCIDR(gateway)
-	if err != nil {
-		return fmt.Errorf("failed to parse CIDR %q: %w", gateway, err)
-	}
-	if gwIP.IsUnspecified() || gwIP.IsLoopback() {
-		return fmt.Errorf("invalid IP address: %v", gwIP)
-	}
-	gwMaskStr := "255.255.255.0"
-	if gwMask != nil {
-		gwMaskStr = net.IP(gwMask.Mask).String()
+		yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"gateway":%q,"netmask":%q,"interface":%q}`, name, mode, gwIP.String(), gwMaskStr, intf)
+		return networkApplyYQ(yq)
 	}
-	// TODO: check IP range collision
-
-	yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"gateway":%q,"netmask":%q,"interface":%q}`, name, mode, gwIP.String(), gwMaskStr, intf)
-
-	return networkApplyYQ(yq)
 }
 
 func networkApplyYQ(yq string) error {
diff --git a/pkg/guestagent/kubernetesservice/kubernetesservice.go b/pkg/guestagent/kubernetesservice/kubernetesservice.go
index 843c0b3c..d84ee1a2 100644
--- a/pkg/guestagent/kubernetesservice/kubernetesservice.go
+++ b/pkg/guestagent/kubernetesservice/kubernetesservice.go
@@ -149,7 +149,7 @@ func (s *ServiceWatcher) GetPorts() []Entry {
 
 			entries = append(entries, Entry{
 				Protocol: Protocol(strings.ToLower(string(portEntry.Protocol))),
-				IP:       net.ParseIP("0.0.0.0"),
+				IP:       net.IPv4zero,
 				Port:     uint16(port),
 			})
 		}



Test patch:
diff --git a/pkg/guestagent/kubernetesservice/kubernetesservice_test.go b/pkg/guestagent/kubernetesservice/kubernetesservice_test.go
index 94630ecf..7c0303cc 100644
--- a/pkg/guestagent/kubernetesservice/kubernetesservice_test.go
+++ b/pkg/guestagent/kubernetesservice/kubernetesservice_test.go
@@ -63,7 +63,7 @@ func TestGetPorts(t *testing.T) {
 			},
 			want: []Entry{{
 				Protocol: TCP,
-				IP:       net.ParseIP("0.0.0.0"),
+				IP:       net.IPv4zero,
 				Port:     8080,
 			}},
 		},
@@ -85,7 +85,7 @@ func TestGetPorts(t *testing.T) {
 			},
 			want: []Entry{{
 				Protocol: TCP,
-				IP:       net.ParseIP("0.0.0.0"),
+				IP:       net.IPv4zero,
 				Port:     8081,
 			}},
 		},