Skip to content

tsh kube login by name prefix, labels, and/or query#30252

Merged
GavinFrazar merged 2 commits intomasterfrom
gavinfrazar/tsh-resource-selection-ux-kube
Aug 14, 2023
Merged

tsh kube login by name prefix, labels, and/or query#30252
GavinFrazar merged 2 commits intomasterfrom
gavinfrazar/tsh-resource-selection-ux-kube

Conversation

@GavinFrazar
Copy link
Copy Markdown
Contributor

@GavinFrazar GavinFrazar commented Aug 10, 2023

This PR allows a user to select the current context by prefix name during tsh kube login, and to filter the kube clusters for login by --labels and/or predicate --query - these flags are not allowed with --all.

changelog: tsh kube login now supports --query and --labels optional arguments.

Part of the implementation for RFD 129.

As part of the changes, I also reduced the number of API calls to list kube clusters from 3 to one - in the case of a relogin required, or an access request is auto-generated, we of course still incur unavoidable extra API calls.

Example

$ tsh kube ls
Kube Cluster Name Labels                                                         Selected
----------------- -------------------------------------------------------------- --------
test              account-id=123456789012,env=fake,origin=gavin,region=us-west-1
test-cluster      account-id=123456789012,origin=gavin,region=us-west-1

$ tsh kube login --labels env=fake
Logged into Kubernetes clusters:
test

Select a context and try 'kubectl version' to test the connection.

$ tsh kube login --query 'labels["region"] == "us-west-1" && labels["env"] != "fake"'
Logged into Kubernetes clusters:
test-cluster-eks-us-west-1-123456789012

Select a context and try 'kubectl version' to test the connection.

$ tsh kube ls
Kube Cluster Name Labels                                                         Selected
----------------- -------------------------------------------------------------- --------
test              account-id=123456789012,env=fake,origin=gavin,region=us-west-1
test-cluster      account-id=123456789012,origin=gavin,region=us-west-1

$ tsh kube login test-clus
Logged into Kubernetes cluster "test-cluster-eks-us-west-1-123456789012". Try 'kubectl version' to test the connection.

$ tsh kube login test
Logged into Kubernetes cluster "test". Try 'kubectl version' to test the connection.

$ tsh kube ls
Kube Cluster Name Labels                                                         Selected
----------------- -------------------------------------------------------------- --------
test              account-id=123456789012,env=fake,origin=gavin,region=us-west-1 *
test-cluster      account-id=123456789012,origin=gavin,region=us-west-1

$ tsh kube login test-clus
Logged into Kubernetes cluster "test-cluster-eks-us-west-1-123456789012". Try 'kubectl version' to test the connection.

$ tsh kube ls
Kube Cluster Name Labels                                                         Selected
----------------- -------------------------------------------------------------- --------
test              account-id=123456789012,env=fake,origin=gavin,region=us-west-1
test-cluster      account-id=123456789012,origin=gavin,region=us-west-1          *

$ tsh kube login te
ERROR: kubernetes cluster "te" matches multiple kubernetes clusters:

Kube Cluster Name                       Labels                                                                                                                                                                                               Selected
--------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------
test                                    account-id=123456789012,env=fake,origin=gavin,region=us-west-1,teleport.dev/cloud=AWS,teleport.dev/origin=dynamic
test-cluster-eks-us-west-1-123456789012 account-id=123456789012,origin=gavin,region=us-west-1,teleport.dev/cloud=AWS,teleport.dev/origin=cloud,teleport.internal/discovered-name=test-cluster,teleport.internal/discovery-group-name=aws-dev

Hint: use 'tsh kube ls -v' or 'tsh kube ls --format=[json|yaml]' to list all kubernetes clusters with full details.
Hint: try selecting the kubernetes cluster with a more specific name (ex: tsh kube login test).
Hint: try selecting the kubernetes cluster with additional --labels or --query predicate.

Related issue:

@GavinFrazar GavinFrazar added ux kubernetes-access tsh tsh - Teleport's command line tool for logging into nodes running Teleport. backport/branch/v13 labels Aug 10, 2023
@GavinFrazar GavinFrazar requested a review from tigrato August 10, 2023 01:16
@GavinFrazar GavinFrazar requested a review from smallinsky August 10, 2023 02:32
Copy link
Copy Markdown
Contributor

@smallinsky smallinsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested with this with GCP audto-discover and it works fine. But wonder if we can delay backporting to v13 resource name collision CLI changes after we will finish v13 test plan regression.

@GavinFrazar
Additionally we have also the tsh proxy kube command that needs to be extended.

Comment thread tool/tsh/common/kube.go
@GavinFrazar GavinFrazar added this pull request to the merge queue Aug 14, 2023
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 14, 2023
@GavinFrazar GavinFrazar added this pull request to the merge queue Aug 14, 2023
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 14, 2023
@GavinFrazar GavinFrazar enabled auto-merge August 14, 2023 20:30
@GavinFrazar GavinFrazar added this pull request to the merge queue Aug 14, 2023
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to Branch Protection failures Aug 14, 2023
You're not authorized to push to this branch. Visit "About protected branches" for more information.
@GavinFrazar GavinFrazar added this pull request to the merge queue Aug 14, 2023
@GavinFrazar GavinFrazar removed this pull request from the merge queue due to a manual request Aug 14, 2023
* fetch kube clusters once for login
@GavinFrazar GavinFrazar force-pushed the gavinfrazar/tsh-resource-selection-ux-kube branch from 6b88a49 to 5d2a5a6 Compare August 14, 2023 21:21
@GavinFrazar GavinFrazar enabled auto-merge August 14, 2023 21:21
@GavinFrazar GavinFrazar added this pull request to the merge queue Aug 14, 2023
Merged via the queue into master with commit 2f50a7f Aug 14, 2023
@GavinFrazar GavinFrazar deleted the gavinfrazar/tsh-resource-selection-ux-kube branch August 14, 2023 22:39
@public-teleport-github-review-bot
Copy link
Copy Markdown

@GavinFrazar See the table below for backport results.

Branch Result
branch/v13 Failed

GavinFrazar added a commit that referenced this pull request Sep 16, 2023
backports #30252 to branch/v13.

* fetch kube clusters once for login
GavinFrazar added a commit that referenced this pull request Sep 19, 2023
backports #30252 to branch/v13.

* fetch kube clusters once for login
GavinFrazar added a commit that referenced this pull request Sep 19, 2023
backports #30252 to branch/v13.

* fetch kube clusters once for login
github-merge-queue Bot pushed a commit that referenced this pull request Sep 19, 2023
* [v13] Fix Kubernetes selected cluster

backports #30167 to branch/v13.

* Fix Kubernetes selected cluster

Kubeconfig context name can be customized using `--set-context-name`
flag. When using it, the selected Kubernetes cluster fails to correctly
identify the cluster name.

* [v13] tsh kube login by prefix, query, labels (#32088)

backports #30252 to branch/v13.

* fetch kube clusters once for login

* [v13] update `tsh proxy kube` cluster selection ux (#32089)

backports #30478 to branch/v13.

* select by labels, query predicate, name, and/or prefix of name.
* fix --cluster flag not being propagated

* [v13] simplify tsh db prefix predicate logic (#32090)

backports #30531 to branch/v13.

* [v13] check for discovered kube cluster name exact match (#32091)

backports #31744 to branch/v13.

* [v13] rework tsh database selection logic (#32092)

backports #31689 to branch/v13.

* disallow prefix matching
* select by exact name match first,
* otherwise look for unambiguous discovered name label match.
* look for an active db to resolve discovered name match ambiguity.
* add more predicate builder helpers
* check for db name in not found error for stale cert hint
* no error status on tsh db logout with no logged in dbs
* remove dead code
* refactor helper funcs to simplify code and make it easier to test
* test complex database selection
* test findActiveDatabase
* test choosing one db by discovered name
* add more resource selectors tests
* test formatDatabaseLoginCommand
* add debug logging for db selection

* [v13] Fix `tsh kube login` when creds are expired or doesn't exist (#32095)

Backports #31418 to branch/v13.

This PR uses `client.RetryWithRelogin` helper to deal with cases where user's
credentials don't exist or are already expired.

Co-authored-by: Tiago Silva <tiago.silva@goteleport.com>

* remove tsh kube prefix matching (#31852) (#32097)

* fix retry with relogin for ambiguous clusters
* consolidate test setup for login/proxy kube selection tests
* add more test cases for kube selection
* remove prefix testing
* add origin cloud label in tests
* refactor the check for multiple cluster login into a func

* [v13] remove prefix matching in tctl (#32104)

Backports #31916 to branch/v13.

* remove prefix matching in tctl
* replace prefix matching with exact discovered name match as a fallback
  when no resource full name matches the name given by a user
* refactor test helpers
* avoid decoding yaml/json into already initialized var

---------

Co-authored-by: Tiago Silva <tiago.silva@goteleport.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kubernetes-access size/md tsh tsh - Teleport's command line tool for logging into nodes running Teleport. ux

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants