Skip to content

delete devops namespace when deleting devops project#1070

Merged
stoneshi-yunify merged 1 commit intokubesphere:masterfrom
renyunkang:delete-ns
Mar 18, 2026
Merged

delete devops namespace when deleting devops project#1070
stoneshi-yunify merged 1 commit intokubesphere:masterfrom
renyunkang:delete-ns

Conversation

@renyunkang
Copy link
Member

What type of PR is this?

/kind bug

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes # https://github.com/kubesphere/project/issues/7404

Special notes for reviewers:

Please check the following list before waiting reviewers:

  • Already committed the CRD files to the Helm Chart if you created some new CRDs
  • Already added the permission for the new API
  • Already added the RBAC markers for the new controllers

Does this PR introduce a user-facing change??

none

Signed-off-by: renyunkang <rykren1998@gmail.com>
@gemini-code-assist
Copy link

Summary of Changes

Hello, 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 addresses a bug where namespaces linked to a DevOps project were not being cleaned up upon the project's deletion. It introduces a mechanism within the DevOps project controller to identify and remove these associated namespaces, improving resource management and ensuring proper cleanup when a project is deleted.

Highlights

  • DevOps Project Deletion: Implemented logic to automatically delete all associated namespaces when a DevOps project is removed, ensuring complete cleanup and preventing orphaned resources.
Changelog
  • controllers/jenkins/devopsproject/devopsproject_controller.go
    • Added logic to list and delete namespaces associated with a DevOps project during its deletion.
Activity
  • No review comments or other activity have been recorded for this pull request yet.
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.

Copy link

@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 adds logic to delete associated namespaces when a DevOpsProject is deleted. The implementation correctly uses a finalizer to ensure cleanup and handles errors gracefully during namespace deletion. My feedback focuses on improving logging to use structured logging consistently and clarifying a code comment, which will enhance observability and maintainability.

Comment on lines +345 to +358
// find all namespaces with the prefix of devopsproject
namespaces, err := c.namespaceLister.List(labels.SelectorFromSet(labels.Set{constants.DevOpsProjectLabelKey: project.Name}))
if err != nil {
klog.ErrorS(err, fmt.Sprintf("failed to list namespaces when deleting devopsproject %s", key))
return err
}
for _, ns := range namespaces {
klog.Infof("deleting namespace %s for devopsproject %s", ns.Name, project.Name)
err := c.client.CoreV1().Namespaces().Delete(context.Background(), ns.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
// do not return error, continue to delete other namespaces
klog.ErrorS(err, fmt.Sprintf("failed to delete namespace %s for devopsproject %s", ns.Name, project.Name))
}
}

Choose a reason for hiding this comment

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

medium

The logging within this block could be improved by consistently using structured logging (klog.InfoS and klog.ErrorS) instead of klog.Infof and fmt.Sprintf. This enhances readability and makes logs easier to parse and query for observability purposes. Additionally, the leading comment can be improved to better describe the purpose of this code block.

Suggested change
// find all namespaces with the prefix of devopsproject
namespaces, err := c.namespaceLister.List(labels.SelectorFromSet(labels.Set{constants.DevOpsProjectLabelKey: project.Name}))
if err != nil {
klog.ErrorS(err, fmt.Sprintf("failed to list namespaces when deleting devopsproject %s", key))
return err
}
for _, ns := range namespaces {
klog.Infof("deleting namespace %s for devopsproject %s", ns.Name, project.Name)
err := c.client.CoreV1().Namespaces().Delete(context.Background(), ns.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
// do not return error, continue to delete other namespaces
klog.ErrorS(err, fmt.Sprintf("failed to delete namespace %s for devopsproject %s", ns.Name, project.Name))
}
}
// Clean up associated namespaces before removing the finalizer.
namespaces, err := c.namespaceLister.List(labels.SelectorFromSet(labels.Set{constants.DevOpsProjectLabelKey: project.Name}))
if err != nil {
klog.ErrorS(err, "failed to list namespaces when deleting devopsproject", "devopsproject", key)
return err
}
for _, ns := range namespaces {
klog.InfoS("deleting namespace for devopsproject", "namespace", ns.Name, "devopsproject", project.Name)
err := c.client.CoreV1().Namespaces().Delete(context.Background(), ns.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
// do not return error, continue to delete other namespaces
klog.ErrorS(err, "failed to delete namespace", "namespace", ns.Name, "devopsproject", project.Name)
}
}

@stoneshi-yunify
Copy link
Member

/lgtm

@stoneshi-yunify stoneshi-yunify merged commit 04a0842 into kubesphere:master Mar 18, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants