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.clientset doesn't behave properly on Apply #1184

Closed
miguescri opened this issue Nov 9, 2022 · 9 comments
Closed

fake.clientset doesn't behave properly on Apply #1184

miguescri opened this issue Nov 9, 2022 · 9 comments

Comments

@miguescri
Copy link

What I'm trying to do: use the fake.Clientset mock to unit test a declarative Apply operation.
Expected behavior: if the declarative resource doesn't refer to an existing object, create it; else, patch it.
Actual behavior: when the declarative resource doesn't refer to an existing object, returns error "Not found".

Minimal reproducible test:

package clientest_bug

import (
	"context"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	applyv1 "k8s.io/client-go/applyconfigurations/core/v1"
	"k8s.io/client-go/kubernetes/fake"
	"testing"
)

func TestClientsetApply(t *testing.T) {
	clientset := fake.NewSimpleClientset()
	namespaceClient := clientset.CoreV1().Namespaces()

	namespace := applyv1.Namespace("test-namespace")
	_, err := namespaceClient.Apply(context.TODO(), namespace, v1.ApplyOptions{FieldManager: "test"})
	if err != nil {
		t.Fatalf("Error: %s", err)
	}
}

go test output:

/usr/lib/go-1.18/bin/go tool test2json -t /tmp/GoLand/___go_test_clientest_bug.test -test.v -test.paniconexit0
=== RUN   TestClientsetApply
    main_test.go:18: Error: namespaces "test-namespace" not found
--- FAIL: TestClientsetApply (0.00s)

FAIL

Process finished with the exit code 1
@vietanhduong
Copy link

Not only apply, I think this happens with all other verbs and CRDs.

@therealmitchconnors
Copy link

I was able to work around this with a Deployment reactor. A smarter man than myself might combine this with a restmapper and scheme to work for any type until the client-go library is fixed.

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
},
)

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 19, 2023
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels May 19, 2023
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

@k8s-ci-robot k8s-ci-robot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2023
@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@paulopiriquito
Copy link

/reopen

@k8s-ci-robot
Copy link
Contributor

@paulopiriquito: You can't reopen an issue/PR unless you authored it or you are a collaborator.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@paulopiriquito
Copy link

/remove-lifecycle rotten

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

No branches or pull requests

6 participants