e22e5f81ec70e6d717375f083b64aabac43443ed
[oom/registrator.git] /
1 /*
2 Copyright 2014 The Kubernetes Authors.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package v1
18
19 import (
20         "sort"
21
22         "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
23         "k8s.io/kubernetes/pkg/conversion"
24         "k8s.io/kubernetes/pkg/runtime"
25 )
26
27 func init() {
28         err := api.Scheme.AddConversionFuncs(
29                 func(in *Cluster, out *api.Cluster, s conversion.Scope) error {
30                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
31                 },
32                 func(in *api.Cluster, out *Cluster, s conversion.Scope) error {
33                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
34                 },
35                 func(in *Preferences, out *api.Preferences, s conversion.Scope) error {
36                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
37                 },
38                 func(in *api.Preferences, out *Preferences, s conversion.Scope) error {
39                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
40                 },
41                 func(in *AuthInfo, out *api.AuthInfo, s conversion.Scope) error {
42                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
43                 },
44                 func(in *api.AuthInfo, out *AuthInfo, s conversion.Scope) error {
45                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
46                 },
47                 func(in *Context, out *api.Context, s conversion.Scope) error {
48                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
49                 },
50                 func(in *api.Context, out *Context, s conversion.Scope) error {
51                         return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
52                 },
53
54                 func(in *Config, out *api.Config, s conversion.Scope) error {
55                         out.CurrentContext = in.CurrentContext
56                         if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil {
57                                 return err
58                         }
59
60                         out.Clusters = make(map[string]*api.Cluster)
61                         if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil {
62                                 return err
63                         }
64                         out.AuthInfos = make(map[string]*api.AuthInfo)
65                         if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil {
66                                 return err
67                         }
68                         out.Contexts = make(map[string]*api.Context)
69                         if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
70                                 return err
71                         }
72                         out.Extensions = make(map[string]runtime.Object)
73                         if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
74                                 return err
75                         }
76                         return nil
77                 },
78                 func(in *api.Config, out *Config, s conversion.Scope) error {
79                         out.CurrentContext = in.CurrentContext
80                         if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil {
81                                 return err
82                         }
83
84                         out.Clusters = make([]NamedCluster, 0, 0)
85                         if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil {
86                                 return err
87                         }
88                         out.AuthInfos = make([]NamedAuthInfo, 0, 0)
89                         if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil {
90                                 return err
91                         }
92                         out.Contexts = make([]NamedContext, 0, 0)
93                         if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
94                                 return err
95                         }
96                         out.Extensions = make([]NamedExtension, 0, 0)
97                         if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
98                                 return err
99                         }
100                         return nil
101                 },
102                 func(in *[]NamedCluster, out *map[string]*api.Cluster, s conversion.Scope) error {
103                         for _, curr := range *in {
104                                 newCluster := api.NewCluster()
105                                 if err := s.Convert(&curr.Cluster, newCluster, 0); err != nil {
106                                         return err
107                                 }
108                                 (*out)[curr.Name] = newCluster
109                         }
110
111                         return nil
112                 },
113                 func(in *map[string]*api.Cluster, out *[]NamedCluster, s conversion.Scope) error {
114                         allKeys := make([]string, 0, len(*in))
115                         for key := range *in {
116                                 allKeys = append(allKeys, key)
117                         }
118                         sort.Strings(allKeys)
119
120                         for _, key := range allKeys {
121                                 newCluster := (*in)[key]
122                                 oldCluster := &Cluster{}
123                                 if err := s.Convert(newCluster, oldCluster, 0); err != nil {
124                                         return err
125                                 }
126
127                                 namedCluster := NamedCluster{key, *oldCluster}
128                                 *out = append(*out, namedCluster)
129                         }
130
131                         return nil
132                 },
133                 func(in *[]NamedAuthInfo, out *map[string]*api.AuthInfo, s conversion.Scope) error {
134                         for _, curr := range *in {
135                                 newAuthInfo := api.NewAuthInfo()
136                                 if err := s.Convert(&curr.AuthInfo, newAuthInfo, 0); err != nil {
137                                         return err
138                                 }
139                                 (*out)[curr.Name] = newAuthInfo
140                         }
141
142                         return nil
143                 },
144                 func(in *map[string]*api.AuthInfo, out *[]NamedAuthInfo, s conversion.Scope) error {
145                         allKeys := make([]string, 0, len(*in))
146                         for key := range *in {
147                                 allKeys = append(allKeys, key)
148                         }
149                         sort.Strings(allKeys)
150
151                         for _, key := range allKeys {
152                                 newAuthInfo := (*in)[key]
153                                 oldAuthInfo := &AuthInfo{}
154                                 if err := s.Convert(newAuthInfo, oldAuthInfo, 0); err != nil {
155                                         return err
156                                 }
157
158                                 namedAuthInfo := NamedAuthInfo{key, *oldAuthInfo}
159                                 *out = append(*out, namedAuthInfo)
160                         }
161
162                         return nil
163                 },
164                 func(in *[]NamedContext, out *map[string]*api.Context, s conversion.Scope) error {
165                         for _, curr := range *in {
166                                 newContext := api.NewContext()
167                                 if err := s.Convert(&curr.Context, newContext, 0); err != nil {
168                                         return err
169                                 }
170                                 (*out)[curr.Name] = newContext
171                         }
172
173                         return nil
174                 },
175                 func(in *map[string]*api.Context, out *[]NamedContext, s conversion.Scope) error {
176                         allKeys := make([]string, 0, len(*in))
177                         for key := range *in {
178                                 allKeys = append(allKeys, key)
179                         }
180                         sort.Strings(allKeys)
181
182                         for _, key := range allKeys {
183                                 newContext := (*in)[key]
184                                 oldContext := &Context{}
185                                 if err := s.Convert(newContext, oldContext, 0); err != nil {
186                                         return err
187                                 }
188
189                                 namedContext := NamedContext{key, *oldContext}
190                                 *out = append(*out, namedContext)
191                         }
192
193                         return nil
194                 },
195                 func(in *[]NamedExtension, out *map[string]runtime.Object, s conversion.Scope) error {
196                         for _, curr := range *in {
197                                 var newExtension runtime.Object
198                                 if err := s.Convert(&curr.Extension, &newExtension, 0); err != nil {
199                                         return err
200                                 }
201                                 (*out)[curr.Name] = newExtension
202                         }
203
204                         return nil
205                 },
206                 func(in *map[string]runtime.Object, out *[]NamedExtension, s conversion.Scope) error {
207                         allKeys := make([]string, 0, len(*in))
208                         for key := range *in {
209                                 allKeys = append(allKeys, key)
210                         }
211                         sort.Strings(allKeys)
212
213                         for _, key := range allKeys {
214                                 newExtension := (*in)[key]
215                                 oldExtension := &runtime.RawExtension{}
216                                 if err := s.Convert(newExtension, oldExtension, 0); err != nil {
217                                         return err
218                                 }
219
220                                 namedExtension := NamedExtension{key, *oldExtension}
221                                 *out = append(*out, namedExtension)
222                         }
223
224                         return nil
225                 },
226         )
227         if err != nil {
228                 // If one of the conversion functions is malformed, detect it immediately.
229                 panic(err)
230         }
231 }