From 24a7919002b9c58d0376168ffaec371b523785f9 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Mon, 21 Dec 2015 00:31:34 -0500 Subject: [PATCH] Update clientcmd.Config to use new Decode methods --- .../clientcmd/api/latest/latest.go | 16 +++++++++------ .../unversioned/clientcmd/api/register.go | 2 +- pkg/client/unversioned/clientcmd/api/types.go | 20 +++++++++---------- .../clientcmd/api/v1/conversion.go | 10 +++++----- .../unversioned/clientcmd/api/v1/register.go | 4 ---- pkg/client/unversioned/clientcmd/loader.go | 19 ++++++------------ .../unversioned/clientcmd/loader_test.go | 7 ++++--- 7 files changed, 36 insertions(+), 42 deletions(-) diff --git a/pkg/client/unversioned/clientcmd/api/latest/latest.go b/pkg/client/unversioned/clientcmd/api/latest/latest.go index ecce3cbdd51..90d5c5380b1 100644 --- a/pkg/client/unversioned/clientcmd/api/latest/latest.go +++ b/pkg/client/unversioned/clientcmd/api/latest/latest.go @@ -18,8 +18,11 @@ package latest import ( "k8s.io/kubernetes/pkg/api/unversioned" - "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/v1" + "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" + _ "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/v1" "k8s.io/kubernetes/pkg/runtime" + "k8s.io/kubernetes/pkg/runtime/serializer/json" + "k8s.io/kubernetes/pkg/runtime/serializer/versioning" ) // Version is the string that represents the current external default version. @@ -37,8 +40,9 @@ const OldestVersion = "v1" // with a set of versions to choose. var Versions = []string{"v1"} -// Codec is the default codec for serializing output that should use -// the latest supported version. Use this Codec when writing to -// disk, a data store that is not dynamically versioned, or in tests. -// This codec can decode any object that Kubernetes is aware of. -var Codec = runtime.YAMLDecoder(v1.Codec) +var Codec = versioning.NewCodecForScheme( + api.Scheme, + json.NewYAMLSerializer(json.DefaultMetaFactory, api.Scheme, runtime.ObjectTyperToTyper(api.Scheme)), + []unversioned.GroupVersion{{Version: Version}}, + []unversioned.GroupVersion{{Version: runtime.APIVersionInternal}}, +) diff --git a/pkg/client/unversioned/clientcmd/api/register.go b/pkg/client/unversioned/clientcmd/api/register.go index f3be591718b..e4e23998abb 100644 --- a/pkg/client/unversioned/clientcmd/api/register.go +++ b/pkg/client/unversioned/clientcmd/api/register.go @@ -26,7 +26,7 @@ var Scheme = runtime.NewScheme() // SchemeGroupVersion is group version used to register these objects // TODO this should be in the "kubeconfig" group -var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: ""} +var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: runtime.APIVersionInternal} func init() { Scheme.AddKnownTypes(SchemeGroupVersion, diff --git a/pkg/client/unversioned/clientcmd/api/types.go b/pkg/client/unversioned/clientcmd/api/types.go index 818ab25b2a8..4a1dc364f43 100644 --- a/pkg/client/unversioned/clientcmd/api/types.go +++ b/pkg/client/unversioned/clientcmd/api/types.go @@ -42,14 +42,14 @@ type Config struct { // CurrentContext is the name of the context that you would like to use by default CurrentContext string `json:"current-context"` // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields - Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"` + Extensions map[string]runtime.Object `json:"extensions,omitempty"` } // IMPORTANT if you add fields to this struct, please update IsConfigEmpty() type Preferences struct { Colors bool `json:"colors,omitempty"` // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields - Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"` + Extensions map[string]runtime.Object `json:"extensions,omitempty"` } // Cluster contains information about how to communicate with a kubernetes cluster @@ -67,7 +67,7 @@ type Cluster struct { // CertificateAuthorityData contains PEM-encoded certificate authority certificates. Overrides CertificateAuthority CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"` // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields - Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"` + Extensions map[string]runtime.Object `json:"extensions,omitempty"` } // AuthInfo contains information that describes identity information. This is use to tell the kubernetes cluster who you are. @@ -89,7 +89,7 @@ type AuthInfo struct { // Password is the password for basic authentication to the kubernetes cluster. Password string `json:"password,omitempty"` // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields - Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"` + Extensions map[string]runtime.Object `json:"extensions,omitempty"` } // Context is a tuple of references to a cluster (how do I communicate with a kubernetes cluster), a user (how do I identify myself), and a namespace (what subset of resources do I want to work with) @@ -103,7 +103,7 @@ type Context struct { // Namespace is the default namespace to use on unspecified requests Namespace string `json:"namespace,omitempty"` // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields - Extensions map[string]*runtime.EmbeddedObject `json:"extensions,omitempty"` + Extensions map[string]runtime.Object `json:"extensions,omitempty"` } // NewConfig is a convenience function that returns a new Config object with non-nil maps @@ -113,26 +113,26 @@ func NewConfig() *Config { Clusters: make(map[string]*Cluster), AuthInfos: make(map[string]*AuthInfo), Contexts: make(map[string]*Context), - Extensions: make(map[string]*runtime.EmbeddedObject), + Extensions: make(map[string]runtime.Object), } } // NewConfig is a convenience function that returns a new Config object with non-nil maps func NewContext() *Context { - return &Context{Extensions: make(map[string]*runtime.EmbeddedObject)} + return &Context{Extensions: make(map[string]runtime.Object)} } // NewConfig is a convenience function that returns a new Config object with non-nil maps func NewCluster() *Cluster { - return &Cluster{Extensions: make(map[string]*runtime.EmbeddedObject)} + return &Cluster{Extensions: make(map[string]runtime.Object)} } // NewConfig is a convenience function that returns a new Config object with non-nil maps func NewAuthInfo() *AuthInfo { - return &AuthInfo{Extensions: make(map[string]*runtime.EmbeddedObject)} + return &AuthInfo{Extensions: make(map[string]runtime.Object)} } // NewConfig is a convenience function that returns a new Config object with non-nil maps func NewPreferences() *Preferences { - return &Preferences{Extensions: make(map[string]*runtime.EmbeddedObject)} + return &Preferences{Extensions: make(map[string]runtime.Object)} } diff --git a/pkg/client/unversioned/clientcmd/api/v1/conversion.go b/pkg/client/unversioned/clientcmd/api/v1/conversion.go index cffdeb6ea9e..e03fc60b164 100644 --- a/pkg/client/unversioned/clientcmd/api/v1/conversion.go +++ b/pkg/client/unversioned/clientcmd/api/v1/conversion.go @@ -69,7 +69,7 @@ func init() { if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil { return err } - out.Extensions = make(map[string]*runtime.EmbeddedObject) + out.Extensions = make(map[string]runtime.Object) if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil { return err } @@ -192,10 +192,10 @@ func init() { return nil }, - func(in *[]NamedExtension, out *map[string]*runtime.EmbeddedObject, s conversion.Scope) error { + func(in *[]NamedExtension, out *map[string]runtime.Object, s conversion.Scope) error { for _, curr := range *in { - newExtension := &runtime.EmbeddedObject{} - if err := s.Convert(&curr.Extension, newExtension, 0); err != nil { + var newExtension runtime.Object + if err := s.Convert(&curr.Extension, &newExtension, 0); err != nil { return err } (*out)[curr.Name] = newExtension @@ -203,7 +203,7 @@ func init() { return nil }, - func(in *map[string]*runtime.EmbeddedObject, out *[]NamedExtension, s conversion.Scope) error { + func(in *map[string]runtime.Object, out *[]NamedExtension, s conversion.Scope) error { allKeys := make([]string, 0, len(*in)) for key := range *in { allKeys = append(allKeys, key) diff --git a/pkg/client/unversioned/clientcmd/api/v1/register.go b/pkg/client/unversioned/clientcmd/api/v1/register.go index 055ce52d3ae..edf9fe1a70a 100644 --- a/pkg/client/unversioned/clientcmd/api/v1/register.go +++ b/pkg/client/unversioned/clientcmd/api/v1/register.go @@ -19,16 +19,12 @@ package v1 import ( "k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" - "k8s.io/kubernetes/pkg/runtime" ) // SchemeGroupVersion is group version used to register these objects // TODO this should be in the "kubeconfig" group var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: "v1"} -// Codec encodes internal objects to the v1 scheme -var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion) - func init() { api.Scheme.AddKnownTypes(SchemeGroupVersion, &Config{}, diff --git a/pkg/client/unversioned/clientcmd/loader.go b/pkg/client/unversioned/clientcmd/loader.go index 42f1210d5b0..55b3dbcd2ce 100644 --- a/pkg/client/unversioned/clientcmd/loader.go +++ b/pkg/client/unversioned/clientcmd/loader.go @@ -25,12 +25,13 @@ import ( "path/filepath" "strings" - "github.com/ghodss/yaml" "github.com/golang/glog" "github.com/imdario/mergo" + "k8s.io/kubernetes/pkg/api/unversioned" clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest" + "k8s.io/kubernetes/pkg/runtime" utilerrors "k8s.io/kubernetes/pkg/util/errors" ) @@ -263,11 +264,11 @@ func Load(data []byte) (*clientcmdapi.Config, error) { if len(data) == 0 { return config, nil } - - if err := clientcmdlatest.Codec.DecodeInto(data, config); err != nil { + decoded, _, err := clientcmdlatest.Codec.Decode(data, &unversioned.GroupVersionKind{Version: clientcmdlatest.Version, Kind: "Config"}, config) + if err != nil { return nil, err } - return config, nil + return decoded.(*clientcmdapi.Config), nil } // WriteToFile serializes the config to yaml and writes it out to a file. If not present, it creates the file with the mode 0600. If it is present @@ -292,15 +293,7 @@ func WriteToFile(config clientcmdapi.Config, filename string) error { // Write serializes the config to yaml. // Encapsulates serialization without assuming the destination is a file. func Write(config clientcmdapi.Config) ([]byte, error) { - json, err := clientcmdlatest.Codec.Encode(&config) - if err != nil { - return nil, err - } - content, err := yaml.JSONToYAML(json) - if err != nil { - return nil, err - } - return content, nil + return runtime.Encode(clientcmdlatest.Codec, &config) } func (rules ClientConfigLoadingRules) ResolvePaths() bool { diff --git a/pkg/client/unversioned/clientcmd/loader_test.go b/pkg/client/unversioned/clientcmd/loader_test.go index 22926e7752a..0921f7043ae 100644 --- a/pkg/client/unversioned/clientcmd/loader_test.go +++ b/pkg/client/unversioned/clientcmd/loader_test.go @@ -30,6 +30,7 @@ import ( clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest" + "k8s.io/kubernetes/pkg/runtime" ) var ( @@ -391,7 +392,7 @@ func ExampleNoMergingOnExplicitPaths() { mergedConfig, err := loadingRules.Load() - json, err := clientcmdlatest.Codec.Encode(mergedConfig) + json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig) if err != nil { fmt.Printf("Unexpected error: %v", err) } @@ -437,7 +438,7 @@ func ExampleMergingSomeWithConflict() { mergedConfig, err := loadingRules.Load() - json, err := clientcmdlatest.Codec.Encode(mergedConfig) + json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig) if err != nil { fmt.Printf("Unexpected error: %v", err) } @@ -496,7 +497,7 @@ func ExampleMergingEverythingNoConflicts() { mergedConfig, err := loadingRules.Load() - json, err := clientcmdlatest.Codec.Encode(mergedConfig) + json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig) if err != nil { fmt.Printf("Unexpected error: %v", err) }