Commit Graph

67 Commits

Author SHA1 Message Date
Alvaro Aleman
81ecec406a FieldManagedObjectTracker: Fix to work with unstructured
Prior to this patch, this fails because the skipnonappliedfieldmanager
uses the `objectcreater` (aka `*runtime.Scheme`) to create a new object
for which it never sets the GVK. In the case of
`*unstructured.Unstructured`, the GVK can not be derived from the object
itself so the operation would subsequently fail [here][0] with an
```
Object 'Kind' is missing in 'unstructured object has no kind'
```

error. Fix this by explicitly setting the GVK in case of unstructured.

[0]: 02eb7d424a/staging/src/k8s.io/apimachinery/pkg/util/managedfields/internal/structuredmerge.go (L98)

Kubernetes-commit: dbdd6a3b4358d91a064de9c0f01d3050e606d553
2025-05-30 15:30:33 -04:00
Alvaro Aleman
a539a79dea ManagedFieldsObjectTracker: Reload scheme
In controller-runtime it is generally not expected to do any sort of
scheme registration if unstructured.Unstructured is used. To make this
work in the fakeclient, the fakeclient will register unstructured to
scheme if the scheme doesn't recognize the GVK.

This currently doesn't work with the `ManagedFieldsObjectTracker` as it
never reloads the scheme. This change makes it reload the scheme in an
inefficient but simple manner, which should be good enough for unit
tests.

Kubernetes-commit: 1cd71cbb14ecfd0ec6c5e8a57db6be00ecb8a4d1
2025-03-02 12:43:08 -05:00
Joe Betz
571b9f39e0 Reorganize scheme type converter into apimachinery utils
This removes a dependency from generated applyconfigurations to a testing
package. To do this, the type converter in the testing package has been
moved out to the apimachinery package and the utilities the converter
depend on have been reorganized.

Kubernetes-commit: 4821604f83a6f4764497879b666087ba7cb05060
2025-05-07 10:07:55 -04:00
Stephen Kitt
c475fe0910 Generify fake clientsets
This adds a generic implementation of a fake clientset, and uses it to
replace the template code in generated fake clientsets for the default
methods. The templates are preserved as-is (or as close as they can
be) for use in extensions, whether for resources or subresources.

Fake clientsets with no extensions are reduced to their main getter,
their specific struct, and their constructor. All method
implementations are provided by the generic implementation. The
dedicated struct is preserved to allow extensions and expansions to be
defined where necessary.

Instead of handling the variants (with/without list, apply) with a
complex sequence of if statements, build up an index into an array
containing the various declarations.

Similarly, instead of calling different action constructors for
namespaced and non-namespaced clientsets, assume the current behaviour
of non-namespaced action creation (equivalent to creating a namespaced
action with an empty namespace) and document that assumption in the
action implementation.

Signed-off-by: Stephen Kitt <skitt@redhat.com>

Kubernetes-commit: b0ce65df9b74d4dc72050840d5ad067596d7b822
2024-07-30 15:47:02 +02:00
Matthieu MOREL
9d7f486102 fix: enable expected-actual rule from testifylint in module k8s.io/client-go
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

Kubernetes-commit: 8286a69034d58e1ceafd6a6e5590bfcebd920b5b
2024-09-27 07:48:55 +02:00
Matthieu MOREL
84137c2466 fix: enable error-nil and nil-compare rules from testifylint in module k8s.io/client-go
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

Kubernetes-commit: 71ced25f44dce83297388ca8ac0aa60afb93b009
2024-09-27 07:28:52 +02:00
Joe Betz
86c96117c6 Use Fatalf for non-recoverable errors in test
Kubernetes-commit: 5784e58446b7dd2aca0af0e84e43395c9391ae03
2024-08-19 11:18:04 -04:00
Joe Betz
908d899011 Stamp fake client apply reuqests with name from action
Kubernetes-commit: 5f1c7ae6346f9028fef77b76e5555aa37f1fc39a
2024-08-15 21:24:28 -04:00
Joe Betz
dd940936ee Remove test dependency on swwagger.json to fix client-go repo
Kubernetes-commit: 1095af88e7d30198e13299ea90c5f81b95ec8a3b
2024-06-25 13:31:03 -04:00
Joe Betz
2c866525dd Add field tracker support to client fake fixtures
Kubernetes-commit: 75d6f024326dadc13807b8221bedd8da7924c2ba
2024-06-24 15:42:29 -04:00
Joe Betz
96f66e9159 Support options for all client fake actions
Kubernetes-commit: 599f03c72264d5149ae63b1c9eccb33e8b32e900
2024-06-24 15:41:38 -04:00
Stephen Kitt
16552d4656 Use canonical json-patch v4 import
The canonical import for json-patch v4 is
gopkg.in/evanphx/json-patch.v4 (see
https://github.com/evanphx/json-patch/blob/master/README.md#get-it for
reference).

Using the v4-specific path should also reduce the risk of unwanted v5
upgrade attempts, because they won't be offered as automated upgrades
by dependency upgrade management tools, and they won't happen through
indirect dependencies (see
https://github.com/kubernetes/kubernetes/pull/120327 for context).

Signed-off-by: Stephen Kitt <skitt@redhat.com>

Kubernetes-commit: 5300466a5c8988b479a151ceb77f49dd00065c83
2024-02-16 13:57:24 +01:00
Michal Wozniak
a191e58772 SSA to add pod failure conditions - ready for review
Kubernetes-commit: fea883687feafc597591e15773f8c6f491b35d08
2022-10-24 14:55:16 +02:00
LY-today
9ac8bfecbb fix: list pod err after an pod evicted
Signed-off-by: LY-today <724102053@qq.com>

Kubernetes-commit: f299494e7977a935511208eb91001508b4b629ce
2022-06-22 23:30:40 +08:00
Daniel Smith
14e253c7f8 remove unneeded references
Kubernetes-commit: 2831f9a343ec405efce60d09da482a654971018e
2022-03-17 18:35:00 +00:00
Wojciech Tyczyński
a806c6e4fd Remove selflink references in different testing-related files
Kubernetes-commit: 551790729f1d26d75c1d3fa1411e341eb367f9f3
2022-01-13 11:33:26 +01:00
Chun Chen
d478a5752d Pass DeleteOptions down to the Reactor
Co-authored-by: Mo Khan <theenjeru@gmail.com>

Kubernetes-commit: 621970476f8f06419716325d573580be7b3378d9
2021-06-17 18:06:47 +08:00
novahe
b7e5fce706 client-go: copying object to fix data race (#103148)
Kubernetes-commit: ce257266aa7f3f104e656b722310be32e95a9cb5
2021-07-05 21:55:55 +08:00
Shiming Zhang
87154808e1 Support subresource match
Signed-off-by: Shiming Zhang <wzshiming@foxmail.com>

Kubernetes-commit: 58833d652d59229d49f001256f6ade4a46ae53ca
2021-04-09 12:55:57 +08:00
Shiming Zhang
ce9fcb2fe0 Add test
Signed-off-by: Shiming Zhang <wzshiming@foxmail.com>

Kubernetes-commit: 2c9f02c3290ce7e8bcdc94bea7d96edf92834cd7
2021-04-09 13:07:46 +08:00
Mengjiao Liu
8cb6ac7646 Prevent data race condition in csi unit tests
Kubernetes-commit: 92d0f8e6cc3b3d40194759afa4c7f2880dd5ada4
2021-05-18 18:00:03 +08:00
Markus Thömmes
3579fb3012 Implement a FakeClient interface
Kubernetes-commit: cf26825e3d9b89526e13bb53f74354593477205c
2021-03-15 14:47:24 +01:00
Stephen Solka
166114c4a0 prefer NoError/Error over Nil/NotNil
Kubernetes-commit: 203679cc6105ea490e75af1efa83497b771d7d36
2020-07-18 20:23:35 -04:00
Quan Tian
faf5681d1c Improve fake clientset performance
The fake clientset used a slice to store each kind of objects, it's
quite slow to init the clientset with massive objects because it checked
existence of an object by traversing all objects before adding it, which
leads to O(n^2) time complexity. Also, the Create, Update, Get, Delete
methods needs to traverse all objects, which affects the time statistic
of code that calls them.

This patch changed to use a map to store each kind of objects, reduced
the time complexity of initializing clientset to O(n) and the Create,
Update, Get, Delete to O(1).

For example:
Before this patch, it took ~29s to init a clientset with 30000 Pods,
and 2~4ms to create and get an Pod.
After this patch, it took ~50ms to init a clientset with 30000 Pods,
and tens of µs to create and get an Pod.

Kubernetes-commit: 7e15e31e11e48a6db855e30ca9b07dbce3047577
2020-03-27 18:39:20 +08:00
Markus Thömmes
1133cbffc1 Allow Action's Matches function to specify a subresource.
In other parts of the system (notably in RBAC rules), the "resource/subresource" notation is common to specify an explicit subresource. This makes this notation available to tests that use the `Matches` function on client actions as well.

Backwards compatibility is kept by ignoring the `Subresource` field if no specific subresource is defined in the resource string itself.

Kubernetes-commit: 47277f281eb0e7d484555e4d210b0ddb42974793
2020-01-30 12:04:23 +01:00
Jan Chaloupka
f8b3113764 Require exact match when calling Get method within fake clientset
`Get` method within the fake clientset returns an object that would not be normally returned when using the real clientset. Reproducer:

```go
package main

import (
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
)

func main () {
	cm := &v1.ConfigMap{
		ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "cm"},
		}

	client := fake.NewSimpleClientset(cm)
	obj, err := client.CoreV1().ConfigMaps("").Get("", metav1.GetOptions{})
	if err != nil {
		panic(err)
	}
	fmt.Printf("obj: %#v\n", obj)
}
```

stored under `test.go` of `github.com/kubernetes/kubernetes` (master HEAD) root directory and ran:

```sh
$ go run test.go
obj: &v1.ConfigMap{TypeMeta:v1.TypeMeta{Kind:"", APIVersion:""}, ObjectMeta:v1.ObjectMeta{Name:"cm", GenerateName:"", Namespace:"kube-system", SelfLink:"", UID:"", ResourceVersion:"", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string(nil), Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Data:map[string]string(nil), BinaryData:map[string][]uint8(nil)}
```

As you can see fake clientset with a "test" configmap is created. When getting the object through the clientset back, I intentionally set the object name to an empty string. I would expect to get an error saying config map "" was not found. However, I get "test" configmap instead.

Reason for that is inside implementation of `filterByNamespaceAndName` private function:
```go
func filterByNamespaceAndName(objs []runtime.Object, ns, name string) ([]runtime.Object, error) {
	var res []runtime.Object

	for _, obj := range objs {
		acc, err := meta.Accessor(obj)
		if err != nil {
			return nil, err
		}
		if ns != "" && acc.GetNamespace() != ns {
			continue
		}
		if name != "" && acc.GetName() != name {
			continue
		}
		res = append(res, obj)
	}

	return res, nil
}
```

When `name` is empty, `name != "" && acc.GetName() != name` condition is false and thus `obj` is consider as a fit.

[1] https://github.com/kubernetes/client-go/blob/master/testing/fixture.go#L481-L493

Kubernetes-commit: d32c76fc03381784516c47cb1bf62ef932189afa
2019-09-17 17:52:28 +02:00
RainbowMango
532b6f676e Cleanup staticcheck issues for package in client-go.
Kubernetes-commit: c8c055b3163dd2661b3f9dd1b0ffb718a61aba24
2019-08-26 20:55:32 +08:00
Clayton Coleman
b83dc9a7d9 Fake ObjectReaction should handle PartialObjectMetadata special
When a client requests a PartialObjectMetadata returned from the
ObjectReaction type, if the object has a GVK set use that instead
of what the schema returns, since the majority of clients getting
partial object metadata will be doing so using the metadata client
or server side conversion.

Kubernetes-commit: baf091e9dbad00db39e246815f9d7a21d148044f
2019-04-03 12:12:11 -04:00
Clayton Coleman
58c2617e28 Calling PatchAction on typed objects should work correctly
Only Unstructured objects worked (because unstructured implicitly
clears the .Object map when Unmarshal is called). We must reset
obj before we attempt to unmarshal into it.

Kubernetes-commit: 3b599b383d32d80188fdc73adae1613cc167cbdd
2019-06-05 14:27:59 -04:00
Steve Kriss
f0c6576981 client-fake object tracker: support merge patch
Signed-off-by: Steve Kriss <krisss@vmware.com>

Kubernetes-commit: d425fe29bd808db54c59e3e0ecdedbe735b8f68b
2019-02-21 13:47:16 -07:00
James Munnelly
711082b69f Use a single deep copied object between all reactors in fake client
Kubernetes-commit: 41ebb22011cb79aed2241417249e824cdfcda5fc
2019-01-31 17:42:04 +00:00
Bouke van der Bijl
213a1e8b13 client-go/testing: properly handle Patch actions for missing Objects
Currently the fake client will return a default empty Object when a
Patch action is submitted on a missing Object. The correct behavior is to
instead propagate the NotFound error.

Kubernetes-commit: 96d0588440a96c5eba8b3ba0810563ad1e1a08b6
2018-11-09 19:25:02 +00:00
João Taveira Araújo
93c815ca99 Fix duped watch in client-go/testing.
This commit fixes a bug in the client-go/testing fixture whereby a
watcher would fire twice for objects with no namespace.

Kubernetes-commit: bd268c9971efd4db46074a80e52ce765583e0787
2018-11-19 09:50:17 -08:00
Ville Aikas
41406bf985 Add support for JSON patch in fake client
Kubernetes-commit: a363b153851326ece7f81f4c1ae0a1ab8700a209
2018-10-02 13:30:52 +00:00
Minhan Xia
6f353b5328 add Patch support in fake kubeClient
Kubernetes-commit: 8b3b4e4deabe4cf922eee752df2fad189b2c1471
2018-04-11 11:37:30 -07:00
David Eads
77541e5cb7 add subresource support for the dynamic client
Kubernetes-commit: 82e32d2a3260807237a69bc40fb371db640d26fc
2018-05-09 12:52:12 -04:00
Josh Horwitz
64101cbe6a Fixes fake client generation for non-namespaced subresources
Kubernetes-commit: 0f62a8cddac0a48506fc1b93f37da32530fd2358
2018-02-26 14:03:11 -05:00
hangaoshuai
e39ef65572 catch err when Watch testResource failed in func TestWatchCallNonNamespace
Kubernetes-commit: c63e22eea56c2ba3c24a284505fbe869b7678e14
2018-03-20 16:35:41 +08:00
David Eads
f8d192aa24 deep copy fake client actions to avoid accidental mutation
Kubernetes-commit: 9ef99d189f2e454a293b8c343fee173c876ed21f
2018-03-02 09:19:52 -05:00
Grant Rodgers
949db79a1d Use RaceFreeFakeWatcher in ObjectTracker
The FakeWatcher allows sends on the result channel after it's closed,
for example calling Stop() then Add() will panic. RaceFreeFakeWatcher
checks whether the watcher is stopped before attempting to send. It also
panics instead of blocking when the result channel is full.

Kubernetes-commit: b84ad8828b6ffe0dd289f69e395968eabb9fbeaa
2018-03-14 11:38:19 -07:00
yue9944882
d89f1e4c08 remove redundant fake discovery code
Kubernetes-commit: 38243460fa272a5503b7c1b43655d09caa65dd1c
2018-02-27 17:34:08 +08:00
Kubernetes Publisher
fff8c3d73e sync: initially remove files BUILD */BUILD BUILD.bazel */BUILD.bazel 2018-03-15 09:19:38 +00:00
Jeff Grafton
fca8bb2928 Autogenerated: hack/update-bazel.sh
Kubernetes-commit: ef56a8d6bb3800ab7803713eafc4191e8202ad6e
2018-02-16 13:43:01 -08:00
yue9944882
af8ed43b01 fix(fakeclient): write event to watch channel on add/update/delete
fix races with watch call

add test for non-namespace resource watch

add matching for all-namespace-watch

fix delete namespace watch & restrict test

fix multiple invocation on same resource & namespace

add descriptive doc for tracker.watchers

Kubernetes-commit: f57cc0b22d282bc8fe68faf91529e7175bc3918a
2017-12-21 16:50:16 +08:00
Jeff Grafton
c92755ea3b update BUILD files
Kubernetes-commit: aee5f457dbfd70c2d15c33e392dce6a3ca710116
2017-10-12 13:52:10 -07:00
Dr. Stefan Schimanski
0cfc379ff8 apimachinery: mechanical removal of ObjectCopier plumbing
Kubernetes-commit: 509df603b18d356777176953e5d160b6f3d0bba9
2017-10-06 13:30:12 +02:00
Dr. Stefan Schimanski
7cdaec7d07 client-go: simplify deepcopy calls
Kubernetes-commit: ed423054ba0089a6d58fb0e048fe1acfd966f0d7
2017-08-15 14:15:58 +02:00
Michal Fojtik
bf85a9d89b add missing sub-resources test actions
Kubernetes-commit: c026b62d19d83d4f68235d1bd039a469e87d215d
2017-09-04 13:49:16 +00:00
Lucas Käldström
1bc6d1247e kubeadm: Adds dry-run support for kubeadm using the '--dry-run' option
Kubernetes-commit: 0bf84aa182449ab51cbb5f819da3fbcad19690a2
2017-08-29 12:52:28 +00:00
Jeff Grafton
5da217e5c4 Use buildozer to delete licenses() rules except under third_party/
Kubernetes-commit: a7f49c906df816123e7d4ccbd4cebab411519465
2017-08-29 12:51:55 +00:00