mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 15:05:20 +00:00
Break kubectl from assuming details of codecs
Most of the logic related to type and kind retrieval belongs in the codec, not in the various classes. Make it explicit that the codec should handle these details. Factory now returns a universal Decoder and a JSONEncoder to assist code in kubectl that needs to specifically deal with JSON serialization (apply, merge, patch, edit, jsonpath). Add comments to indicate the serialization is explicit in those places. These methods decode to internal and encode to the preferred API version as previous, although in the future they may be changed. React to removing Codec from version interfaces and RESTMapping by passing it in to all the places that it is needed.
This commit is contained in:
@@ -45,6 +45,7 @@ import (
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/runtime/serializer/json"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
@@ -63,13 +64,19 @@ type Factory struct {
|
||||
|
||||
// Returns interfaces for dealing with arbitrary runtime.Objects.
|
||||
Object func() (meta.RESTMapper, runtime.ObjectTyper)
|
||||
// Returns interfaces for decoding objects - if toInternal is set, decoded objects will be converted
|
||||
// into their internal form (if possible). Eventually the internal form will be removed as an option,
|
||||
// and only versioned objects will be returned.
|
||||
Decoder func(toInternal bool) runtime.Decoder
|
||||
// Returns an encoder capable of encoding a provided object into JSON in the default desired version.
|
||||
JSONEncoder func() runtime.Encoder
|
||||
// Returns a client for accessing Kubernetes resources or an error.
|
||||
Client func() (*client.Client, error)
|
||||
// Returns a client.Config for accessing the Kubernetes server.
|
||||
ClientConfig func() (*client.Config, error)
|
||||
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
||||
// for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
|
||||
RESTClient func(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
||||
ClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
||||
// Returns a Describer for displaying the specified RESTMapping type or an error.
|
||||
Describer func(mapping *meta.RESTMapping) (kubectl.Describer, error)
|
||||
// Returns a Printer for formatting objects of the given type or an error.
|
||||
@@ -185,7 +192,7 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
||||
ClientConfig: func() (*client.Config, error) {
|
||||
return clients.ClientConfigForVersion(nil)
|
||||
},
|
||||
RESTClient: func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
||||
ClientForMapping: func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
||||
mappingVersion := mapping.GroupVersionKind.GroupVersion()
|
||||
client, err := clients.ClientForVersion(&mappingVersion)
|
||||
if err != nil {
|
||||
@@ -210,6 +217,15 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
||||
}
|
||||
return nil, fmt.Errorf("no description has been implemented for %q", mapping.GroupVersionKind.Kind)
|
||||
},
|
||||
Decoder: func(toInternal bool) runtime.Decoder {
|
||||
if toInternal {
|
||||
return api.Codecs.UniversalDecoder()
|
||||
}
|
||||
return api.Codecs.UniversalDeserializer()
|
||||
},
|
||||
JSONEncoder: func() runtime.Encoder {
|
||||
return api.Codecs.LegacyCodec(registered.EnabledVersions()...)
|
||||
},
|
||||
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, absoluteTimestamps bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
|
||||
return kubectl.NewHumanReadablePrinter(noHeaders, withNamespace, wide, showAll, absoluteTimestamps, columnLabels), nil
|
||||
},
|
||||
@@ -531,7 +547,7 @@ func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cac
|
||||
}
|
||||
|
||||
func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
|
||||
gvk, err := runtime.UnstructuredJSONScheme.DataKind(data)
|
||||
gvk, err := json.DefaultMetaFactory.Interpret(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -663,24 +679,9 @@ func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMappin
|
||||
return printer, nil
|
||||
}
|
||||
|
||||
// ClientMapperForCommand returns a ClientMapper for the factory.
|
||||
func (f *Factory) ClientMapperForCommand() resource.ClientMapper {
|
||||
return resource.ClientMapperFunc(func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
||||
return f.RESTClient(mapping)
|
||||
})
|
||||
}
|
||||
|
||||
// NilClientMapperForCommand returns a ClientMapper which always returns nil.
|
||||
// When command is running locally and client isn't needed, this mapper can be parsed to NewBuilder.
|
||||
func (f *Factory) NilClientMapperForCommand() resource.ClientMapper {
|
||||
return resource.ClientMapperFunc(func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
||||
return nil, nil
|
||||
})
|
||||
}
|
||||
|
||||
// One stop shopping for a Builder
|
||||
func (f *Factory) NewBuilder() *resource.Builder {
|
||||
mapper, typer := f.Object()
|
||||
|
||||
return resource.NewBuilder(mapper, typer, f.ClientMapperForCommand())
|
||||
return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true))
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ func TestValidateCachesSchema(t *testing.T) {
|
||||
os.RemoveAll(dir)
|
||||
|
||||
obj := &api.Pod{}
|
||||
data, err := testapi.Default.Codec().Encode(obj)
|
||||
data, err := runtime.Encode(testapi.Default.Codec(), obj)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
t.FailNow()
|
||||
|
||||
@@ -18,7 +18,6 @@ package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -28,10 +27,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/apimachinery/registered"
|
||||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
@@ -371,38 +368,11 @@ func ReadConfigDataFromLocation(location string) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func Merge(dst runtime.Object, fragment, kind string) (runtime.Object, error) {
|
||||
// Ok, this is a little hairy, we'd rather not force the user to specify a kind for their JSON
|
||||
// So we pull it into a map, add the Kind field, and then reserialize.
|
||||
// We also pull the apiVersion for proper parsing
|
||||
var intermediate interface{}
|
||||
if err := json.Unmarshal([]byte(fragment), &intermediate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataMap, ok := intermediate.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Expected a map, found something else: %s", fragment)
|
||||
}
|
||||
version, found := dataMap["apiVersion"]
|
||||
if !found {
|
||||
return nil, fmt.Errorf("Inline JSON requires an apiVersion field")
|
||||
}
|
||||
versionString, ok := version.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("apiVersion must be a string")
|
||||
}
|
||||
groupVersion, err := unversioned.ParseGroupVersion(versionString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i, err := registered.GroupOrDie(api.GroupName).InterfacesFor(groupVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Merge requires JSON serialization
|
||||
// TODO: merge assumes JSON serialization, and does not properly abstract API retrieval
|
||||
func Merge(codec runtime.Codec, dst runtime.Object, fragment, kind string) (runtime.Object, error) {
|
||||
// encode dst into versioned json and apply fragment directly too it
|
||||
target, err := i.Codec.Encode(dst)
|
||||
target, err := runtime.Encode(codec, dst)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -410,7 +380,7 @@ func Merge(dst runtime.Object, fragment, kind string) (runtime.Object, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := i.Codec.Decode(patched)
|
||||
out, err := runtime.Decode(codec, patched)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -443,7 +413,7 @@ func DumpReaderToFile(reader io.Reader, filename string) error {
|
||||
}
|
||||
|
||||
// UpdateObject updates resource object with updateFn
|
||||
func UpdateObject(info *resource.Info, updateFn func(runtime.Object) error) (runtime.Object, error) {
|
||||
func UpdateObject(info *resource.Info, codec runtime.Codec, updateFn func(runtime.Object) error) (runtime.Object, error) {
|
||||
helper := resource.NewHelper(info.Client, info.Mapping)
|
||||
|
||||
if err := updateFn(info.Object); err != nil {
|
||||
@@ -451,7 +421,7 @@ func UpdateObject(info *resource.Info, updateFn func(runtime.Object) error) (run
|
||||
}
|
||||
|
||||
// Update the annotation used by kubectl apply
|
||||
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
|
||||
if err := kubectl.UpdateApplyAnnotation(info, codec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -183,7 +183,7 @@ func TestMerge(t *testing.T) {
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
out, err := Merge(test.obj, test.fragment, test.kind)
|
||||
out, err := Merge(testapi.Default.Codec(), test.obj, test.fragment, test.kind)
|
||||
if !test.expectErr {
|
||||
if err != nil {
|
||||
t.Errorf("testcase[%d], unexpected error: %v", i, err)
|
||||
|
||||
Reference in New Issue
Block a user