Skip to content

Commit

Permalink
Merge pull request #255 from LinuxSuRen/k3d-ports
Browse files Browse the repository at this point in the history
Support to add the extra free port to k3d
  • Loading branch information
ks-ci-bot committed Mar 3, 2022
2 parents e6ef407 + b92e0d9 commit 3688ad9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
11 changes: 11 additions & 0 deletions kubectl-plugin/common/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 17 additions & 9 deletions kubectl-plugin/install/k3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 3688ad9

Please sign in to comment.