From 1b45834c6a35c3955cb494434157ceb4084ec993 Mon Sep 17 00:00:00 2001 From: Md Soharab Ansari Date: Mon, 26 Feb 2024 15:28:53 +0530 Subject: [PATCH] update validateserviceexport function Signed-off-by: Md Soharab Ansari --- pkg/webhook/pod/webhook.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/pkg/webhook/pod/webhook.go b/pkg/webhook/pod/webhook.go index fddcc4f26..c7ed29333 100644 --- a/pkg/webhook/pod/webhook.go +++ b/pkg/webhook/pod/webhook.go @@ -160,14 +160,11 @@ func (wh *WebhookServer) Handle(ctx context.Context, req admission.Request) admi } log := logger.FromContext(ctx) - log.Info("fetching all serviceexport objects belonging to the slice", "slice", serviceexport.Spec.Slice) - serviceExportList, err := wh.SliceInfoClient.GetAllServiceExports(context.Background(), wh.Client, serviceexport.Spec.Slice) + log.Info("validating serviceexport", "serviceexport spec", serviceexport.Spec) + validation, conflictingAlias, err := wh.ValidateServiceExport(serviceexport, ctx) if err != nil { return admission.Errored(http.StatusInternalServerError, err) } - - log.Info("validating serviceexport", "serviceexport spec", serviceexport.Spec) - validation, conflictingAlias := ValidateServiceExport(serviceexport, serviceExportList) if !validation { log.Info("serviceexport validation failed: alias already exist", "serviceexport-name", serviceexport.ObjectMeta.Name) return admission.Denied(fmt.Sprintf("Alias %s already exist", conflictingAlias)) @@ -284,24 +281,31 @@ func MutateDaemonSet(ds *appsv1.DaemonSet, sliceName string) *appsv1.DaemonSet { return ds } -func ValidateServiceExport(sExport *v1beta1.ServiceExport, seList *v1beta1.ServiceExportList) (bool, string) { +func (wh *WebhookServer) ValidateServiceExport(svcex *v1beta1.ServiceExport, ctx context.Context) (bool, string, error) { + + log := logger.FromContext(ctx) + log.Info("fetching all serviceexport objects belonging to the slice", "slice", svcex.Spec.Slice) + serviceExportList, err := wh.SliceInfoClient.GetAllServiceExports(context.Background(), wh.Client, svcex.Spec.Slice) + if err != nil { + return false, "", err + } - newAliases := sExport.Spec.Aliases + newAliases := svcex.Spec.Aliases - for _, serviceExport := range seList.Items { + for _, serviceExport := range serviceExportList.Items { // In case we are updating an existing ServiceExport resource - if sExport.ObjectMeta.Name == serviceExport.ObjectMeta.Name && - sExport.ObjectMeta.Namespace == serviceExport.ObjectMeta.Namespace { + if svcex.ObjectMeta.Name == serviceExport.ObjectMeta.Name && + svcex.ObjectMeta.Namespace == serviceExport.ObjectMeta.Namespace { continue } existingAliases := serviceExport.Spec.Aliases for _, newAlias := range newAliases { if aliasExist(existingAliases, newAlias) { - return false, newAlias + return false, newAlias, nil } } } - return true, "" + return true, "", nil } func (wh *WebhookServer) MutationRequired(metadata metav1.ObjectMeta, ctx context.Context, kind string) (bool, string) {