From e5e3df891dd456bb90fc4dc2e0ab7af7928f0146 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Mon, 14 Oct 2024 21:06:47 +0300 Subject: [PATCH] fix: save container name (#5931) * fix: save container nname Signed-off-by: Vladislav Sukhin * fix: optional id message Signed-off-by: Vladislav Sukhin * fix: save config Signed-off-by: Vladislav Sukhin --------- Signed-off-by: Vladislav Sukhin --- cmd/kubectl-testkube/commands/common/flags.go | 11 ++++++++--- .../commands/common/helper.go | 5 ++++- .../commands/common/masterFlags_test.go | 2 +- cmd/kubectl-testkube/commands/context/set.go | 19 +++++++++++++------ cmd/kubectl-testkube/commands/init.go | 2 +- cmd/kubectl-testkube/commands/pro/connect.go | 2 +- cmd/kubectl-testkube/commands/pro/docker.go | 8 ++++++-- cmd/kubectl-testkube/commands/pro/init.go | 9 +++++++-- cmd/kubectl-testkube/commands/pro/login.go | 2 +- cmd/kubectl-testkube/commands/upgrade.go | 2 +- 10 files changed, 43 insertions(+), 19 deletions(-) diff --git a/cmd/kubectl-testkube/commands/common/flags.go b/cmd/kubectl-testkube/commands/common/flags.go index 0b48f38db59..a59bd198ec4 100644 --- a/cmd/kubectl-testkube/commands/common/flags.go +++ b/cmd/kubectl-testkube/commands/common/flags.go @@ -59,7 +59,7 @@ func CreateVariables(cmd *cobra.Command, ignoreSecretVariable bool) (vars map[st return } -func PopulateMasterFlags(cmd *cobra.Command, opts *HelmOptions) { +func PopulateMasterFlags(cmd *cobra.Command, opts *HelmOptions, optionalIds bool) { var ( apiURIPrefix, uiURIPrefix, agentURIPrefix, cloudRootDomain, proRootDomain string insecure bool @@ -88,8 +88,13 @@ func PopulateMasterFlags(cmd *cobra.Command, opts *HelmOptions) { cmd.Flags().StringVar(&opts.Master.URIs.Agent, "agent-uri", "", "Testkube Pro agent URI [required for centralized mode]") cmd.Flags().StringVar(&opts.Master.URIs.Logs, "logs-uri", "", "Testkube Pro logs URI [required for centralized mode]") cmd.Flags().StringVar(&opts.Master.AgentToken, "agent-token", "", "Testkube Pro agent key [required for centralized mode]") - cmd.Flags().StringVar(&opts.Master.OrgId, "org-id", "", "Testkube Pro organization id [required for centralized mode]") - cmd.Flags().StringVar(&opts.Master.EnvId, "env-id", "", "Testkube Pro environment id [required for centralized mode]") + neededForLogin := "" + if optionalIds { + neededForLogin = ". It can be skipped for no login mode" + } + + cmd.Flags().StringVar(&opts.Master.OrgId, "org-id", "", "Testkube Pro organization id [required for centralized mode]"+neededForLogin) + cmd.Flags().StringVar(&opts.Master.EnvId, "env-id", "", "Testkube Pro environment id [required for centralized mode]"+neededForLogin) cmd.Flags().BoolVar(&opts.Master.Features.LogsV2, "feature-logs-v2", false, "Logs v2 feature flag") } diff --git a/cmd/kubectl-testkube/commands/common/helper.go b/cmd/kubectl-testkube/commands/common/helper.go index 722e596b1ea..3ce2daa823d 100644 --- a/cmd/kubectl-testkube/commands/common/helper.go +++ b/cmd/kubectl-testkube/commands/common/helper.go @@ -458,7 +458,7 @@ func PopulateOrgAndEnvNames(cfg config.Data, orgId, envId, apiUrl string) (confi return cfg, nil } -func PopulateCloudConfig(cfg config.Data, apiKey string, opts *HelmOptions) config.Data { +func PopulateCloudConfig(cfg config.Data, apiKey string, dockerContainerName *string, opts *HelmOptions) config.Data { if apiKey != "" { cfg.CloudContext.ApiKey = apiKey } @@ -466,6 +466,9 @@ func PopulateCloudConfig(cfg config.Data, apiKey string, opts *HelmOptions) conf cfg.CloudContext.ApiUri = opts.Master.URIs.Api cfg.CloudContext.UiUri = opts.Master.URIs.Ui cfg.CloudContext.AgentUri = opts.Master.URIs.Agent + if dockerContainerName != nil { + cfg.CloudContext.DockerContainerName = *dockerContainerName + } return cfg } diff --git a/cmd/kubectl-testkube/commands/common/masterFlags_test.go b/cmd/kubectl-testkube/commands/common/masterFlags_test.go index c621dd821ef..8e0ceeac733 100644 --- a/cmd/kubectl-testkube/commands/common/masterFlags_test.go +++ b/cmd/kubectl-testkube/commands/common/masterFlags_test.go @@ -24,7 +24,7 @@ func NewTestCmd() *cobra.Command { }, } - PopulateMasterFlags(cmd, &opts) + PopulateMasterFlags(cmd, &opts, false) PopulateHelmFlags(cmd, &opts) return cmd } diff --git a/cmd/kubectl-testkube/commands/context/set.go b/cmd/kubectl-testkube/commands/context/set.go index 3fa0638f82a..b927238bda1 100644 --- a/cmd/kubectl-testkube/commands/context/set.go +++ b/cmd/kubectl-testkube/commands/context/set.go @@ -11,10 +11,11 @@ import ( func NewSetContextCmd() *cobra.Command { var ( - org, env, apiKey string - kubeconfig bool - namespace string - opts common.HelmOptions + org, env, apiKey string + kubeconfig bool + namespace string + opts common.HelmOptions + dockerContainerName string ) cmd := &cobra.Command{ @@ -46,7 +47,12 @@ func NewSetContextCmd() *cobra.Command { ui.Errf("Please provide at least one of the following flags: --org-id, --env-id, --api-key, --root-domain") } - cfg = common.PopulateCloudConfig(cfg, apiKey, &opts) + var dcName *string + if cmd.Flags().Changed("docker-container") { + dcName = &dockerContainerName + } + + cfg = common.PopulateCloudConfig(cfg, apiKey, dcName, &opts) if cfg.CloudContext.ApiKey != "" { var err error @@ -96,7 +102,8 @@ func NewSetContextCmd() *cobra.Command { cmd.Flags().String("ui-uri-override", "", "ui uri override") cmd.Flags().String("agent-uri-override", "", "agnet uri override") cmd.Flags().String("logs-uri-override", "", "logs service uri override") + cmd.Flags().StringVar(&dockerContainerName, "docker-container", "testkube-agent", "Docker container name for Testkube Docker Agent") - common.PopulateMasterFlags(cmd, &opts) + common.PopulateMasterFlags(cmd, &opts, false) return cmd } diff --git a/cmd/kubectl-testkube/commands/init.go b/cmd/kubectl-testkube/commands/init.go index 0458ad99752..aa434d03be2 100644 --- a/cmd/kubectl-testkube/commands/init.go +++ b/cmd/kubectl-testkube/commands/init.go @@ -98,7 +98,7 @@ func NewInitCmdStandalone() *cobra.Command { cmd.Flags().BoolVarP(&export, "export", "", false, "Export the values.yaml") common.PopulateHelmFlags(cmd, &options) - common.PopulateMasterFlags(cmd, &options) + common.PopulateMasterFlags(cmd, &options, false) return cmd } diff --git a/cmd/kubectl-testkube/commands/pro/connect.go b/cmd/kubectl-testkube/commands/pro/connect.go index b3c15d4f9ed..b928ffd76db 100644 --- a/cmd/kubectl-testkube/commands/pro/connect.go +++ b/cmd/kubectl-testkube/commands/pro/connect.go @@ -182,7 +182,7 @@ func NewConnectCmd() *cobra.Command { } common.PopulateHelmFlags(cmd, &opts) - common.PopulateMasterFlags(cmd, &opts) + common.PopulateMasterFlags(cmd, &opts, false) cmd.Flags().IntVar(&opts.MinioReplicas, "minio-replicas", 0, "MinIO replicas") cmd.Flags().IntVar(&opts.MongoReplicas, "mongo-replicas", 0, "MongoDB replicas") diff --git a/cmd/kubectl-testkube/commands/pro/docker.go b/cmd/kubectl-testkube/commands/pro/docker.go index 263b49dbf58..49f8e1b8b5a 100644 --- a/cmd/kubectl-testkube/commands/pro/docker.go +++ b/cmd/kubectl-testkube/commands/pro/docker.go @@ -102,7 +102,11 @@ func NewDockerCmd() *cobra.Command { if noLogin { ui.Alert("Saving Testkube CLI Pro context, you need to authorize CLI through `testkube set context` later") - common.PopulateCloudConfig(cfg, "", &options) + cfg = common.PopulateCloudConfig(cfg, "", &dockerContainerName, &options) + + err = config.Save(cfg) + ui.ExitOnError("saving config file", err) + ui.Info(" Happy Testing! 🚀") ui.NL() return @@ -125,7 +129,7 @@ func NewDockerCmd() *cobra.Command { }, } - common.PopulateMasterFlags(cmd, &options) + common.PopulateMasterFlags(cmd, &options, true) cmd.Flags().BoolVarP(&noLogin, "no-login", "", false, "Ignore login prompt, set existing token later by `testkube set context`") cmd.Flags().StringVar(&dockerContainerName, "docker-container", "testkube-agent", "Docker container name for Testkube Docker Agent") diff --git a/cmd/kubectl-testkube/commands/pro/init.go b/cmd/kubectl-testkube/commands/pro/init.go index ad7e7673036..b1fd6f6ee12 100644 --- a/cmd/kubectl-testkube/commands/pro/init.go +++ b/cmd/kubectl-testkube/commands/pro/init.go @@ -8,6 +8,7 @@ import ( "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common" "github.com/kubeshop/testkube/cmd/kubectl-testkube/config" + commonint "github.com/kubeshop/testkube/internal/common" "github.com/kubeshop/testkube/pkg/telemetry" "github.com/kubeshop/testkube/pkg/ui" ) @@ -84,7 +85,11 @@ func NewInitCmd() *cobra.Command { if noLogin { ui.Alert("Saving Testkube CLI Pro context, you need to authorize CLI through `testkube set context` later") - common.PopulateCloudConfig(cfg, "", &options) + cfg = common.PopulateCloudConfig(cfg, "", commonint.Ptr(""), &options) + + err = config.Save(cfg) + ui.ExitOnError("saving config file", err) + ui.Info(" Happy Testing! 🚀") ui.NL() return @@ -108,7 +113,7 @@ func NewInitCmd() *cobra.Command { } common.PopulateHelmFlags(cmd, &options) - common.PopulateMasterFlags(cmd, &options) + common.PopulateMasterFlags(cmd, &options, false) cmd.Flags().BoolVarP(&noLogin, "no-login", "", false, "Ignore login prompt, set existing token later by `testkube set context`") cmd.Flags().BoolVarP(&export, "export", "", false, "Export the values.yaml") diff --git a/cmd/kubectl-testkube/commands/pro/login.go b/cmd/kubectl-testkube/commands/pro/login.go index 7119bfd94ec..c57a67f76c1 100644 --- a/cmd/kubectl-testkube/commands/pro/login.go +++ b/cmd/kubectl-testkube/commands/pro/login.go @@ -45,7 +45,7 @@ func NewLoginCmd() *cobra.Command { }, } - common.PopulateMasterFlags(cmd, &opts) + common.PopulateMasterFlags(cmd, &opts, false) return cmd } diff --git a/cmd/kubectl-testkube/commands/upgrade.go b/cmd/kubectl-testkube/commands/upgrade.go index 5fbd4abe5af..9a1ab0387c5 100644 --- a/cmd/kubectl-testkube/commands/upgrade.go +++ b/cmd/kubectl-testkube/commands/upgrade.go @@ -106,7 +106,7 @@ func NewUpgradeCmd() *cobra.Command { } common.PopulateHelmFlags(cmd, &options) - common.PopulateMasterFlags(cmd, &options) + common.PopulateMasterFlags(cmd, &options, false) cmd.Flags().StringVar(&dockerContainerName, "docker-container", "testkube-agent", "Docker container name for Testkube Docker Agent")