kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.78k stars 2.9k forks source link

fake client patch does not work if object is not found #1323

Closed mishindm closed 1 month ago

mishindm commented 6 months ago

We have the error after Get returning "object not found" error https://github.com/kubernetes/client-go/blob/fb8b7346aacefea5ee2ab2e234afc4451c90c435/testing/fixture.go#L146

However k8s will create a new object if the patch type == ApplyPatchType

mishindm commented 6 months ago

A basic implementation (obviously without necessary checks) but working for us:

                        if err != nil {
                                if errors.IsNotFound(err) && action.GetSubresource() == "" {
                                        switch action.GetPatchType() {
                                        case types.ApplyPatchType:
                                                body := map[string]interface{}{}
                                                err := json.Unmarshal(action.GetPatch(), &body)
                                                if err != nil {
                                                        return true, nil, err
                                                }
                                                objcopy := &unstructured.Unstructured{
                                                        Object: body,
                                                }
                                                err = tracker.Create(gvr, objcopy, ns)
                                                if err != nil {
                                                        return true, nil, err
                                                }
                                                obj, err = tracker.Get(gvr, ns, action.GetName())
                                                return true, obj, err
                                        }
k8s-triage-robot commented 3 months ago

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

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

You can:

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

/lifecycle stale

k8s-triage-robot commented 2 months ago

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:

You can:

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

/lifecycle rotten

k8s-triage-robot commented 1 month 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 month ago

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

In response to [this](https://github.com/kubernetes/client-go/issues/1323#issuecomment-2117746699): >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-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.