kubernetes / autoscaler

Autoscaling components for Kubernetes
Apache License 2.0
8.04k stars 3.96k forks source link

Can't unit test with fake VPA client #5199

Closed jcstanaway closed 1 year ago

jcstanaway commented 2 years ago

Which component are you using?:

vertical-pod-autoscaler

What version of the component are you using?:

Component version: 0.12.0

What k8s version are you using (kubectl version)?:

kubectl version Output
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.1", GitCommit:"86ec240af8cbd1b60bcc4c03c20da9b98005b92e", GitTreeState:"clean", BuildDate:"2021-12-16T11:33:37Z", GoVersion:"go1.17.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.11", GitCommit:"5824e3251d294d324320db85bf63a53eb0767af2", GitTreeState:"clean", BuildDate:"2022-06-16T05:33:55Z", GoVersion:"go1.16.15", Compiler:"gc", Platform:"linux/amd64"}

What environment is this in?: n/a

What did you expect to happen?:

I have a golang app that creates a VPA client and retrieves the VPA objects per namespace. This is working just fine using k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned and instantiating it as:

vpaClient, err := vpa_clientset.NewForConfig(clientCfg)

I attempt to create a unit test and use k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned/fake and instantiate is as:

fakeVpaCs := fake_vpa.NewSimpleClientset()  // Initial unit test scenario has no VPAs defined.

When calling a function under test and passing fakeVpaCs to that function which is expecting a parameter of type k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned.Clientset I expect there to be no type errors.

The function under test (in the main app) has the following signature:

import (
    vpa_clientset "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned"
    "k8s.io/client-go/kubernetes"
)

func MyFunc(k8sClient kubernetes.Interface, vpaClient *vpa_clientset.Clientset) { ... }

What happened instead?:

I'm getting:

pkg/my_pkg/myfunc_test.go:26:23: cannot use fakeVpaCs (variable of type *"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned/fake".Clientset) as *versioned.Clientset value in argument to MyFunc

I'm also using the regular fake k8s client (k8s.io/client-go/kubernetes/fake) without issue in other unit tests.

Due to this error, I'm unable to create any unit tests where I need to stub out the VPA client.

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

From the package documentation for k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned/fake:

type Clientset

type Clientset struct { testing.Fake // contains filtered or unexported fields }

Clientset implements clientset.Interface. Meant to be embedded into a struct to get a default implementation. This makes faking out just the method you want to test easier.

func NewSimpleClientset

func NewSimpleClientset(objects ...runtime.Object) *Clientset

NewSimpleClientset returns a clientset that will respond with the provided objects. It's backed by a very simple object tracker that processes creates, updates and deletions as-is, without applying any validations and/or defaults. It shouldn't be considered a replacement for a real clientset and is mostly useful in simple unit tests.

k8s-triage-robot commented 1 year ago

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

This bot triages issues and PRs according to the following rules:

You can:

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

/lifecycle stale

k8s-triage-robot commented 1 year ago

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

This bot triages issues and PRs according to the following rules:

You can:

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

/lifecycle rotten

k8s-triage-robot commented 1 year ago

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:

You can:

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

/close not-planned

k8s-ci-robot commented 1 year ago

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

In response to [this](https://github.com/kubernetes/autoscaler/issues/5199#issuecomment-1445204754): >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][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close not-planned > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.