Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kubeconfig path changes in context create #535

Merged
merged 1 commit into from
Oct 13, 2023

Conversation

prkalle
Copy link
Contributor

@prkalle prkalle commented Oct 11, 2023

What this PR does / why we need it

This PR implements kubeconfig path changes in context create command.

Changes Summary:

  • Support multi-file KUBECONFIG path in context creation (e.g. export KUBECONFIG=/temp/non-existing.kfg:/temp/tkgCluster_admin.kfg)
  • Modify kubeconfig file path to use default kubeconfig path instead of $HOME/.kube-tanzu/config while creating context for TKG pinniped endpoint

Notes:

  • If user uses KUBECONFIG environment variable and has set multiple paths (ex:KUBECONFIG=/path1/file1:/path2/file2:/path3/file3) CLI would use the default kubeconfig file path which would be the first existing(file already created in user file system) file path, while creating the context. This means the --kubecontext provided by user should be available in the first existing file path, else CLI would throw error stating that kubecontext provided by user doesn't exist.

Which issue(s) this PR fixes

Fixes #

Describe testing done for PR

Created the context by exporting KUBECONFIG environment variable with single kubeconfig file

❯ export KUBECONFIG=~/temp/tkgCluster_admin.kfg
❯ ./bin/tanzu context create tkg-mgmt-vc  --kubecontext tkg-mgmt-vc-admin@tkg-mgmt-vc
[ok] successfully created a kubernetes context using the kubeconfig /Users/pkalle/temp/tkgCluster_admin.kfg
[i] Checking for required plugins...
[i] All required plugins are already installed and up-to-date

Created the context by exporting KUBECONFIG environment variable with multiple kubeconfig files and the first one is non-existent. Verified the second file path is considered for context creation as expected

❯ export KUBECONFIG=~/temp/non-existing.kfg:~/temp/tkgCluster_admin.kfg
❯ ./bin/tanzu context create tkg-mgmt-vc  --kubecontext tkg-mgmt-vc-admin@tkg-mgmt-vc
[ok] successfully created a kubernetes context using the kubeconfig /Users/pkalle/temp/tkgCluster_admin.kfg
[i] Checking for required plugins...
[i] All required plugins are already installed and up-to-date

Created the context by exporting KUBECONFIG environment variable with multiple kubeconfig files and the first file path one is valid. Verified the first file path is considered for context creation as expected

❯ export KUBECONFIG=~/temp/tkgCluster_admin.kfg:/Users/pkalle/temp/mc-kubeconfig
❯ ./bin/tanzu context create tkg-mgmt-vc  --kubecontext tkg-mgmt-vc-admin@tkg-mgmt-vc
[ok] successfully created a kubernetes context using the kubeconfig /Users/pkalle/temp/tkgCluster_admin.kfg
[i] Checking for required plugins...
[i] All required plugins are already installed and up-to-date

Tried to create the context by exporting KUBECONFIG environment variable with multiple kubeconfig files and the first file path one is valid file path but the context provided doesn't exist. Verified the context creation failed as expected

❯ export KUBECONFIG=/Users/pkalle/temp/mc-kubeconfig:~/temp/tkgCluster_admin.kfg
❯ ./bin/tanzu context create tkg-mgmt-vc  --kubecontext tkg-mgmt-vc-admin@tkg-mgmt-vc
[x] : failed to create context "tkg-mgmt-vc" for a kubernetes cluster, Unable to set up rest config due to : invalid configuration: [context was not found for specified context: tkg-mgmt-vc-admin@tkg-mgmt-vc, cluster has no server defined]
[x] : failed to create context "tkg-mgmt-vc" for a kubernetes cluster, Unable to set up rest config due to : invalid configuration: [context was not found for specified context: tkg-mgmt-vc-admin@tkg-mgmt-vc, cluster has no server defined]

Testing for updated kubeconfig path (using default kubeconfig path for context creation given TKG endpoint with pinniped support)

####Before the change

❯ ./bin/tanzu context create tkg-mgmt-vc-pin --endpoint  https://10.180.68.150:6443  --insecure-skip-tls-verify
[i] Could not get login banner from server, response code = 403
[ok] successfully created a kubernetes context using the kubeconfig /Users/pkalle/.kube-tanzu/config
[i] Checking for required plugins...
[!] unable to automatically sync the plugins from target context. Please run 'tanzu plugin sync' command to sync plugins manually, error: 'unable to list plugins from discovery source 'default-tkg-mgmt-vc-pin': cliplugins.cli.tanzu.vmware.com is forbidden: User "pkalle" cannot list resource "cliplugins" in API group "cli.tanzu.vmware.com" at the cluster scope'


#### After the change
// modified the default kubeconfig path and executed the context create and verified the pinniped kubeconfig generated is merged into default kubeconfig
❯ export KUBECONFIG=/Users/pkalle/temp/tkgCluster_admin.kfg:/Users/pkalle/temp/mc-kubeconfig
❯ ./bin/tanzu context create tkg-mgmt-vc-pin --endpoint  https://10.180.68.150:6443  --insecure-skip-tls-verify
[i] Could not get login banner from server, response code = 403
Log in by visiting this link:

    https://10.180.68.237/oauth2/authorize?access_type=offline&client_id=pinniped-cli&code_challenge=yzGTYuTmVYcmZ4d2TzAFHpw2-HqNpVIiIxmTVIh50QA&code_challenge_method=S256&nonce=0e4ee2e1bae20be363ef3c8b08fd2ffc&redirect_uri=http%3A%2F%2F127.0.0.1%3A52147%2Fcallback&response_mode=form_post&response_type=code&scope=offline_access+openid+pinniped%3Arequest-audience&state=c03c3d1d3ed2455aa017b2a79b22bed1

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

[ok] successfully created a kubernetes context using the kubeconfig /Users/pkalle/temp/tkgCluster_admin.kfg
[i] Checking for required plugins...
[!] unable to automatically sync the plugins from target context. Please run 'tanzu plugin sync' command to sync plugins manually, error: 'unable to list plugins from discovery source 'default-tkg-mgmt-vc-pin': cliplugins.cli.tanzu.vmware.com is forbidden: User "pkalle" cannot list resource "cliplugins" in API group "cli.tanzu.vmware.com" at the cluster scope'


// Tested after unsetting the KUBECONFIG variable
// the kubeconfig is stored in the default kubeconfig file ( /Users/pkalle/.kube/config)
❯ unset KUBECONFIG
❯ ./bin/tanzu context create tkg-mgmt-vc-pin --endpoint  https://10.180.68.150:6443  --insecure-skip-tls-verify
[i] Could not get login banner from server, response code = 403
Log in by visiting this link:

    https://10.180.68.237/oauth2/authorize?access_type=offline&client_id=pinniped-cli&code_challenge=CXQV3t62gFqanK6aMSFaa6pp69HZzg5cXQvuHCjMElA&code_challenge_method=S256&nonce=2acf3b7649c2cb27216e0670b63d893b&redirect_uri=http%3A%2F%2F127.0.0.1%3A59276%2Fcallback&response_mode=form_post&response_type=code&scope=offline_access+openid+pinniped%3Arequest-audience&state=638a56deee7833c2fd85308539d8d77f

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

[ok] successfully created a kubernetes context using the kubeconfig /Users/pkalle/.kube/config

Release note

Support multi-file KUBECONFIG path in context creation and updated kubeconfig file path to use default kubeconfig path instead of $HOME/.kube-tanzu/config while creating context for TKG pinniped endpoint

Additional information

Special notes for your reviewer

- Support multi-file KUBECONFIG path in context creation
- Modify kubeconfig file path to use default kubeconfig path instead of $HOME/.kube-tanzu/config while creating context for TKG pinniped endpoint

Signed-off-by: Prem Kumar Kalle <pkalle@vmware.com>
@prkalle prkalle requested a review from a team as a code owner October 11, 2023 16:20
Copy link
Contributor

@anujc25 anujc25 left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@vuil vuil left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

@prkalle prkalle merged commit 7966dad into vmware-tanzu:main Oct 13, 2023
7 checks passed
@marckhouzam
Copy link
Contributor

@prkalle I'm trying the new "Support multi-file KUBECONFIG path in context create" and I'm unable to get it to work as I had expected. Maybe I misunderstood what this feature really means...
Here is what I'm doing:

# I have two clusters, each with its own kubeconfig file
# kubeconfig-tkg1.yaml contains one context k3d-tkg1
# kubeconfig-tkg3.yaml contains one context k3d-tkg3
# To use both, I export my variable as follows:
$ export KUBECONFIG=/Users/kmarc/.k3d/kubeconfig-tkg1.yaml:/Users/kmarc/.k3d/kubeconfig-tkg3.yaml

# Notice how kubectl can find both contexts, one in each file
$ kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO         NAMESPACE
*         k3d-tkg1   k3d-tkg1   admin@k3d-tkg1
          k3d-tkg3   k3d-tkg3   admin@k3d-tkg3

# I can create a context using the kubecontext of the first file
$ tz context create tkg1 --kubecontext k3d-tkg1
[ok] successfully created a kubernetes context using the kubeconfig /Users/kmarc/.k3d/kubeconfig-tkg1.yaml
[i] Checking for required plugins...
^C

# But not the second
$ tz context create tkg3 --kubecontext k3d-tkg3
[x] : failed to create context "tkg3" for a kubernetes cluster, Unable to set up rest config due to : invalid configuration: [context was not found for specified context: k3d-tkg3, cluster has no server defined]
[x] : failed to create context "tkg3" for a kubernetes cluster, Unable to set up rest config due to : invalid configuration: [context was not found for specified context: k3d-tkg3, cluster has no server defined]

# But it works if I specify the file using --kubeconfig
$ tz context create tkg3 --kubecontext k3d-tkg3 --kubeconfig /Users/kmarc/.k3d/kubeconfig-tkg3.yaml
[ok] successfully created a kubernetes context using the kubeconfig /Users/kmarc/.k3d/kubeconfig-tkg3.yaml
[i] Checking for required plugins...
^C

@marckhouzam marckhouzam added this to the 1.1.0 milestone Oct 20, 2023
@prkalle prkalle added the docs-impact issues with documentation impact label Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-not-required docs-impact issues with documentation impact
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants