mirror of https://github.com/kubernetes/kops.git
Rationalize deserialiation code
Put it all through kopscodecs.Decode, so that we can rewrite the API group more easily in a subsequent PR.
This commit is contained in:
parent
f5ce011fa0
commit
4aa2498203
|
@ -118,11 +118,6 @@ func RunCreate(f *util.Factory, out io.Writer, c *CreateOptions) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Codecs provides access to encoding and decoding for the scheme
|
|
||||||
codecs := kopscodecs.Codecs //serializer.NewCodecFactory(scheme)
|
|
||||||
|
|
||||||
codec := codecs.UniversalDecoder(kopsapi.SchemeGroupVersion)
|
|
||||||
|
|
||||||
var clusterName = ""
|
var clusterName = ""
|
||||||
//var cSpec = false
|
//var cSpec = false
|
||||||
var sb bytes.Buffer
|
var sb bytes.Buffer
|
||||||
|
@ -147,7 +142,7 @@ func RunCreate(f *util.Factory, out io.Writer, c *CreateOptions) error {
|
||||||
Group: v1alpha1.SchemeGroupVersion.Group,
|
Group: v1alpha1.SchemeGroupVersion.Group,
|
||||||
Version: v1alpha1.SchemeGroupVersion.Version,
|
Version: v1alpha1.SchemeGroupVersion.Version,
|
||||||
}
|
}
|
||||||
o, gvk, err := codec.Decode(section, defaults, nil)
|
o, gvk, err := kopscodecs.Decode(section, defaults)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing file %q: %v", f, err)
|
return fmt.Errorf("error parsing file %q: %v", f, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,7 +212,7 @@ func RunCreateInstanceGroup(f *util.Factory, cmd *cobra.Command, args []string,
|
||||||
return fmt.Errorf("error launching editor: %v", err)
|
return fmt.Errorf("error launching editor: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
obj, _, err := kopscodecs.ParseVersionedYaml(edited)
|
obj, _, err := kopscodecs.Decode(edited, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing yaml: %v", err)
|
return fmt.Errorf("error parsing yaml: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,9 +97,6 @@ func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunDelete(factory *util.Factory, out io.Writer, d *DeleteOptions) error {
|
func RunDelete(factory *util.Factory, out io.Writer, d *DeleteOptions) error {
|
||||||
// Codecs provides access to encoding and decoding for the scheme
|
|
||||||
codec := kopscodecs.Codecs.UniversalDecoder(kopsapi.SchemeGroupVersion)
|
|
||||||
|
|
||||||
// We could have more than one cluster in a manifest so we are using a set
|
// We could have more than one cluster in a manifest so we are using a set
|
||||||
deletedClusters := sets.NewString()
|
deletedClusters := sets.NewString()
|
||||||
|
|
||||||
|
@ -124,7 +121,7 @@ func RunDelete(factory *util.Factory, out io.Writer, d *DeleteOptions) error {
|
||||||
Group: v1alpha1.SchemeGroupVersion.Group,
|
Group: v1alpha1.SchemeGroupVersion.Group,
|
||||||
Version: v1alpha1.SchemeGroupVersion.Version,
|
Version: v1alpha1.SchemeGroupVersion.Version,
|
||||||
}
|
}
|
||||||
o, gvk, err := codec.Decode(section, defaults, nil)
|
o, gvk, err := kopscodecs.Decode(section, defaults)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing file %q: %v", f, err)
|
return fmt.Errorf("error parsing file %q: %v", f, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,7 +167,7 @@ func RunEditCluster(f *util.Factory, cmd *cobra.Command, args []string, out io.W
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
newObj, _, err := kopscodecs.ParseVersionedYaml(edited)
|
newObj, _, err := kopscodecs.Decode(edited, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return preservedFile(fmt.Errorf("error parsing config: %s", err), file, out)
|
return preservedFile(fmt.Errorf("error parsing config: %s", err), file, out)
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,7 +142,7 @@ func RunEditInstanceGroup(f *util.Factory, cmd *cobra.Command, args []string, ou
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
newObj, _, err := kopscodecs.ParseVersionedYaml(edited)
|
newObj, _, err := kopscodecs.Decode(edited, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing InstanceGroup: %v", err)
|
return fmt.Errorf("error parsing InstanceGroup: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,11 +94,6 @@ func RunReplace(f *util.Factory, cmd *cobra.Command, out io.Writer, c *replaceOp
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Codecs provides access to encoding and decoding for the scheme
|
|
||||||
codecs := kopscodecs.Codecs //serializer.NewCodecFactory(scheme)
|
|
||||||
|
|
||||||
codec := codecs.UniversalDecoder(kopsapi.SchemeGroupVersion)
|
|
||||||
|
|
||||||
for _, f := range c.Filenames {
|
for _, f := range c.Filenames {
|
||||||
var contents []byte
|
var contents []byte
|
||||||
if f == "-" {
|
if f == "-" {
|
||||||
|
@ -115,7 +110,7 @@ func RunReplace(f *util.Factory, cmd *cobra.Command, out io.Writer, c *replaceOp
|
||||||
sections := bytes.Split(contents, []byte("\n---\n"))
|
sections := bytes.Split(contents, []byte("\n---\n"))
|
||||||
|
|
||||||
for _, section := range sections {
|
for _, section := range sections {
|
||||||
o, gvk, err := codec.Decode(section, nil, nil)
|
o, gvk, err := kopscodecs.Decode(section, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing file %q: %v", f, err)
|
return fmt.Errorf("error parsing file %q: %v", f, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,6 @@ type ValidationFunction func(o runtime.Object) error
|
||||||
type commonVFS struct {
|
type commonVFS struct {
|
||||||
kind string
|
kind string
|
||||||
basePath vfs.Path
|
basePath vfs.Path
|
||||||
decoder runtime.Decoder
|
|
||||||
encoder runtime.Encoder
|
encoder runtime.Encoder
|
||||||
defaultReadVersion *schema.GroupVersionKind
|
defaultReadVersion *schema.GroupVersionKind
|
||||||
validate ValidationFunction
|
validate ValidationFunction
|
||||||
|
@ -57,7 +56,6 @@ func (c *commonVFS) init(kind string, basePath vfs.Path, storeVersion runtime.Gr
|
||||||
glog.Fatalf("no YAML serializer registered")
|
glog.Fatalf("no YAML serializer registered")
|
||||||
}
|
}
|
||||||
c.encoder = codecs.EncoderForVersion(yaml.Serializer, storeVersion)
|
c.encoder = codecs.EncoderForVersion(yaml.Serializer, storeVersion)
|
||||||
c.decoder = codecs.DecoderToVersion(yaml.Serializer, kops.SchemeGroupVersion)
|
|
||||||
|
|
||||||
c.kind = kind
|
c.kind = kind
|
||||||
c.basePath = basePath
|
c.basePath = basePath
|
||||||
|
@ -126,7 +124,7 @@ func (c *commonVFS) readConfig(configPath vfs.Path) (runtime.Object, error) {
|
||||||
return nil, fmt.Errorf("error reading %s: %v", configPath, err)
|
return nil, fmt.Errorf("error reading %s: %v", configPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
object, _, err := c.decoder.Decode(data, c.defaultReadVersion, nil)
|
object, _, err := kopscodecs.Decode(data, c.defaultReadVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing %s: %v", configPath, err)
|
return nil, fmt.Errorf("error parsing %s: %v", configPath, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,6 +84,10 @@ func ToVersionedJSONWithVersion(obj runtime.Object, version runtime.GroupVersion
|
||||||
return w.Bytes(), nil
|
return w.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseVersionedYaml(data []byte) (runtime.Object, *schema.GroupVersionKind, error) {
|
// Decode decodes the specified data, with the specified default version
|
||||||
return decoder().Decode(data, nil, nil)
|
func Decode(data []byte, defaultReadVersion *schema.GroupVersionKind) (runtime.Object, *schema.GroupVersionKind, error) {
|
||||||
|
decoder := decoder()
|
||||||
|
|
||||||
|
object, gvk, err := decoder.Decode(data, defaultReadVersion, nil)
|
||||||
|
return object, gvk, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,18 +49,13 @@ func LoadModel(basedir string) (*Model, error) {
|
||||||
|
|
||||||
spec := &Model{}
|
spec := &Model{}
|
||||||
|
|
||||||
// Codecs provides access to encoding and decoding for the scheme
|
|
||||||
codecs := kopscodecs.Codecs
|
|
||||||
|
|
||||||
codec := codecs.UniversalDecoder(kops.SchemeGroupVersion)
|
|
||||||
|
|
||||||
sections := bytes.Split(clusterYaml, []byte("\n---\n"))
|
sections := bytes.Split(clusterYaml, []byte("\n---\n"))
|
||||||
for _, section := range sections {
|
for _, section := range sections {
|
||||||
defaults := &schema.GroupVersionKind{
|
defaults := &schema.GroupVersionKind{
|
||||||
Group: v1alpha2.SchemeGroupVersion.Group,
|
Group: v1alpha2.SchemeGroupVersion.Group,
|
||||||
Version: v1alpha2.SchemeGroupVersion.Version,
|
Version: v1alpha2.SchemeGroupVersion.Version,
|
||||||
}
|
}
|
||||||
o, gvk, err := codec.Decode(section, defaults, nil)
|
o, gvk, err := kopscodecs.Decode(section, defaults)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing file %v", err)
|
return nil, fmt.Errorf("error parsing file %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ go_test(
|
||||||
"exported_testdata", # keep
|
"exported_testdata", # keep
|
||||||
],
|
],
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/apis/kops:go_default_library",
|
|
||||||
"//pkg/apis/kops/v1alpha1:go_default_library",
|
"//pkg/apis/kops/v1alpha1:go_default_library",
|
||||||
"//pkg/apis/kops/v1alpha2:go_default_library",
|
"//pkg/apis/kops/v1alpha2:go_default_library",
|
||||||
"//pkg/diff:go_default_library",
|
"//pkg/diff:go_default_library",
|
||||||
|
|
|
@ -25,7 +25,6 @@ import (
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/kops/pkg/apis/kops"
|
|
||||||
"k8s.io/kops/pkg/apis/kops/v1alpha1"
|
"k8s.io/kops/pkg/apis/kops/v1alpha1"
|
||||||
"k8s.io/kops/pkg/apis/kops/v1alpha2"
|
"k8s.io/kops/pkg/apis/kops/v1alpha2"
|
||||||
"k8s.io/kops/pkg/diff"
|
"k8s.io/kops/pkg/diff"
|
||||||
|
@ -54,8 +53,6 @@ func runTest(t *testing.T, srcDir string, fromVersion string, toVersion string)
|
||||||
t.Fatalf("unexpected error reading expectedPath %q: %v", expectedPath, err)
|
t.Fatalf("unexpected error reading expectedPath %q: %v", expectedPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
codec := kopscodecs.Codecs.UniversalDecoder(kops.SchemeGroupVersion)
|
|
||||||
|
|
||||||
defaults := &schema.GroupVersionKind{
|
defaults := &schema.GroupVersionKind{
|
||||||
Group: v1alpha1.SchemeGroupVersion.Group,
|
Group: v1alpha1.SchemeGroupVersion.Group,
|
||||||
Version: v1alpha1.SchemeGroupVersion.Version,
|
Version: v1alpha1.SchemeGroupVersion.Version,
|
||||||
|
@ -77,12 +74,10 @@ func runTest(t *testing.T, srcDir string, fromVersion string, toVersion string)
|
||||||
t.Fatalf("unknown version %q", toVersion)
|
t.Fatalf("unknown version %q", toVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
//decoder := k8sapi.Codecs.DecoderToVersion(yaml.Serializer, kops.SchemeGroupVersion)
|
|
||||||
|
|
||||||
var actual []string
|
var actual []string
|
||||||
|
|
||||||
for _, s := range strings.Split(string(sourceBytes), "\n---\n") {
|
for _, s := range strings.Split(string(sourceBytes), "\n---\n") {
|
||||||
o, gvk, err := codec.Decode([]byte(s), defaults, nil)
|
o, gvk, err := kopscodecs.Decode([]byte(s), defaults)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error parsing file %q: %v", sourcePath, err)
|
t.Fatalf("error parsing file %q: %v", sourcePath, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ func runChannelBuilderTest(t *testing.T, key string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error reading cluster yaml file %q: %v", clusterYamlPath, err)
|
t.Fatalf("error reading cluster yaml file %q: %v", clusterYamlPath, err)
|
||||||
}
|
}
|
||||||
obj, _, err := kopscodecs.ParseVersionedYaml(clusterYaml)
|
obj, _, err := kopscodecs.Decode(clusterYaml, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error parsing cluster yaml %q: %v", clusterYamlPath, err)
|
t.Fatalf("error parsing cluster yaml %q: %v", clusterYamlPath, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -217,16 +217,9 @@ func (c *VFSCAStore) buildPrivateKeyPath(name string, id string) vfs.Path {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *VFSCAStore) parseKeysetYaml(data []byte) (*kops.Keyset, KeysetFormat, error) {
|
func (c *VFSCAStore) parseKeysetYaml(data []byte) (*kops.Keyset, KeysetFormat, error) {
|
||||||
codecs := kopscodecs.Codecs
|
|
||||||
yaml, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), "application/yaml")
|
|
||||||
if !ok {
|
|
||||||
glog.Fatalf("no YAML serializer registered")
|
|
||||||
}
|
|
||||||
decoder := codecs.DecoderToVersion(yaml.Serializer, kops.SchemeGroupVersion)
|
|
||||||
|
|
||||||
defaultReadVersion := v1alpha2.SchemeGroupVersion.WithKind("Keyset")
|
defaultReadVersion := v1alpha2.SchemeGroupVersion.WithKind("Keyset")
|
||||||
|
|
||||||
object, gvk, err := decoder.Decode(data, &defaultReadVersion, nil)
|
object, gvk, err := kopscodecs.Decode(data, &defaultReadVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", fmt.Errorf("error parsing keyset: %v", err)
|
return nil, "", fmt.Errorf("error parsing keyset: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue