Skip to content

new kmeshctl secret command#1495

Merged
kmesh-bot merged 7 commits intokmesh-net:mainfrom
zrggw:new_kmeshctl
Sep 2, 2025
Merged

new kmeshctl secret command#1495
kmesh-bot merged 7 commits intokmesh-net:mainfrom
zrggw:new_kmeshctl

Conversation

@zrggw
Copy link
Copy Markdown
Contributor

@zrggw zrggw commented Sep 1, 2025

What type of PR is this?
/kind enhancement

What this PR does / why we need it:
This PR introduces a new kmeshctl secret command that provides comprehensive management of IPsec encryption keys for Kmesh's secure communication between nodes. The command enables users to create, retrieve, and delete IPsec secrets stored as Kubernetes secrets.

Command Structure

kmeshctl secret create [--key=<string>]  # Create/update IPsec secret
kmeshctl secret get                          # Display current IPsec configuration  
kmeshctl secret delete                       # Remove IPsec secret

Documentation
kmeshctl_secret.md - Main command overview
kmeshctl_secret_create.md - Create command details
kmeshctl_secret_get.md - Get command details
kmeshctl_secret_delete.md - Delete command details

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Signed-off-by: aicee <hhbin2000@foxmail.com>
Copilot AI review requested due to automatic review settings September 1, 2025 10:52
@kmesh-bot kmesh-bot added the kind/enhancement New feature or request label Sep 1, 2025
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @zrggw, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances Kmesh's command-line interface by providing a dedicated and comprehensive tool for managing IPsec encryption keys. This new functionality streamlines the process of securing inter-node communication within Kmesh deployments, offering users direct control over the lifecycle of these critical security assets.

Highlights

  • New kmeshctl secret command: Introduces a new top-level command kmeshctl secret to manage IPsec encryption keys for Kmesh's secure communication.
  • Comprehensive IPsec Secret Management: The kmeshctl secret command now includes create, get, and delete subcommands, allowing users to fully manage IPsec secrets stored as Kubernetes secrets.
  • Flexible Key Creation: The create subcommand supports generating cryptographically secure random keys automatically or accepting a user-defined hexadecimal key string.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Keys in secret kept,Network whispers, safe and sound,Kmesh guards the way.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new kmeshctl secret command with create, get, and delete subcommands to manage IPsec secrets. The implementation is well-structured, using cobra for the CLI and interacting with the Kubernetes API to manage secrets. The documentation has also been updated accordingly. My review focuses on improving context handling in Kubernetes API calls. I've identified several places where context.TODO() is used, and I recommend replacing it with the command's context (cmd.Context()) for proper cancellation and timeout handling. This is important for making the CLI tool more robust and well-behaved.


ipSecKey.Length = AeadAlgoICVLength

secretOld, err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Get(context.TODO(), SecretName, metav1.GetOptions{})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Using context.TODO() is not recommended as it provides an empty, non-cancellable context. Since this function already receives the cmd object from cobra, you should use cmd.Context() for all Kubernetes API calls. This allows for proper cancellation (e.g., if the user presses Ctrl+C) and timeout handling. This change should be applied to all Kubernetes client calls in this function (lines 151, 184, and 190).

Suggested change
secretOld, err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Get(context.TODO(), SecretName, metav1.GetOptions{})
secretOld, err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Get(cmd.Context(), SecretName, metav1.GetOptions{})

Comment on lines +198 to +201
func GetSecret() {
clientset := createKubeClientOrExit()

secret, err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Get(context.TODO(), SecretName, metav1.GetOptions{})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The GetSecret function uses context.TODO() for its Kubernetes API call. To enable proper context propagation and cancellation, the function should accept the command's context. You should change the function signature to func GetSecret(cmd *cobra.Command) and use cmd.Context(). You will also need to update the call to this function in the getCmd definition (line 85) to pass cmd.

Suggested change
func GetSecret() {
clientset := createKubeClientOrExit()
secret, err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Get(context.TODO(), SecretName, metav1.GetOptions{})
func GetSecret(cmd *cobra.Command) {
clientset := createKubeClientOrExit()
secret, err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Get(cmd.Context(), SecretName, metav1.GetOptions{})

Comment on lines +249 to +252
func DeleteSecret() {
clientset := createKubeClientOrExit()

err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Delete(context.TODO(), SecretName, metav1.DeleteOptions{})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Similar to GetSecret, the DeleteSecret function uses context.TODO(). It should be updated to accept the command's context for proper cancellation handling. Please change the function signature to func DeleteSecret(cmd *cobra.Command), use cmd.Context() for the API call, and update the call site at line 96 to pass the cmd object.

Suggested change
func DeleteSecret() {
clientset := createKubeClientOrExit()
err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Delete(context.TODO(), SecretName, metav1.DeleteOptions{})
func DeleteSecret(cmd *cobra.Command) {
clientset := createKubeClientOrExit()
err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Delete(cmd.Context(), SecretName, metav1.DeleteOptions{})

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new kmeshctl secret command for comprehensive IPsec secret management in Kubernetes. The command provides create, get, and delete operations for IPsec encryption keys used in secure node-to-node communication.

  • Adds three subcommands: create, get, and delete for IPsec secret lifecycle management
  • Supports both auto-generated and user-defined encryption keys with proper validation
  • Includes comprehensive documentation for all command variants

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
ctl/secret/secret.go Implements the core secret management functionality with create, get, and delete operations
docs/ctl/kmeshctl_secret.md Updated main command documentation with examples for all subcommands
docs/ctl/kmeshctl_secret_create.md Documentation for the create subcommand with user-defined key support
docs/ctl/kmeshctl_secret_get.md New documentation for retrieving and displaying IPsec configurations
docs/ctl/kmeshctl_secret_delete.md New documentation for deleting IPsec secrets

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +127 to +140
if !cmd.Flags().Changed("key") {
aeadKey = make([]byte, AeadKeyLength)
_, err := rand.Read(aeadKey)
if err != nil {
log.Errorf("failed to generate random key: %v", err)
os.Exit(1)
}
} else {
aeadKey, err = hex.DecodeString(aeadKeyArg)
if err != nil {
log.Errorf("failed to decode hex string: %v, input: %v", err, aeadKeyArg)
os.Exit(1)
}
}
Copy link

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

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

[nitpick] The key generation and validation logic should be extracted into separate functions to improve readability and testability. Consider creating generateRandomKey() and validateUserKey() functions.

Copilot uses AI. Check for mistakes.
- Fix spacing around & operators in BPF macros to match CI expectations
- Regenerate kmeshctl documentation after adding secret command
- Apply consistent code formatting via make gen

Signed-off-by: aicee <hhbin2000@foxmail.com>
Signed-off-by: aicee <hhbin2000@foxmail.com>
Signed-off-by: aicee <hhbin2000@foxmail.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Sep 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 38.27%. Comparing base (24be567) to head (8118999).
⚠️ Report is 19 commits behind head on main.
see 8 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 79623e0...8118999. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: aicee <hhbin2000@foxmail.com>
Signed-off-by: aicee <hhbin2000@foxmail.com>
}

func GetSecret() {
clientset := createKubeClientOrExit()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ithink can move the client builde out of all the functions Get/Delete/Create

Signed-off-by: aicee <hhbin2000@foxmail.com>
Copy link
Copy Markdown
Member

@hzxuzhonghu hzxuzhonghu left a comment

Choose a reason for hiding this comment

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

/lgtm

)

var log = logger.NewLoggerScope("kmeshctl/secret")
var clientset kube.CLIClient
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Basically i am not a fan of global varible, you can pass the client in the function arguments

@kmesh-bot
Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hzxuzhonghu

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmesh-bot kmesh-bot merged commit c700423 into kmesh-net:main Sep 2, 2025
12 checks passed
@zrggw zrggw deleted the new_kmeshctl branch September 2, 2025 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants