Skip to content

Commit

Permalink
Support to install k3d without KubeSphere (#195)
Browse files Browse the repository at this point in the history
* Support do not install KubeSphere with ks install k3d

* Install kubectl before run k3d

* Support re-install k3d cluster with given name
  • Loading branch information
LinuxSuRen committed Sep 7, 2021
1 parent 8d97c87 commit c8bf19c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
9 changes: 5 additions & 4 deletions kubectl-plugin/install/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ You can get more details about the ks-installer from https://github.com/kubesphe
}

type installerOption struct {
version string
nightly string
components []string
fetch bool
version string
nightly string
components []string
fetch bool
withKubeSphere bool

// inner fields
client dynamic.Interface
Expand Down
41 changes: 28 additions & 13 deletions kubectl-plugin/install/k3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can get more details from https://github.com/rancher/k3d/`,
}

flags := cmd.Flags()
flags.StringVarP(&opt.name, "name", "n", "",
flags.StringVarP(&opt.name, "name", "n", "k3s-default",
"The name of k3d cluster")
flags.IntVarP(&opt.agents, "agents", "", 1,
"Specify how many agents you want to create")
Expand All @@ -32,6 +32,12 @@ You can get more details from https://github.com/rancher/k3d/`,
"The image of k3s, get more images from https://hub.docker.com/r/rancher/k3s/tags")
flags.StringVarP(&opt.registry, "registry", "r", "registry",
"Connect to one or more k3d-managed registries running locally")
flags.BoolVarP(&opt.withKubeSphere, "with-kubesphere", "", true,
"Indicate if install KubeSphere as well")
flags.BoolVarP(&opt.withKubeSphere, "with-ks", "", true,
"Indicate if install KubeSphere as well")
flags.BoolVarP(&opt.reInstall, "reinstall", "", false,
"Indicate if re-install the k3d cluster with given name")

// TODO find a better way to reuse the flags from another command
flags.StringVarP(&opt.version, "version", "", types.KsVersion,
Expand All @@ -48,15 +54,16 @@ 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
image string
name string
agents int
servers int
registry string
reInstall bool
}

func (o *k3dOption) preRunE(cmd *cobra.Command, args []string) (err error) {
if o.name == "" && len(args) > 0 {
if len(args) > 0 {
o.name = args[0]
}

Expand All @@ -67,8 +74,9 @@ func (o *k3dOption) preRunE(cmd *cobra.Command, args []string) (err error) {
Fetch: o.fetch,
}
err = is.CheckDepAndInstall(map[string]string{
"k3d": "rancher/k3d",
"docker": "docker",
"k3d": "rancher/k3d",
"docker": "docker",
"kubectl": "kubectl",
})
return
}
Expand All @@ -83,21 +91,28 @@ func (o *k3dOption) runE(cmd *cobra.Command, args []string) (err error) {
// always to create a registry to make sure it's exist
_ = common.ExecCommand("k3d", "registry", "create", o.registry)

if o.reInstall {
_ = common.ExecCommand("k3d", "cluster", "delete", o.name)
}

k3dArgs := []string{"cluster", "create",
"-p", fmt.Sprintf(`%d:30880@agent[0]`, ports[0]),
"-p", fmt.Sprintf(`%d:30180@agent[0]`, ports[1]),
"--agents", fmt.Sprintf("%d", o.agents),
"--servers", fmt.Sprintf("%d", o.servers),
"--image", o.image,
"--registry-use", o.registry}
if o.name != "" {
k3dArgs = append(k3dArgs, o.name)
}
"--registry-use", o.registry,
o.name}
err = common.ExecCommand("k3d", k3dArgs...)
return
}

func (o *k3dOption) postRunE(cmd *cobra.Command, args []string) (err error) {
if !o.withKubeSphere {
// no need to continue due to no require for KubeSphere
return
}

if err = o.installerOption.preRunE(cmd, args); err == nil {
err = o.installerOption.runE(cmd, args)
}
Expand Down

0 comments on commit c8bf19c

Please sign in to comment.