From b92e0d93fb3f39cf143b85e14cf68b08c24154c6 Mon Sep 17 00:00:00 2001 From: rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Thu, 3 Mar 2022 14:12:04 +0800 Subject: [PATCH] Support to add the extra free port to k3d --- go.mod | 5 ++++- go.sum | 9 ++++++--- kubectl-plugin/common/net.go | 11 +++++++++++ kubectl-plugin/install/k3d.go | 26 +++++++++++++++++--------- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 6d98bc2..5405135 100644 --- a/go.mod +++ b/go.mod @@ -16,10 +16,13 @@ require ( github.com/linuxsuren/http-downloader v0.0.35 github.com/mitchellh/copystructure v1.1.1 // indirect github.com/rivo/tview v0.0.0-20210923051754-2cb20002bc4c - github.com/sirupsen/logrus v1.8.1 // indirect + github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.2.1 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.7.0 + golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect + golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect + golang.org/x/text v0.3.7 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b k8s.io/api v0.19.4 k8s.io/apimachinery v0.19.4 diff --git a/go.sum b/go.sum index b3f4141..f8cc867 100644 --- a/go.sum +++ b/go.sum @@ -657,8 +657,9 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -743,8 +744,9 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -757,8 +759,9 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs= diff --git a/kubectl-plugin/common/net.go b/kubectl-plugin/common/net.go index 4898572..9b000d7 100644 --- a/kubectl-plugin/common/net.go +++ b/kubectl-plugin/common/net.go @@ -21,6 +21,17 @@ type FreePort struct { defaultPorts []int } +// NewFreePort creates a FreePort with a specific extra count of ports +func NewFreePort(extraCount int) *FreePort { + initPorts := []int{30880, 30180} + if extraCount > 0 { + for i := 0; i < extraCount; i++ { + initPorts = append(initPorts, initPorts[len(initPorts)-1]+1) + } + } + return &FreePort{defaultPorts: initPorts} +} + // FindFreePortsOfKubeSphere returns the free ports of KubeSphere // The order of the ports is: console, jenkins func (f *FreePort) FindFreePortsOfKubeSphere() ([]int, error) { diff --git a/kubectl-plugin/install/k3d.go b/kubectl-plugin/install/k3d.go index 6d160b4..890e6ba 100644 --- a/kubectl-plugin/install/k3d.go +++ b/kubectl-plugin/install/k3d.go @@ -40,6 +40,8 @@ You can get more details from https://github.com/rancher/k3d/`, "Indicate if install KubeSphere as well") flags.BoolVarP(&opt.reInstall, "reinstall", "", false, "Indicate if re-install the k3d cluster with given name") + flags.IntVarP(&opt.extraFreePorts, "extra-free-ports", "", 0, + "Open more extra free ports for the k3d, this flag is the count of the extra free ports instead of the value") // TODO find a better way to reuse the flags from another command flags.StringVarP(&opt.version, "version", "", types.KsVersion, @@ -63,12 +65,13 @@ You can get more details from https://github.com/rancher/k3d/`, type k3dOption struct { installerOption - image string - name string - agents int - servers int - registry string - reInstall bool + image string + name string + agents int + servers int + registry string + reInstall bool + extraFreePorts int } func (o *k3dOption) preRunE(cmd *cobra.Command, args []string) (err error) { @@ -96,7 +99,7 @@ func (o *k3dOption) preRunE(cmd *cobra.Command, args []string) (err error) { } func (o *k3dOption) runE(cmd *cobra.Command, args []string) (err error) { - freePort := &common.FreePort{} + freePort := common.NewFreePort(o.extraFreePorts) var ports []int if ports, err = freePort.FindFreePortsOfKubeSphere(); err != nil { return @@ -114,14 +117,19 @@ func (o *k3dOption) runE(cmd *cobra.Command, args []string) (err error) { return err } + var freePortList []string + for i := range ports { + port := ports[i] + freePortList = append(freePortList, "-p", fmt.Sprintf(`%d:%d@%s`, port, port, agentPort)) + } + k3dArgs := []string{"cluster", "create", - "-p", fmt.Sprintf(`%d:30880@%s`, ports[0], agentPort), - "-p", fmt.Sprintf(`%d:30180@%s`, ports[1], agentPort), "--agents", fmt.Sprintf("%d", o.agents), "--servers", fmt.Sprintf("%d", o.servers), "--image", o.image, "--registry-use", o.registry, o.name} + k3dArgs = append(k3dArgs, freePortList...) err = common.ExecCommand("k3d", k3dArgs...) return }