@@ -821,17 +821,25 @@ func (a *Operator) copyToNamespace(prototype *v1alpha1.ClusterServiceVersion, ns
821821 if _ , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (nsTo ).UpdateStatus (context .TODO (), created , metav1.UpdateOptions {}); err != nil {
822822 return nil , fmt .Errorf ("failed to update status on new CSV: %w" , err )
823823 }
824- prototype .Annotations [statusCopyHashAnnotation ] = status
825- if _ , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (nsTo ).Update (context .TODO (), prototype , metav1.UpdateOptions {}); err != nil {
826- return nil , fmt .Errorf ("failed to update annotations after updating status: %w" , err )
827- }
828- return & v1alpha1.ClusterServiceVersion {
829- ObjectMeta : metav1.ObjectMeta {
830- Name : created .Name ,
831- Namespace : created .Namespace ,
832- UID : created .UID ,
833- },
834- }, nil
824+ prototype .Annotations [statusCopyHashAnnotation ] = status
825+ // persist status-hash annotation
826+ updatedCreated , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (nsTo ).Update (context .TODO (), prototype , metav1.UpdateOptions {})
827+ if err != nil {
828+ return nil , fmt .Errorf ("failed to update annotations after updating status: %w" , err )
829+ }
830+ // record observed generation and resourceVersion for metadata-drift guard
831+ updatedCreated .Annotations [observedGenerationAnnotation ] = fmt .Sprint (updatedCreated .GetGeneration ())
832+ updatedCreated .Annotations [observedResourceVersionAnnotation ] = updatedCreated .ResourceVersion
833+ if _ , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (nsTo ).Update (context .TODO (), updatedCreated , metav1.UpdateOptions {}); err != nil {
834+ return nil , fmt .Errorf ("failed to update metadata guard annotations after creation: %w" , err )
835+ }
836+ return & v1alpha1.ClusterServiceVersion {
837+ ObjectMeta : metav1.ObjectMeta {
838+ Name : updatedCreated .Name ,
839+ Namespace : updatedCreated .Namespace ,
840+ UID : updatedCreated .UID ,
841+ },
842+ }, nil
835843 } else if err != nil {
836844 return nil , err
837845 }
@@ -897,11 +905,18 @@ func (a *Operator) copyToNamespace(prototype *v1alpha1.ClusterServiceVersion, ns
897905 }
898906 // Update the status first if the existing copied CSV status hash doesn't match what we expect
899907 // to prevent a scenario where the hash annotations match but the contents do not.
900- // We also need to update the CSV itself in this case to ensure we set the status hash annotation.
901- prototype .Annotations [statusCopyHashAnnotation ] = status
902- if updated , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (nsTo ).Update (context .TODO (), prototype , metav1.UpdateOptions {}); err != nil {
903- return nil , fmt .Errorf ("failed to update: %w" , err )
904- }
908+ // persist status-hash annotation
909+ prototype .Annotations [statusCopyHashAnnotation ] = status
910+ updated , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (nsTo ).Update (context .TODO (), prototype , metav1.UpdateOptions {})
911+ if err != nil {
912+ return nil , fmt .Errorf ("failed to update: %w" , err )
913+ }
914+ // record observed generation and resourceVersion for metadata-drift guard
915+ updated .Annotations [observedGenerationAnnotation ] = fmt .Sprint (updated .GetGeneration ())
916+ updated .Annotations [observedResourceVersionAnnotation ] = updated .ResourceVersion
917+ if updated , err = a .client .OperatorsV1alpha1 ().ClusterServiceVersions (nsTo ).Update (context .TODO (), updated , metav1.UpdateOptions {}); err != nil {
918+ return nil , fmt .Errorf ("failed to update metadata guard annotations after status update: %w" , err )
919+ }
905920 } else {
906921 // Even if they're the same, ensure the returned prototype is annotated.
907922 prototype .Annotations [statusCopyHashAnnotation ] = status
0 commit comments