@@ -420,6 +420,7 @@ func TestApplyMutationIgnoredObjects(t *testing.T) {
420
420
serverObjs []client.Object
421
421
declaredObjs []client.Object
422
422
cachedIgnoredObjs []client.Object
423
+ applyEvents []event.Event
423
424
expectedObjsToApply object.UnstructuredSet
424
425
expectedItemsInIgnoreCache []client.Object
425
426
}{
@@ -595,6 +596,54 @@ func TestApplyMutationIgnoredObjects(t *testing.T) {
595
596
syncertest .IgnoreMutationAnnotation )),
596
597
},
597
598
},
599
+ {
600
+ name : "mutation-ignored object should be evicted from cache when apply fails" ,
601
+ serverObjs : []client.Object {
602
+ k8sobjects .NamespaceObject ("test-ns" ,
603
+ syncertest .ManagementEnabled ,
604
+ core .Label ("foo" , "baz" )),
605
+ },
606
+ declaredObjs : []client.Object {
607
+ k8sobjects .UnstructuredObject (kinds .Namespace (), core .Name ("test-ns" ),
608
+ core .Label (metadata .ManagedByKey , metadata .ManagedByValue ),
609
+ core .Label (metadata .ApplySetPartOfLabel , applySetID ),
610
+ core .Label (metadata .DeclaredVersionLabel , "v1" ),
611
+ metadata .WithManagementMode (metadata .ManagementEnabled ),
612
+ core .Annotation (metadata .GitContextKey , gitContextOutput ),
613
+ core .Annotation (metadata .SyncTokenAnnotationKey , testGitCommit ),
614
+ core .Annotation (metadata .OwningInventoryKey , InventoryID (rootSyncName , configmanagement .ControllerNamespace )),
615
+ difftest .ManagedBy (declared .RootScope , rootSyncName ),
616
+ core .Label ("foo" , "bar" ),
617
+ syncertest .ManagementEnabled ,
618
+ syncertest .IgnoreMutationAnnotation )},
619
+ cachedIgnoredObjs : []client.Object {
620
+ & queue.Deleted {
621
+ Object : k8sobjects .UnstructuredObject (kinds .Namespace (),
622
+ core .Name ("test-ns" ),
623
+ syncertest .ManagementEnabled ,
624
+ syncertest .IgnoreMutationAnnotation )}},
625
+ applyEvents : []event.Event {
626
+ formApplyEvent (event .ApplyFailed ,
627
+ k8sobjects .UnstructuredObject (kinds .Namespace (), core .Name ("test-ns" )),
628
+ fmt .Errorf ("test error" )),
629
+ },
630
+ // The object should be purged from the ignore cache due to ApplyFailed
631
+ expectedItemsInIgnoreCache : nil ,
632
+ expectedObjsToApply : object.UnstructuredSet {
633
+ asUnstructuredSanitizedObj (k8sobjects .UnstructuredObject (kinds .Namespace (), core .Name ("test-ns" ),
634
+ core .Label (metadata .ManagedByKey , metadata .ManagedByValue ),
635
+ core .Label (metadata .ApplySetPartOfLabel , applySetID ),
636
+ core .Label (metadata .DeclaredVersionLabel , "v1" ),
637
+ metadata .WithManagementMode (metadata .ManagementEnabled ),
638
+ core .Annotation (metadata .GitContextKey , gitContextOutput ),
639
+ core .Annotation (metadata .SyncTokenAnnotationKey , testGitCommit ),
640
+ core .Annotation (metadata .OwningInventoryKey , InventoryID (rootSyncName , configmanagement .ControllerNamespace )),
641
+ difftest .ManagedBy (declared .RootScope , rootSyncName ),
642
+ syncertest .ManagementEnabled ,
643
+ syncertest .IgnoreMutationAnnotation ,
644
+ core .Label ("foo" , "bar" ))),
645
+ },
646
+ },
598
647
}
599
648
600
649
for _ , tc := range testcases {
@@ -608,7 +657,7 @@ func TestApplyMutationIgnoredObjects(t *testing.T) {
608
657
syncScope := declared .Scope ("test-namespace" )
609
658
syncName := "rs"
610
659
fakeClient := testingfake .NewClient (t , core .Scheme , tc .serverObjs ... )
611
- fakeKptApplier := newFakeKptApplier ([]event. Event {} )
660
+ fakeKptApplier := newFakeKptApplier (tc . applyEvents )
612
661
cs := & ClientSet {
613
662
KptApplier : fakeKptApplier ,
614
663
Client : fakeClient ,
@@ -915,6 +964,7 @@ func TestProcessApplyEvent(t *testing.T) {
915
964
deploymentObj := newDeploymentObj ()
916
965
deploymentObjID := core .IDOf (deploymentObj )
917
966
testObj1 := newTestObj ("test-1" )
967
+ core .SetAnnotation (testObj1 , metadata .LifecycleMutationAnnotation , metadata .IgnoreMutation )
918
968
testObj1ID := core .IDOf (testObj1 )
919
969
920
970
ctx := context .Background ()
@@ -925,10 +975,15 @@ func TestProcessApplyEvent(t *testing.T) {
925
975
926
976
resourceMap := make (map [core.ID ]client.Object )
927
977
resourceMap [deploymentObjID ] = deploymentObj
978
+ resourceMap [testObj1ID ] = testObj1
979
+
980
+ resources := & declared.Resources {}
981
+ resources .UpdateIgnored (testObj1 )
928
982
929
- err := s .processApplyEvent (ctx , formApplyEvent (event .ApplyFailed , deploymentObj , fmt .Errorf ("test error" )).ApplyEvent , syncStats .ApplyEvent , objStatusMap , unknownTypeResources , resourceMap )
983
+ // process failed apply of deploymentObj
984
+ err := s .processApplyEvent (ctx , formApplyEvent (event .ApplyFailed , deploymentObj , fmt .Errorf ("test error" )).ApplyEvent , syncStats .ApplyEvent , objStatusMap , unknownTypeResources , resourceMap , resources )
930
985
expectedError := ErrorForResourceWithResource (fmt .Errorf ("test error" ), deploymentObjID , deploymentObj )
931
- testutil .AssertEqual (t , expectedError , err , "expected processPruneEvent to error on apply %s" , event .ApplyFailed )
986
+ testutil .AssertEqual (t , expectedError , err , "expected processApplyEvent to error on apply %s" , event .ApplyFailed )
932
987
933
988
expectedCSE := v1beta1.ConfigSyncError {
934
989
Code : "2009" ,
@@ -945,8 +1000,10 @@ func TestProcessApplyEvent(t *testing.T) {
945
1000
}},
946
1001
}
947
1002
testutil .AssertEqual (t , expectedCSE , err .ToCSE (), "expected CSEs to match" )
1003
+ testutil .AssertEqual (t , 1 , len (resources .IgnoredObjects ()))
948
1004
949
- err = s .processApplyEvent (ctx , formApplyEvent (event .ApplySuccessful , testObj1 , nil ).ApplyEvent , syncStats .ApplyEvent , objStatusMap , unknownTypeResources , resourceMap )
1005
+ // process successful apply of testObj1
1006
+ err = s .processApplyEvent (ctx , formApplyEvent (event .ApplySuccessful , testObj1 , nil ).ApplyEvent , syncStats .ApplyEvent , objStatusMap , unknownTypeResources , resourceMap , resources )
950
1007
assert .Nil (t , err , "expected processApplyEvent NOT to error on apply %s" , event .ApplySuccessful )
951
1008
952
1009
expectedApplyStatus := stats .NewSyncStats ()
@@ -965,6 +1022,39 @@ func TestProcessApplyEvent(t *testing.T) {
965
1022
},
966
1023
}
967
1024
testutil .AssertEqual (t , expectedObjStatusMap , objStatusMap , "expected object status to match" )
1025
+ testutil .AssertEqual (t , 1 , len (resources .IgnoredObjects ()))
1026
+
1027
+ // process failed apply of testObj1 (ignore-mutation object)
1028
+ err = s .processApplyEvent (ctx , formApplyEvent (event .ApplyFailed , testObj1 , fmt .Errorf ("test error" )).ApplyEvent , syncStats .ApplyEvent , objStatusMap , unknownTypeResources , resourceMap , resources )
1029
+ expectedError = ErrorForResourceWithResource (fmt .Errorf ("test error" ), testObj1ID , testObj1 )
1030
+ testutil .AssertEqual (t , expectedError , err , "expected processApplyEvent to error on apply %s" , event .ApplyFailed )
1031
+
1032
+ expectedCSE = v1beta1.ConfigSyncError {
1033
+ Code : "2009" ,
1034
+ ErrorMessage : `KNV2009: failed to apply Test.configsync.test, test-namespace/test-1: test error
1035
+
1036
+ source: foo/test.yaml
1037
+ namespace: test-namespace
1038
+ metadata.name: test-1
1039
+ group: configsync.test
1040
+ version: v1
1041
+ kind: Test
1042
+
1043
+ For more information, see https://g.co/cloud/acm-errors#knv2009` ,
1044
+ Resources : []v1beta1.ResourceRef {{
1045
+ SourcePath : "foo/test.yaml" ,
1046
+ Name : "test-1" ,
1047
+ Namespace : "test-namespace" ,
1048
+ GVK : metav1.GroupVersionKind {
1049
+ Group : "configsync.test" ,
1050
+ Version : "v1" ,
1051
+ Kind : "Test" ,
1052
+ },
1053
+ }},
1054
+ }
1055
+ testutil .AssertEqual (t , expectedCSE , err .ToCSE (), "expected CSEs to match" )
1056
+ // The object should be removed from the IgnoredObjects cache when ApplyFailed
1057
+ testutil .AssertEqual (t , 0 , len (resources .IgnoredObjects ()))
968
1058
969
1059
// TODO: test handleMetrics on success
970
1060
// TODO: test unknownTypeResources on UnknownTypeError
0 commit comments