Skip to content

Commit

Permalink
update validateserviceexport function
Browse files Browse the repository at this point in the history
Signed-off-by: Md Soharab Ansari <[email protected]>
  • Loading branch information
soharab-ic committed Feb 26, 2024
1 parent d23874e commit 1b45834
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pkg/webhook/pod/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1b45834

Please sign in to comment.