Skip to content

Commit 0299493

Browse files
Add delete api and delete webhook commands
Add delete functionality to remove APIs and webhooks from projects. Users can now clean up scaffolded resources with proper validation.
1 parent bd63fdc commit 0299493

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3374
-5
lines changed

docs/book/src/plugins/available/autoupdate-v1-alpha.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ kubebuilder edit --plugins="autoupdate/v1-alpha"
3838
kubebuilder init --plugins=go/v4,autoupdate/v1-alpha
3939
```
4040

41+
- To remove the auto-update workflow from your project:
42+
43+
```shell
44+
kubebuilder delete --plugins=autoupdate/v1-alpha
45+
46+
# Or combine with other plugins
47+
kubebuilder delete --plugins=autoupdate/v1-alpha,grafana/v1-alpha
48+
```
49+
4150
### Optional: GitHub Models AI Summary
4251

4352
By default, the workflow works without GitHub Models to avoid permission errors.

docs/book/src/plugins/available/go-v4-plugin.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ kubebuilder init --domain tutorial.kubebuilder.io --repo tutorial.kubebuilder.io
3232
- Edit - `kubebuilder edit [OPTIONS]`
3333
- Create API - `kubebuilder create api [OPTIONS]`
3434
- Create Webhook - `kubebuilder create webhook [OPTIONS]`
35+
- Delete API - `kubebuilder delete api [OPTIONS]`
36+
- Delete Webhook - `kubebuilder delete webhook [OPTIONS]`
3537

3638
## Further resources
3739

docs/book/src/plugins/available/grafana-v1-alpha.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,23 @@ kubebuilder init --plugins grafana.kubebuilder.io/v1-alpha
5050

5151
# Enable grafana plugin to an existing project
5252
kubebuilder edit --plugins grafana.kubebuilder.io/v1-alpha
53+
54+
# Remove grafana dashboards from project
55+
kubebuilder delete --plugins grafana.kubebuilder.io/v1-alpha
5356
```
5457

5558
The plugin will create a new directory and scaffold the JSON files under it (i.e. `grafana/controller-runtime-metrics.json`).
5659

60+
### Removing Grafana Dashboards
61+
62+
To remove Grafana manifests from your project:
63+
64+
```sh
65+
kubebuilder delete --plugins=grafana.kubebuilder.io/v1-alpha
66+
```
67+
68+
This removes the `grafana/` directory and all dashboard files.
69+
5770
#### Show case:
5871

5972
See an example of how to use the plugin in your project:

docs/book/src/plugins/available/helm-v2-alpha.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ kubebuilder edit --plugins=helm/v2-alpha \
7373
--output-dir=helm-charts
7474
```
7575

76+
### Removing Helm Charts
77+
78+
To remove the Helm chart from your project:
79+
80+
```shell
81+
# Delete Helm chart generation
82+
kubebuilder delete --plugins=helm/v2-alpha
83+
84+
# Delete multiple optional plugins at once
85+
kubebuilder delete --plugins=helm/v2-alpha,grafana/v1-alpha
86+
```
87+
88+
This removes:
89+
- `dist/chart/` directory
90+
- `.github/workflows/test-chart.yml`
91+
- Plugin configuration from PROJECT file
92+
7693
## Chart Structure
7794

7895
The plugin creates a chart layout that matches your `config/`:

docs/book/src/plugins/extending/extending_cli_features_and_plugins.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ of the following CLI commands:
6262
- `init`: Initializes the project structure.
6363
- `create api`: Scaffolds a new API and controller.
6464
- `create webhook`: Scaffolds a new webhook.
65-
- `edit`: edit the project structure.
65+
- `delete api`: Deletes an API and its associated files.
66+
- `delete webhook`: Deletes a webhook and its associated files.
67+
- `edit`: Updates the project structure.
6668

6769
Here’s an example of using the `init` subcommand with a custom plugin:
6870

docs/book/src/plugins/extending/external-plugins.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ External plugins can support the following Kubebuilder subcommands:
120120
- `init`: Project initialization
121121
- `create api`: Scaffold Kubernetes API definitions
122122
- `create webhook`: Scaffold Kubernetes webhooks
123+
- `delete api`: Delete Kubernetes API definitions and associated files
124+
- `delete webhook`: Delete Kubernetes webhooks and associated files
123125
- `edit`: Update project configuration
124126

125127
**Optional subcommands for enhanced user experience:**

docs/book/src/plugins/plugins.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ kubebuilder create api --plugins=pluginA,pluginB,pluginC
3131
OR
3232
kubebuilder create webhook --plugins=pluginA,pluginB,pluginC
3333
OR
34+
kubebuilder delete api --plugins=pluginA,pluginB,pluginC
35+
OR
36+
kubebuilder delete webhook --plugins=pluginA,pluginB,pluginC
37+
OR
3438
kubebuilder edit --plugins=pluginA,pluginB,pluginC
3539
```
3640

pkg/cli/cli.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,15 @@ func (c *CLI) addSubcommands() {
519519
c.cmd.AddCommand(createCmd)
520520
}
521521

522+
// kubebuilder delete
523+
deleteCmd := c.newDeleteCmd()
524+
// kubebuilder delete api
525+
deleteCmd.AddCommand(c.newDeleteAPICmd())
526+
deleteCmd.AddCommand(c.newDeleteWebhookCmd())
527+
if deleteCmd.HasSubCommands() {
528+
c.cmd.AddCommand(deleteCmd)
529+
}
530+
522531
// kubebuilder edit
523532
c.cmd.AddCommand(c.newEditCmd())
524533

pkg/cli/delete.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2026 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cli
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
24+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
25+
)
26+
27+
func (c CLI) newDeleteCmd() *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "delete",
30+
Short: "Delete a Kubernetes API, webhook, or plugin features",
31+
Long: `Delete a Kubernetes API, webhook, or plugin features.
32+
33+
For resource-specific deletion:
34+
kubebuilder delete api --group <group> --version <version> --kind <kind>
35+
kubebuilder delete webhook --group <group> --version <version> --kind <kind>
36+
37+
For project-level plugin deletion:
38+
kubebuilder delete --plugins=helm/v2-alpha
39+
kubebuilder delete --plugins=grafana/v1-alpha,autoupdate/v1-alpha
40+
`,
41+
RunE: errCmdFunc(
42+
fmt.Errorf("delete requires a subcommand (api, webhook) or --plugins flag"),
43+
),
44+
}
45+
46+
// If plugins are specified, route to Edit subcommand with delete context
47+
if len(c.resolvedPlugins) > 0 {
48+
subcommands := c.filterSubcommands(
49+
func(p plugin.Plugin) bool {
50+
_, isValid := p.(plugin.Edit)
51+
return isValid
52+
},
53+
func(p plugin.Plugin) plugin.Subcommand {
54+
return p.(plugin.Edit).GetEditSubcommand()
55+
},
56+
)
57+
58+
if len(subcommands) > 0 {
59+
c.applySubcommandHooks(cmd, subcommands, "failed to delete plugin features", false)
60+
}
61+
}
62+
63+
return cmd
64+
}

pkg/cli/delete_api.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2026 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cli
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
24+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
25+
)
26+
27+
const deleteAPIErrorMsg = "failed to delete API"
28+
29+
func (c CLI) newDeleteAPICmd() *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: "api",
32+
Short: "Delete a Kubernetes API",
33+
Long: `Delete a Kubernetes API and its associated files.
34+
`,
35+
RunE: errCmdFunc(
36+
fmt.Errorf("api subcommand requires an existing project"),
37+
),
38+
}
39+
40+
// In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of
41+
// this subcommand. This allows the use of subcommands that do not require resolved plugins like help.
42+
if len(c.resolvedPlugins) == 0 {
43+
cmdErr(cmd, noResolvedPluginError{})
44+
return cmd
45+
}
46+
47+
// Obtain the plugin keys and subcommands from the plugins that implement plugin.DeleteAPI.
48+
subcommands := c.filterSubcommands(
49+
func(p plugin.Plugin) bool {
50+
_, isValid := p.(plugin.DeleteAPI)
51+
return isValid
52+
},
53+
func(p plugin.Plugin) plugin.Subcommand {
54+
return p.(plugin.DeleteAPI).GetDeleteAPISubcommand()
55+
},
56+
)
57+
58+
// Verify that there is at least one remaining plugin.
59+
if len(subcommands) == 0 {
60+
cmdErr(cmd, noAvailablePluginError{"API deletion"})
61+
return cmd
62+
}
63+
64+
c.applySubcommandHooks(cmd, subcommands, deleteAPIErrorMsg, false)
65+
66+
return cmd
67+
}

0 commit comments

Comments
 (0)