-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support objects for integrating dynamic client the kubectl builder #25540
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package dynamic | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/kubernetes/pkg/api/meta" | ||
"k8s.io/kubernetes/pkg/api/unversioned" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
) | ||
|
||
// VersionInterfaces provides an object converter and metadata | ||
// accessor appropriate for use with unstructured objects. | ||
func VersionInterfaces(unversioned.GroupVersion) (*meta.VersionInterfaces, error) { | ||
return &meta.VersionInterfaces{ | ||
ObjectConvertor: &runtime.UnstructuredObjectConverter{}, | ||
MetadataAccessor: meta.NewAccessor(), | ||
}, nil | ||
} | ||
|
||
// NewDiscoveryRESTMapper returns a RESTMapper based on discovery information. | ||
func NewDiscoveryRESTMapper(resources []*unversioned.APIResourceList, versionFunc meta.VersionInterfacesFunc) (*meta.DefaultRESTMapper, error) { | ||
rm := meta.NewDefaultRESTMapper(nil, versionFunc) | ||
for _, resourceList := range resources { | ||
gv, err := unversioned.ParseGroupVersion(resourceList.GroupVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, resource := range resourceList.APIResources { | ||
gvk := gv.WithKind(resource.Kind) | ||
scope := meta.RESTScopeRoot | ||
if resource.Namespaced { | ||
scope = meta.RESTScopeNamespace | ||
} | ||
rm.Add(gvk, scope) | ||
} | ||
} | ||
return rm, nil | ||
} | ||
|
||
// ObjectTyper provides an ObjectTyper implmentation for | ||
// runtime.Unstructured object based on discovery information. | ||
type ObjectTyper struct { | ||
registered map[unversioned.GroupVersionKind]bool | ||
} | ||
|
||
// NewObjectTyper constructs an ObjectTyper from discovery information. | ||
func NewObjectTyper(resources []*unversioned.APIResourceList) (runtime.ObjectTyper, error) { | ||
ot := &ObjectTyper{registered: make(map[unversioned.GroupVersionKind]bool)} | ||
for _, resourceList := range resources { | ||
gv, err := unversioned.ParseGroupVersion(resourceList.GroupVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, resource := range resourceList.APIResources { | ||
ot.registered[gv.WithKind(resource.Kind)] = true | ||
} | ||
} | ||
return ot, nil | ||
} | ||
|
||
// ObjectKind returns the group,version,kind of the provided object, | ||
// or an error if the object in not *runtime.Unstructured or has no | ||
// group,version,kind information. | ||
func (ot *ObjectTyper) ObjectKind(obj runtime.Object) (unversioned.GroupVersionKind, error) { | ||
if _, ok := obj.(*runtime.Unstructured); !ok { | ||
return unversioned.GroupVersionKind{}, fmt.Errorf("type %T is invalid for dynamic object typer", obj) | ||
} | ||
|
||
return obj.GetObjectKind().GroupVersionKind(), nil | ||
} | ||
|
||
// ObjectKinds returns a slice of one element with the | ||
// group,version,kind of the provided object, or an error if the | ||
// object is not *runtime.Unstructured or has no group,version,kind | ||
// information. | ||
func (ot *ObjectTyper) ObjectKinds(obj runtime.Object) ([]unversioned.GroupVersionKind, error) { | ||
gvk, err := ot.ObjectKind(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []unversioned.GroupVersionKind{gvk}, nil | ||
} | ||
|
||
// Recognizes returns true if the provided group,version,kind was in | ||
// the discovery information. | ||
func (ot *ObjectTyper) Recognizes(gvk unversioned.GroupVersionKind) bool { | ||
return ot.registered[gvk] | ||
} | ||
|
||
// IsUnversioned returns false always because *runtime.Unstructured | ||
// objects should always have group,version,kind information set. ok | ||
// will be true if the object's group,version,kind is registered. | ||
func (ot *ObjectTyper) IsUnversioned(obj runtime.Object) (unversioned bool, ok bool) { | ||
gvk, err := ot.ObjectKind(obj) | ||
if err != nil { | ||
return false, false | ||
} | ||
|
||
return false, ot.registered[gvk] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package dynamic | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/kubernetes/pkg/api/unversioned" | ||
) | ||
|
||
func TestDiscoveryRESTMapper(t *testing.T) { | ||
resources := []*unversioned.APIResourceList{ | ||
{ | ||
GroupVersion: "test/beta1", | ||
APIResources: []unversioned.APIResource{ | ||
{ | ||
Name: "test_kinds", | ||
Namespaced: true, | ||
Kind: "test_kind", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
gvk := unversioned.GroupVersionKind{ | ||
Group: "test", | ||
Version: "beta1", | ||
Kind: "test_kind", | ||
} | ||
|
||
mapper, err := NewDiscoveryRESTMapper(resources, VersionInterfaces) | ||
if err != nil { | ||
t.Fatalf("unexpected error creating mapper: %s", err) | ||
} | ||
|
||
for _, res := range []unversioned.GroupVersionResource{ | ||
{ | ||
Group: "test", | ||
Version: "beta1", | ||
Resource: "test_kinds", | ||
}, | ||
{ | ||
Version: "beta1", | ||
Resource: "test_kinds", | ||
}, | ||
{ | ||
Group: "test", | ||
Resource: "test_kinds", | ||
}, | ||
{ | ||
Resource: "test_kinds", | ||
}, | ||
} { | ||
got, err := mapper.KindFor(res) | ||
if err != nil { | ||
t.Errorf("KindFor(%#v) unexpected error: %s", res, err) | ||
continue | ||
} | ||
|
||
if got != gvk { | ||
t.Errorf("KindFor(%#v) = %#v; want %#v", res, got, gvk) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ package runtime | |
|
||
import ( | ||
gojson "encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"k8s.io/kubernetes/pkg/api/unversioned" | ||
|
@@ -98,6 +100,13 @@ func (s unstructuredJSONScheme) decodeInto(data []byte, obj Object) error { | |
return s.decodeToUnstructured(data, x) | ||
case *UnstructuredList: | ||
return s.decodeToList(data, x) | ||
case *VersionedObjects: | ||
u := new(Unstructured) | ||
err := s.decodeToUnstructured(data, u) | ||
if err == nil { | ||
x.Objects = []Object{u} | ||
} | ||
return err | ||
default: | ||
return json.Unmarshal(data, x) | ||
} | ||
|
@@ -139,3 +148,39 @@ func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList | |
} | ||
return nil | ||
} | ||
|
||
// UnstructuredObjectConverter is an ObjectConverter for use with | ||
// Unstructured objects. Since it has no schema or type information, | ||
// it will only succeed for no-op conversions. This is provided as a | ||
// sane implementation for APIs that require an object converter. | ||
type UnstructuredObjectConverter struct{} | ||
|
||
func (UnstructuredObjectConverter) Convert(in, out interface{}) error { | ||
unstructIn, ok := in.(*Unstructured) | ||
if !ok { | ||
return fmt.Errorf("input type %T in not valid for unstructured conversion", in) | ||
} | ||
|
||
unstructOut, ok := out.(*Unstructured) | ||
if !ok { | ||
return fmt.Errorf("output type %T in not valid for unstructured conversion", out) | ||
} | ||
|
||
// maybe deep copy the map? It is documented in the | ||
// ObjectConverter interface that this function is not | ||
// guaranteeed to not mutate the input. Or maybe set the input | ||
// object to nil. | ||
unstructOut.Object = unstructIn.Object | ||
return nil | ||
} | ||
|
||
func (UnstructuredObjectConverter) ConvertToVersion(in Object, outVersion unversioned.GroupVersion) (Object, error) { | ||
if gvk := in.GetObjectKind().GroupVersionKind(); gvk.GroupVersion() != outVersion { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line changed in the rebase. |
||
return nil, errors.New("unstructured converter cannot convert versions") | ||
} | ||
return in, nil | ||
} | ||
|
||
func (UnstructuredObjectConverter) ConvertFieldLabel(version, kind, label, value string) (string, string, error) { | ||
return "", "", errors.New("unstructured cannot convert field labels") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now this is fine, I have a todo to make the conversion guarantees much more explicit, and I'll handle the implications if any here.