@@ -1851,3 +1851,47 @@ func TestMarkAsDeleting(t *testing.T) {
1851
1851
})
1852
1852
}
1853
1853
}
1854
+
1855
+ type staleGuaranteedUpdateStorage struct {
1856
+ storage.Interface
1857
+ cachedObj runtime.Object
1858
+ }
1859
+
1860
+ // GuaranteedUpdate overwrites the method with one that always suggests the cachedObj.
1861
+ func (s * staleGuaranteedUpdateStorage ) GuaranteedUpdate (
1862
+ ctx context.Context , key string , ptrToType runtime.Object , ignoreNotFound bool ,
1863
+ preconditions * storage.Preconditions , tryUpdate storage.UpdateFunc , _ ... runtime.Object ) error {
1864
+ return s .Interface .GuaranteedUpdate (ctx , key , ptrToType , ignoreNotFound , preconditions , tryUpdate , s .cachedObj )
1865
+ }
1866
+
1867
+ func TestDeleteWithCachedObject (t * testing.T ) {
1868
+ podName := "foo"
1869
+ podWithFinalizer := & example.Pod {
1870
+ ObjectMeta : metav1.ObjectMeta {Name : podName , Finalizers : []string {"foo.com/x" }},
1871
+ Spec : example.PodSpec {NodeName : "machine" },
1872
+ }
1873
+ podWithNoFinalizer := & example.Pod {
1874
+ ObjectMeta : metav1.ObjectMeta {Name : podName },
1875
+ Spec : example.PodSpec {NodeName : "machine" },
1876
+ }
1877
+ ctx := genericapirequest .WithNamespace (genericapirequest .NewContext (), "test" )
1878
+ destroyFunc , registry := newTestGenericStoreRegistry (t , scheme , false )
1879
+ defer destroyFunc ()
1880
+ // cached object does not have any finalizer.
1881
+ registry .Storage .Storage = & staleGuaranteedUpdateStorage {Interface : registry .Storage .Storage , cachedObj : podWithNoFinalizer }
1882
+ // created object with pending finalizer.
1883
+ _ , err := registry .Create (ctx , podWithFinalizer , rest .ValidateAllObjectFunc , & metav1.CreateOptions {})
1884
+ if err != nil {
1885
+ t .Fatal (err )
1886
+ }
1887
+ // The object shouldn't be deleted, because the persisted object has pending finalizers.
1888
+ _ , _ , err = registry .Delete (ctx , podName , nil )
1889
+ if err != nil {
1890
+ t .Fatal (err )
1891
+ }
1892
+ // The object should still be there
1893
+ _ , err = registry .Get (ctx , podName , & metav1.GetOptions {})
1894
+ if err != nil {
1895
+ t .Fatal (err )
1896
+ }
1897
+ }
0 commit comments