2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
23 "k8s.io/apimachinery/pkg/conversion"
24 "k8s.io/apimachinery/pkg/runtime"
25 "k8s.io/client-go/tools/clientcmd/api"
28 func addConversionFuncs(scheme *runtime.Scheme) error {
29 return scheme.AddConversionFuncs(
30 func(in *Cluster, out *api.Cluster, s conversion.Scope) error {
31 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
33 func(in *api.Cluster, out *Cluster, s conversion.Scope) error {
34 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
36 func(in *Preferences, out *api.Preferences, s conversion.Scope) error {
37 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
39 func(in *api.Preferences, out *Preferences, s conversion.Scope) error {
40 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
42 func(in *AuthInfo, out *api.AuthInfo, s conversion.Scope) error {
43 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
45 func(in *api.AuthInfo, out *AuthInfo, s conversion.Scope) error {
46 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
48 func(in *Context, out *api.Context, s conversion.Scope) error {
49 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
51 func(in *api.Context, out *Context, s conversion.Scope) error {
52 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
55 func(in *Config, out *api.Config, s conversion.Scope) error {
56 out.CurrentContext = in.CurrentContext
57 if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil {
61 out.Clusters = make(map[string]*api.Cluster)
62 if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil {
65 out.AuthInfos = make(map[string]*api.AuthInfo)
66 if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil {
69 out.Contexts = make(map[string]*api.Context)
70 if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
73 out.Extensions = make(map[string]runtime.Object)
74 if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
79 func(in *api.Config, out *Config, s conversion.Scope) error {
80 out.CurrentContext = in.CurrentContext
81 if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil {
85 out.Clusters = make([]NamedCluster, 0, 0)
86 if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil {
89 out.AuthInfos = make([]NamedAuthInfo, 0, 0)
90 if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil {
93 out.Contexts = make([]NamedContext, 0, 0)
94 if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
97 out.Extensions = make([]NamedExtension, 0, 0)
98 if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
103 func(in *[]NamedCluster, out *map[string]*api.Cluster, s conversion.Scope) error {
104 for _, curr := range *in {
105 newCluster := api.NewCluster()
106 if err := s.Convert(&curr.Cluster, newCluster, 0); err != nil {
109 if (*out)[curr.Name] == nil {
110 (*out)[curr.Name] = newCluster
112 return fmt.Errorf("error converting *[]NamedCluster into *map[string]*api.Cluster: duplicate name \"%v\" in list: %v", curr.Name, *in)
118 func(in *map[string]*api.Cluster, out *[]NamedCluster, s conversion.Scope) error {
119 allKeys := make([]string, 0, len(*in))
120 for key := range *in {
121 allKeys = append(allKeys, key)
123 sort.Strings(allKeys)
125 for _, key := range allKeys {
126 newCluster := (*in)[key]
127 oldCluster := &Cluster{}
128 if err := s.Convert(newCluster, oldCluster, 0); err != nil {
132 namedCluster := NamedCluster{key, *oldCluster}
133 *out = append(*out, namedCluster)
138 func(in *[]NamedAuthInfo, out *map[string]*api.AuthInfo, s conversion.Scope) error {
139 for _, curr := range *in {
140 newAuthInfo := api.NewAuthInfo()
141 if err := s.Convert(&curr.AuthInfo, newAuthInfo, 0); err != nil {
144 if (*out)[curr.Name] == nil {
145 (*out)[curr.Name] = newAuthInfo
147 return fmt.Errorf("error converting *[]NamedAuthInfo into *map[string]*api.AuthInfo: duplicate name \"%v\" in list: %v", curr.Name, *in)
153 func(in *map[string]*api.AuthInfo, out *[]NamedAuthInfo, s conversion.Scope) error {
154 allKeys := make([]string, 0, len(*in))
155 for key := range *in {
156 allKeys = append(allKeys, key)
158 sort.Strings(allKeys)
160 for _, key := range allKeys {
161 newAuthInfo := (*in)[key]
162 oldAuthInfo := &AuthInfo{}
163 if err := s.Convert(newAuthInfo, oldAuthInfo, 0); err != nil {
167 namedAuthInfo := NamedAuthInfo{key, *oldAuthInfo}
168 *out = append(*out, namedAuthInfo)
173 func(in *[]NamedContext, out *map[string]*api.Context, s conversion.Scope) error {
174 for _, curr := range *in {
175 newContext := api.NewContext()
176 if err := s.Convert(&curr.Context, newContext, 0); err != nil {
179 if (*out)[curr.Name] == nil {
180 (*out)[curr.Name] = newContext
182 return fmt.Errorf("error converting *[]NamedContext into *map[string]*api.Context: duplicate name \"%v\" in list: %v", curr.Name, *in)
188 func(in *map[string]*api.Context, out *[]NamedContext, s conversion.Scope) error {
189 allKeys := make([]string, 0, len(*in))
190 for key := range *in {
191 allKeys = append(allKeys, key)
193 sort.Strings(allKeys)
195 for _, key := range allKeys {
196 newContext := (*in)[key]
197 oldContext := &Context{}
198 if err := s.Convert(newContext, oldContext, 0); err != nil {
202 namedContext := NamedContext{key, *oldContext}
203 *out = append(*out, namedContext)
208 func(in *[]NamedExtension, out *map[string]runtime.Object, s conversion.Scope) error {
209 for _, curr := range *in {
210 var newExtension runtime.Object
211 if err := s.Convert(&curr.Extension, &newExtension, 0); err != nil {
214 if (*out)[curr.Name] == nil {
215 (*out)[curr.Name] = newExtension
217 return fmt.Errorf("error converting *[]NamedExtension into *map[string]runtime.Object: duplicate name \"%v\" in list: %v", curr.Name, *in)
223 func(in *map[string]runtime.Object, out *[]NamedExtension, s conversion.Scope) error {
224 allKeys := make([]string, 0, len(*in))
225 for key := range *in {
226 allKeys = append(allKeys, key)
228 sort.Strings(allKeys)
230 for _, key := range allKeys {
231 newExtension := (*in)[key]
232 oldExtension := &runtime.RawExtension{}
233 if err := s.Convert(newExtension, oldExtension, 0); err != nil {
237 namedExtension := NamedExtension{key, *oldExtension}
238 *out = append(*out, namedExtension)