conversion.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1
  14. import (
  15. "fmt"
  16. "sort"
  17. "k8s.io/apimachinery/pkg/conversion"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/client-go/tools/clientcmd/api"
  20. )
  21. func addConversionFuncs(scheme *runtime.Scheme) error {
  22. return scheme.AddConversionFuncs(
  23. func(in *Cluster, out *api.Cluster, s conversion.Scope) error {
  24. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  25. },
  26. func(in *api.Cluster, out *Cluster, s conversion.Scope) error {
  27. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  28. },
  29. func(in *Preferences, out *api.Preferences, s conversion.Scope) error {
  30. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  31. },
  32. func(in *api.Preferences, out *Preferences, s conversion.Scope) error {
  33. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  34. },
  35. func(in *AuthInfo, out *api.AuthInfo, s conversion.Scope) error {
  36. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  37. },
  38. func(in *api.AuthInfo, out *AuthInfo, s conversion.Scope) error {
  39. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  40. },
  41. func(in *Context, out *api.Context, s conversion.Scope) error {
  42. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  43. },
  44. func(in *api.Context, out *Context, s conversion.Scope) error {
  45. return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
  46. },
  47. func(in *Config, out *api.Config, s conversion.Scope) error {
  48. out.CurrentContext = in.CurrentContext
  49. if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil {
  50. return err
  51. }
  52. out.Clusters = make(map[string]*api.Cluster)
  53. if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil {
  54. return err
  55. }
  56. out.AuthInfos = make(map[string]*api.AuthInfo)
  57. if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil {
  58. return err
  59. }
  60. out.Contexts = make(map[string]*api.Context)
  61. if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
  62. return err
  63. }
  64. out.Extensions = make(map[string]runtime.Object)
  65. if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
  66. return err
  67. }
  68. return nil
  69. },
  70. func(in *api.Config, out *Config, s conversion.Scope) error {
  71. out.CurrentContext = in.CurrentContext
  72. if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil {
  73. return err
  74. }
  75. out.Clusters = make([]NamedCluster, 0, 0)
  76. if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil {
  77. return err
  78. }
  79. out.AuthInfos = make([]NamedAuthInfo, 0, 0)
  80. if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil {
  81. return err
  82. }
  83. out.Contexts = make([]NamedContext, 0, 0)
  84. if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil {
  85. return err
  86. }
  87. out.Extensions = make([]NamedExtension, 0, 0)
  88. if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil {
  89. return err
  90. }
  91. return nil
  92. },
  93. func(in *[]NamedCluster, out *map[string]*api.Cluster, s conversion.Scope) error {
  94. for _, curr := range *in {
  95. newCluster := api.NewCluster()
  96. if err := s.Convert(&curr.Cluster, newCluster, 0); err != nil {
  97. return err
  98. }
  99. if (*out)[curr.Name] == nil {
  100. (*out)[curr.Name] = newCluster
  101. } else {
  102. return fmt.Errorf("error converting *[]NamedCluster into *map[string]*api.Cluster: duplicate name \"%v\" in list: %v", curr.Name, *in)
  103. }
  104. }
  105. return nil
  106. },
  107. func(in *map[string]*api.Cluster, out *[]NamedCluster, s conversion.Scope) error {
  108. allKeys := make([]string, 0, len(*in))
  109. for key := range *in {
  110. allKeys = append(allKeys, key)
  111. }
  112. sort.Strings(allKeys)
  113. for _, key := range allKeys {
  114. newCluster := (*in)[key]
  115. oldCluster := &Cluster{}
  116. if err := s.Convert(newCluster, oldCluster, 0); err != nil {
  117. return err
  118. }
  119. namedCluster := NamedCluster{key, *oldCluster}
  120. *out = append(*out, namedCluster)
  121. }
  122. return nil
  123. },
  124. func(in *[]NamedAuthInfo, out *map[string]*api.AuthInfo, s conversion.Scope) error {
  125. for _, curr := range *in {
  126. newAuthInfo := api.NewAuthInfo()
  127. if err := s.Convert(&curr.AuthInfo, newAuthInfo, 0); err != nil {
  128. return err
  129. }
  130. if (*out)[curr.Name] == nil {
  131. (*out)[curr.Name] = newAuthInfo
  132. } else {
  133. return fmt.Errorf("error converting *[]NamedAuthInfo into *map[string]*api.AuthInfo: duplicate name \"%v\" in list: %v", curr.Name, *in)
  134. }
  135. }
  136. return nil
  137. },
  138. func(in *map[string]*api.AuthInfo, out *[]NamedAuthInfo, s conversion.Scope) error {
  139. allKeys := make([]string, 0, len(*in))
  140. for key := range *in {
  141. allKeys = append(allKeys, key)
  142. }
  143. sort.Strings(allKeys)
  144. for _, key := range allKeys {
  145. newAuthInfo := (*in)[key]
  146. oldAuthInfo := &AuthInfo{}
  147. if err := s.Convert(newAuthInfo, oldAuthInfo, 0); err != nil {
  148. return err
  149. }
  150. namedAuthInfo := NamedAuthInfo{key, *oldAuthInfo}
  151. *out = append(*out, namedAuthInfo)
  152. }
  153. return nil
  154. },
  155. func(in *[]NamedContext, out *map[string]*api.Context, s conversion.Scope) error {
  156. for _, curr := range *in {
  157. newContext := api.NewContext()
  158. if err := s.Convert(&curr.Context, newContext, 0); err != nil {
  159. return err
  160. }
  161. if (*out)[curr.Name] == nil {
  162. (*out)[curr.Name] = newContext
  163. } else {
  164. return fmt.Errorf("error converting *[]NamedContext into *map[string]*api.Context: duplicate name \"%v\" in list: %v", curr.Name, *in)
  165. }
  166. }
  167. return nil
  168. },
  169. func(in *map[string]*api.Context, out *[]NamedContext, s conversion.Scope) error {
  170. allKeys := make([]string, 0, len(*in))
  171. for key := range *in {
  172. allKeys = append(allKeys, key)
  173. }
  174. sort.Strings(allKeys)
  175. for _, key := range allKeys {
  176. newContext := (*in)[key]
  177. oldContext := &Context{}
  178. if err := s.Convert(newContext, oldContext, 0); err != nil {
  179. return err
  180. }
  181. namedContext := NamedContext{key, *oldContext}
  182. *out = append(*out, namedContext)
  183. }
  184. return nil
  185. },
  186. func(in *[]NamedExtension, out *map[string]runtime.Object, s conversion.Scope) error {
  187. for _, curr := range *in {
  188. var newExtension runtime.Object
  189. if err := s.Convert(&curr.Extension, &newExtension, 0); err != nil {
  190. return err
  191. }
  192. if (*out)[curr.Name] == nil {
  193. (*out)[curr.Name] = newExtension
  194. } else {
  195. return fmt.Errorf("error converting *[]NamedExtension into *map[string]runtime.Object: duplicate name \"%v\" in list: %v", curr.Name, *in)
  196. }
  197. }
  198. return nil
  199. },
  200. func(in *map[string]runtime.Object, out *[]NamedExtension, s conversion.Scope) error {
  201. allKeys := make([]string, 0, len(*in))
  202. for key := range *in {
  203. allKeys = append(allKeys, key)
  204. }
  205. sort.Strings(allKeys)
  206. for _, key := range allKeys {
  207. newExtension := (*in)[key]
  208. oldExtension := &runtime.RawExtension{}
  209. if err := s.Convert(newExtension, oldExtension, 0); err != nil {
  210. return err
  211. }
  212. namedExtension := NamedExtension{key, *oldExtension}
  213. *out = append(*out, namedExtension)
  214. }
  215. return nil
  216. },
  217. )
  218. }