Commit Graph

32 Commits

Author SHA1 Message Date
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
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
2c866525dd Add field tracker support to client fake fixtures
Kubernetes-commit: 75d6f024326dadc13807b8221bedd8da7924c2ba
2024-06-24 15:42:29 -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
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
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
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
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
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
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
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
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
Chao Xu
85c50f4ccb remove registry from testing/fixture.go; update client-gen to not use
registry in the generated clients

Kubernetes-commit: bbb94e42c108840c540c5667f8da97c33b81d84a
2017-05-04 20:28:29 +00:00
Kubernetes Publisher
fb6075f2e0 published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is abed7461722c195f8c77b4c502743e012d19d095
2017-01-27 15:19:42 +00:00
Kubernetes Publisher
0eb5431cb7 published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is 8d5227bb2e05e01031ace81fa6611a13f598278e
2017-01-21 15:19:42 +00:00
Kubernetes Publisher
8fa0506b26 published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is d9467519245c3858ac58c5fa91ec9e5b16c1f507
2017-01-18 15:19:42 +00:00
Kubernetes Publisher
204f12b1f3 published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is 616038db1b0d1e852b4a3d10c8c512a052f91fba
2017-01-14 15:19:47 +00:00
Kubernetes Publisher
5fe6fc56cb published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is 5b2823adb9a8777f7819884fa79a6e1daa26b9c2
2016-12-17 15:19:41 +00:00
Kubernetes Publisher
124670e99d published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is 81d788dd6e0748e5d53a62c16d933c5f7a0718af
2016-12-04 11:39:55 +00:00
Kubernetes Publisher
5d8c36c93c published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is 124fb610dcbd445fa710da67508ac6d5b822f61d
2016-11-24 08:15:51 +00:00
Kubernetes Publisher
75399f68c8 published by bot
(https://github.com/kubernetes/contrib/tree/master/mungegithub)

copied from https://github.com/kubernetes/kubernetes.git, branch master,
last commit is e56cfc5322138aa23e6418ee30a6ab54c7c6fe8c
2016-10-21 04:44:19 +00:00
Chao Xu
a6d206121d remove the top-level folders for versions
remove scripts
2016-10-19 14:34:19 -07:00