mirror of
https://github.com/kubernetes/client-go.git
synced 2026-06-20 00:35:39 +00:00
Compare commits
12 Commits
v0.20.10-r
...
v0.20.15-r
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3a17ade26 | ||
|
|
7e4462aced | ||
|
|
2cd3643e6a | ||
|
|
3fc097df6b | ||
|
|
68daa7cdd6 | ||
|
|
2f46a8a991 | ||
|
|
180e34d83f | ||
|
|
f1c0c4f471 | ||
|
|
f93fe2d9cf | ||
|
|
6a1c1a666c | ||
|
|
c8ecd0ac2a | ||
|
|
1930bf4db5 |
@@ -35,7 +35,35 @@ import (
|
||||
)
|
||||
|
||||
func NewSimpleDynamicClient(scheme *runtime.Scheme, objects ...runtime.Object) *FakeDynamicClient {
|
||||
return NewSimpleDynamicClientWithCustomListKinds(scheme, nil, objects...)
|
||||
unstructuredScheme := runtime.NewScheme()
|
||||
for gvk := range scheme.AllKnownTypes() {
|
||||
if unstructuredScheme.Recognizes(gvk) {
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(gvk.Kind, "List") {
|
||||
unstructuredScheme.AddKnownTypeWithName(gvk, &unstructured.UnstructuredList{})
|
||||
continue
|
||||
}
|
||||
unstructuredScheme.AddKnownTypeWithName(gvk, &unstructured.Unstructured{})
|
||||
}
|
||||
|
||||
objects, err := convertObjectsToUnstructured(scheme, objects)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, obj := range objects {
|
||||
gvk := obj.GetObjectKind().GroupVersionKind()
|
||||
if !unstructuredScheme.Recognizes(gvk) {
|
||||
unstructuredScheme.AddKnownTypeWithName(gvk, &unstructured.Unstructured{})
|
||||
}
|
||||
gvk.Kind += "List"
|
||||
if !unstructuredScheme.Recognizes(gvk) {
|
||||
unstructuredScheme.AddKnownTypeWithName(gvk, &unstructured.UnstructuredList{})
|
||||
}
|
||||
}
|
||||
|
||||
return NewSimpleDynamicClientWithCustomListKinds(unstructuredScheme, nil, objects...)
|
||||
}
|
||||
|
||||
// NewSimpleDynamicClientWithCustomListKinds try not to use this. In general you want to have the scheme have the List types registered
|
||||
@@ -417,3 +445,41 @@ func (c *dynamicResourceClient) Patch(ctx context.Context, name string, pt types
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func convertObjectsToUnstructured(s *runtime.Scheme, objs []runtime.Object) ([]runtime.Object, error) {
|
||||
ul := make([]runtime.Object, 0, len(objs))
|
||||
|
||||
for _, obj := range objs {
|
||||
u, err := convertToUnstructured(s, obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ul = append(ul, u)
|
||||
}
|
||||
return ul, nil
|
||||
}
|
||||
|
||||
func convertToUnstructured(s *runtime.Scheme, obj runtime.Object) (runtime.Object, error) {
|
||||
var (
|
||||
err error
|
||||
u unstructured.Unstructured
|
||||
)
|
||||
|
||||
u.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert to unstructured: %w", err)
|
||||
}
|
||||
|
||||
gvk := u.GroupVersionKind()
|
||||
if gvk.Group == "" || gvk.Kind == "" {
|
||||
gvks, _, err := s.ObjectKinds(obj)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert to unstructured - unable to get GVK %w", err)
|
||||
}
|
||||
apiv, k := gvks[0].ToAPIVersionAndKind()
|
||||
u.SetAPIVersion(apiv)
|
||||
u.SetKind(k)
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
@@ -303,3 +304,166 @@ func TestPatch(t *testing.T) {
|
||||
t.Run(tc.name, tc.runner)
|
||||
}
|
||||
}
|
||||
|
||||
// This test ensures list works when the fake dynamic client is seeded with a typed scheme and
|
||||
// unstructured type fixtures
|
||||
func TestListWithUnstructuredObjectsAndTypedScheme(t *testing.T) {
|
||||
gvr := schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: testResource}
|
||||
gvk := gvr.GroupVersion().WithKind(testKind)
|
||||
|
||||
listGVK := gvk
|
||||
listGVK.Kind += "List"
|
||||
|
||||
u := unstructured.Unstructured{}
|
||||
u.SetGroupVersionKind(gvk)
|
||||
u.SetName("name")
|
||||
u.SetNamespace("namespace")
|
||||
|
||||
typedScheme := runtime.NewScheme()
|
||||
typedScheme.AddKnownTypeWithName(gvk, &mockResource{})
|
||||
typedScheme.AddKnownTypeWithName(listGVK, &mockResourceList{})
|
||||
|
||||
client := NewSimpleDynamicClient(typedScheme, &u)
|
||||
list, err := client.Resource(gvr).Namespace("namespace").List(context.Background(), metav1.ListOptions{})
|
||||
|
||||
if err != nil {
|
||||
t.Error("error listing", err)
|
||||
}
|
||||
|
||||
expectedList := &unstructured.UnstructuredList{}
|
||||
expectedList.SetGroupVersionKind(listGVK)
|
||||
expectedList.SetResourceVersion("") // by product of the fake setting resource version
|
||||
expectedList.Items = append(expectedList.Items, u)
|
||||
|
||||
if diff := cmp.Diff(expectedList, list); diff != "" {
|
||||
t.Fatal("unexpected diff (-want, +got): ", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListWithNoFixturesAndTypedScheme(t *testing.T) {
|
||||
gvr := schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: testResource}
|
||||
gvk := gvr.GroupVersion().WithKind(testKind)
|
||||
|
||||
listGVK := gvk
|
||||
listGVK.Kind += "List"
|
||||
|
||||
typedScheme := runtime.NewScheme()
|
||||
typedScheme.AddKnownTypeWithName(gvk, &mockResource{})
|
||||
typedScheme.AddKnownTypeWithName(listGVK, &mockResourceList{})
|
||||
|
||||
client := NewSimpleDynamicClient(typedScheme)
|
||||
list, err := client.Resource(gvr).Namespace("namespace").List(context.Background(), metav1.ListOptions{})
|
||||
|
||||
if err != nil {
|
||||
t.Error("error listing", err)
|
||||
}
|
||||
|
||||
expectedList := &unstructured.UnstructuredList{}
|
||||
expectedList.SetGroupVersionKind(listGVK)
|
||||
expectedList.SetResourceVersion("") // by product of the fake setting resource version
|
||||
|
||||
if diff := cmp.Diff(expectedList, list); diff != "" {
|
||||
t.Fatal("unexpected diff (-want, +got): ", diff)
|
||||
}
|
||||
}
|
||||
|
||||
// This test ensures list works when the dynamic client is seeded with an empty scheme and
|
||||
// unstructured typed fixtures
|
||||
func TestListWithNoScheme(t *testing.T) {
|
||||
gvr := schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: testResource}
|
||||
gvk := gvr.GroupVersion().WithKind(testKind)
|
||||
|
||||
listGVK := gvk
|
||||
listGVK.Kind += "List"
|
||||
|
||||
u := unstructured.Unstructured{}
|
||||
u.SetGroupVersionKind(gvk)
|
||||
u.SetName("name")
|
||||
u.SetNamespace("namespace")
|
||||
|
||||
emptyScheme := runtime.NewScheme()
|
||||
|
||||
client := NewSimpleDynamicClient(emptyScheme, &u)
|
||||
list, err := client.Resource(gvr).Namespace("namespace").List(context.Background(), metav1.ListOptions{})
|
||||
|
||||
if err != nil {
|
||||
t.Error("error listing", err)
|
||||
}
|
||||
|
||||
expectedList := &unstructured.UnstructuredList{}
|
||||
expectedList.SetGroupVersionKind(listGVK)
|
||||
expectedList.SetResourceVersion("") // by product of the fake setting resource version
|
||||
expectedList.Items = append(expectedList.Items, u)
|
||||
|
||||
if diff := cmp.Diff(expectedList, list); diff != "" {
|
||||
t.Fatal("unexpected diff (-want, +got): ", diff)
|
||||
}
|
||||
}
|
||||
|
||||
// This test ensures list works when the dynamic client is seeded with an empty scheme and
|
||||
// unstructured typed fixtures
|
||||
func TestListWithTypedFixtures(t *testing.T) {
|
||||
gvr := schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: testResource}
|
||||
gvk := gvr.GroupVersion().WithKind(testKind)
|
||||
|
||||
listGVK := gvk
|
||||
listGVK.Kind += "List"
|
||||
|
||||
r := mockResource{}
|
||||
r.SetGroupVersionKind(gvk)
|
||||
r.SetName("name")
|
||||
r.SetNamespace("namespace")
|
||||
|
||||
u := unstructured.Unstructured{}
|
||||
u.SetGroupVersionKind(r.GetObjectKind().GroupVersionKind())
|
||||
u.SetName(r.GetName())
|
||||
u.SetNamespace(r.GetNamespace())
|
||||
// Needed see: https://github.com/kubernetes/kubernetes/issues/67610
|
||||
unstructured.SetNestedField(u.Object, nil, "metadata", "creationTimestamp")
|
||||
|
||||
typedScheme := runtime.NewScheme()
|
||||
typedScheme.AddKnownTypeWithName(gvk, &mockResource{})
|
||||
typedScheme.AddKnownTypeWithName(listGVK, &mockResourceList{})
|
||||
|
||||
client := NewSimpleDynamicClient(typedScheme, &r)
|
||||
list, err := client.Resource(gvr).Namespace("namespace").List(context.Background(), metav1.ListOptions{})
|
||||
|
||||
if err != nil {
|
||||
t.Error("error listing", err)
|
||||
}
|
||||
|
||||
expectedList := &unstructured.UnstructuredList{}
|
||||
expectedList.SetGroupVersionKind(listGVK)
|
||||
expectedList.SetResourceVersion("") // by product of the fake setting resource version
|
||||
expectedList.Items = []unstructured.Unstructured{u}
|
||||
|
||||
if diff := cmp.Diff(expectedList, list); diff != "" {
|
||||
t.Fatal("unexpected diff (-want, +got): ", diff)
|
||||
}
|
||||
}
|
||||
|
||||
type (
|
||||
mockResource struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ObjectMeta `json:"metadata"`
|
||||
}
|
||||
mockResourceList struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ListMeta `json:"metadata"`
|
||||
|
||||
Items []mockResource
|
||||
}
|
||||
)
|
||||
|
||||
func (l *mockResourceList) DeepCopyObject() runtime.Object {
|
||||
o := *l
|
||||
return &o
|
||||
}
|
||||
|
||||
func (r *mockResource) DeepCopyObject() runtime.Object {
|
||||
o := *r
|
||||
return &o
|
||||
}
|
||||
|
||||
var _ runtime.Object = (*mockResource)(nil)
|
||||
var _ runtime.Object = (*mockResourceList)(nil)
|
||||
|
||||
8
go.mod
8
go.mod
@@ -26,14 +26,14 @@ require (
|
||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
|
||||
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
|
||||
k8s.io/api v0.20.10-rc.0
|
||||
k8s.io/apimachinery v0.20.10-rc.0
|
||||
k8s.io/api v0.20.15-rc.0
|
||||
k8s.io/apimachinery v0.20.15-rc.0
|
||||
k8s.io/klog/v2 v2.4.0
|
||||
k8s.io/utils v0.0.0-20201110183641-67b214c5f920
|
||||
sigs.k8s.io/yaml v1.2.0
|
||||
)
|
||||
|
||||
replace (
|
||||
k8s.io/api => k8s.io/api v0.20.10-rc.0
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.20.10-rc.0
|
||||
k8s.io/api => k8s.io/api v0.20.15-rc.0
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.20.15-rc.0
|
||||
)
|
||||
|
||||
16
go.sum
16
go.sum
@@ -305,8 +305,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
|
||||
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
@@ -425,16 +425,16 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
k8s.io/api v0.20.10-rc.0 h1:Dzywi1gDFY6aHfFAKXMUxwf6kofn7sAZk9TyH3cjkiQ=
|
||||
k8s.io/api v0.20.10-rc.0/go.mod h1:kkNjbH1Wn07+8i4dG29JGg9SEJtgzdBONXN6E0p8lq0=
|
||||
k8s.io/apimachinery v0.20.10-rc.0 h1:Su9ZLaxqsBgP413gFrurTGjBN9LET/xvNsu0sZOHvsY=
|
||||
k8s.io/apimachinery v0.20.10-rc.0/go.mod h1:kQa//VOAwyVwJ2+L9kOREbsnryfsGSkSM1przND4+mw=
|
||||
k8s.io/api v0.20.15-rc.0 h1:xjLH2OtyX/H7UFRg9sKOUDR1FCNau2uY1/1bdV5uJjM=
|
||||
k8s.io/api v0.20.15-rc.0/go.mod h1:dSrrkWq7+Pcw3Bf5wHU5SwEb1YkuDi8I37SesMYqvEU=
|
||||
k8s.io/apimachinery v0.20.15-rc.0 h1:iC5tpfCz0/3HIv7TOI0FoaEAB/Ohkf2qposYAqTmdpM=
|
||||
k8s.io/apimachinery v0.20.15-rc.0/go.mod h1:4KFiDSxCoGviCiRk9kTXIROsIf4VSGkVYjVJjJln3pg=
|
||||
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
|
||||
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
|
||||
k8s.io/klog/v2 v2.4.0 h1:7+X0fUguPyrKEC4WjH8iGDg3laWgMo5tMnRTIGTTxGQ=
|
||||
k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
|
||||
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd h1:sOHNzJIkytDF6qadMNKhhDRpc6ODik8lVC6nOur7B2c=
|
||||
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
|
||||
k8s.io/kube-openapi v0.0.0-20211110013926-83f114cd0513 h1:pbudjNtv90nOgR0/DUhPwKHnQ55Khz8+sNhJBIK7A5M=
|
||||
k8s.io/kube-openapi v0.0.0-20211110013926-83f114cd0513/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
|
||||
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 h1:CbnUZsM497iRC5QMVkHwyl8s2tB3g7yaSHkYPkpgelw=
|
||||
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
|
||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||
|
||||
27
third_party/forked/golang/LICENSE
vendored
Normal file
27
third_party/forked/golang/LICENSE
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
22
third_party/forked/golang/PATENTS
vendored
Normal file
22
third_party/forked/golang/PATENTS
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
Additional IP Rights Grant (Patents)
|
||||
|
||||
"This implementation" means the copyrightable works distributed by
|
||||
Google as part of the Go project.
|
||||
|
||||
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
||||
no-charge, royalty-free, irrevocable (except as stated in this section)
|
||||
patent license to make, have made, use, offer to sell, sell, import,
|
||||
transfer and otherwise run, modify and propagate the contents of this
|
||||
implementation of Go, where such license applies only to those patent
|
||||
claims, both currently owned or controlled by Google and acquired in
|
||||
the future, licensable by Google that are necessarily infringed by this
|
||||
implementation of Go. This grant does not include claims that would be
|
||||
infringed only as a consequence of further modification of this
|
||||
implementation. If you or your agent or exclusive licensee institute or
|
||||
order or agree to the institution of patent litigation against any
|
||||
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
||||
that this implementation of Go or any code incorporated within this
|
||||
implementation of Go constitutes direct or contributory patent
|
||||
infringement, or inducement of patent infringement, then any patent
|
||||
rights granted to you under this License for this implementation of Go
|
||||
shall terminate as of the date such litigation is filed.
|
||||
@@ -165,7 +165,7 @@ func Convert_Map_string_To_runtime_Object_To_Slice_v1_NamedExtension(in *map[str
|
||||
newExtension := (*in)[key]
|
||||
oldExtension := runtime.RawExtension{}
|
||||
if err := runtime.Convert_runtime_Object_To_runtime_RawExtension(&newExtension, &oldExtension, s); err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
namedExtension := NamedExtension{key, oldExtension}
|
||||
*out = append(*out, namedExtension)
|
||||
|
||||
Reference in New Issue
Block a user