@@ -643,6 +643,24 @@ var _ = Describe("Clientutils", func() {
643643 })
644644 })
645645
646+ Describe ("IsOlderThan" , func () {
647+ It ("should return true if an object is older than another" , func () {
648+ cm1 := & corev1.ConfigMap {
649+ ObjectMeta : metav1.ObjectMeta {
650+ CreationTimestamp : metav1 .Unix (100 , 0 ),
651+ },
652+ }
653+ cm2 := & corev1.ConfigMap {
654+ ObjectMeta : metav1.ObjectMeta {
655+ CreationTimestamp : metav1 .Unix (0 , 0 ),
656+ },
657+ }
658+ Expect (IsOlderThan (cm2 )(cm1 )).To (BeFalse (), "cm1 should not be older than cm1" )
659+ Expect (IsOlderThan (cm1 )(cm2 )).To (BeTrue (), "cm2 should be older than cm1" )
660+ Expect (IsOlderThan (cm1 )(cm1 )).To (BeFalse (), "cm1 should not be older than itself" )
661+ })
662+ })
663+
646664 Describe ("CreateOrUseAndPatch" , func () {
647665 var (
648666 cm1 , cm2 , cm3 corev1.ConfigMap
@@ -656,8 +674,9 @@ var _ = Describe("Clientutils", func() {
656674 }
657675 cm2 = corev1.ConfigMap {
658676 ObjectMeta : metav1.ObjectMeta {
659- Namespace : "foo" ,
660- Name : "n2" ,
677+ CreationTimestamp : metav1 .Unix (100 , 0 ),
678+ Namespace : "foo" ,
679+ Name : "n2" ,
661680 },
662681 }
663682 cm3 = corev1.ConfigMap {
@@ -683,7 +702,7 @@ var _ = Describe("Clientutils", func() {
683702 cm := & corev1.ConfigMap {}
684703 res , other , err := CreateOrUseAndPatch (ctx , c , []client.Object {& cm1 , & cm2 , & cm3 }, cm , func () (bool , error ) {
685704 return cm .Name == "n3" , nil
686- }, func () error {
705+ }, IsOlderThan ( cm ), func () error {
687706 cm .Annotations = annotations
688707 return nil
689708 })
@@ -703,7 +722,18 @@ var _ = Describe("Clientutils", func() {
703722 cm := & corev1.ConfigMap {}
704723 res , other , err := CreateOrUseAndPatch (ctx , c , []client.Object {& cm1 , & cm2 , & cm3 }, cm , func () (bool , error ) {
705724 return cm .Name == "n3" , nil
706- }, nil )
725+ }, IsOlderThan (cm ), nil )
726+ Expect (err ).NotTo (HaveOccurred ())
727+ Expect (other ).To (Equal ([]client.Object {& cm1 , & cm2 }))
728+ Expect (res ).To (Equal (controllerutil .OperationResultNone ))
729+ Expect (cm ).To (Equal (& cm3 ))
730+ })
731+
732+ It ("should use the older object when multiple objects match" , func () {
733+ cm := & corev1.ConfigMap {}
734+ res , other , err := CreateOrUseAndPatch (ctx , c , []client.Object {& cm1 , & cm2 , & cm3 }, cm , func () (bool , error ) {
735+ return cm .Name == "n2" || cm .Name == "n3" , nil
736+ }, IsOlderThan (cm ), nil )
707737 Expect (err ).NotTo (HaveOccurred ())
708738 Expect (other ).To (Equal ([]client.Object {& cm1 , & cm2 }))
709739 Expect (res ).To (Equal (controllerutil .OperationResultNone ))
@@ -715,7 +745,7 @@ var _ = Describe("Clientutils", func() {
715745 c .EXPECT ().Create (ctx , cm )
716746 res , other , err := CreateOrUseAndPatch (ctx , c , []client.Object {& cm1 , & cm2 , & cm3 }, cm , func () (bool , error ) {
717747 return false , nil
718- }, func () error {
748+ }, IsOlderThan ( cm ), func () error {
719749 cm .Name = "n4"
720750 return nil
721751 })
0 commit comments