Skip to content

Commit 4e042c8

Browse files
committed
UPSTREAM: 134442: Fix ResourceQuota test for CRDs with long names
1 parent 7c815a8 commit 4e042c8

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

test/e2e/apimachinery/resource_quota.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"strconv"
24+
"strings"
2425
"time"
2526

2627
appsv1 "k8s.io/api/apps/v1"
2728
v1 "k8s.io/api/core/v1"
2829
resourceapi "k8s.io/api/resource/v1"
2930
schedulingv1 "k8s.io/api/scheduling/v1"
31+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3032
apiequality "k8s.io/apimachinery/pkg/api/equality"
3133
apierrors "k8s.io/apimachinery/pkg/api/errors"
3234
"k8s.io/apimachinery/pkg/api/resource"
@@ -658,7 +660,7 @@ var _ = SIGDescribe("ResourceQuota", func() {
658660

659661
ginkgo.It("should create a ResourceQuota and capture the life of a custom resource.", func(ctx context.Context) {
660662
ginkgo.By("Creating a Custom Resource Definition")
661-
testcrd, err := crd.CreateTestCRD(f)
663+
testcrd, err := crd.CreateTestCRD(f, ensureShortCRDNamesForResourceQuota())
662664
framework.ExpectNoError(err)
663665
ginkgo.DeferCleanup(testcrd.CleanUp)
664666
countResourceName := "count/" + testcrd.Crd.Spec.Names.Plural + "." + testcrd.Crd.Spec.Group
@@ -2144,6 +2146,35 @@ func newTestResourceQuotaWithScopeForVolumeAttributesClass(name string, hard v1.
21442146
}
21452147
}
21462148

2149+
// ensureShortCRDNamesForResourceQuota returns a CRD Option that truncates the plural name
2150+
// if needed to ensure "count/{plural}.{group}" fits within 63 characters (Kubernetes resource name limit).
2151+
func ensureShortCRDNamesForResourceQuota() crd.Option {
2152+
return func(c *apiextensionsv1.CustomResourceDefinition) {
2153+
const maxResourceNameLength = 63
2154+
countResourcePrefix := "count/"
2155+
2156+
// Calculate what the resource name would be
2157+
fullResourceName := countResourcePrefix + c.Spec.Names.Plural + "." + c.Spec.Group
2158+
2159+
if len(fullResourceName) > maxResourceNameLength {
2160+
// Calculate max length for the plural name
2161+
maxPluralLen := maxResourceNameLength - len(countResourcePrefix) - len(".") - len(c.Spec.Group)
2162+
2163+
if maxPluralLen > 0 && len(c.Spec.Names.Plural) > maxPluralLen {
2164+
// Truncate the plural name
2165+
originalPlural := c.Spec.Names.Plural
2166+
truncatedPlural := strings.TrimRight(originalPlural[:maxPluralLen], "-.")
2167+
2168+
// Update the CRD spec
2169+
c.Spec.Names.Plural = truncatedPlural
2170+
c.ObjectMeta.Name = truncatedPlural + "." + c.Spec.Group
2171+
2172+
framework.Logf("Truncated CRD plural name from %q to %q to fit ResourceQuota naming constraints", originalPlural, truncatedPlural)
2173+
}
2174+
}
2175+
}
2176+
}
2177+
21472178
// newTestResourceQuota returns a quota that enforces default constraints for testing
21482179
func newTestResourceQuota(name string) *v1.ResourceQuota {
21492180
hard := v1.ResourceList{}

0 commit comments

Comments
 (0)