Skip to content

Commit dd9ae3c

Browse files
(chore) Add delete interface and implementation for all options
Add delete functionality to remove APIs and webhooks from projects. Users can now clean up scaffolded resources.
1 parent e1e130d commit dd9ae3c

55 files changed

Lines changed: 5463 additions & 27 deletions

Some content is hidden

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

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ docs/book/src/docs
3030
/testdata/**legacy**
3131

3232
## Skip testdata files that generate by tests using TestContext
33-
**/e2e-*/**
33+
e2e-*

AGENTS.md

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,21 @@ Plugins implement interfaces from `pkg/plugin/`:
199199
- `Init` - project initialization (`kubebuilder init`)
200200
- `CreateAPI` - API creation (`kubebuilder create api`)
201201
- `CreateWebhook` - webhook creation (`kubebuilder create webhook`)
202+
- `DeleteAPI` - API deletion (`kubebuilder delete api`)
203+
- `DeleteWebhook` - webhook deletion (`kubebuilder delete webhook`)
202204
- `Edit` - post-init modifications (`kubebuilder edit`)
203205
- `Bundle` - groups multiple plugins
204206

207+
**Delete = Undo of Create:**
208+
209+
Each plugin's delete implementation MUST undo exactly what its create implementation did:
210+
- `go/v4`: Removes Go code (API types, controllers, main.go imports/setup, suite_test.go)
211+
- `kustomize/v2`: Removes manifests (samples, RBAC, CRD kustomization entries)
212+
- `deploy-image/v1-alpha`: Removes plugin metadata from PROJECT file
213+
- When plugins run in chain (e.g., `--plugins deploy-image/v1-alpha`), both layout and additional plugins execute
214+
215+
**Integration tests MUST verify**: `state_before_create == state_after_delete`
216+
205217
**Plugin Bundles:**
206218

207219
Default bundle (`pkg/cli/init.go`): `go.kubebuilder.io/v4` + `kustomize.common.kubebuilder.io/v2`
@@ -249,7 +261,8 @@ Controllers implement `Reconcile(ctx, req) (ctrl.Result, error)`:
249261
- **Requeue on pending work** - Return `ctrl.Result{Requeue: true}`
250262

251263
### Testing Pattern
252-
E2E tests use `utils.TestContext` from `test/e2e/utils/test_context.go`:
264+
265+
**Integration Tests** use `utils.TestContext` from `test/e2e/utils/test_context.go`:
253266

254267
```go
255268
ctx := utils.NewTestContext(util.KubebuilderBinName, "GO111MODULE=on")
@@ -259,19 +272,70 @@ ctx.Make("build", "test")
259272
ctx.LoadImageToKindCluster()
260273
```
261274

275+
**Baseline Testing (Required for Delete):**
276+
277+
Delete integration tests MUST verify exact state restoration:
278+
279+
```go
280+
It("should restore exact state after delete", func() {
281+
mainBefore, _ := os.ReadFile("cmd/main.go")
282+
ctx.CreateAPI(...)
283+
ctx.DeleteAPI(..., "-y")
284+
mainAfter, _ := os.ReadFile("cmd/main.go")
285+
Expect(mainAfter).To(Equal(mainBefore)) // Exact match required
286+
})
287+
```
288+
262289
## CLI Reference
263290

264291
After `make install`:
265292

266293
```bash
294+
# Initialize project
267295
kubebuilder init --domain example.com --repo github.com/example/myproject
296+
297+
# Create resources
268298
kubebuilder create api --group batch --version v1 --kind CronJob
269299
kubebuilder create webhook --group batch --version v1 --kind CronJob
270-
kubebuilder edit --plugins=helm/v2-alpha
300+
301+
# Delete resources (complete undo of create)
302+
kubebuilder delete api --group batch --version v1 --kind CronJob
303+
kubebuilder delete webhook --group batch --version v1 --kind CronJob --defaulting
304+
305+
# Delete with plugin chain
306+
kubebuilder delete api --group app --version v1 --kind Cache --plugins deploy-image/v1-alpha
307+
308+
# Delete optional plugin features
309+
kubebuilder delete --plugins helm/v2-alpha
310+
kubebuilder delete --plugins grafana/v1-alpha
311+
312+
# Edit project
313+
kubebuilder edit --plugins helm/v2-alpha
314+
315+
# Alpha commands
271316
kubebuilder alpha generate # Experimental: generate from PROJECT file
272317
kubebuilder alpha update # Experimental: update to latest plugin versions
273318
```
274319

320+
## Implementing Delete
321+
322+
**Rule**: If you add a `create` command, you MUST add the corresponding `delete` command.
323+
324+
**Key Principle**: Each plugin undoes ONLY what it created. When plugins run in chain (default: `go/v4` + `kustomize/v2`), each cleans its own artifacts:
325+
- `go/v4` → removes Go code (types, controllers, main.go, suite_test.go)
326+
- `kustomize/v2` → removes manifests (samples, RBAC, CRD entries)
327+
- Additional plugins → remove their metadata from PROJECT file
328+
329+
**Shared Resources**: Imports/code used by multiple resources are preserved until the last one is deleted (e.g., `appv1` import kept while any app/v1 API exists).
330+
331+
**Integration Test**: Add `delete_integration_test.go` with baseline verification:
332+
```go
333+
baseline := captureState()
334+
createResource()
335+
deleteResource("-y")
336+
Expect(currentState()).To(Equal(baseline)) // Exact match required
337+
```
338+
275339
## Common Patterns
276340

277341
### Code Style
@@ -334,7 +398,7 @@ log.Error(err, "Failed to create Pod", "name", name)
334398
- **Integration tests** (`*_integration_test.go` in `pkg/`) - Test multiple components together without cluster
335399
- Must have `//go:build integration` tag at the top
336400
- May create temp dirs, download binaries, or scaffold files
337-
- Examples: alpha update, grafana scaffolding, helm chart generation
401+
- **Delete tests**: MUST use baseline pattern (verify before_create == after_delete)
338402
- **E2E tests** (`test/e2e/`) - **ONLY** for tests requiring a Kubernetes cluster (KIND)
339403
- `v4/plugin_cluster_test.go` - Test v4 plugin deployment
340404
- `helm/plugin_cluster_test.go` - Test Helm chart deployment

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ 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+
4147
### Optional: GitHub Models AI Summary
4248

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

docs/book/src/plugins/available/deploy-image-plugin-v1-alpha.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,25 @@ export MEMCACHED_IMAGE="memcached:1.4.36-alpine"
8282

8383
## Subcommands
8484

85-
The `deploy-image` plugin includes the following subcommand:
85+
The `deploy-image` plugin implements:
8686

87-
- `create api`: Use this command to scaffold the API and controller code to manage the container image.
87+
- `create api` - Scaffolds the API and controller code to manage the container image
88+
89+
### Deleting APIs Created with Deploy-Image
90+
91+
To delete an API created with deploy-image, you **must** include the plugin flag:
92+
93+
```sh
94+
kubebuilder delete api --group <group> --version <version> --kind <kind> \
95+
--plugins=deploy-image/v1-alpha
96+
```
97+
98+
If you forget the `--plugins` flag, you'll receive an error message showing the exact command to use.
99+
100+
The delete operation removes:
101+
- API and controller files (via go/v4 plugin in the chain)
102+
- Deploy-image metadata from the PROJECT file
103+
- Kustomize manifests (samples, RBAC)
88104

89105
## Affected files
90106

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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,24 @@ The Grafana plugin is attached to the `init` subcommand and the `edit` subcomman
4949
kubebuilder init --plugins grafana.kubebuilder.io/v1-alpha
5050

5151
# Enable grafana plugin to an existing project
52-
kubebuilder edit --plugins grafana.kubebuilder.io/v1-alpha
52+
kubebuilder edit --plugins grafana/v1-alpha
53+
54+
# Remove grafana dashboards from project
55+
kubebuilder delete --plugins grafana/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/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:
@@ -203,7 +216,7 @@ customMetrics:
203216
204217
#### Scaffold Manifest
205218
206-
Once `config.yaml` is configured, you can run `kubebuilder edit --plugins grafana.kubebuilder.io/v1-alpha` again.
219+
Once `config.yaml` is configured, you can run `kubebuilder edit --plugins grafana/v1-alpha` again.
207220
This time, the plugin will generate `grafana/custom-metrics/custom-metrics-dashboard.json`, which can be imported to Grafana UI.
208221

209222
#### Show case:

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

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

77+
### Removing Helm Charts
78+
79+
To remove the Helm chart from your project:
80+
81+
```shell
82+
kubebuilder delete --plugins helm/v2-alpha
83+
```
84+
85+
This removes:
86+
- `dist/chart/` directory
87+
- `.github/workflows/test-chart.yml`
88+
- Plugin configuration from PROJECT file
89+
7790
## Chart Structure
7891

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

docs/book/src/plugins/available/kustomize-v2.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ The kustomize plugin implements the following subcommands:
7070

7171
* init (`$ kubebuilder init [OPTIONS]`)
7272
* create api (`$ kubebuilder create api [OPTIONS]`)
73-
* create webhook (`$ kubebuilder create api [OPTIONS]`)
73+
* create webhook (`$ kubebuilder create webhook [OPTIONS]`)
74+
* delete api (`$ kubebuilder delete api [OPTIONS]`)
75+
* delete webhook (`$ kubebuilder delete webhook [OPTIONS]`)
7476

7577
<aside class="note">
76-
<h1>Create API and Webhook</h1>
78+
<h1>Create and Delete</h1>
7779

78-
The implementation for the `create api` subcommand scaffolds the kustomize
79-
manifests specific to each API. See more [here][kustomize-create-api].
80-
The same applies to `create webhook`.
80+
The `create api` subcommand scaffolds kustomize manifests specific to each API
81+
(CRD bases, samples, RBAC). See more [here][kustomize-create-api].
82+
83+
The `delete api` subcommand removes these manifests when the API is deleted.
84+
The same applies to `create webhook` and `delete webhook`.
8185

8286
</aside>
8387

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
@@ -133,6 +133,8 @@ External plugins can support the following Kubebuilder subcommands:
133133
- `init`: Project initialization
134134
- `create api`: Scaffold Kubernetes API definitions
135135
- `create webhook`: Scaffold Kubernetes webhooks
136+
- `delete api`: Delete Kubernetes API definitions and associated files
137+
- `delete webhook`: Delete Kubernetes webhooks and associated files
136138
- `edit`: Update project configuration
137139

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

0 commit comments

Comments
 (0)