Skip to content

Commit

Permalink
Adding context flag on server install (#2477)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbdias authored May 8, 2023
1 parent 5e35e36 commit dd6c3e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
9 changes: 6 additions & 3 deletions cli/cmd/server_install_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var (
force = false
runEnvironment = installer.NoneRunEnvironmentType
installationMode = installer.NotChosenInstallationModeType
force = false
runEnvironment = installer.NoneRunEnvironmentType
installationMode = installer.NotChosenInstallationModeType
kubernetesContext = ""
)

var serverInstallCmd = &cobra.Command{
Expand All @@ -21,6 +22,7 @@ var serverInstallCmd = &cobra.Command{
installer.Force = force
installer.RunEnvironment = runEnvironment
installer.InstallationMode = installationMode
installer.KubernetesContext = kubernetesContext

analytics.Track("Server Install", "cmd", map[string]string{})
installer.Start()
Expand All @@ -30,6 +32,7 @@ var serverInstallCmd = &cobra.Command{

func init() {
serverInstallCmd.Flags().BoolVarP(&force, "force", "f", false, "Overwrite existing files")
serverInstallCmd.Flags().StringVar(&kubernetesContext, "kubernetes-context", "", "Kubernetes context used to install Tracetest. It will be only used if 'run-environment' is set as 'kubernetes'.")

// these commands will not have shorthand parameters to avoid colision with existing ones in other commands
serverInstallCmd.Flags().Var(&installationMode, "mode", "Indicate the type of demo environment to be installed with Tracetest. It can be 'with-demo' or 'just-tracetest'.")
Expand Down
7 changes: 4 additions & 3 deletions cli/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

var (
Force = false
RunEnvironment = NoneRunEnvironmentType
InstallationMode = NotChosenInstallationModeType
Force = false
RunEnvironment = NoneRunEnvironmentType
InstallationMode = NotChosenInstallationModeType
KubernetesContext = ""
)

const createIssueMsg = "If you need help, please create an issue: https://github.com/kubeshop/tracetest/issues/new/choose"
Expand Down
51 changes: 37 additions & 14 deletions cli/installer/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,17 @@ func getKubernetesContextArray(kubeconfig string) ([][]string, error) {
return records, nil
}

func configureKubernetes(conf configuration, ui cliUI.UI) configuration {
conf.set("k8s.kubeconfig", "${HOME}/.kube/config")
func kubernetesContextExists(name string, contexts []k8sContext) bool {
for _, context := range contexts {
if context.name == name {
return true
}
}

return false
}

func getKubernetesContext(conf configuration, ui cliUI.UI) string {
contexts := getK8sContexts(conf, ui)
if len(contexts) == 0 {
ui.Exit(
Expand All @@ -302,24 +310,39 @@ func configureKubernetes(conf configuration, ui cliUI.UI) configuration {
)
}

if KubernetesContext != "" && kubernetesContextExists(KubernetesContext, contexts) {
ui.Println("On which kubectl context do you want to install Tracetest?")
ui.Println(fmt.Sprintf(" > %s", KubernetesContext))

return KubernetesContext
}

if len(contexts) == 1 {
conf.set("k8s.context", contexts[0].name)
ui.Println("On which kubectl context do you want to install Tracetest?")
ui.Println(fmt.Sprintf(" > %s", contexts[0].name))

return contexts[0].name
}

if len(contexts) > 1 {
options := []cliUI.Option{}
defaultIndex := 0
for i, c := range contexts {
if c.selected {
defaultIndex = i
}
options = append(options, cliUI.Option{Text: c.name, Fn: func(ui cliUI.UI) {}})
options := []cliUI.Option{}
defaultIndex := 0
for i, c := range contexts {
if c.selected {
defaultIndex = i
}

selected := ui.Select("Kubectl context", options, defaultIndex)
conf.set("k8s.context", selected.Text)
options = append(options, cliUI.Option{Text: c.name, Fn: func(ui cliUI.UI) {}})
}

selected := ui.Select("On which kubectl context do you want to install Tracetest?", options, defaultIndex)
return selected.Text
}

func configureKubernetes(conf configuration, ui cliUI.UI) configuration {
conf.set("k8s.kubeconfig", "${HOME}/.kube/config")

context := getKubernetesContext(conf, ui)
conf.set("k8s.context", context)

conf.set("k8s.namespace", "tracetest")
return conf
}
Expand Down

0 comments on commit dd6c3e1

Please sign in to comment.