Skip to content

Commit

Permalink
[Misc] Webhook - Service name length check (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhprasad-sap authored Sep 12, 2024
1 parent 3313fee commit 368127a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/web-hooks/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,22 @@ func validateWorkloads(cavObjNew *ResponseCav) validateResource {
uniqueWorkloadNameCountMap := make(map[string]int)
for _, workload := range cavObjNew.Spec.Workloads {

// check workload name matches the regex pattern
if !regex.MatchString(workload.Name) {
return validateResource{
allowed: false,
message: fmt.Sprintf("%s %s Invalid workload name: %s", InvalidationMessage, cavObjNew.Kind, workload.Name),
}
}

// Allowed length for service name is 63 characters
if len(cavObjNew.Name+"-"+workload.Name+"-svc") > 63 {
return validateResource{
allowed: false,
message: fmt.Sprintf("%s %s Derived service name: %s for workload %s will exceed 63 character limit. Adjust CAPApplicationVerion resource name or the workload name accordingly", InvalidationMessage, cavObjNew.Kind, cavObjNew.Name+"-"+workload.Name+"-svc", workload.Name),
}
}

if workloadTypeValidate := checkWorkloadType(&workload); !workloadTypeValidate.allowed {
return workloadTypeValidate
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/web-hooks/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ func TestCavInvalidity(t *testing.T) {
missingContentJobinContentJobs bool
invalidJobinContentJobs bool
invalidWorkloadName bool
longWorkloadName bool
backlogItems []string
}{
{
Expand Down Expand Up @@ -880,6 +881,11 @@ func TestCavInvalidity(t *testing.T) {
invalidWorkloadName: true,
backlogItems: []string{},
},
{
operation: admissionv1.Create,
longWorkloadName: true,
backlogItems: []string{},
},
}
for _, test := range tests {
nameParts := []string{"Testing CAPApplicationversion invalidity for operation " + string(test.operation) + "; "}
Expand Down Expand Up @@ -1153,6 +1159,8 @@ func TestCavInvalidity(t *testing.T) {
crd.Spec.ContentJobs = append(crd.Spec.ContentJobs, "content", "content-2", "dummy")
} else if test.invalidWorkloadName == true {
crd.Spec.Workloads[0].Name = "WrongWorkloadName"
} else if test.longWorkloadName == true {
crd.Spec.Workloads[0].Name = "extralongworkloadnamecontainingmorethan64characters"
}

rawBytes, _ := json.Marshal(crd)
Expand Down Expand Up @@ -1212,6 +1220,8 @@ func TestCavInvalidity(t *testing.T) {
errorMessage = fmt.Sprintf("%s %s job dummy specified as part of ContentJobs is not a valid content job", InvalidationMessage, v1alpha1.CAPApplicationVersionKind)
} else if test.invalidWorkloadName == true {
errorMessage = fmt.Sprintf("%s %s Invalid workload name: %s", InvalidationMessage, v1alpha1.CAPApplicationVersionKind, "WrongWorkloadName")
} else if test.longWorkloadName == true {
errorMessage = fmt.Sprintf("%s %s Derived service name: %s for workload %s will exceed 63 character limit. Adjust CAPApplicationVerion resource name or the workload name accordingly", InvalidationMessage, v1alpha1.CAPApplicationVersionKind, crd.Name+"-"+"extralongworkloadnamecontainingmorethan64characters"+"-svc", "extralongworkloadnamecontainingmorethan64characters")
}

if admissionReviewRes.Response.Allowed || admissionReviewRes.Response.Result.Message != errorMessage {
Expand Down

0 comments on commit 368127a

Please sign in to comment.