diff --git a/docs/book/src/component-config-tutorial/testdata/project/config/crd/kustomization.yaml b/docs/book/src/component-config-tutorial/testdata/project/config/crd/kustomization.yaml index 22fff65b92c..97cb6432412 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/config/crd/kustomization.yaml +++ b/docs/book/src/component-config-tutorial/testdata/project/config/crd/kustomization.yaml @@ -8,12 +8,12 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- patches/webhook_in_projectconfigs.yaml +#- path: patches/webhook_in_projectconfigs.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD -#- patches/cainjection_in_projectconfigs.yaml +#- path: patches/cainjection_in_projectconfigs.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml index ed16f6b985b..726852ee68b 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml @@ -8,12 +8,12 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -- patches/webhook_in_cronjobs.yaml +- path: patches/webhook_in_cronjobs.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD -- patches/cainjection_in_cronjobs.yaml +- path: patches/cainjection_in_cronjobs.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/docs/book/src/cronjob-tutorial/testdata/project/go.sum b/docs/book/src/cronjob-tutorial/testdata/project/go.sum index 2f447cdbd84..da86b48f019 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/go.sum +++ b/docs/book/src/cronjob-tutorial/testdata/project/go.sum @@ -25,6 +25,7 @@ github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJ github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -79,8 +80,10 @@ github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -102,6 +105,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q= github.com/onsi/ginkgo/v2 v2.9.5/go.mod h1:tvAoo1QUJwNEU2ITftXTpR7R1RbCzoZUOs3RonqW57k= github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU= diff --git a/docs/book/src/reference/events.md b/docs/book/src/reference/events.md new file mode 100644 index 00000000000..9c9df281cc8 --- /dev/null +++ b/docs/book/src/reference/events.md @@ -0,0 +1,52 @@ +# Creating Events + +`Events` can be useful alongside a Custom Resource's status information as they can +show users or administrators a record of previous states or occurrences of events in +addition to the current state. In general, events should only be generated when it +would be useful for a user or administrator to know about it - it is not generally +good practice to emit events for all operations. + +Refer to [Kubernetes API Conventions](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#events) +for more best practices and details about events. + + +## Implementing Events + +First, add an event recorder to your reconciler struct: + +```go +import "k8s.io/client-go/tools/record" + +type CronJobReconciler struct { + client.Client + Scheme *runtime.Scheme + Recorder record.EventRecorder +} +``` + +Setup in main.go with + +```go + if *ctrlConfig.EnableVpcEndpointController { + setupLog.Info("starting controller", "controller", vpcendpoint.ControllerName) + if err = (&vpcendpoint.VpcEndpointReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor(vpcendpoint.ControllerName), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", vpcendpoint.ControllerName) + os.Exit(1) + } + } +``` + +Highlights: +- If the object is not being deleted and does not have the finalizer registered, + then add the finalizer and update the object in Kubernetes. +- If object is being deleted and the finalizer is still present in finalizers list, + then execute the pre-delete logic and remove the finalizer and update the + object. +- Ensure that the pre-delete logic is idempotent. + +{{#literatego ../cronjob-tutorial/testdata/finalizer_example.go}} + diff --git a/hack/docs/internal/cronjob-tutorial/generate_cronjob.go b/hack/docs/internal/cronjob-tutorial/generate_cronjob.go index 9a404a6fc2a..3eb13ea54c3 100644 --- a/hack/docs/internal/cronjob-tutorial/generate_cronjob.go +++ b/hack/docs/internal/cronjob-tutorial/generate_cronjob.go @@ -581,12 +581,12 @@ func updateKustomization(sp *Sample) { // uncomment crd/kustomization err = pluginutil.UncommentCode( filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"), - `#- patches/webhook_in_cronjobs.yaml`, `#`) + `#- path: patches/webhook_in_cronjobs.yaml`, `#`) CheckError("fixing crd/kustomization", err) err = pluginutil.UncommentCode( filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"), - `#- patches/cainjection_in_cronjobs.yaml`, `#`) + `#- path: patches/cainjection_in_cronjobs.yaml`, `#`) CheckError("fixing crd/kustomization", err) } diff --git a/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/kustomization.go b/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/kustomization.go index 0cdca88ad14..960ad2c5da3 100644 --- a/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/kustomization.go +++ b/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/kustomization.go @@ -68,9 +68,9 @@ func (f *Kustomization) GetMarkers() []machinery.Marker { const ( resourceCodeFragment = `- bases/%s_%s.yaml ` - webhookPatchCodeFragment = `#- patches/webhook_in_%s.yaml + webhookPatchCodeFragment = `#- path: patches/webhook_in_%s.yaml ` - caInjectionPatchCodeFragment = `#- patches/cainjection_in_%s.yaml + caInjectionPatchCodeFragment = `#- path: patches/cainjection_in_%s.yaml ` ) diff --git a/testdata/project-v4-declarative-v1/config/crd/kustomization.yaml b/testdata/project-v4-declarative-v1/config/crd/kustomization.yaml index bf4c93a811c..687526d8e7c 100644 --- a/testdata/project-v4-declarative-v1/config/crd/kustomization.yaml +++ b/testdata/project-v4-declarative-v1/config/crd/kustomization.yaml @@ -10,16 +10,16 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- patches/webhook_in_captains.yaml -#- patches/webhook_in_firstmates.yaml -#- patches/webhook_in_admirals.yaml +#- path: patches/webhook_in_captains.yaml +#- path: patches/webhook_in_firstmates.yaml +#- path: patches/webhook_in_admirals.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD -#- patches/cainjection_in_captains.yaml -#- patches/cainjection_in_firstmates.yaml -#- patches/cainjection_in_admirals.yaml +#- path: patches/cainjection_in_captains.yaml +#- path: patches/cainjection_in_firstmates.yaml +#- path: patches/cainjection_in_admirals.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/testdata/project-v4-multigroup/config/crd/kustomization.yaml b/testdata/project-v4-multigroup/config/crd/kustomization.yaml index 8c2e5c49499..633e725aa1a 100644 --- a/testdata/project-v4-multigroup/config/crd/kustomization.yaml +++ b/testdata/project-v4-multigroup/config/crd/kustomization.yaml @@ -17,28 +17,28 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- patches/webhook_in_captains.yaml -#- patches/webhook_in_frigates.yaml -#- patches/webhook_in_destroyers.yaml -#- patches/webhook_in_cruisers.yaml -#- patches/webhook_in_krakens.yaml -#- patches/webhook_in_leviathans.yaml -#- patches/webhook_in_healthcheckpolicies.yaml -#- patches/webhook_in_bars.yaml -#- patches/webhook_in_lakers.yaml +#- path: patches/webhook_in_captains.yaml +#- path: patches/webhook_in_frigates.yaml +#- path: patches/webhook_in_destroyers.yaml +#- path: patches/webhook_in_cruisers.yaml +#- path: patches/webhook_in_krakens.yaml +#- path: patches/webhook_in_leviathans.yaml +#- path: patches/webhook_in_healthcheckpolicies.yaml +#- path: patches/webhook_in_bars.yaml +#- path: patches/webhook_in_lakers.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD -#- patches/cainjection_in_captains.yaml -#- patches/cainjection_in_frigates.yaml -#- patches/cainjection_in_destroyers.yaml -#- patches/cainjection_in_cruisers.yaml -#- patches/cainjection_in_krakens.yaml -#- patches/cainjection_in_leviathans.yaml -#- patches/cainjection_in_healthcheckpolicies.yaml -#- patches/cainjection_in_bars.yaml -#- patches/cainjection_in_lakers.yaml +#- path: patches/cainjection_in_captains.yaml +#- path: patches/cainjection_in_frigates.yaml +#- path: patches/cainjection_in_destroyers.yaml +#- path: patches/cainjection_in_cruisers.yaml +#- path: patches/cainjection_in_krakens.yaml +#- path: patches/cainjection_in_leviathans.yaml +#- path: patches/cainjection_in_healthcheckpolicies.yaml +#- path: patches/cainjection_in_bars.yaml +#- path: patches/cainjection_in_lakers.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. 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 4d4c5a9212d..1385006e680 100644 --- a/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml +++ b/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml @@ -9,14 +9,14 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- patches/webhook_in_memcacheds.yaml -#- patches/webhook_in_busyboxes.yaml +#- 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. # patches here are for enabling the CA injection for each CRD -#- patches/cainjection_in_memcacheds.yaml -#- patches/cainjection_in_busyboxes.yaml +#- path: patches/cainjection_in_memcacheds.yaml +#- path: patches/cainjection_in_busyboxes.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/testdata/project-v4/config/crd/kustomization.yaml b/testdata/project-v4/config/crd/kustomization.yaml index 75e0b8e4258..a67f99e5ea3 100644 --- a/testdata/project-v4/config/crd/kustomization.yaml +++ b/testdata/project-v4/config/crd/kustomization.yaml @@ -10,16 +10,16 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- patches/webhook_in_captains.yaml -#- patches/webhook_in_firstmates.yaml -#- patches/webhook_in_admirales.yaml +#- path: patches/webhook_in_captains.yaml +#- path: patches/webhook_in_firstmates.yaml +#- path: patches/webhook_in_admirales.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD -#- patches/cainjection_in_captains.yaml -#- patches/cainjection_in_firstmates.yaml -#- patches/cainjection_in_admirales.yaml +#- path: patches/cainjection_in_captains.yaml +#- path: patches/cainjection_in_firstmates.yaml +#- path: patches/cainjection_in_admirales.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs.