diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index aaa5fbe55e21..7810dd0ce74d 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -25,6 +25,7 @@ import ( "os" "os/exec" "os/user" + "regexp" "runtime" "strings" @@ -71,6 +72,7 @@ var ( insecureRegistry []string apiServerNames []string apiServerIPs []net.IP + hostRe = regexp.MustCompile(`[\w\.-]+`) ) func init() { @@ -897,6 +899,7 @@ func validateRequestedMemorySize(req int, drvName string) { func validateCPUCount(drvName string) { var cpuCount int if driver.BareMetal(drvName) { + // Uses the gopsutil cpu package to count the number of physical cpu cores ci, err := cpu.Counts(false) if err != nil { @@ -1041,6 +1044,8 @@ func validateFlags(cmd *cobra.Command, drvName string) { } validateRegistryMirror() + validateInsecureRegistry() + } // This function validates if the --registry-mirror @@ -1060,6 +1065,34 @@ func validateRegistryMirror() { } } +// This function validates that the --insecure-registry follows one of the following formats: +// ":" ":" "/" +func validateInsecureRegistry() { + if len(insecureRegistry) > 0 { + for _, addr := range insecureRegistry { + hostnameOrIP, port, err := net.SplitHostPort(addr) + if err != nil { + _, _, err := net.ParseCIDR(addr) + if err == nil { + continue + } + } + if port == "" { + exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: :, : or /", out.V{"addr": addr}) + } + // checks both IPv4 and IPv6 + ipAddr := net.ParseIP(hostnameOrIP) + if ipAddr != nil { + continue + } + isValidHost := hostRe.MatchString(hostnameOrIP) + if err != nil || !isValidHost { + exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: :, : or /", out.V{"addr": addr}) + } + } + } +} + func createNode(cc config.ClusterConfig, kubeNodeName string, existing *config.ClusterConfig) (config.ClusterConfig, config.Node, error) { // Create the initial node, which will necessarily be a control plane if existing != nil {