@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "errors"
2020 "fmt"
21+ "reflect"
2122 "strings"
2223
2324 "github.com/golang/mock/gomock"
@@ -642,7 +643,7 @@ var _ = Describe("Clientutils", func() {
642643 })
643644 })
644645
645- Describe ("CreateOrUse " , func () {
646+ Describe ("CreateOrUseAndPatch " , func () {
646647 var (
647648 cm1 , cm2 , cm3 corev1.ConfigMap
648649 )
@@ -667,154 +668,60 @@ var _ = Describe("Clientutils", func() {
667668 }
668669 })
669670
670- It ("should use an object if it matches" , func () {
671- cm := & corev1.ConfigMap {}
672- res , other , err := CreateOrUse (ctx , c , []client.Object {& cm1 , & cm2 , & cm3 }, cm , func () (bool , error ) {
673- return cm .Name == "n3" , nil
674- }, func () error {
675- Fail ("init should not be called" )
676- return nil
677- })
671+ It ("should use an object if it matches and patch it when it's mutated" , func () {
672+ annotations := map [string ]string {"foo" : "bar" }
673+ withoutAnnotations := & corev1.ConfigMap {}
674+ withAnnotations := & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Annotations : annotations }}
675+ expectedPatchData , err := client .MergeFrom (withoutAnnotations ).Data (withAnnotations )
678676 Expect (err ).NotTo (HaveOccurred ())
679- Expect (other ).To (Equal ([]client.Object {& cm1 , & cm2 }))
680- Expect (res ).To (Equal (CreateOrUseOperationResultUsed ))
681- Expect (cm ).To (Equal (& cm3 ))
682- })
683677
684- It ("should create a new object if it does not match" , func () {
685- cm := & corev1.ConfigMap {}
686- c .EXPECT ().Create (ctx , cm )
687- res , other , err := CreateOrUse (ctx , c , []client.Object {& cm1 , & cm2 , & cm3 }, cm , func () (bool , error ) {
688- return false , nil
689- }, func () error {
690- cm .Name = "n4"
691- return nil
692- })
693- Expect (err ).NotTo (HaveOccurred ())
694- Expect (other ).To (Equal ([]client.Object {& cm1 , & cm2 , & cm3 }))
695- Expect (res ).To (Equal (CreateOrUseOperationResultCreated ))
696- Expect (cm ).To (Equal (& corev1.ConfigMap {
697- ObjectMeta : metav1.ObjectMeta {
698- Name : "n4" ,
699- },
700- }))
701- })
702- })
703-
704- Describe ("CreateOrUseWithList" , func () {
705- var (
706- cm1 , cm2 , cm3 corev1.ConfigMap
707- )
708- BeforeEach (func () {
709- cm1 = corev1.ConfigMap {
710- ObjectMeta : metav1.ObjectMeta {
711- Namespace : "foo" ,
712- Name : "n1" ,
713- },
714- }
715- cm2 = corev1.ConfigMap {
716- ObjectMeta : metav1.ObjectMeta {
717- Namespace : "foo" ,
718- Name : "n2" ,
719- },
720- }
721- cm3 = corev1.ConfigMap {
722- ObjectMeta : metav1.ObjectMeta {
723- Namespace : "foo" ,
724- Name : "n3" ,
725- },
726- }
727- })
728-
729- It ("should use an object if it matches" , func () {
678+ c .EXPECT ().Patch (ctx , gomock .AssignableToTypeOf (& corev1.ConfigMap {}), gomock .AssignableToTypeOf (reflect .TypeOf ((* client .Patch )(nil )).Elem ())).
679+ Do (func (_ context.Context , cm * corev1.ConfigMap , patch client.Patch , opts ... client.PatchOption ) {
680+ Expect (patch .Data (cm )).To (Equal (expectedPatchData ))
681+ cm .Annotations = annotations
682+ })
730683 cm := & corev1.ConfigMap {}
731- list := & corev1.ConfigMapList {Items : []corev1.ConfigMap {cm1 , cm2 , cm3 }}
732- res , err := CreateOrUseWithList (ctx , c , list , cm , func () (bool , error ) {
684+ res , other , err := CreateOrUseAndPatch (ctx , c , []client.Object {& cm1 , & cm2 , & cm3 }, cm , func () (bool , error ) {
733685 return cm .Name == "n3" , nil
734686 }, func () error {
735- Fail ( "init should not be called" )
687+ cm . Annotations = annotations
736688 return nil
737689 })
738690 Expect (err ).NotTo (HaveOccurred ())
739- Expect (list .Items ).To (Equal ([]corev1.ConfigMap {cm1 , cm2 }))
740- Expect (res ).To (Equal (CreateOrUseOperationResultUsed ))
741- Expect (cm ).To (Equal (& cm3 ))
742- })
743-
744- It ("should create a new object if it does not match" , func () {
745- cm := & corev1.ConfigMap {}
746- list := & corev1.ConfigMapList {Items : []corev1.ConfigMap {cm1 , cm2 , cm3 }}
747- c .EXPECT ().Create (ctx , cm )
748- res , err := CreateOrUseWithList (ctx , c , list , cm , func () (bool , error ) {
749- return false , nil
750- }, func () error {
751- cm .Name = "n4"
752- return nil
753- })
754- Expect (err ).NotTo (HaveOccurred ())
755- Expect (list .Items ).To (Equal ([]corev1.ConfigMap {cm1 , cm2 , cm3 }))
756- Expect (res ).To (Equal (CreateOrUseOperationResultCreated ))
691+ Expect (other ).To (Equal ([]client.Object {& cm1 , & cm2 }))
692+ Expect (res ).To (Equal (controllerutil .OperationResultUpdated ))
757693 Expect (cm ).To (Equal (& corev1.ConfigMap {
758694 ObjectMeta : metav1.ObjectMeta {
759- Name : "n4" ,
695+ Namespace : "foo" ,
696+ Name : "n3" ,
697+ Annotations : annotations ,
760698 },
761699 }))
762700 })
763- })
764-
765- Describe ("CreateOrUseWithObjectSlicePointer" , func () {
766- var (
767- cm1 , cm2 , cm3 corev1.ConfigMap
768- slice []corev1.ConfigMap
769- )
770- BeforeEach (func () {
771- cm1 = corev1.ConfigMap {
772- ObjectMeta : metav1.ObjectMeta {
773- Namespace : "foo" ,
774- Name : "n1" ,
775- },
776- }
777- cm2 = corev1.ConfigMap {
778- ObjectMeta : metav1.ObjectMeta {
779- Namespace : "foo" ,
780- Name : "n2" ,
781- },
782- }
783- cm3 = corev1.ConfigMap {
784- ObjectMeta : metav1.ObjectMeta {
785- Namespace : "foo" ,
786- Name : "n3" ,
787- },
788- }
789- slice = []corev1.ConfigMap {cm1 , cm2 , cm3 }
790- })
791701
792- It ("should use an object if it matches " , func () {
702+ It ("should use an object without updating it if it's mutation semantically equals its original " , func () {
793703 cm := & corev1.ConfigMap {}
794- res , err := CreateOrUseWithObjectSlicePointer (ctx , c , & slice , cm , func () (bool , error ) {
704+ res , other , err := CreateOrUseAndPatch (ctx , c , []client. Object { & cm1 , & cm2 , & cm3 } , cm , func () (bool , error ) {
795705 return cm .Name == "n3" , nil
796- }, func () error {
797- Fail ("init should not be called" )
798- return nil
799- })
706+ }, nil )
800707 Expect (err ).NotTo (HaveOccurred ())
801- Expect (slice ).To (Equal ([]corev1. ConfigMap { cm1 , cm2 }))
802- Expect (res ).To (Equal (CreateOrUseOperationResultUsed ))
708+ Expect (other ).To (Equal ([]client. Object { & cm1 , & cm2 }))
709+ Expect (res ).To (Equal (controllerutil . OperationResultNone ))
803710 Expect (cm ).To (Equal (& cm3 ))
804711 })
805712
806- It ("should create a new object if it does not match " , func () {
713+ It ("should create a new object if none matches " , func () {
807714 cm := & corev1.ConfigMap {}
808715 c .EXPECT ().Create (ctx , cm )
809- res , err := CreateOrUseWithObjectSlicePointer (ctx , c , & slice , cm , func () (bool , error ) {
716+ res , other , err := CreateOrUseAndPatch (ctx , c , []client. Object { & cm1 , & cm2 , & cm3 } , cm , func () (bool , error ) {
810717 return false , nil
811718 }, func () error {
812719 cm .Name = "n4"
813720 return nil
814721 })
815722 Expect (err ).NotTo (HaveOccurred ())
816- Expect (slice ).To (Equal ([]corev1. ConfigMap { cm1 , cm2 , cm3 }))
817- Expect (res ).To (Equal (CreateOrUseOperationResultCreated ))
723+ Expect (other ).To (Equal ([]client. Object { & cm1 , & cm2 , & cm3 }))
724+ Expect (res ).To (Equal (controllerutil . OperationResultCreated ))
818725 Expect (cm ).To (Equal (& corev1.ConfigMap {
819726 ObjectMeta : metav1.ObjectMeta {
820727 Name : "n4" ,
0 commit comments