Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --subnet validation #15530

Merged
merged 5 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 53 additions & 20 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/minikube/pkg/minikube/command"
netutil "k8s.io/minikube/pkg/network"

"k8s.io/klog/v2"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
Expand Down Expand Up @@ -1212,32 +1213,19 @@ func validateFlags(cmd *cobra.Command, drvName string) {

}

if cmd.Flags().Changed(containerRuntime) {
err := validateRuntime(viper.GetString(containerRuntime))
if cmd.Flags().Changed(subnet) {
err := validateSubnet(viper.GetString(subnet))
if err != nil {
exit.Message(reason.Usage, "{{.err}}", out.V{"err": err})
}
validateCNI(cmd, viper.GetString(containerRuntime))
}

if driver.BareMetal(drvName) {
if ClusterFlagValue() != constants.DefaultClusterName {
exit.Message(reason.DrvUnsupportedProfile, "The '{{.name}} driver does not support multiple profiles: https://minikube.sigs.k8s.io/docs/reference/drivers/none/", out.V{"name": drvName})
}

// default container runtime varies, starting with Kubernetes 1.24 - assume that only the default container runtime has been tested
rtime := viper.GetString(containerRuntime)
if rtime != constants.DefaultContainerRuntime && rtime != defaultRuntime(getKubernetesVersion(nil)) {
out.WarningT("Using the '{{.runtime}}' runtime with the 'none' driver is an untested configuration!", out.V{"runtime": rtime})
}

// conntrack is required starting with Kubernetes 1.18, include the release candidates for completion
version, _ := util.ParseKubernetesVersion(getKubernetesVersion(nil))
if version.GTE(semver.MustParse("1.18.0-beta.1")) {
if _, err := exec.LookPath("conntrack"); err != nil {
exit.Message(reason.GuestMissingConntrack, "Sorry, Kubernetes {{.k8sVersion}} requires conntrack to be installed in root's path", out.V{"k8sVersion": version.String()})
}
if cmd.Flags().Changed(containerRuntime) {
err := validateRuntime(viper.GetString(containerRuntime))
if err != nil {
exit.Message(reason.Usage, "{{.err}}", out.V{"err": err})
}
validateCNI(cmd, viper.GetString(containerRuntime))
}

if driver.IsSSH(drvName) {
Expand Down Expand Up @@ -1279,6 +1267,7 @@ func validateFlags(cmd *cobra.Command, drvName string) {
exit.Message(reason.Usage, "Sorry, please set the --output flag to one of the following valid options: [text,json]")
}

validateBareMetal(drvName)
validateRegistryMirror()
validateInsecureRegistry()
}
Expand Down Expand Up @@ -1763,6 +1752,50 @@ func validateDockerStorageDriver(drvName string) {
viper.Set(preload, false)
}

// validateSubnet checks that the subnet provided has a private IP
// and does not have a mask of more that /30
func validateSubnet(subnet string) error {
ip, cidr, err := netutil.ParseAddr(subnet)
if err != nil {
return errors.Errorf("Sorry, unable to parse subnet: %v", err)
}
if !ip.IsPrivate() {
return errors.Errorf("Sorry, the subnet %s is not a private IP", ip)
}

if cidr != nil {
mask, _ := cidr.Mask.Size()
if mask > 30 {
return errors.Errorf("Sorry, the subnet provided does not have a mask less than or equal to /30")
}
}
return nil
}

func validateBareMetal(drvName string) {
if !driver.BareMetal(drvName) {
return
}

if ClusterFlagValue() != constants.DefaultClusterName {
exit.Message(reason.DrvUnsupportedProfile, "The '{{.name}} driver does not support multiple profiles: https://minikube.sigs.k8s.io/docs/reference/drivers/none/", out.V{"name": drvName})
}

// default container runtime varies, starting with Kubernetes 1.24 - assume that only the default container runtime has been tested
rtime := viper.GetString(containerRuntime)
if rtime != constants.DefaultContainerRuntime && rtime != defaultRuntime(getKubernetesVersion(nil)) {
out.WarningT("Using the '{{.runtime}}' runtime with the 'none' driver is an untested configuration!", out.V{"runtime": rtime})
}

// conntrack is required starting with Kubernetes 1.18, include the release candidates for completion
version, _ := util.ParseKubernetesVersion(getKubernetesVersion(nil))
if version.GTE(semver.MustParse("1.18.0-beta.1")) {
if _, err := exec.LookPath("conntrack"); err != nil {
exit.Message(reason.GuestMissingConntrack, "Sorry, Kubernetes {{.k8sVersion}} requires conntrack to be installed in root's path", out.V{"k8sVersion": version.String()})
}
}
}

func exitIfNotForced(r reason.Kind, message string, v ...out.V) {
if !viper.GetBool(force) {
exit.Message(r, message, v...)
Expand Down
37 changes: 37 additions & 0 deletions cmd/minikube/cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,40 @@ func TestValidatePorts(t *testing.T) {
})
}
}

func TestValidateSubnet(t *testing.T) {
type subnetTest struct {
subnet string
errorMsg string
}
var tests = []subnetTest{
{
subnet: "192.168.1.1",
errorMsg: "",
},
{
subnet: "193.169.1.1",
errorMsg: "Sorry, the subnet 193.169.1.1 is not a private IP",
},
{
subnet: "192.168.1.1/24",
errorMsg: "",
},
{
subnet: "192.168.1.1/32",
errorMsg: "Sorry, the subnet provided does not have a mask less than or equal to /30",
},
}
for _, test := range tests {
t.Run(test.subnet, func(t *testing.T) {
gotError := ""
got := validateSubnet(test.subnet)
if got != nil {
gotError = got.Error()
}
if gotError != test.errorMsg {
t.Errorf("validateSubnet(subnet=%v): got %v, expected %v", test.subnet, got, test.errorMsg)
}
})
}
}
20 changes: 15 additions & 5 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,9 @@ func lookupInInterfaces(ip net.IP) (*Parameters, *net.IPNet, error) {
var inspect = func(addr string) (*Parameters, error) {

// extract ip from addr
ip, network, err := net.ParseCIDR(addr)
ip, network, err := ParseAddr(addr)
if err != nil {
ip = net.ParseIP(addr)
if ip == nil {
return nil, fmt.Errorf("failed parsing address %s: %w", addr, err)
}
return nil, err
}

n := &Parameters{}
Expand Down Expand Up @@ -261,6 +258,19 @@ func FreeSubnet(startSubnet string, step, tries int) (*Parameters, error) {
return nil, fmt.Errorf("no free private network subnets found with given parameters (start: %q, step: %d, tries: %d)", startSubnet, step, tries)
}

// ParseAddr will try to parse an ip or a cidr address
func ParseAddr(addr string) (net.IP, *net.IPNet, error) {
ip, network, err := net.ParseCIDR(addr)
if err != nil {
ip = net.ParseIP(addr)
if ip == nil {
return nil, nil, fmt.Errorf("failed parsing address %s: %w", addr, err)
}
err = nil
}
return ip, network, err
}

// reserveSubnet returns if subnet was successfully reserved for given period:
// - false, if it already has unexpired reservation
// - true, if new reservation was created or expired one renewed
Expand Down
45 changes: 45 additions & 0 deletions pkg/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,48 @@ func TestFreeSubnet(t *testing.T) {
}
})
}

func TestParseAddr(t *testing.T) {
t.Run("ValidIP", func(t *testing.T) {
addr := "192.168.9.0"
ip, _, err := ParseAddr(addr)
if err != nil {
t.Fatal(err)
}
if ip.String() != addr {
t.Errorf("expected IP = %q; got = %q", addr, ip.String())
}
})

t.Run("ValidCIDR", func(t *testing.T) {
addr := "192.168.9.0/30"
ip, cidr, err := ParseAddr(addr)
if err != nil {
t.Fatal(err)
}

expected := "192.168.9.0"
if ip.String() != expected {
t.Errorf("expected IP = %q; got = %q", expected, ip.String())
}

mask, _ := cidr.Mask.Size()
expectedMask := 30
if mask != expectedMask {
t.Errorf("expected mask = %q; got = %q", mask, expectedMask)
}
})

t.Run("InvalidAddr", func(t *testing.T) {
tests := []string{
"192.168.9",
"192.168.9.0/30000",
}
for _, test := range tests {
_, _, err := ParseAddr(test)
if err == nil {
t.Fatalf("expected to fail since address is invalid")
}
}
})
}