createCluster: fallback to default api port if random allocation fails

This commit is contained in:
iwilltry42 2020-06-12 13:32:19 +02:00
parent 3205cbac67
commit d6b5d4dbf5
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
3 changed files with 9 additions and 7 deletions

View File

@ -110,7 +110,7 @@ func NewCmdCreateCluster() *cobra.Command {
/********* /*********
* Flags * * Flags *
*********/ *********/
cmd.Flags().StringP("api-port", "a", "", "Specify the Kubernetes API server port exposed on the LoadBalancer (Format: `--api-port [HOST:]HOSTPORT`)\n - Example: `k3d create -m 3 -a 0.0.0.0:6550`") cmd.Flags().StringP("api-port", "a", "random", "Specify the Kubernetes API server port exposed on the LoadBalancer (Format: `--api-port [HOST:]HOSTPORT`)\n - Example: `k3d create -m 3 -a 0.0.0.0:6550`")
cmd.Flags().IntP("masters", "m", 1, "Specify how many masters you want to create") cmd.Flags().IntP("masters", "m", 1, "Specify how many masters you want to create")
cmd.Flags().IntP("workers", "w", 0, "Specify how many workers you want to create") cmd.Flags().IntP("workers", "w", 0, "Specify how many workers you want to create")
cmd.Flags().StringP("image", "i", fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)), "Specify k3s image that you want to use for the nodes") cmd.Flags().StringP("image", "i", fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)), "Specify k3s image that you want to use for the nodes")

View File

@ -54,15 +54,17 @@ func ParseAPIPort(portString string) (k3d.ExposeAPI, error) {
} }
// Verify 'port' is an integer and within port ranges // Verify 'port' is an integer and within port ranges
if exposeAPI.Port == "" { if exposeAPI.Port == "" || exposeAPI.Port == "random" {
log.Debugf("API-Port Mapping didn't specify hostPort, choosing one randomly...") log.Debugf("API-Port Mapping didn't specify hostPort, choosing one randomly...")
freePort, err := GetFreePort() freePort, err := GetFreePort()
if err != nil || freePort == 0 { if err != nil || freePort == 0 {
log.Errorln("Failed to get a free port") log.Warnf("Failed to get random free port:\n%+v", err)
return exposeAPI, err log.Warnf("Falling back to default port %s (may be blocked though)...", k3d.DefaultAPIPort)
exposeAPI.Port = k3d.DefaultAPIPort
} else {
exposeAPI.Port = strconv.Itoa(freePort)
log.Debugf("Got free port for API: '%d'", freePort)
} }
exposeAPI.Port = strconv.Itoa(freePort)
log.Debugf("Got free port for API: '%d'", freePort)
} }
p, err := strconv.Atoi(exposeAPI.Port) p, err := strconv.Atoi(exposeAPI.Port)
if err != nil { if err != nil {

View File

@ -8,7 +8,7 @@ source "$CURR_DIR/common.sh"
info "Creating two clusters..." info "Creating two clusters..."
$EXE create cluster c1 --wait --timeout 60s --api-port 6443 || failed "could not create cluster c1" $EXE create cluster c1 --wait --timeout 60s --api-port 6443 || failed "could not create cluster c1"
$EXE create cluster c2 --wait --timeout 60s --api-port 6444 || failed "could not create cluster c2" $EXE create cluster c2 --wait --timeout 60s || failed "could not create cluster c2"
info "Checking that we can get both clusters..." info "Checking that we can get both clusters..."
check_cluster_count 2 check_cluster_count 2