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: allow select context for kubecm add and merge command #952

Merged
merged 2 commits into from
May 20, 2024

Conversation

cr7258
Copy link
Contributor

@cr7258 cr7258 commented May 18, 2024

Description

Add the --select-context parameter allowing the user to choose which context should be added when running the kubecm add and kubecm merge commands.

Test

Prepare two kubeconfig.

./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  |    default   |
+------------+-------------+-------------+----------+-------------------------+--------------+
|            |   context3  |   cluster3  |   user3  |   https://example3.com  |    default   |
+------------+-------------+-------------+----------+-------------------------+--------------+
|            |   context4  |   cluster4  |   user4  |   https://example4.com  |    default   |
+------------+-------------+-------------+----------+-------------------------+--------------+

Run the kubecm add command with the --select-context parameter. The user needs to confirm each context to be imported. If the --select-context parameter is omitted, confirmation is not required.

./kubecm add --config kubeconfig1 -f kubeconfig2 --select-context

asciicast

Run the kubecm merge command with the --select-context parameter.

./kubecm merge --config output  kubeconfig1 kubeconfig2 --select-context

asciicast

If there is no context to add or merge, the program ends directly instead of asking Does it overwrite File xxx ?.
asciicast

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/feature ategorizes issue or PR as related to a new feature. size/M size/M labels May 18, 2024
Copy link

the-label-bot bot commented May 18, 2024

The Label Bot has predicted the following:

Category Value Confidence Applied Label
Kind feature 0.701 ✔️
Kind M 0.962 ✔️

Copy link

codecov bot commented May 18, 2024

Codecov Report

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

Project coverage is 19.40%. Comparing base (c940d7b) to head (bd57195).

Files Patch % Lines
cmd/add.go 21.42% 9 Missing and 2 partials ⚠️
cmd/cloud_add.go 0.00% 10 Missing ⚠️
cmd/merge.go 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #952      +/-   ##
==========================================
- Coverage   19.54%   19.40%   -0.15%     
==========================================
  Files          21       21              
  Lines        2062     2077      +15     
==========================================
  Hits          403      403              
- Misses       1617     1630      +13     
- Partials       42       44       +2     
Flag Coverage Δ
unittests 19.40% <10.00%> (-0.15%) ⬇️

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.

@sunny0826 sunny0826 self-assigned this May 20, 2024
@sunny0826
Copy link
Owner

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

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 might be better to use String for this flag.

@@ -46,6 +48,7 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error {
file, _ := ac.command.Flags().GetString("file")
cover, _ := ac.command.Flags().GetBool("cover")
contextName, _ := ac.command.Flags().GetString("context-name")
selectContext, _ := ac.command.Flags().GetBool("select-context")
Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps GetString is used here and followed by the relevant checks: exists, does not exist, is empty, etc.
This would allow for better automated testing in e2e test.

selectContext, _ := ac.command.Flags().GetString("select-context")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to choose contexts interactively, so I use bool type.

kubecm add -f kubeconfig1 --select-context

For my understanding, you want this:

kubecm add -f kubeconfig1 --select-context context1

It seems like we couldn't implement different types for the same flag (bool and string) in Cobra. So I think your idea should be implemented by a different flag.

Copy link
Owner

Choose a reason for hiding this comment

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

Indeed, a new flag should be created here.

Copy link
Owner

Choose a reason for hiding this comment

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

@cr7258 But this new flag is a new feature, which is not part of this PR, would you like to create a new PR to contribute this this feature? I will merge this PR first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sunny0826 I will contribute this feature in a new PR.

Copy link
Owner

Choose a reason for hiding this comment

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

extremely grateful !

cmd/merge.go Show resolved Hide resolved
@sunny0826 sunny0826 assigned cr7258 and unassigned sunny0826 May 20, 2024
@sunny0826
Copy link
Owner

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

@sunny0826 sunny0826 merged commit adf5297 into sunny0826:master May 20, 2024
6 checks passed
@sunny0826
Copy link
Owner

@all-contributors please add @cr7258 for doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature ategorizes issue or PR as related to a new feature. size/M size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants