Created
January 22, 2023 14:41
-
-
Save 196Ikuchil/3143cab1ca0e86a0e093983fc8a524ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f := fake.NewSimpleClientset(objects...) | |
f.PrependReactor( | |
"patch", | |
"deployments", | |
func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) { | |
pa := action.(clienttesting.PatchAction) | |
if pa.GetPatchType() == types.ApplyPatchType { | |
// Apply patches are supposed to upsert, but fake client fails if the object doesn't exist, | |
// if an apply patch occurs for a deployment that doesn't yet exist, create it. | |
// However, we already hold the fakeclient lock, so we can't use the front door. | |
rfunc := clienttesting.ObjectReaction(f.Tracker()) | |
_, obj, err := rfunc( | |
clienttesting.NewGetAction(pa.GetResource(), pa.GetNamespace(), pa.GetName()), | |
) | |
if kerrors.IsNotFound(err) || obj == nil { | |
_, _, _ = rfunc( | |
clienttesting.NewCreateAction( | |
pa.GetResource(), | |
pa.GetNamespace(), | |
&appsv1.Deployment{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: pa.GetName(), | |
Namespace: pa.GetNamespace(), | |
}, | |
}, | |
), | |
) | |
} | |
return rfunc(clienttesting.NewPatchAction( | |
pa.GetResource(), | |
pa.GetNamespace(), | |
pa.GetName(), | |
types.StrategicMergePatchType, | |
pa.GetPatch())) | |
} | |
return false, nil, nil | |
}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment