Skip to content

Commit

Permalink
⚠️ (go/v4) decouple webhooks from APIs - Move Webhooks from `api/<ver…
Browse files Browse the repository at this point in the history
…sion>` or `api/<group>/<version>` to `internal/webhook/<version>` or `internal/webhook/<group>/<version>`

This PR decouples the webhooks from the API, aligning with the recent breaking changes introduced in controller-runtime to ensure that kubebuilder still compatbile with its next release.  Webhooks are now scaffolded under `internal/webhook` to comply with the latest standards.

**Context:**

Controller-runtime deprecated and removed the webhook methods in favor of CustomInterfaces (see [controller-runtime#2641](kubernetes-sigs/controller-runtime#2641)). The motivation for this change is outlined in [controller-runtime#2596](kubernetes-sigs/controller-runtime#2596).

See that the current master branch already reflects these changes, using the CustomInterfaces: [kubebuilder#4060](kubernetes-sigs#4060).

**Changes:**

- Webhooks are now scaffolded in `internal/webhook/<version>` or `internal/webhook/<group>/<version>`.
- However, to ensure backwards compatibility, a new `--legacy` flag is introduced. Running `kubebuilder create webhook [options] --legacy` will scaffold webhooks in the legacy location for projects that need to retain the old structure. However, users will still to address the breaking changes in the source code by replacing the old methods by the new CustomInterfaces.
  • Loading branch information
camilamacedo86 committed Sep 21, 2024
1 parent 983a929 commit c2a4bd4
Show file tree
Hide file tree
Showing 81 changed files with 1,808 additions and 1,250 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/legacy-webhook-path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This test ensure that the legacy webhook path
# still working. The option is deprecated
# and should be removed when we no longer need
# to support go/v4 plugin.
name: Legacy Webhook Path

on:
push:
paths:
- 'testdata/**'
- '.github/workflows/legacy-webhook-path.yml'
pull_request:
paths:
- 'testdata/**'
- '.github/workflows/legacy-webhook-path.yml'

jobs:
webhook-legacy-path:
name: Verify Legacy Webhook Path
runs-on: ubuntu-latest
# Pull requests from the same repository won't trigger this checks as they were already triggered by the push
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
steps:
- name: Clone the code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.22.3'
- name: Run make test-legacy
run: make test-legacy

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ docs/book/src/docs
# skip testdata go.sum, since it may have
# different result depending on go version
/testdata/**/go.sum
/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/bin
/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/bin
/testdata/**legacy**
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,10 @@ test-license: ## Run the license check
.PHONY: test-spaces
test-spaces: ## Run the trailing spaces check
./test/check_spaces.sh

## TODO: Remove me when go/v4 plugin be removed
## Deprecated
.PHONY: test-legacy
test-legacy: ## Run the tests to validate legacy path for webhooks
rm -rf ./testdata/**legacy**/
./test/testdata/legacy-webhook-path.sh
2 changes: 1 addition & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN go mod download
# Copy the go source
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/controller/ internal/controller/
COPY internal/ internal/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (

batchv1 "tutorial.kubebuilder.io/project/api/v1"
"tutorial.kubebuilder.io/project/internal/controller"
webhookbatchv1 "tutorial.kubebuilder.io/project/internal/webhook/v1"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -183,7 +184,7 @@ func main() {
*/
// nolint:goconst
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
if err = webhookbatchv1.SetupCronJobWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
os.Exit(1)
}
Expand Down
Loading

0 comments on commit c2a4bd4

Please sign in to comment.