Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Misc] Webhook - Service name length check #128

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading