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:
Clayton Coleman
2015-12-21 00:37:49 -05:00
parent 181dc7c64c
commit 2fd38a7dc0
40 changed files with 245 additions and 232 deletions

View File

@@ -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
}