Update clientcmd.Config to use new Decode methods

This commit is contained in:
Clayton Coleman 2015-12-21 00:31:34 -05:00
parent 4386e8cc38
commit 24a7919002
7 changed files with 36 additions and 42 deletions

View File

@ -18,8 +18,11 @@ package latest
import ( import (
"k8s.io/kubernetes/pkg/api/unversioned" "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"
"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. // 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. // with a set of versions to choose.
var Versions = []string{"v1"} var Versions = []string{"v1"}
// Codec is the default codec for serializing output that should use var Codec = versioning.NewCodecForScheme(
// the latest supported version. Use this Codec when writing to api.Scheme,
// disk, a data store that is not dynamically versioned, or in tests. json.NewYAMLSerializer(json.DefaultMetaFactory, api.Scheme, runtime.ObjectTyperToTyper(api.Scheme)),
// This codec can decode any object that Kubernetes is aware of. []unversioned.GroupVersion{{Version: Version}},
var Codec = runtime.YAMLDecoder(v1.Codec) []unversioned.GroupVersion{{Version: runtime.APIVersionInternal}},
)

View File

@ -26,7 +26,7 @@ var Scheme = runtime.NewScheme()
// SchemeGroupVersion is group version used to register these objects // SchemeGroupVersion is group version used to register these objects
// TODO this should be in the "kubeconfig" group // TODO this should be in the "kubeconfig" group
var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: ""} var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: runtime.APIVersionInternal}
func init() { func init() {
Scheme.AddKnownTypes(SchemeGroupVersion, Scheme.AddKnownTypes(SchemeGroupVersion,

View File

@ -42,14 +42,14 @@ type Config struct {
// CurrentContext is the name of the context that you would like to use by default // CurrentContext is the name of the context that you would like to use by default
CurrentContext string `json:"current-context"` 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 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() // IMPORTANT if you add fields to this struct, please update IsConfigEmpty()
type Preferences struct { type Preferences struct {
Colors bool `json:"colors,omitempty"` 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 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 // 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 contains PEM-encoded certificate authority certificates. Overrides CertificateAuthority
CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"` 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 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. // 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 is the password for basic authentication to the kubernetes cluster.
Password string `json:"password,omitempty"` 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 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) // 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 is the default namespace to use on unspecified requests
Namespace string `json:"namespace,omitempty"` 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 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 // 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), Clusters: make(map[string]*Cluster),
AuthInfos: make(map[string]*AuthInfo), AuthInfos: make(map[string]*AuthInfo),
Contexts: make(map[string]*Context), 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 // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewContext() *Context { 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 // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewCluster() *Cluster { 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 // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewAuthInfo() *AuthInfo { 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 // NewConfig is a convenience function that returns a new Config object with non-nil maps
func NewPreferences() *Preferences { func NewPreferences() *Preferences {
return &Preferences{Extensions: make(map[string]*runtime.EmbeddedObject)} return &Preferences{Extensions: make(map[string]runtime.Object)}
} }

View File

@ -69,7 +69,7 @@ func init() {
if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil { if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
return err 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 { if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
return err return err
} }
@ -192,10 +192,10 @@ func init() {
return nil 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 { for _, curr := range *in {
newExtension := &runtime.EmbeddedObject{} var newExtension runtime.Object
if err := s.Convert(&curr.Extension, newExtension, 0); err != nil { if err := s.Convert(&curr.Extension, &newExtension, 0); err != nil {
return err return err
} }
(*out)[curr.Name] = newExtension (*out)[curr.Name] = newExtension
@ -203,7 +203,7 @@ func init() {
return nil 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)) allKeys := make([]string, 0, len(*in))
for key := range *in { for key := range *in {
allKeys = append(allKeys, key) allKeys = append(allKeys, key)

View File

@ -19,16 +19,12 @@ package v1
import ( import (
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
"k8s.io/kubernetes/pkg/runtime"
) )
// SchemeGroupVersion is group version used to register these objects // SchemeGroupVersion is group version used to register these objects
// TODO this should be in the "kubeconfig" group // TODO this should be in the "kubeconfig" group
var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: "v1"} var SchemeGroupVersion = unversioned.GroupVersion{Group: "", Version: "v1"}
// Codec encodes internal objects to the v1 scheme
var Codec = runtime.CodecFor(api.Scheme, SchemeGroupVersion)
func init() { func init() {
api.Scheme.AddKnownTypes(SchemeGroupVersion, api.Scheme.AddKnownTypes(SchemeGroupVersion,
&Config{}, &Config{},

View File

@ -25,12 +25,13 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/imdario/mergo" "github.com/imdario/mergo"
"k8s.io/kubernetes/pkg/api/unversioned"
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest" clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
"k8s.io/kubernetes/pkg/runtime"
utilerrors "k8s.io/kubernetes/pkg/util/errors" utilerrors "k8s.io/kubernetes/pkg/util/errors"
) )
@ -263,11 +264,11 @@ func Load(data []byte) (*clientcmdapi.Config, error) {
if len(data) == 0 { if len(data) == 0 {
return config, nil return config, nil
} }
decoded, _, err := clientcmdlatest.Codec.Decode(data, &unversioned.GroupVersionKind{Version: clientcmdlatest.Version, Kind: "Config"}, config)
if err := clientcmdlatest.Codec.DecodeInto(data, config); err != nil { if err != nil {
return nil, err 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 // 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. // Write serializes the config to yaml.
// Encapsulates serialization without assuming the destination is a file. // Encapsulates serialization without assuming the destination is a file.
func Write(config clientcmdapi.Config) ([]byte, error) { func Write(config clientcmdapi.Config) ([]byte, error) {
json, err := clientcmdlatest.Codec.Encode(&config) return runtime.Encode(clientcmdlatest.Codec, &config)
if err != nil {
return nil, err
}
content, err := yaml.JSONToYAML(json)
if err != nil {
return nil, err
}
return content, nil
} }
func (rules ClientConfigLoadingRules) ResolvePaths() bool { func (rules ClientConfigLoadingRules) ResolvePaths() bool {

View File

@ -30,6 +30,7 @@ import (
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest" clientcmdlatest "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
"k8s.io/kubernetes/pkg/runtime"
) )
var ( var (
@ -391,7 +392,7 @@ func ExampleNoMergingOnExplicitPaths() {
mergedConfig, err := loadingRules.Load() mergedConfig, err := loadingRules.Load()
json, err := clientcmdlatest.Codec.Encode(mergedConfig) json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
if err != nil { if err != nil {
fmt.Printf("Unexpected error: %v", err) fmt.Printf("Unexpected error: %v", err)
} }
@ -437,7 +438,7 @@ func ExampleMergingSomeWithConflict() {
mergedConfig, err := loadingRules.Load() mergedConfig, err := loadingRules.Load()
json, err := clientcmdlatest.Codec.Encode(mergedConfig) json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
if err != nil { if err != nil {
fmt.Printf("Unexpected error: %v", err) fmt.Printf("Unexpected error: %v", err)
} }
@ -496,7 +497,7 @@ func ExampleMergingEverythingNoConflicts() {
mergedConfig, err := loadingRules.Load() mergedConfig, err := loadingRules.Load()
json, err := clientcmdlatest.Codec.Encode(mergedConfig) json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
if err != nil { if err != nil {
fmt.Printf("Unexpected error: %v", err) fmt.Printf("Unexpected error: %v", err)
} }