Skip to content

Commit

Permalink
Add Webhooks for ServiceTemplate to ValidatingWebhookConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
wahabmk authored and Kshatrix committed Oct 5, 2024
1 parent f8cf4c5 commit af8858f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
6 changes: 3 additions & 3 deletions api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func ExtractServiceTemplateName(rawObj client.Object) []string {
return nil
}

services := []string{}
templates := []string{}
for _, s := range cluster.Spec.Services {
services = append(services, s.Template)
templates = append(templates, s.Template)
}

return services
return templates
}
24 changes: 11 additions & 13 deletions internal/webhook/template_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ type ServiceTemplateValidator struct {
client.Client
}

func (in *ServiceTemplateValidator) SetupWebhookWithManager(mgr ctrl.Manager) error {
in.Client = mgr.GetClient()
func (v *ServiceTemplateValidator) SetupWebhookWithManager(mgr ctrl.Manager) error {
v.Client = mgr.GetClient()
return ctrl.NewWebhookManagedBy(mgr).
For(&v1alpha1.ServiceTemplate{}).
WithValidator(in).
WithDefaulter(in).
WithValidator(v).
WithDefaulter(v).
Complete()
}

Expand All @@ -119,20 +119,18 @@ func (*ServiceTemplateValidator) ValidateUpdate(_ context.Context, _ runtime.Obj
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (in *ServiceTemplateValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
func (v *ServiceTemplateValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
tmpl, ok := obj.(*v1alpha1.ServiceTemplate)
if !ok {
return admission.Warnings{"Wrong object"}, apierrors.NewBadRequest(fmt.Sprintf("expected ServiceTemplate but got a %T", obj))
}

managedClusters := &v1alpha1.ManagedClusterList{}
listOptions := client.ListOptions{
if err := v.Client.List(ctx, managedClusters, &client.ListOptions{
FieldSelector: fields.SelectorFromSet(fields.Set{v1alpha1.ServicesTemplateKey: tmpl.Name}),
Limit: 1,
Namespace: tmpl.Namespace,
}
err := in.Client.List(ctx, managedClusters, &listOptions)
if err != nil {
}); err != nil {
return nil, err
}

Expand All @@ -152,12 +150,12 @@ type ProviderTemplateValidator struct {
client.Client
}

func (in *ProviderTemplateValidator) SetupWebhookWithManager(mgr ctrl.Manager) error {
in.Client = mgr.GetClient()
func (v *ProviderTemplateValidator) SetupWebhookWithManager(mgr ctrl.Manager) error {
v.Client = mgr.GetClient()
return ctrl.NewWebhookManagedBy(mgr).
For(&v1alpha1.ProviderTemplate{}).
WithValidator(in).
WithDefaulter(in).
WithValidator(v).
WithDefaulter(v).
Complete()
}

Expand Down
23 changes: 21 additions & 2 deletions templates/provider/hmc/templates/webhooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,31 @@ webhooks:
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
- DELETE
resources:
- clustertemplates
sideEffects: None
- admissionReviewVersions:
- v1
- v1beta1
clientConfig:
service:
name: {{ include "hmc.webhook.serviceName" . }}
namespace: {{ include "hmc.webhook.serviceNamespace" . }}
path: /validate-hmc-mirantis-com-v1alpha1-servicetemplate
failurePolicy: Fail
matchPolicy: Equivalent
name: validation.servicetemplate.hmc.mirantis.com
rules:
- apiGroups:
- hmc.mirantis.com
apiVersions:
- v1alpha1
operations:
- DELETE
resources:
- servicetemplates
sideEffects: None
- admissionReviewVersions:
- v1
- v1beta1
Expand Down

0 comments on commit af8858f

Please sign in to comment.