vmware-tanzu / tanzu-cli

The Tanzu Core CLI project provides the core functionality of the Tanzu CLI. The CLI is based on a plugin architecture where CLI command functionality can be delivered through independently developed plugin binaries
Apache License 2.0
33 stars 21 forks source link

Update kubecontext name associated with `tanzu` CLI Context when active resource is updated #746

Closed prkalle closed 4 months ago

prkalle commented 4 months ago

What this PR does / why we need it

This PR has changes to update kubecontext name associated with tanzu CLI context when active resource is updated.

Summary of changes:

Which issue(s) this PR fixes

Fixes #

Describe testing done for PR

Used tanzu login command to login to create a tanzu context and updated the context to point to project and later to space and verified the kubecontext name is updated to reflect the current active resource. Also the kubecontext name is updated in the CLI context too.

❯ tanzu context list
  NAME            ISACTIVE  TYPE             PROJECT  SPACE
  CMBU_TMM        false     tanzu
  TAP_SAAS_PRD    false     tanzu
  mytmc-ctx       false     mission-control  n/a      n/a
  tkg-mgmt-vc     false     kubernetes       n/a      n/a
  tt-test-selfmg  true      mission-control  n/a      n/a

[i] Use '--wide' flag to view additional columns.

❯ ./bin/tanzu login --staging --endpoint https://api.tanzu-dev.cloud.vmware.com
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console-stg.cloud.vmware.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=OdeLWpjmXYg4CVlr_gO7xxGyOcudSuohKJWrUTe2FD8&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A60617%2Fcallback&response_type=code&state=d49e34d2ad788dab6a6f191faf620ae6

    Optionally, paste your authorization code: [...]

[ok] Successfully logged into 'TAP pre-integration' organization and created a tanzu context

❯ ./bin/tanzu context list
  NAME                                  ISACTIVE  TYPE             PROJECT  SPACE
  CMBU_TMM                              false     tanzu
  TAP_SAAS_PRD                          false     tanzu
  TAP_pre-integration-staging-d03c5c97  true      tanzu
  mytmc-ctx                             false     mission-control  n/a      n/a
  tkg-mgmt-vc                           false     kubernetes       n/a      n/a
  tt-test-selfmg                        true      mission-control  n/a      n/a

[i] Use '--wide' flag to view additional columns.

// kubectl context show the kubecontext name as tanzu-cli-<contextname>
❯ kubectl config get-contexts
CURRENT   NAME                                                          CLUSTER                                                       AUTHINFO                                                      NAMESPACE
          kind-ucp                                                      kind-ucp                                                      kind-ucp
          tanzu-cli-CMBU_TMM                                            tanzu-cli-CMBU_TMM/current                                    tanzu-cli-CMBU_TMM-user
          tanzu-cli-TAP_SAAS_PRD                                        tanzu-cli-TAP_SAAS_PRD/current                                tanzu-cli-TAP_SAAS_PRD-user
*         tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97-user

❯ ./bin/tanzu project use helm-repo
✓ Successfully set project to helm-repo
❯ tanzu context list
  NAME                                  ISACTIVE  TYPE             PROJECT    SPACE
  CMBU_TMM                              false     tanzu
  TAP_SAAS_PRD                          false     tanzu
  TAP_pre-integration-staging-d03c5c97  true      tanzu            helm-repo
  mytmc-ctx                             false     mission-control  n/a        n/a
  tkg-mgmt-vc                           false     kubernetes       n/a        n/a
  tt-test-selfmg                        true      mission-control  n/a        n/a

[i] Use '--wide' flag to view additional columns.
❯ kubectl config get-contexts
CURRENT   NAME                                                               CLUSTER                                                            AUTHINFO                                                      NAMESPACE
          kind-ucp                                                           kind-ucp                                                           kind-ucp
          tanzu-cli-CMBU_TMM                                                 tanzu-cli-CMBU_TMM/current                                         tanzu-cli-CMBU_TMM-user
          tanzu-cli-TAP_SAAS_PRD                                             tanzu-cli-TAP_SAAS_PRD/current                                     tanzu-cli-TAP_SAAS_PRD-user
*         tanzu-cli-TAP_pre-integration-staging-d03c5c97:helm-repo   tanzu-cli-TAP_pre-integration-staging-d03c5c97:helm-repo   tanzu-cli-TAP_pre-integration-staging-d03c5c97-user

❯ ./bin/tanzu space use wfd-official
✓ Successfully set space to wfd-official

❯ tanzu context list --current
  NAME                                  ISACTIVE  TYPE             PROJECT    SPACE
  TAP_pre-integration-staging-d03c5c97  true      tanzu            helm-repo  wfd-official

❯ kubectl config get-contexts
CURRENT   NAME                                                                                  CLUSTER                                                                               AUTHINFO                                                      NAMESPACE
          kind-ucp                                                                              kind-ucp                                                                              kind-ucp
          tanzu-cli-CMBU_TMM                                                                    tanzu-cli-CMBU_TMM/current                                                            tanzu-cli-CMBU_TMM-user
          tanzu-cli-TAP_SAAS_PRD                                                                tanzu-cli-TAP_SAAS_PRD/current                                                        tanzu-cli-TAP_SAAS_PRD-user
*         tanzu-cli-TAP_pre-integration-staging-d03c5c97:helm-repo:wfd-official   tanzu-cli-TAP_pre-integration-staging-d03c5c97:helm-repo:wfd-official   tanzu-cli-TAP_pre-integration-staging-d03c5c97-user

## Now change the CLI to point to different project (user currently in a space under different project). The kubecontext name is updated to show the new projectname and no space

❯ ./bin/tanzu project use kshaheer-project
✓ Successfully set project to kshaheer-project

❯ tanzu context list --current
  NAME                                  ISACTIVE  TYPE             PROJECT           SPACE
  TAP_pre-integration-staging-d03c5c97  true      tanzu            kshaheer-project

[i] Use '--wide' flag to view additional columns.

❯ kubectl config get-contexts
CURRENT   NAME                                                                      CLUSTER                                                                   AUTHINFO                                                      NAMESPACE
          kind-ucp                                                                  kind-ucp                                                                  kind-ucp
          tanzu-cli-CMBU_TMM                                                        tanzu-cli-CMBU_TMM/current                                                tanzu-cli-CMBU_TMM-user
          tanzu-cli-TAP_SAAS_PRD                                                    tanzu-cli-TAP_SAAS_PRD/current                                            tanzu-cli-TAP_SAAS_PRD-user
*         tanzu-cli-TAP_pre-integration-staging-d03c5c97:kshaheer-project   tanzu-cli-TAP_pre-integration-staging-d03c5c97:kshaheer-project   tanzu-cli-TAP_pre-integration-staging-d03c5c97-user

Verified that kubecontext name would not change once we export the environment variable TANZU_CLI_USE_STABLE_KUBE_CONTEXT_NAME to true

❯ export TANZU_CLI_USE_STABLE_KUBE_CONTEXT_NAME=true
❯ tanzu context delete TAP_pre-integration-staging-d03c5c97
Deleting the context entry from the config will remove it from the list of tracked contexts. You will need to use `tanzu context create` to re-create this context. Are you sure you want to continue? [y/N]: y
[i] Deleting kubeconfig context 'tanzu-cli-TAP_pre-integration-staging-d03c5c97:project:kshaheer-project' from the file '/Users/pkalle/.kube/config'
[!] WARNING: this removed your active context, use "kubectl config use-context" to select a different one
[ok] Successfully deleted context "TAP_pre-integration-staging-d03c5c97"
❯ ./bin/tanzu login --staging --endpoint https://api.tanzu-dev.cloud.vmware.com
[i] Opening the browser window to complete the login
Log in by visiting this link:

    https://console-stg.cloud.vmware.com/csp/gateway/discovery?client_id=tanzu-cli-client-id&code_challenge=X5dbJTGCA21nvCKsehGz8UwxsYdJrMEXGeVo75g7q7w&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A61100%2Fcallback&response_type=code&state=8c735d32055d3629fb0fa4e3bd8d9453

    Optionally, paste your authorization code: [...]

