Issue tracker and mirror of kubectl code
Go to file
Jan Chaloupka 6fd1ff2fb0 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
.github Adds staging directory for kubectl code 2019-05-29 23:31:23 -07:00
Godeps Merge pull request #83987 from wenjiaswe/etcd_client_3_4_2 2019-10-25 23:30:23 +00:00
docs fixed typos in kubectl book 2019-08-15 15:44:19 +09:00
images Adds kubectl logo images 2019-06-21 11:34:42 -07:00
pkg Require exact match when calling Get method within fake clientset 2019-09-17 17:52:28 +02:00
test/data pkg/apis/policy: Fix "informatio" -> "information" comment typo 2019-10-08 12:27:50 -07:00
CONTRIBUTING.md Adds staging directory for kubectl code 2019-05-29 23:31:23 -07:00
LICENSE Adds staging directory for kubectl code 2019-05-29 23:31:23 -07:00
OWNERS Adds staging directory for kubectl code 2019-05-29 23:31:23 -07:00
README.md Adds staging directory for kubectl code 2019-05-29 23:31:23 -07:00
SECURITY_CONTACTS Update security contacts for kubectl 2019-09-03 10:15:43 +02:00
code-of-conduct.md Adds staging directory for kubectl code 2019-05-29 23:31:23 -07:00
go.mod Merge pull request #83987 from wenjiaswe/etcd_client_3_4_2 2019-10-25 23:30:23 +00:00
go.sum Merge pull request #83987 from wenjiaswe/etcd_client_3_4_2 2019-10-25 23:30:23 +00:00

README.md

Kubectl

kubectl logo

Build Status GoDoc

The k8s.io/kubectl repo is used to track issues for the kubectl cli distributed with k8s.io/kubernetes. It also contains packages intended for use by client programs. E.g. these packages are vendored into k8s.io/kubernetes for use in the kubectl cli client. That client will eventually move here too.

Contribution Requirements

  • Full unit-test coverage.

  • Go tools compliant (go get, go test, etc.). It needs to be vendorable somewhere else.

  • No dependence on k8s.io/kubernetes. Dependence on other repositories is fine.

  • Code must be usefully commented. Not only for developers on the project, but also for external users of these packages.

  • When reviewing PRs, you are encouraged to use Golang's code review comments page.

  • Packages in this repository should aspire to implement sensible, small interfaces and import a limited set of dependencies.

Dependencies

Dependencies are managed using dep. Please refer to its documentation if needed.

Community, discussion, contribution, and support

See this document for how to reach the maintainers of this project.

Code of conduct

Participation in the Kubernetes community is governed by the Kubernetes Code of Conduct.