Skip to content

Commit 42a1f10

Browse files
committed
Persist observed annotations on all status updates
Signed-off-by: Brett Tofel <[email protected]>
1 parent 2215296 commit 42a1f10

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

pkg/controller/operators/olm/operatorgroup.go

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

0 commit comments

Comments
 (0)