[ok] Successfully logged into 'TAP pre-integration' organization and created a tanzu context

❯ tanzu context list --current
  NAME                                  ISACTIVE  TYPE             PROJECT  SPACE
  TAP_pre-integration-staging-d03c5c97  true      tanzu
  tt-test-selfmg                        true      mission-control  n/a      n/a

[i] Use '--wide' flag to view additional columns.
❯ kubectl config get-contexts
CURRENT   NAME                                                          CLUSTER                                                       AUTHINFO                                                      NAMESPACE
          kind-ucp                                                      kind-ucp                                                      kind-ucp
          tanzu-cli-CMBU_TMM                                            tanzu-cli-CMBU_TMM/current                                    tanzu-cli-CMBU_TMM-user
          tanzu-cli-TAP_SAAS_PRD                                        tanzu-cli-TAP_SAAS_PRD/current                                tanzu-cli-TAP_SAAS_PRD-user
*         tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97-user
❯
❯ ./bin/tanzu project use helm-repo
✓ Successfully set project to helm-repo
❯ kubectl config get-contexts
CURRENT   NAME                                                          CLUSTER                                                       AUTHINFO                                                      NAMESPACE
          kind-ucp                                                      kind-ucp                                                      kind-ucp
          tanzu-cli-CMBU_TMM                                            tanzu-cli-CMBU_TMM/current                                    tanzu-cli-CMBU_TMM-user
          tanzu-cli-TAP_SAAS_PRD                                        tanzu-cli-TAP_SAAS_PRD/current                                tanzu-cli-TAP_SAAS_PRD-user
*         tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97-user
❯ ./bin/tanzu space use wfd-official
✓ Successfully set space to wfd-official
❯ kubectl config get-contexts
CURRENT   NAME                                                          CLUSTER                                                       AUTHINFO                                                      NAMESPACE
          kind-ucp                                                      kind-ucp                                                      kind-ucp
          tanzu-cli-CMBU_TMM                                            tanzu-cli-CMBU_TMM/current                                    tanzu-cli-CMBU_TMM-user
          tanzu-cli-TAP_SAAS_PRD                                        tanzu-cli-TAP_SAAS_PRD/current                                tanzu-cli-TAP_SAAS_PRD-user
*         tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97                tanzu-cli-TAP_pre-integration-staging-d03c5c97-user

Release note

Update kubecontext name associated with `tanzu` CLI Context when active resource is updated. User can skip this kubecontext name update by setting the environment variable `TANZU_CLI_USE_STABLE_KUBE_CONTEXT_NAME` to "true" 

Additional information

Note: If user sets the environment variable TANZU_CLI_USE_STABLE_KUBE_CONTEXT_NAME to "true" while being currently at the project/space, there won't be any kubecotnext name changes thereof. Lets say if the user is at space and the current kubecontext name is tanzu-cli-TAP_pre-integration-staging-d03c5c97:project:helm-repo:space:wfd-official , and then if user sets the TANZU_CLI_USE_STABLE_KUBE_CONTEXT_NAME to true , then the same existing name would previal even if the user updates the CLI context to point to different active resource.

Special notes for your reviewer

vuil commented 4 months ago

still looking at the change, but highlevel comment is that the context name feels really long what do you think of doing away with project: and space:. I to recognize in some situation the third part could be clustergroup: but even so I don't feel too bad about not having the resource type in the context name. cc @anujc25 @prkalle

vuil commented 4 months ago

lgtm. A nit on the documentation, but my main question is able whether we should go for a more compact kube context name format.

anujc25 commented 4 months ago

still looking at the change, but highlevel comment is that the context name feels really long what do you think of doing away with project: and space:. I to recognize in some situation the third part could be clustergroup: but even so I don't feel too bad about not having the resource type in the context name. cc @anujc25 @prkalle

I like this as well. I would prefer the compact format if there are no other issues.

prkalle commented 4 months ago

still looking at the change, but highlevel comment is that the context name feels really long what do you think of doing away with project: and space:. I to recognize in some situation the third part could be clustergroup: but even so I don't feel too bad about not having the resource type in the context name. cc @anujc25 @prkalle

Sounds good to me.