2014-12-27 21:48:27 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-12-27 21:48:27 +00:00
|
|
|
|
|
|
|
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 resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2014-12-27 21:48:27 +00:00
|
|
|
)
|
|
|
|
|
2017-11-14 03:56:59 +00:00
|
|
|
// Mapper is a convenience struct for holding references to the interfaces
|
2014-12-27 21:48:27 +00:00
|
|
|
// needed to create Info for arbitrary objects.
|
|
|
|
type Mapper struct {
|
2018-04-26 20:18:17 +00:00
|
|
|
RESTMapper meta.RESTMapper
|
|
|
|
ClientMapper ClientMapper
|
|
|
|
Decoder runtime.Decoder
|
2017-11-14 03:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-27 21:48:27 +00:00
|
|
|
// InfoForData creates an Info object for the given data. An error is returned
|
|
|
|
// if any of the decoding or client lookup steps fail. Name and namespace will be
|
|
|
|
// set into Info if the mapping's MetadataAccessor can retrieve them.
|
|
|
|
func (m *Mapper) InfoForData(data []byte, source string) (*Info, error) {
|
2018-04-26 20:18:17 +00:00
|
|
|
obj, gvk, err := m.Decoder.Decode(data, nil, nil)
|
2016-04-28 09:14:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode %q: %v", source, err)
|
|
|
|
}
|
2016-08-08 22:30:56 +00:00
|
|
|
|
2018-04-26 20:18:17 +00:00
|
|
|
mapping, err := m.RESTMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
|
2014-12-27 21:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to recognize %q: %v", source, err)
|
|
|
|
}
|
2015-12-21 05:37:49 +00:00
|
|
|
|
2018-04-26 20:18:17 +00:00
|
|
|
client, err := m.ClientMapper.ClientForMapping(mapping)
|
2014-12-27 21:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
|
|
|
|
}
|
2015-07-16 09:20:53 +00:00
|
|
|
|
2018-04-23 14:23:01 +00:00
|
|
|
name, _ := metadataAccessor.Name(obj)
|
|
|
|
namespace, _ := metadataAccessor.Namespace(obj)
|
|
|
|
resourceVersion, _ := metadataAccessor.ResourceVersion(obj)
|
2014-12-27 21:48:27 +00:00
|
|
|
|
2015-06-23 01:56:53 +00:00
|
|
|
return &Info{
|
2018-04-27 12:40:57 +00:00
|
|
|
Client: client,
|
|
|
|
Mapping: mapping,
|
2017-11-14 03:56:59 +00:00
|
|
|
|
|
|
|
Source: source,
|
2015-06-23 01:56:53 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Name: name,
|
2014-12-27 21:48:27 +00:00
|
|
|
ResourceVersion: resourceVersion,
|
2017-11-14 03:56:59 +00:00
|
|
|
|
|
|
|
Object: obj,
|
2014-12-27 21:48:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:46:49 +00:00
|
|
|
// InfoForObject creates an Info object for the given Object. An error is returned
|
2014-12-27 21:48:27 +00:00
|
|
|
// if the object cannot be introspected. Name and namespace will be set into Info
|
|
|
|
// if the mapping's MetadataAccessor can retrieve them.
|
2018-04-27 12:40:57 +00:00
|
|
|
func (m *Mapper) InfoForObject(obj runtime.Object, typer runtime.ObjectTyper, preferredGVKs []schema.GroupVersionKind) (*Info, error) {
|
|
|
|
groupVersionKinds, _, err := typer.ObjectKinds(obj)
|
2014-12-27 21:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get type info from the object %q: %v", reflect.TypeOf(obj), err)
|
|
|
|
}
|
2016-03-11 04:49:00 +00:00
|
|
|
|
|
|
|
groupVersionKind := groupVersionKinds[0]
|
|
|
|
if len(groupVersionKinds) > 1 && len(preferredGVKs) > 0 {
|
|
|
|
groupVersionKind = preferredObjectKind(groupVersionKinds, preferredGVKs)
|
|
|
|
}
|
|
|
|
|
2018-04-26 20:18:17 +00:00
|
|
|
mapping, err := m.RESTMapper.RESTMapping(groupVersionKind.GroupKind(), groupVersionKind.Version)
|
2014-12-27 21:48:27 +00:00
|
|
|
if err != nil {
|
2015-11-20 12:38:32 +00:00
|
|
|
return nil, fmt.Errorf("unable to recognize %v: %v", groupVersionKind, err)
|
2014-12-27 21:48:27 +00:00
|
|
|
}
|
2017-11-14 03:56:59 +00:00
|
|
|
|
2018-04-26 20:18:17 +00:00
|
|
|
client, err := m.ClientMapper.ClientForMapping(mapping)
|
2014-12-27 21:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
|
|
|
|
}
|
2018-04-23 14:23:01 +00:00
|
|
|
name, _ := metadataAccessor.Name(obj)
|
|
|
|
namespace, _ := metadataAccessor.Namespace(obj)
|
|
|
|
resourceVersion, _ := metadataAccessor.ResourceVersion(obj)
|
2014-12-27 21:48:27 +00:00
|
|
|
return &Info{
|
2018-04-27 12:40:57 +00:00
|
|
|
Client: client,
|
|
|
|
Mapping: mapping,
|
2014-12-27 21:48:27 +00:00
|
|
|
|
2017-11-14 03:56:59 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Name: name,
|
2014-12-27 21:48:27 +00:00
|
|
|
ResourceVersion: resourceVersion,
|
2017-11-14 03:56:59 +00:00
|
|
|
|
|
|
|
Object: obj,
|
2014-12-27 21:48:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2016-03-11 04:49:00 +00:00
|
|
|
|
|
|
|
// preferredObjectKind picks the possibility that most closely matches the priority list in this order:
|
|
|
|
// GroupVersionKind matches (exact match)
|
|
|
|
// GroupKind matches
|
|
|
|
// Group matches
|
2016-11-21 02:55:31 +00:00
|
|
|
func preferredObjectKind(possibilities []schema.GroupVersionKind, preferences []schema.GroupVersionKind) schema.GroupVersionKind {
|
2016-03-11 04:49:00 +00:00
|
|
|
// Exact match
|
|
|
|
for _, priority := range preferences {
|
|
|
|
for _, possibility := range possibilities {
|
|
|
|
if possibility == priority {
|
|
|
|
return possibility
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupKind match
|
|
|
|
for _, priority := range preferences {
|
|
|
|
for _, possibility := range possibilities {
|
|
|
|
if possibility.GroupKind() == priority.GroupKind() {
|
|
|
|
return possibility
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group match
|
|
|
|
for _, priority := range preferences {
|
|
|
|
for _, possibility := range possibilities {
|
|
|
|
if possibility.Group == priority.Group {
|
|
|
|
return possibility
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just pick the first
|
|
|
|
return possibilities[0]
|
|
|
|
}
|
2017-11-14 03:56:59 +00:00
|
|
|
|
|
|
|
// DisabledClientForMapping allows callers to avoid allowing remote calls when handling
|
|
|
|
// resources.
|
|
|
|
type DisabledClientForMapping struct {
|
|
|
|
ClientMapper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f DisabledClientForMapping) ClientForMapping(mapping *meta.RESTMapping) (RESTClient, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|