Skip to content

Instantly share code, notes, and snippets.

@196Ikuchil
Created January 22, 2023 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 196Ikuchil/3143cab1ca0e86a0e093983fc8a524ab to your computer and use it in GitHub Desktop.
Save 196Ikuchil/3143cab1ca0e86a0e093983fc8a524ab to your computer and use it in GitHub Desktop.
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