@@ -34,10 +34,8 @@ import (
3434 "k8s.io/apimachinery/pkg/runtime/schema"
3535 "k8s.io/apimachinery/pkg/util/validation/field"
3636 "k8s.io/utils/ptr"
37- ctrl "sigs.k8s.io/controller-runtime"
3837 "sigs.k8s.io/controller-runtime/pkg/client"
3938 logf "sigs.k8s.io/controller-runtime/pkg/log"
40- "sigs.k8s.io/controller-runtime/pkg/webhook"
4139 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
4240
4341 barbicanv1 "github.com/openstack-k8s-operators/barbican-operator/api/v1beta1"
@@ -60,28 +58,11 @@ import (
6058 telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
6159)
6260
63- var ctlplaneWebhookClient client.Client
64-
6561// log is for logging in this package.
6662var openstackcontrolplanelog = logf .Log .WithName ("openstackcontrolplane-resource" )
6763
68- // SetupWebhookWithManager sets up the Webhook with the Manager.
69- func (r * OpenStackControlPlane ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
70- if ctlplaneWebhookClient == nil {
71- ctlplaneWebhookClient = mgr .GetClient ()
72- }
73-
74- return ctrl .NewWebhookManagedBy (mgr ).
75- For (r ).
76- Complete ()
77- }
78-
79- // +kubebuilder:webhook:path=/validate-core-openstack-org-v1beta1-openstackcontrolplane,mutating=false,failurePolicy=Fail,sideEffects=None,groups=core.openstack.org,resources=openstackcontrolplanes,verbs=create;update,versions=v1beta1,name=vopenstackcontrolplane.kb.io,admissionReviewVersions=v1
80-
81- var _ webhook.Validator = & OpenStackControlPlane {}
82-
83- // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
84- func (r * OpenStackControlPlane ) ValidateCreate () (admission.Warnings , error ) {
64+ // ValidateCreate validates the OpenStackControlPlane on creation
65+ func (r * OpenStackControlPlane ) ValidateCreate (ctx context.Context , c client.Client ) (admission.Warnings , error ) {
8566 openstackcontrolplanelog .Info ("validate create" , "name" , r .Name )
8667
8768 var allWarn []string
@@ -91,7 +72,7 @@ func (r *OpenStackControlPlane) ValidateCreate() (admission.Warnings, error) {
9172 listOpts := []client.ListOption {
9273 client .InNamespace (r .Namespace ),
9374 }
94- if err := ctlplaneWebhookClient .List (context . TODO () , ctlplaneList , listOpts ... ); err != nil {
75+ if err := c .List (ctx , ctlplaneList , listOpts ... ); err != nil {
9576 return nil , apierrors .NewForbidden (
9677 schema.GroupResource {
9778 Group : GroupVersion .WithKind ("OpenStackControlPlane" ).Group ,
@@ -118,7 +99,7 @@ func (r *OpenStackControlPlane) ValidateCreate() (admission.Warnings, error) {
11899 )
119100 }
120101
121- allErrs , err := r .ValidateVersion ()
102+ allErrs , err := r .ValidateVersion (ctx , c )
122103
123104 // Version validation can generate non-field errors, so we consider those first
124105 if err != nil {
@@ -145,8 +126,8 @@ func (r *OpenStackControlPlane) ValidateCreate() (admission.Warnings, error) {
145126 return allWarn , nil
146127}
147128
148- // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
149- func (r * OpenStackControlPlane ) ValidateUpdate (old runtime.Object ) (admission.Warnings , error ) {
129+ // ValidateUpdate validates the OpenStackControlPlane on update
130+ func (r * OpenStackControlPlane ) ValidateUpdate (ctx context. Context , old runtime.Object , c client. Client ) (admission.Warnings , error ) {
150131 openstackcontrolplanelog .Info ("validate update" , "name" , r .Name )
151132
152133 oldControlPlane , ok := old .(* OpenStackControlPlane )
@@ -178,8 +159,8 @@ func (r *OpenStackControlPlane) ValidateUpdate(old runtime.Object) (admission.Wa
178159 return allWarn , nil
179160}
180161
181- // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
182- func (r * OpenStackControlPlane ) ValidateDelete () (admission.Warnings , error ) {
162+ // ValidateDelete validates the OpenStackControlPlane on deletion
163+ func (r * OpenStackControlPlane ) ValidateDelete (ctx context. Context , c client. Client ) (admission.Warnings , error ) {
183164 openstackcontrolplanelog .Info ("validate delete" , "name" , r .Name )
184165
185166 return nil , nil
@@ -734,10 +715,10 @@ func (r *OpenStackControlPlane) ValidateServiceDependencies(basePath *field.Path
734715 return allErrs
735716}
736717
737- func (r * OpenStackControlPlane ) ValidateVersion () (field.ErrorList , error ) {
718+ func (r * OpenStackControlPlane ) ValidateVersion (ctx context. Context , c client. Client ) (field.ErrorList , error ) {
738719 var allErrs field.ErrorList
739720
740- openStackVersionList , err := GetOpenStackVersions (r .Namespace , ctlplaneWebhookClient )
721+ openStackVersionList , err := GetOpenStackVersions (r .Namespace , c )
741722
742723 if err != nil {
743724 return allErrs , apierrors .NewForbidden (
@@ -784,11 +765,7 @@ func (r *OpenStackControlPlane) ValidateVersion() (field.ErrorList, error) {
784765 return allErrs , nil
785766}
786767
787- // +kubebuilder:webhook:path=/mutate-core-openstack-org-v1beta1-openstackcontrolplane,mutating=true,failurePolicy=fail,sideEffects=None,groups=core.openstack.org,resources=openstackcontrolplanes,verbs=create;update,versions=v1beta1,name=mopenstackcontrolplane.kb.io,admissionReviewVersions=v1
788-
789- var _ webhook.Defaulter = & OpenStackControlPlane {}
790-
791- // Default implements webhook.Defaulter so a webhook will be registered for the type
768+ // Default sets default values for the OpenStackControlPlane
792769func (r * OpenStackControlPlane ) Default () {
793770 openstackcontrolplanelog .Info ("default" , "name" , r .Name )
794771
0 commit comments