@@ -44,6 +44,7 @@ import (
4444 "k8s.io/apimachinery/pkg/runtime/schema"
4545 "k8s.io/apimachinery/pkg/runtime/serializer"
4646 "k8s.io/apimachinery/pkg/types"
47+ "k8s.io/apimachinery/pkg/util/sets"
4748 "k8s.io/apimachinery/pkg/watch"
4849 clientgoapplyconfigurations "k8s.io/client-go/applyconfigurations"
4950 corev1applyconfigurations "k8s.io/client-go/applyconfigurations/core/v1"
@@ -485,6 +486,47 @@ var _ = Describe("Fake client", func() {
485486 Expect (list .Items [0 ].Name ).NotTo (BeEmpty ())
486487 })
487488
489+ It ("should retry GenerateName on name collision and succeed" , func (ctx SpecContext ) {
490+ // Create many objects with the same short GenerateName prefix so that
491+ // birthday-paradox collisions are highly likely. The retry loop
492+ // (max 7 attempts per object) must absorb every collision.
493+ const n = 500
494+ names := sets .New [string ]()
495+ for i := range n {
496+ cm := & corev1.ConfigMap {
497+ ObjectMeta : metav1.ObjectMeta {
498+ GenerateName : "this-generate-name-prefix-is-longer-than-max-generated-name-length-and-will-be-truncated-" ,
499+ Namespace : "ns2" ,
500+ },
501+ }
502+ Expect (cl .Create (ctx , cm )).To (Succeed (), "create #%d failed" , i )
503+ Expect (cm .Name ).NotTo (BeEmpty ())
504+ Expect (cm .Name ).To (HaveLen (maxNameLength ))
505+ Expect (names .Has (cm .Name )).To (BeFalse (), "duplicate name %q" , cm .Name )
506+ names .Insert (cm .Name )
507+ }
508+ })
509+
510+ It ("should not retry AlreadyExists when Name is set explicitly" , func (ctx SpecContext ) {
511+ cm := & corev1.ConfigMap {
512+ ObjectMeta : metav1.ObjectMeta {
513+ Name : "explicit-name" ,
514+ Namespace : "ns2" ,
515+ },
516+ }
517+ Expect (cl .Create (ctx , cm )).To (Succeed ())
518+
519+ // Second create with the same explicit name must fail immediately.
520+ cm2 := & corev1.ConfigMap {
521+ ObjectMeta : metav1.ObjectMeta {
522+ Name : "explicit-name" ,
523+ Namespace : "ns2" ,
524+ },
525+ }
526+ err := cl .Create (ctx , cm2 )
527+ Expect (apierrors .IsAlreadyExists (err )).To (BeTrue ())
528+ })
529+
488530 It ("should be able to Update" , func (ctx SpecContext ) {
489531 By ("Updating a new configmap" )
490532 newcm := & corev1.ConfigMap {
0 commit comments