Skip to content

Commit

Permalink
added ctout check for update
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhprasad-sap committed Sep 6, 2024
1 parent 919e0a3 commit 7fad840
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
36 changes: 19 additions & 17 deletions cmd/web-hooks/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,26 +544,28 @@ func (wh *WebhookHandler) validateCAPTenant(w http.ResponseWriter, admissionRevi
func (wh *WebhookHandler) validateCAPTenantOutput(w http.ResponseWriter, admissionReview *admissionv1.AdmissionReview) validateResource {
ctoutObjNew := ResponseCtout{}

if admissionReview.Request.Operation == admissionv1.Create {
if validatedResource := unmarshalRawObj(w, admissionReview.Request.Object.Raw, &ctoutObjNew, v1alpha1.CAPTenantOutputKind); !validatedResource.allowed {
return validatedResource
}
if admissionReview.Request.Operation == admissionv1.Delete {
return validAdmissionReviewObj()
}

if validatedResource := unmarshalRawObj(w, admissionReview.Request.Object.Raw, &ctoutObjNew, v1alpha1.CAPTenantOutputKind); !validatedResource.allowed {
return validatedResource
}

if _, exists := ctoutObjNew.Labels[LabelTenantId]; !exists {
if _, exists := ctoutObjNew.Labels[LabelTenantId]; !exists {
return validateResource{
allowed: false,
message: fmt.Sprintf("%s %s label %s missing on CAP tenant output %s", InvalidationMessage, v1alpha1.CAPTenantOutputKind, LabelTenantId, ctoutObjNew.Name),
}
} else {
labelSelector, _ := labels.ValidatedSelectorFromSet(map[string]string{
LabelTenantId: ctoutObjNew.Labels[LabelTenantId],
})
ctList, err := wh.CrdClient.SmeV1alpha1().CAPTenants(ctoutObjNew.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector.String()})
if err != nil || len(ctList.Items) == 0 {
return validateResource{
allowed: false,
message: fmt.Sprintf("%s %s label %s missing on CAP tenant output %s", InvalidationMessage, v1alpha1.CAPTenantOutputKind, LabelTenantId, ctoutObjNew.Name),
}
} else {
labelSelector, _ := labels.ValidatedSelectorFromSet(map[string]string{
LabelTenantId: ctoutObjNew.Labels[LabelTenantId],
})
ctList, err := wh.CrdClient.SmeV1alpha1().CAPTenants(ctoutObjNew.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector.String()})
if err != nil || len(ctList.Items) == 0 {
return validateResource{
allowed: false,
message: fmt.Sprintf("%s %s label %s on CAP tenant output %s does not contain a valid tenant ID", InvalidationMessage, v1alpha1.CAPTenantOutputKind, LabelTenantId, ctoutObjNew.Name),
}
message: fmt.Sprintf("%s %s label %s on CAP tenant output %s does not contain a valid tenant ID", InvalidationMessage, v1alpha1.CAPTenantOutputKind, LabelTenantId, ctoutObjNew.Name),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/web-hooks/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,14 @@ func TestCtoutInvalidity(t *testing.T) {
operation: admissionv1.Create,
labelPresent: false,
},
{
operation: admissionv1.Update,
labelPresent: true,
},
{
operation: admissionv1.Update,
labelPresent: false,
},
}
for _, test := range tests {
t.Run("Testing CAPTenantOutput invalidity for operation "+string(test.operation), func(t *testing.T) {
Expand Down

0 comments on commit 7fad840

Please sign in to comment.