Skip to content

Device edit#82

Open
kearen001 wants to merge 2 commits intokubeedge:mainfrom
kearen001:device_edit
Open

Device edit#82
kearen001 wants to merge 2 commits intokubeedge:mainfrom
kearen001:device_edit

Conversation

@kearen001
Copy link
Copy Markdown

What type of PR is this?
/kind cleanup

What this PR does / why we need it:
Refactor device handler to extract a common helper function
for getting kubeedge client and namespace.
This improves code readability and reduces duplication.
Which issue(s) this PR fixes:

Fixes #
NONE
Special notes for your reviewer:
NONE
Does this PR introduce a user-facing change?:

NONE

NONE

@kubeedge-bot kubeedge-bot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Sep 17, 2025
@kubeedge-bot
Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign vincentgoat after the PR has been reviewed.
You can assign the PR to them by writing /assign @vincentgoat in a comment when ready.

The full list of commands accepted by this bot can be found 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

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 @kearen001, 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 performs a significant cleanup and refactoring of the device handler module. The primary goal is to enhance code readability and reduce redundancy by extracting common logic for KubeEdge client acquisition and error handling into a dedicated helper function. This change streamlines the implementation of various device-related API operations, making the codebase more maintainable and robust.

Highlights

  • Refactored Device Handlers: Introduced a new helper function, "withKubeEdgeClient", to centralize the logic for obtaining the KubeEdge client and handling initial errors, reducing code duplication across multiple device API handlers.
  • Improved Code Readability: Modified "handleGetDevices", "handleGetDevice", "handleCreateDevice", "handleUpdateDevice", and "handleDeleteDevice" to leverage the new "withKubeEdgeClient" helper, making the code cleaner and easier to understand.
  • Documentation Update: Added a "Notice" heading in "README.md" to better structure information regarding API server self-signed certificate verification.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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.

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.

@kubeedge-bot kubeedge-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 17, 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.

Code Review

This pull request effectively refactors the device handlers to reduce code duplication by introducing a withKubeEdgeClient helper function. The changes improve code readability and maintainability. I've added a couple of suggestions to further enhance this refactoring by centralizing error handling within the new helper, and to correct the heading structure in the README.md file for better document clarity. Also, note that the PR description mentions extracting namespace handling, but the current helper function does not do this, which is fine as it keeps the helper more generic.

Comment thread README.md
go run main.go --apiserver-host=https://192.168.33.129:6443
```

### Notice
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The heading level for 'Notice' seems incorrect. It's currently an <h3>, making it a sibling to Backend and Frontend. Since this notice is specific to the backend start command, it would be more semantically correct to make it a sub-heading of Backend by using <h4>.

Suggested change
### Notice
#### Notice

Comment on lines +71 to +81
func (apiHandler *APIHandler) withKubeEdgeClient(
request *restful.Request,
response *restful.Response,
handler func(kubeedgeClient versioned.Interface),
) {
kubeedgeClient, err := apiHandler.getKubeEdgeClient(request, response)
if err != nil {
return
}
handler(kubeedgeClient)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This is a great refactoring to reduce boilerplate. To improve it further, you could centralize the error handling. By modifying the handler function to return an error, withKubeEdgeClient can be responsible for calling errors.HandleInternalError, making the individual handlers even cleaner.

For example, handleGetDevices would then be simplified to:

func (apiHandler *APIHandler) handleGetDevices(request *restful.Request, response *restful.Response) {
	apiHandler.withKubeEdgeClient(request, response, func(kubeedgeClient versioned.Interface) error {
		namespace := request.PathParameter("namespace")
		result, err := device.GetDeviceList(kubeedgeClient, namespace)
		if err != nil {
			return err
		}
		response.WriteEntity(result)
		return nil
	})
}

This pattern could be applied to all the device handlers for better consistency and maintainability.

func (apiHandler *APIHandler) withKubeEdgeClient(
	request *restful.Request,
	response *restful.Response,
	handler func(kubeedgeClient versioned.Interface) error,
) {
	kubeedgeClient, err := apiHandler.getKubeEdgeClient(request, response)
	if err != nil {
		return
	}
	if err := handler(kubeedgeClient); err != nil {
		errors.HandleInternalError(response, err)
	}
}

yeyetianbing and others added 2 commits September 17, 2025 17:17
Signed-off-by: kearen001 <xinye.luo@bluedotai.cn>
Signed-off-by: kearen001 <xinye.luo@bluedotai.cn>
@kubeedge-bot kubeedge-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 9, 2025
@kubeedge-bot
Copy link
Copy Markdown
Collaborator

@kearen001: PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants