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

feat: generate context name by specific attributes #956

Merged
merged 6 commits into from
May 23, 2024

Conversation

cr7258
Copy link
Contributor

@cr7258 cr7258 commented May 21, 2024

Description

Use two kubeconfig files for testing.

./kubecm list --config kubeconfig1
+------------+-------------+-------------+----------+-------------------------+--------------+
|   CURRENT  |     NAME    |   CLUSTER   |   USER   |          SERVER         |   Namespace  |
+============+=============+=============+==========+=========================+==============+
|      *     |   context1  |   cluster1  |   user1  |   https://example1.com  |    default   |
+------------+-------------+-------------+----------+-------------------------+--------------+
./kubecm list --config kubeconfig2
+------------+-------------+-------------+----------+-------------------------+----------------+
|   CURRENT  |     NAME    |   CLUSTER   |   USER   |          SERVER         |    Namespace   |
+============+=============+=============+==========+=========================+================+
|      *     |   context2  |   cluster2  |   user2  |   https://example2.com  |   kube-system  |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |   context3  |   cluster3  |   user3  |   https://example3.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |   context4  |   cluster4  |   user4  |   https://example4.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+

Case 1: Use the default context-template value ("context")
The context name is the same as the original kubeconfig.

./kubecm add --config kubeconfig1 -f kubeconfig2

# result
+------------+-------------+-------------+----------+-------------------------+----------------+
|   CURRENT  |     NAME    |   CLUSTER   |   USER   |          SERVER         |    Namespace   |
+============+=============+=============+==========+=========================+================+
|      *     |   context1  |   cluster1  |   user1  |   https://example1.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |   context2  |   cluster2  |   user2  |   https://example2.com  |   kube-system  |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |   context3  |   cluster3  |   user3  |   https://example3.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |   context4  |   cluster4  |   user4  |   https://example4.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+

Case 2: Define context template

./kubecm add --config kubeconfig1 -f kubeconfig2 --context-template filename,cluster,user

# result
+------------+-------------------------------+-------------+----------+-------------------------+----------------+
|   CURRENT  |              NAME             |   CLUSTER   |   USER   |          SERVER         |    Namespace   |
+============+===============================+=============+==========+=========================+================+
|      *     |            context1           |   cluster1  |   user1  |   https://example1.com  |     default    |
+------------+-------------------------------+-------------+----------+-------------------------+----------------+
|            |   kubeconfig2-cluster2-user2  |   cluster2  |   user2  |   https://example2.com  |   kube-system  |
+------------+-------------------------------+-------------+----------+-------------------------+----------------+
|            |   kubeconfig2-cluster3-user3  |   cluster3  |   user3  |   https://example3.com  |     default    |
+------------+-------------------------------+-------------+----------+-------------------------+----------------+
|            |   kubeconfig2-cluster4-user4  |   cluster4  |   user4  |   https://example4.com  |     default    |
+------------+-------------------------------+-------------+----------+-------------------------+----------------+

Case 3: Define context template and context prefix

./kubecm add --config kubeconfig1 -f kubeconfig2 --context-template cluster,user --context-prefix demo

# result
+------------+------------------------+-------------+----------+-------------------------+----------------+
|   CURRENT  |          NAME          |   CLUSTER   |   USER   |          SERVER         |    Namespace   |
+============+========================+=============+==========+=========================+================+
|      *     |        context1        |   cluster1  |   user1  |   https://example1.com  |     default    |
+------------+------------------------+-------------+----------+-------------------------+----------------+
|            |   demo-cluster2-user2  |   cluster2  |   user2  |   https://example2.com  |   kube-system  |
+------------+------------------------+-------------+----------+-------------------------+----------------+
|            |   demo-cluster3-user3  |   cluster3  |   user3  |   https://example3.com  |     default    |
+------------+------------------------+-------------+----------+-------------------------+----------------+
|            |   demo-cluster4-user4  |   cluster4  |   user4  |   https://example4.com  |     default    |
+------------+------------------------+-------------+----------+-------------------------+----------------+

Case 4: kubecm merge with context template

./kubecm merge kubeconfig1 kubeconfig2 --context-template cluster,user

# result
+------------+-------------------+-------------+----------+-------------------------+----------------+
|   CURRENT  |        NAME       |   CLUSTER   |   USER   |          SERVER         |    Namespace   |
+============+===================+=============+==========+=========================+================+
|            |   cluster1-user1  |   cluster1  |   user1  |   https://example1.com  |     default    |
+------------+-------------------+-------------+----------+-------------------------+----------------+
|            |   cluster2-user2  |   cluster2  |   user2  |   https://example2.com  |   kube-system  |
+------------+-------------------+-------------+----------+-------------------------+----------------+
|            |   cluster3-user3  |   cluster3  |   user3  |   https://example3.com  |     default    |
+------------+-------------------+-------------+----------+-------------------------+----------------+
|            |   cluster4-user4  |   cluster4  |   user4  |   https://example4.com  |     default    |
+------------+-------------------+-------------+----------+-------------------------+----------------+

Case 5: Define context name
it's useful if there is only one context in the kubeconfig file.

./kubecm add --config kubeconfig2 -f kubeconfig1 --context-name seven

# result
+------------+-------------+-------------+----------+-------------------------+----------------+
|   CURRENT  |     NAME    |   CLUSTER   |   USER   |          SERVER         |    Namespace   |
+============+=============+=============+==========+=========================+================+
|      *     |   context2  |   cluster2  |   user2  |   https://example2.com  |   kube-system  |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |   context3  |   cluster3  |   user3  |   https://example3.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |   context4  |   cluster4  |   user4  |   https://example4.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+
|            |    seven    |   cluster1  |   user1  |   https://example1.com  |     default    |
+------------+-------------+-------------+----------+-------------------------+----------------+

If the kubeconfig file contains more than 1 context, an incremental number will be added as a suffix.

./kubecm add --config kubeconfig1 -f kubeconfig2 --context-name seven
Add Context: seven
Add Context: seven-2
Add Context: seven-3

Related Issue

resolves #768

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update (changes only affecting documentation)

Checklist

  • I have tested my changes locally and ensured they are functioning properly. Please run the make build and make test commands.
  • I have added/updated unit or e2e tests to cover my changes.
  • I have updated the relevant documentation. If you change commands or arguments, run make doc-gen to generate new documentation.

@the-label-bot the-label-bot bot added kind/documentation Improvements or additions to documentation size/L size/L labels May 21, 2024
Copy link

the-label-bot bot commented May 21, 2024

The Label Bot has predicted the following:

Category Value Confidence Applied Label
Kind bug 0.547 ✔️
Size XL 0.754 ✔️

@cr7258 cr7258 changed the title Generate context name feat: generate context name by specific attributes May 21, 2024
Copy link

gitguardian bot commented May 21, 2024

⚠️ GitGuardian has uncovered 2 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
4387263 Triggered Generic High Entropy Secret 331be08 cmd/add_test.go View secret
4387264 Triggered Generic High Entropy Secret 331be08 cmd/add_test.go View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Copy link

codecov bot commented May 21, 2024

Codecov Report

Attention: Patch coverage is 30.95238% with 58 lines in your changes are missing coverage. Please review.

Project coverage is 19.65%. Comparing base (da53bb2) to head (d992222).

Files Patch % Lines
cmd/add.go 42.85% 27 Missing and 1 partial ⚠️
cmd/merge.go 0.00% 19 Missing ⚠️
cmd/cloud_add.go 0.00% 10 Missing ⚠️
cmd/root.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #956      +/-   ##
==========================================
+ Coverage   19.40%   19.65%   +0.24%     
==========================================
  Files          21       21              
  Lines        2077     2122      +45     
==========================================
+ Hits          403      417      +14     
- Misses       1630     1661      +31     
  Partials       44       44              
Flag Coverage Δ
unittests 19.65% <30.95%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cr7258 cr7258 force-pushed the generate-context-name branch 2 times, most recently from 0bdc035 to c302e3c Compare May 21, 2024 23:31
Signed-off-by: chengzw <[email protected]>
@sunny0826 sunny0826 added enhancement New feature or request kind/feature ategorizes issue or PR as related to a new feature. e2e and removed kind/documentation Improvements or additions to documentation labels May 22, 2024
@sunny0826 sunny0826 self-requested a review May 22, 2024 02:27
Copy link
Owner

@sunny0826 sunny0826 left a comment

Choose a reason for hiding this comment

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

Excellent, just need to add some unit tests!

cmd/add.go Show resolved Hide resolved
cmd/utils.go Show resolved Hide resolved
Signed-off-by: chengzw <[email protected]>
@cr7258 cr7258 requested a review from sunny0826 May 23, 2024 00:35
Copy link
Owner

@sunny0826 sunny0826 left a comment

Choose a reason for hiding this comment

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

It's close to perfect, please update the Chinese documentation: zh-cn/cli/kubecm_merge.md.

@the-label-bot the-label-bot bot added size/XL size/XL and removed size/L size/L labels May 23, 2024
@cr7258 cr7258 requested a review from sunny0826 May 23, 2024 02:37
Copy link
Owner

@sunny0826 sunny0826 left a comment

Choose a reason for hiding this comment

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

LGTM
Thank you for your valuable contribution to this project! Your work is greatly appreciated. 🎆

@sunny0826 sunny0826 merged commit 915c93b into sunny0826:master May 23, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e enhancement New feature or request kind/feature ategorizes issue or PR as related to a new feature. size/XL size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate context and user name smartly when merging config
2 participants