@@ -18,14 +18,13 @@ package workgenerator
1818
1919import (
2020 "context"
21- "crypto/sha256"
22- "encoding/hex"
2321 "fmt"
2422 "sort"
2523 "strings"
2624
2725 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2826 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27+ "k8s.io/apimachinery/pkg/util/uuid"
2928 "k8s.io/klog/v2"
3029 "sigs.k8s.io/controller-runtime/pkg/client"
3130
@@ -82,19 +81,12 @@ func (r *Reconciler) createOrUpdateEnvelopeCRWorkObj(
8281 var work * fleetv1beta1.Work
8382 switch {
8483 case len (workList .Items ) > 1 :
85- // Multiple matching work objects found; this should never occur under normal conditions
86- // with deterministic naming. Log details for investigation.
84+ // Multiple matching work objects found; this should never occur under normal conditions.
8785 wrappedErr := fmt .Errorf ("%d work objects found for the same envelope %v, only one expected" , len (workList .Items ), envelopeReader .GetEnvelopeObjRef ())
8886 klog .ErrorS (wrappedErr , "Failed to create or update work object for envelope" ,
8987 "resourceBinding" , klog .KObj (binding ),
9088 "resourceSnapshot" , klog .KObj (resourceSnapshot ),
9189 "envelope" , envelopeReader .GetEnvelopeObjRef ())
92- // Log the work object names to help debug
93- for i := range workList .Items {
94- klog .ErrorS (wrappedErr , "Duplicate work object found" ,
95- "work" , klog .KObj (& workList .Items [i ]),
96- "creationTimestamp" , workList .Items [i ].CreationTimestamp )
97- }
9890 return nil , controller .NewUnexpectedBehaviorError (wrappedErr )
9991 case len (workList .Items ) == 1 :
10092 klog .V (2 ).InfoS ("Found existing work object for the envelope; updating it" ,
@@ -204,11 +196,7 @@ func buildNewWorkForEnvelopeCR(
204196 manifests []fleetv1beta1.Manifest ,
205197 resourceOverrideSnapshotHash , clusterResourceOverrideSnapshotHash string ,
206198) * fleetv1beta1.Work {
207- // Generate a deterministic work name based on the envelope identity to prevent duplicate work objects
208- // from being created by concurrent reconciliations. The name is stable across reconciliations for
209- // the same envelope, allowing Kubernetes' built-in duplicate prevention to work correctly.
210- envelopeIdentifier := generateEnvelopeIdentifier (envelopeReader )
211- workName := fmt .Sprintf (fleetv1beta1 .WorkNameWithEnvelopeCRFmt , workNamePrefix , envelopeIdentifier )
199+ workName := fmt .Sprintf (fleetv1beta1 .WorkNameWithEnvelopeCRFmt , workNamePrefix , uuid .NewUUID ())
212200 workNamespace := fmt .Sprintf (utils .NamespaceNameFormat , resourceBinding .GetBindingSpec ().TargetCluster )
213201
214202 // Create the labels map
@@ -246,18 +234,3 @@ func buildNewWorkForEnvelopeCR(
246234 },
247235 }
248236}
249-
250- // generateEnvelopeIdentifier generates a stable, deterministic identifier for an envelope.
251- // This identifier is used in the work name to ensure that the same envelope always produces
252- // the same work name, enabling Kubernetes' atomic create operations to prevent duplicates.
253- func generateEnvelopeIdentifier (envelopeReader fleetv1beta1.EnvelopeReader ) string {
254- // Create a stable identifier based on envelope type, name, and namespace.
255- // For cluster-scoped envelopes, namespace is empty, so we include the type to ensure uniqueness.
256- identifier := fmt .Sprintf ("%s.%s.%s" , envelopeReader .GetEnvelopeType (), envelopeReader .GetNamespace (), envelopeReader .GetName ())
257-
258- // Use SHA256 hash to create a deterministic identifier
259- hash := sha256 .Sum256 ([]byte (identifier ))
260- // Take first 8 characters of the hex-encoded hash to keep work names reasonably short
261- // while maintaining uniqueness (8 hex chars = 4 bytes = 2^32 combinations, sufficient for envelope uniqueness)
262- return hex .EncodeToString (hash [:])[:8 ]
263- }
0 commit comments