-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fake client does not preserve TypeMeta for PartialObjectMetadata after request #2923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hm, seems reasonable to me that PartialObjectMeta keeps kind set. The regular client (cached & apireader) does that, I assume? (Sort of unrelated, but also wondering how fake client behaves with Unstructured) |
(cc @alvaroaleman) |
Yes (at least this is what my brief testing shows). The following test will pass if there is a package tt_test
import (
"context"
"os"
"testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func TestGetPreserveTypeMetaNotFakeClient(t *testing.T) {
kubeconfigBytes, err := os.ReadFile(os.Getenv("KUBECONFIG"))
if err != nil {
t.Fatalf("cannot read KUBECONFIG file: %v", err)
}
cfg, err := clientcmd.RESTConfigFromKubeConfig(kubeconfigBytes)
if err != nil {
t.Fatalf("cannot create rest config from kubeconfig: %v", err)
}
c, err := client.New(cfg, client.Options{})
if err != nil {
t.Fatalf("cannot create client config rest config: %v", err)
}
partialObjMeta := &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
}
partialObjMeta.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))
ctx := context.TODO()
err = c.Get(ctx, client.ObjectKeyFromObject(partialObjMeta), partialObjMeta)
if err != nil {
t.Errorf("get was not successful: %v", err)
}
if partialObjMeta.Kind != "Secret" {
t.Errorf("got kind: %s, want Secret", partialObjMeta.Kind)
}
} |
The problematic part of the code is here controller-runtime/pkg/client/fake/client.go Lines 524 to 529 in b901db1
It creates a new value of whatever type is passed in (in the test case a It could probably be fixed fairly minimally by changing controller-runtime/pkg/client/fake/client.go Line 511 in b901db1
obj being a PartialObjectMetadata .
If it's either a |
Yeah, it needs to be preserved for unstructured and objectMeta types. Please feel free to submit a fix. /kind bug |
Hi folks,
I have code that uses
PartialObjectMetadata
to potentially send two patch requests for one specific object. The second request fails when using the fake client in test suite because after the firstPatch
the object loses itsTypeMeta
even though it was explicitly set. The same requests succeed when ran through the regular client. I managed to reproduce in a minimal test and it seems that the same is true at least forGet
requests as well. Here is a reproducer:I am not sure if this is intended behaviour but I would expect that
partialObjMeta.Kind == "Secret"
at the end of the test.The text was updated successfully, but these errors were encountered: