Skip to content
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

Fake Client can't delete resources from unstructured list #2624

Closed
aclevername opened this issue Dec 18, 2023 · 1 comment
Closed

Fake Client can't delete resources from unstructured list #2624

aclevername opened this issue Dec 18, 2023 · 1 comment

Comments

@aclevername
Copy link

Context

In our code we use a generic function for deletion:

func deleteAllResourcesWithKindMatchingLabel(gvk schema.GroupVersionKind, resourceLabels map[string]string) (bool, error) 

That can be used to either delete CRDs or core resources like Jobs. When migrating some tests over from using envtest to using the fake client we ran into this issue:

"error": "Object 'apiVersion' is missing in 'unstructured object has no version'"

Reproduced with the following code fails:

// You can edit this code!
// Click here and start typing.
package main

import (
	"context"

	batchv1 "k8s.io/api/batch/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/kubernetes/scheme"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func main() {
	fakeK8sClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithStatusSubresource().Build()
	job := &batchv1.Job{
		TypeMeta: metav1.TypeMeta{
			Kind: "Job",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test",
			Namespace: "default",
		},
	}

	err := fakeK8sClient.Create(context.TODO(), job)
	if err != nil {
		panic(err)
	}

	resourceList := &unstructured.UnstructuredList{}
	resourceList.SetGroupVersionKind(schema.GroupVersionKind{
		Group:   batchv1.SchemeGroupVersion.Group,
		Version: batchv1.SchemeGroupVersion.Version,
		Kind:    "Job",
	})

	err = fakeK8sClient.List(context.TODO(), resourceList)
	if err != nil {
		panic(err)
	}

	for _, resource := range resourceList.Items {
		err := fakeK8sClient.Delete(context.TODO(), &resource)
		if err != nil {
			//fails here
			panic(err)
		}
	}
}
go run main.go
panic: Object 'apiVersion' is missing in 'unstructured object has no version'

goroutine 1 [running]:
main.main()
        /Users/jake/workspace/foo/main.go:48 +0x288
exit status 2

I thought #1662 would of resolved this, but it appears not. Any help would be great, thanks!

@aclevername
Copy link
Author

Removing the following from the resource fixed it

		TypeMeta: metav1.TypeMeta{
			Kind: "Job",
		},

I'm assuming the specifying of TypeMeta is making it take precedence over inferring it from the struct, which was causing the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant