From e299edc0938fd4e7d33f84570499bfa3feae8c81 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sat, 31 Aug 2024 10:01:14 +0100 Subject: [PATCH] test == v4 --- test/testdata/generate.sh | 1 + testdata/project-v4-with-deploy-image/PROJECT | 3 ++ .../api/v1alpha1/busybox_webhook.go | 35 +++++++++++++++++++ .../api/v1alpha1/busybox_webhook_test.go | 33 +++++++++++++++++ .../project-v4-with-deploy-image/cmd/main.go | 7 ++++ .../config/crd/kustomization.yaml | 1 + .../crd/patches/cainjection_in_busyboxes.yaml | 7 ++++ .../crd/patches/webhook_in_busyboxes.yaml | 16 +++++++++ .../dist/install.yaml | 10 ++++++ 9 files changed, 113 insertions(+) create mode 100644 testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook.go create mode 100644 testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook_test.go create mode 100644 testdata/project-v4-with-deploy-image/config/crd/patches/cainjection_in_busyboxes.yaml create mode 100644 testdata/project-v4-with-deploy-image/config/crd/patches/webhook_in_busyboxes.yaml diff --git a/test/testdata/generate.sh b/test/testdata/generate.sh index 1781bc6de62..813459791a5 100755 --- a/test/testdata/generate.sh +++ b/test/testdata/generate.sh @@ -93,6 +93,7 @@ function scaffold_test_project { $kb create api --group example.com --version v1alpha1 --kind Busybox --image=busybox:1.36.1 --plugins="deploy-image/v1-alpha" --make=false header_text 'Creating Memcached webhook ...' $kb create webhook --group example.com --version v1alpha1 --kind Memcached --defaulting --programmatic-validation + $kb create webhook --group example.com --version v1alpha1 --kind Busybox --conversion fi if [[ $project == project-v4-with-grafana ]]; then diff --git a/testdata/project-v4-with-deploy-image/PROJECT b/testdata/project-v4-with-deploy-image/PROJECT index beed2814c3f..68ea458e211 100644 --- a/testdata/project-v4-with-deploy-image/PROJECT +++ b/testdata/project-v4-with-deploy-image/PROJECT @@ -48,4 +48,7 @@ resources: kind: Busybox path: sigs.k8s.io/kubebuilder/testdata/project-v4-with-deploy-image/api/v1alpha1 version: v1alpha1 + webhooks: + conversion: true + webhookVersion: v1 version: "3" diff --git a/testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook.go b/testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook.go new file mode 100644 index 00000000000..f1a93a0c351 --- /dev/null +++ b/testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook.go @@ -0,0 +1,35 @@ +/* +Copyright 2024 The Kubernetes authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + ctrl "sigs.k8s.io/controller-runtime" + logf "sigs.k8s.io/controller-runtime/pkg/log" +) + +// nolint:unused +// log is for logging in this package. +var busyboxlog = logf.Log.WithName("busybox-resource") + +// SetupWebhookWithManager will setup the manager to manage the webhooks +func (r *Busybox) SetupWebhookWithManager(mgr ctrl.Manager) error { + return ctrl.NewWebhookManagedBy(mgr). + For(r). + Complete() +} + +// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! diff --git a/testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook_test.go b/testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook_test.go new file mode 100644 index 00000000000..e172921521b --- /dev/null +++ b/testdata/project-v4-with-deploy-image/api/v1alpha1/busybox_webhook_test.go @@ -0,0 +1,33 @@ +/* +Copyright 2024 The Kubernetes authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + . "github.com/onsi/ginkgo/v2" +) + +var _ = Describe("Busybox Webhook", func() { + + Context("When creating Busybox under Conversion Webhook", func() { + It("Should get the converted version of Busybox", func() { + + // TODO(user): Add your logic here + + }) + }) + +}) diff --git a/testdata/project-v4-with-deploy-image/cmd/main.go b/testdata/project-v4-with-deploy-image/cmd/main.go index 24e43ccb13a..67c0b588f3d 100644 --- a/testdata/project-v4-with-deploy-image/cmd/main.go +++ b/testdata/project-v4-with-deploy-image/cmd/main.go @@ -167,6 +167,13 @@ func main() { os.Exit(1) } } + // nolint:goconst + if os.Getenv("ENABLE_WEBHOOKS") != "false" { + if err = (&examplecomv1alpha1.Busybox{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "Busybox") + os.Exit(1) + } + } // +kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml b/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml index 7b2aba4eb3c..ba351e55093 100644 --- a/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml +++ b/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml @@ -10,6 +10,7 @@ patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD - path: patches/webhook_in_memcacheds.yaml +- path: patches/webhook_in_busyboxes.yaml # +kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. diff --git a/testdata/project-v4-with-deploy-image/config/crd/patches/cainjection_in_busyboxes.yaml b/testdata/project-v4-with-deploy-image/config/crd/patches/cainjection_in_busyboxes.yaml new file mode 100644 index 00000000000..5f6b0384f48 --- /dev/null +++ b/testdata/project-v4-with-deploy-image/config/crd/patches/cainjection_in_busyboxes.yaml @@ -0,0 +1,7 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME + name: busyboxes.example.com.testproject.org diff --git a/testdata/project-v4-with-deploy-image/config/crd/patches/webhook_in_busyboxes.yaml b/testdata/project-v4-with-deploy-image/config/crd/patches/webhook_in_busyboxes.yaml new file mode 100644 index 00000000000..5dbd9da7176 --- /dev/null +++ b/testdata/project-v4-with-deploy-image/config/crd/patches/webhook_in_busyboxes.yaml @@ -0,0 +1,16 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: busyboxes.example.com.testproject.org +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: webhook-service + path: /convert + conversionReviewVersions: + - v1 diff --git a/testdata/project-v4-with-deploy-image/dist/install.yaml b/testdata/project-v4-with-deploy-image/dist/install.yaml index df2ac73a9b0..e7a0ee86dcc 100644 --- a/testdata/project-v4-with-deploy-image/dist/install.yaml +++ b/testdata/project-v4-with-deploy-image/dist/install.yaml @@ -14,6 +14,16 @@ metadata: controller-gen.kubebuilder.io/version: v0.16.1 name: busyboxes.example.com.testproject.org spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + name: project-v4-with-deploy-image-webhook-service + namespace: project-v4-with-deploy-image-system + path: /convert + conversionReviewVersions: + - v1 group: example.com.testproject.org names: kind: Busybox