@@ -18,13 +18,15 @@ package releaseplan
1818
1919import (
2020 "context"
21+ "fmt"
2122
2223 "github.com/go-logr/logr"
2324 "github.com/konflux-ci/release-service/api/v1alpha1"
2425 "github.com/konflux-ci/release-service/metadata"
2526 "k8s.io/apimachinery/pkg/runtime"
2627 ctrl "sigs.k8s.io/controller-runtime"
2728 "sigs.k8s.io/controller-runtime/pkg/client"
29+ "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2830)
2931
3032// Webhook describes the data structure for the bar webhook
@@ -49,6 +51,7 @@ func (w *Webhook) Default(ctx context.Context, obj runtime.Object) error {
4951}
5052
5153// +kubebuilder:webhook:path=/mutate-appstudio-redhat-com-v1alpha1-releaseplan,mutating=true,failurePolicy=fail,sideEffects=None,groups=appstudio.redhat.com,resources=releaseplans,verbs=create,versions=v1alpha1,name=mreleaseplan.kb.io,admissionReviewVersions=v1
54+ // +kubebuilder:webhook:path=/validate-appstudio-redhat-com-v1alpha1-releaseplan,mutating=false,failurePolicy=fail,sideEffects=None,groups=appstudio.redhat.com,resources=releaseplans,verbs=create;update,versions=v1alpha1,name=vreleaseplan.kb.io,admissionReviewVersions=v1
5255
5356// Register registers the webhook with the passed manager and log.
5457func (w * Webhook ) Register (mgr ctrl.Manager , log * logr.Logger ) error {
@@ -58,5 +61,35 @@ func (w *Webhook) Register(mgr ctrl.Manager, log *logr.Logger) error {
5861 return ctrl .NewWebhookManagedBy (mgr ).
5962 For (& v1alpha1.ReleasePlan {}).
6063 WithDefaulter (w ).
64+ WithValidator (w ).
6165 Complete ()
6266}
67+
68+ // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
69+ func (w * Webhook ) ValidateCreate (ctx context.Context , obj runtime.Object ) (warnings admission.Warnings , err error ) {
70+ releasePlan := obj .(* v1alpha1.ReleasePlan )
71+
72+ // Validate that the Application field doesn't exceed Kubernetes label value limit (63 characters)
73+ if len (releasePlan .Spec .Application ) > 63 {
74+ return nil , fmt .Errorf ("application name must be no more than 63 characters, got %d characters" , len (releasePlan .Spec .Application ))
75+ }
76+
77+ return nil , nil
78+ }
79+
80+ // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
81+ func (w * Webhook ) ValidateUpdate (ctx context.Context , oldObj , newObj runtime.Object ) (warnings admission.Warnings , err error ) {
82+ releasePlan := newObj .(* v1alpha1.ReleasePlan )
83+
84+ // Validate that the Application field doesn't exceed Kubernetes label value limit (63 characters)
85+ if len (releasePlan .Spec .Application ) > 63 {
86+ return nil , fmt .Errorf ("application name must be no more than 63 characters, got %d characters" , len (releasePlan .Spec .Application ))
87+ }
88+
89+ return nil , nil
90+ }
91+
92+ // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
93+ func (w * Webhook ) ValidateDelete (ctx context.Context , obj runtime.Object ) (warnings admission.Warnings , err error ) {
94+ return nil , nil
95+ }
0 commit comments