conversion.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. Copyright 2015 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. "encoding/json"
  16. "fmt"
  17. "reflect"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/apis/extensions"
  20. "k8s.io/kubernetes/pkg/conversion"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. "k8s.io/kubernetes/pkg/util/validation/field"
  23. "k8s.io/kubernetes/pkg/watch/versioned"
  24. )
  25. const (
  26. // Annotation key used to identify mirror pods.
  27. mirrorAnnotationKey = "kubernetes.io/config.mirror"
  28. // Value used to identify mirror pods from pre-v1.1 kubelet.
  29. mirrorAnnotationValue_1_0 = "mirror"
  30. // annotation key prefix used to identify non-convertible json paths.
  31. NonConvertibleAnnotationPrefix = "kubernetes.io/non-convertible"
  32. )
  33. // This is a "fast-path" that avoids reflection for common types. It focuses on the objects that are
  34. // converted the most in the cluster.
  35. // TODO: generate one of these for every external API group - this is to prove the impact
  36. func addFastPathConversionFuncs(scheme *runtime.Scheme) error {
  37. scheme.AddGenericConversionFunc(func(objA, objB interface{}, s conversion.Scope) (bool, error) {
  38. switch a := objA.(type) {
  39. case *Pod:
  40. switch b := objB.(type) {
  41. case *api.Pod:
  42. return true, Convert_v1_Pod_To_api_Pod(a, b, s)
  43. }
  44. case *api.Pod:
  45. switch b := objB.(type) {
  46. case *Pod:
  47. return true, Convert_api_Pod_To_v1_Pod(a, b, s)
  48. }
  49. case *Event:
  50. switch b := objB.(type) {
  51. case *api.Event:
  52. return true, Convert_v1_Event_To_api_Event(a, b, s)
  53. }
  54. case *api.Event:
  55. switch b := objB.(type) {
  56. case *Event:
  57. return true, Convert_api_Event_To_v1_Event(a, b, s)
  58. }
  59. case *ReplicationController:
  60. switch b := objB.(type) {
  61. case *api.ReplicationController:
  62. return true, Convert_v1_ReplicationController_To_api_ReplicationController(a, b, s)
  63. }
  64. case *api.ReplicationController:
  65. switch b := objB.(type) {
  66. case *ReplicationController:
  67. return true, Convert_api_ReplicationController_To_v1_ReplicationController(a, b, s)
  68. }
  69. case *Node:
  70. switch b := objB.(type) {
  71. case *api.Node:
  72. return true, Convert_v1_Node_To_api_Node(a, b, s)
  73. }
  74. case *api.Node:
  75. switch b := objB.(type) {
  76. case *Node:
  77. return true, Convert_api_Node_To_v1_Node(a, b, s)
  78. }
  79. case *Namespace:
  80. switch b := objB.(type) {
  81. case *api.Namespace:
  82. return true, Convert_v1_Namespace_To_api_Namespace(a, b, s)
  83. }
  84. case *api.Namespace:
  85. switch b := objB.(type) {
  86. case *Namespace:
  87. return true, Convert_api_Namespace_To_v1_Namespace(a, b, s)
  88. }
  89. case *Service:
  90. switch b := objB.(type) {
  91. case *api.Service:
  92. return true, Convert_v1_Service_To_api_Service(a, b, s)
  93. }
  94. case *api.Service:
  95. switch b := objB.(type) {
  96. case *Service:
  97. return true, Convert_api_Service_To_v1_Service(a, b, s)
  98. }
  99. case *Endpoints:
  100. switch b := objB.(type) {
  101. case *api.Endpoints:
  102. return true, Convert_v1_Endpoints_To_api_Endpoints(a, b, s)
  103. }
  104. case *api.Endpoints:
  105. switch b := objB.(type) {
  106. case *Endpoints:
  107. return true, Convert_api_Endpoints_To_v1_Endpoints(a, b, s)
  108. }
  109. case *versioned.Event:
  110. switch b := objB.(type) {
  111. case *versioned.InternalEvent:
  112. return true, versioned.Convert_versioned_Event_to_versioned_InternalEvent(a, b, s)
  113. }
  114. case *versioned.InternalEvent:
  115. switch b := objB.(type) {
  116. case *versioned.Event:
  117. return true, versioned.Convert_versioned_InternalEvent_to_versioned_Event(a, b, s)
  118. }
  119. }
  120. return false, nil
  121. })
  122. return nil
  123. }
  124. func addConversionFuncs(scheme *runtime.Scheme) error {
  125. // Add non-generated conversion functions
  126. err := scheme.AddConversionFuncs(
  127. Convert_api_Pod_To_v1_Pod,
  128. Convert_api_PodSpec_To_v1_PodSpec,
  129. Convert_api_ReplicationControllerSpec_To_v1_ReplicationControllerSpec,
  130. Convert_api_ServiceSpec_To_v1_ServiceSpec,
  131. Convert_v1_Pod_To_api_Pod,
  132. Convert_v1_PodSpec_To_api_PodSpec,
  133. Convert_v1_ReplicationControllerSpec_To_api_ReplicationControllerSpec,
  134. Convert_v1_Secret_To_api_Secret,
  135. Convert_v1_ServiceSpec_To_api_ServiceSpec,
  136. Convert_v1_ResourceList_To_api_ResourceList,
  137. Convert_v1_ReplicationController_to_extensions_ReplicaSet,
  138. Convert_v1_ReplicationControllerSpec_to_extensions_ReplicaSetSpec,
  139. Convert_v1_ReplicationControllerStatus_to_extensions_ReplicaSetStatus,
  140. Convert_extensions_ReplicaSet_to_v1_ReplicationController,
  141. Convert_extensions_ReplicaSetSpec_to_v1_ReplicationControllerSpec,
  142. Convert_extensions_ReplicaSetStatus_to_v1_ReplicationControllerStatus,
  143. )
  144. if err != nil {
  145. return err
  146. }
  147. // Add field label conversions for kinds having selectable nothing but ObjectMeta fields.
  148. for _, k := range []string{
  149. "Endpoints",
  150. "ResourceQuota",
  151. "PersistentVolumeClaim",
  152. "Service",
  153. "ServiceAccount",
  154. "ConfigMap",
  155. } {
  156. kind := k // don't close over range variables
  157. err = scheme.AddFieldLabelConversionFunc("v1", kind,
  158. func(label, value string) (string, string, error) {
  159. switch label {
  160. case "metadata.namespace",
  161. "metadata.name":
  162. return label, value, nil
  163. default:
  164. return "", "", fmt.Errorf("field label %q not supported for %q", label, kind)
  165. }
  166. },
  167. )
  168. if err != nil {
  169. return err
  170. }
  171. }
  172. // Add field conversion funcs.
  173. err = scheme.AddFieldLabelConversionFunc("v1", "Pod",
  174. func(label, value string) (string, string, error) {
  175. switch label {
  176. case "metadata.annotations",
  177. "metadata.labels",
  178. "metadata.name",
  179. "metadata.namespace",
  180. "spec.nodeName",
  181. "spec.restartPolicy",
  182. "spec.serviceAccountName",
  183. "status.phase",
  184. "status.podIP":
  185. return label, value, nil
  186. // This is for backwards compatibility with old v1 clients which send spec.host
  187. case "spec.host":
  188. return "spec.nodeName", value, nil
  189. default:
  190. return "", "", fmt.Errorf("field label not supported: %s", label)
  191. }
  192. },
  193. )
  194. if err != nil {
  195. return err
  196. }
  197. err = scheme.AddFieldLabelConversionFunc("v1", "Node",
  198. func(label, value string) (string, string, error) {
  199. switch label {
  200. case "metadata.name":
  201. return label, value, nil
  202. case "spec.unschedulable":
  203. return label, value, nil
  204. default:
  205. return "", "", fmt.Errorf("field label not supported: %s", label)
  206. }
  207. },
  208. )
  209. if err != nil {
  210. return err
  211. }
  212. err = scheme.AddFieldLabelConversionFunc("v1", "ReplicationController",
  213. func(label, value string) (string, string, error) {
  214. switch label {
  215. case "metadata.name",
  216. "metadata.namespace",
  217. "status.replicas":
  218. return label, value, nil
  219. default:
  220. return "", "", fmt.Errorf("field label not supported: %s", label)
  221. }
  222. })
  223. if err != nil {
  224. return err
  225. }
  226. err = scheme.AddFieldLabelConversionFunc("v1", "PersistentVolume",
  227. func(label, value string) (string, string, error) {
  228. switch label {
  229. case "metadata.name":
  230. return label, value, nil
  231. default:
  232. return "", "", fmt.Errorf("field label not supported: %s", label)
  233. }
  234. },
  235. )
  236. if err != nil {
  237. return err
  238. }
  239. if err := AddFieldLabelConversionsForEvent(scheme); err != nil {
  240. return err
  241. }
  242. if err := AddFieldLabelConversionsForNamespace(scheme); err != nil {
  243. return err
  244. }
  245. if err := AddFieldLabelConversionsForSecret(scheme); err != nil {
  246. return err
  247. }
  248. return nil
  249. }
  250. func Convert_v1_ReplicationController_to_extensions_ReplicaSet(in *ReplicationController, out *extensions.ReplicaSet, s conversion.Scope) error {
  251. if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
  252. defaulting.(func(*ReplicationController))(in)
  253. }
  254. if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil {
  255. return err
  256. }
  257. if err := Convert_v1_ObjectMeta_To_api_ObjectMeta(&in.ObjectMeta, &out.ObjectMeta, s); err != nil {
  258. return err
  259. }
  260. if err := Convert_v1_ReplicationControllerSpec_to_extensions_ReplicaSetSpec(&in.Spec, &out.Spec, s); err != nil {
  261. return err
  262. }
  263. if err := Convert_v1_ReplicationControllerStatus_to_extensions_ReplicaSetStatus(&in.Status, &out.Status, s); err != nil {
  264. return err
  265. }
  266. return nil
  267. }
  268. func Convert_v1_ReplicationControllerSpec_to_extensions_ReplicaSetSpec(in *ReplicationControllerSpec, out *extensions.ReplicaSetSpec, s conversion.Scope) error {
  269. if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
  270. defaulting.(func(*ReplicationControllerSpec))(in)
  271. }
  272. out.Replicas = *in.Replicas
  273. if in.Selector != nil {
  274. api.Convert_map_to_unversioned_LabelSelector(&in.Selector, out.Selector, s)
  275. }
  276. if in.Template != nil {
  277. if err := Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(in.Template, &out.Template, s); err != nil {
  278. return err
  279. }
  280. }
  281. return nil
  282. }
  283. func Convert_v1_ReplicationControllerStatus_to_extensions_ReplicaSetStatus(in *ReplicationControllerStatus, out *extensions.ReplicaSetStatus, s conversion.Scope) error {
  284. if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
  285. defaulting.(func(*ReplicationControllerStatus))(in)
  286. }
  287. out.Replicas = in.Replicas
  288. out.FullyLabeledReplicas = in.FullyLabeledReplicas
  289. out.ObservedGeneration = in.ObservedGeneration
  290. return nil
  291. }
  292. func Convert_extensions_ReplicaSet_to_v1_ReplicationController(in *extensions.ReplicaSet, out *ReplicationController, s conversion.Scope) error {
  293. if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
  294. defaulting.(func(*extensions.ReplicaSet))(in)
  295. }
  296. if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil {
  297. return err
  298. }
  299. if err := Convert_api_ObjectMeta_To_v1_ObjectMeta(&in.ObjectMeta, &out.ObjectMeta, s); err != nil {
  300. return err
  301. }
  302. if err := Convert_extensions_ReplicaSetSpec_to_v1_ReplicationControllerSpec(&in.Spec, &out.Spec, s); err != nil {
  303. fieldErr, ok := err.(*field.Error)
  304. if !ok {
  305. return err
  306. }
  307. if out.Annotations == nil {
  308. out.Annotations = make(map[string]string)
  309. }
  310. out.Annotations[NonConvertibleAnnotationPrefix+"/"+fieldErr.Field] = reflect.ValueOf(fieldErr.BadValue).String()
  311. }
  312. if err := Convert_extensions_ReplicaSetStatus_to_v1_ReplicationControllerStatus(&in.Status, &out.Status, s); err != nil {
  313. return err
  314. }
  315. return nil
  316. }
  317. func Convert_extensions_ReplicaSetSpec_to_v1_ReplicationControllerSpec(in *extensions.ReplicaSetSpec, out *ReplicationControllerSpec, s conversion.Scope) error {
  318. if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
  319. defaulting.(func(*extensions.ReplicaSetSpec))(in)
  320. }
  321. out.Replicas = new(int32)
  322. *out.Replicas = in.Replicas
  323. var invalidErr error
  324. if in.Selector != nil {
  325. invalidErr = api.Convert_unversioned_LabelSelector_to_map(in.Selector, &out.Selector, s)
  326. }
  327. out.Template = new(PodTemplateSpec)
  328. if err := Convert_api_PodTemplateSpec_To_v1_PodTemplateSpec(&in.Template, out.Template, s); err != nil {
  329. return err
  330. }
  331. return invalidErr
  332. }
  333. func Convert_extensions_ReplicaSetStatus_to_v1_ReplicationControllerStatus(in *extensions.ReplicaSetStatus, out *ReplicationControllerStatus, s conversion.Scope) error {
  334. if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found {
  335. defaulting.(func(*extensions.ReplicaSetStatus))(in)
  336. }
  337. out.Replicas = in.Replicas
  338. out.FullyLabeledReplicas = in.FullyLabeledReplicas
  339. out.ObservedGeneration = in.ObservedGeneration
  340. return nil
  341. }
  342. func Convert_api_ReplicationControllerSpec_To_v1_ReplicationControllerSpec(in *api.ReplicationControllerSpec, out *ReplicationControllerSpec, s conversion.Scope) error {
  343. out.Replicas = &in.Replicas
  344. out.Selector = in.Selector
  345. if in.Template != nil {
  346. out.Template = new(PodTemplateSpec)
  347. if err := Convert_api_PodTemplateSpec_To_v1_PodTemplateSpec(in.Template, out.Template, s); err != nil {
  348. return err
  349. }
  350. } else {
  351. out.Template = nil
  352. }
  353. return nil
  354. }
  355. func Convert_v1_ReplicationControllerSpec_To_api_ReplicationControllerSpec(in *ReplicationControllerSpec, out *api.ReplicationControllerSpec, s conversion.Scope) error {
  356. out.Replicas = *in.Replicas
  357. out.Selector = in.Selector
  358. if in.Template != nil {
  359. out.Template = new(api.PodTemplateSpec)
  360. if err := Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(in.Template, out.Template, s); err != nil {
  361. return err
  362. }
  363. } else {
  364. out.Template = nil
  365. }
  366. return nil
  367. }
  368. func Convert_api_PodStatusResult_To_v1_PodStatusResult(in *api.PodStatusResult, out *PodStatusResult, s conversion.Scope) error {
  369. if err := autoConvert_api_PodStatusResult_To_v1_PodStatusResult(in, out, s); err != nil {
  370. return err
  371. }
  372. if old := out.Annotations; old != nil {
  373. out.Annotations = make(map[string]string, len(old))
  374. for k, v := range old {
  375. out.Annotations[k] = v
  376. }
  377. }
  378. if len(out.Status.InitContainerStatuses) > 0 {
  379. if out.Annotations == nil {
  380. out.Annotations = make(map[string]string)
  381. }
  382. value, err := json.Marshal(out.Status.InitContainerStatuses)
  383. if err != nil {
  384. return err
  385. }
  386. out.Annotations[PodInitContainerStatusesAnnotationKey] = string(value)
  387. } else {
  388. delete(out.Annotations, PodInitContainerStatusesAnnotationKey)
  389. }
  390. return nil
  391. }
  392. func Convert_v1_PodStatusResult_To_api_PodStatusResult(in *PodStatusResult, out *api.PodStatusResult, s conversion.Scope) error {
  393. // TODO: sometime after we move init container to stable, remove these conversions
  394. if value, ok := in.Annotations[PodInitContainerStatusesAnnotationKey]; ok {
  395. var values []ContainerStatus
  396. if err := json.Unmarshal([]byte(value), &values); err != nil {
  397. return err
  398. }
  399. // Conversion from external to internal version exists more to
  400. // satisfy the needs of the decoder than it does to be a general
  401. // purpose tool. And Decode always creates an intermediate object
  402. // to decode to. Thus the caller of UnsafeConvertToVersion is
  403. // taking responsibility to ensure mutation of in is not exposed
  404. // back to the caller.
  405. in.Status.InitContainerStatuses = values
  406. }
  407. if err := autoConvert_v1_PodStatusResult_To_api_PodStatusResult(in, out, s); err != nil {
  408. return err
  409. }
  410. if len(out.Annotations) > 0 {
  411. old := out.Annotations
  412. out.Annotations = make(map[string]string, len(old))
  413. for k, v := range old {
  414. out.Annotations[k] = v
  415. }
  416. delete(out.Annotations, PodInitContainerStatusesAnnotationKey)
  417. }
  418. return nil
  419. }
  420. func Convert_api_PodTemplateSpec_To_v1_PodTemplateSpec(in *api.PodTemplateSpec, out *PodTemplateSpec, s conversion.Scope) error {
  421. if err := autoConvert_api_PodTemplateSpec_To_v1_PodTemplateSpec(in, out, s); err != nil {
  422. return err
  423. }
  424. // TODO: sometime after we move init container to stable, remove these conversions.
  425. if old := out.Annotations; old != nil {
  426. out.Annotations = make(map[string]string, len(old))
  427. for k, v := range old {
  428. out.Annotations[k] = v
  429. }
  430. }
  431. if len(out.Spec.InitContainers) > 0 {
  432. if out.Annotations == nil {
  433. out.Annotations = make(map[string]string)
  434. }
  435. value, err := json.Marshal(out.Spec.InitContainers)
  436. if err != nil {
  437. return err
  438. }
  439. out.Annotations[PodInitContainersAnnotationKey] = string(value)
  440. out.Annotations[PodInitContainersBetaAnnotationKey] = string(value)
  441. } else {
  442. delete(out.Annotations, PodInitContainersAnnotationKey)
  443. delete(out.Annotations, PodInitContainersBetaAnnotationKey)
  444. }
  445. return nil
  446. }
  447. func Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(in *PodTemplateSpec, out *api.PodTemplateSpec, s conversion.Scope) error {
  448. // TODO: sometime after we move init container to stable, remove these conversions
  449. // If there is a beta annotation, copy to alpha key.
  450. // See commit log for PR #31026 for why we do this.
  451. if valueBeta, okBeta := in.Annotations[PodInitContainersBetaAnnotationKey]; okBeta {
  452. in.Annotations[PodInitContainersAnnotationKey] = valueBeta
  453. }
  454. // Move the annotation to the internal repr. field
  455. if value, ok := in.Annotations[PodInitContainersAnnotationKey]; ok {
  456. var values []Container
  457. if err := json.Unmarshal([]byte(value), &values); err != nil {
  458. return err
  459. }
  460. // Conversion from external to internal version exists more to
  461. // satisfy the needs of the decoder than it does to be a general
  462. // purpose tool. And Decode always creates an intermediate object
  463. // to decode to. Thus the caller of UnsafeConvertToVersion is
  464. // taking responsibility to ensure mutation of in is not exposed
  465. // back to the caller.
  466. in.Spec.InitContainers = values
  467. }
  468. if err := autoConvert_v1_PodTemplateSpec_To_api_PodTemplateSpec(in, out, s); err != nil {
  469. return err
  470. }
  471. if len(out.Annotations) > 0 {
  472. old := out.Annotations
  473. out.Annotations = make(map[string]string, len(old))
  474. for k, v := range old {
  475. out.Annotations[k] = v
  476. }
  477. delete(out.Annotations, PodInitContainersAnnotationKey)
  478. delete(out.Annotations, PodInitContainersBetaAnnotationKey)
  479. }
  480. return nil
  481. }
  482. // The following two PodSpec conversions are done here to support ServiceAccount
  483. // as an alias for ServiceAccountName.
  484. func Convert_api_PodSpec_To_v1_PodSpec(in *api.PodSpec, out *PodSpec, s conversion.Scope) error {
  485. if err := autoConvert_api_PodSpec_To_v1_PodSpec(in, out, s); err != nil {
  486. return err
  487. }
  488. // DeprecatedServiceAccount is an alias for ServiceAccountName.
  489. out.DeprecatedServiceAccount = in.ServiceAccountName
  490. if in.SecurityContext != nil {
  491. // the host namespace fields have to be handled here for backward compatibility
  492. // with v1.0.0
  493. out.HostPID = in.SecurityContext.HostPID
  494. out.HostNetwork = in.SecurityContext.HostNetwork
  495. out.HostIPC = in.SecurityContext.HostIPC
  496. }
  497. return nil
  498. }
  499. func Convert_v1_PodSpec_To_api_PodSpec(in *PodSpec, out *api.PodSpec, s conversion.Scope) error {
  500. if err := autoConvert_v1_PodSpec_To_api_PodSpec(in, out, s); err != nil {
  501. return err
  502. }
  503. // We support DeprecatedServiceAccount as an alias for ServiceAccountName.
  504. // If both are specified, ServiceAccountName (the new field) wins.
  505. if in.ServiceAccountName == "" {
  506. out.ServiceAccountName = in.DeprecatedServiceAccount
  507. }
  508. // the host namespace fields have to be handled specially for backward compatibility
  509. // with v1.0.0
  510. if out.SecurityContext == nil {
  511. out.SecurityContext = new(api.PodSecurityContext)
  512. }
  513. out.SecurityContext.HostNetwork = in.HostNetwork
  514. out.SecurityContext.HostPID = in.HostPID
  515. out.SecurityContext.HostIPC = in.HostIPC
  516. return nil
  517. }
  518. func Convert_api_Pod_To_v1_Pod(in *api.Pod, out *Pod, s conversion.Scope) error {
  519. if err := autoConvert_api_Pod_To_v1_Pod(in, out, s); err != nil {
  520. return err
  521. }
  522. // TODO: sometime after we move init container to stable, remove these conversions
  523. if len(out.Spec.InitContainers) > 0 || len(out.Status.InitContainerStatuses) > 0 {
  524. old := out.Annotations
  525. out.Annotations = make(map[string]string, len(old))
  526. for k, v := range old {
  527. out.Annotations[k] = v
  528. }
  529. delete(out.Annotations, PodInitContainersAnnotationKey)
  530. delete(out.Annotations, PodInitContainersBetaAnnotationKey)
  531. delete(out.Annotations, PodInitContainerStatusesAnnotationKey)
  532. }
  533. if len(out.Spec.InitContainers) > 0 {
  534. value, err := json.Marshal(out.Spec.InitContainers)
  535. if err != nil {
  536. return err
  537. }
  538. out.Annotations[PodInitContainersAnnotationKey] = string(value)
  539. out.Annotations[PodInitContainersBetaAnnotationKey] = string(value)
  540. }
  541. if len(out.Status.InitContainerStatuses) > 0 {
  542. value, err := json.Marshal(out.Status.InitContainerStatuses)
  543. if err != nil {
  544. return err
  545. }
  546. out.Annotations[PodInitContainerStatusesAnnotationKey] = string(value)
  547. }
  548. // We need to reset certain fields for mirror pods from pre-v1.1 kubelet
  549. // (#15960).
  550. // TODO: Remove this code after we drop support for v1.0 kubelets.
  551. if value, ok := in.Annotations[mirrorAnnotationKey]; ok && value == mirrorAnnotationValue_1_0 {
  552. // Reset the TerminationGracePeriodSeconds.
  553. out.Spec.TerminationGracePeriodSeconds = nil
  554. // Reset the resource requests.
  555. for i := range out.Spec.Containers {
  556. out.Spec.Containers[i].Resources.Requests = nil
  557. }
  558. }
  559. return nil
  560. }
  561. func Convert_v1_Pod_To_api_Pod(in *Pod, out *api.Pod, s conversion.Scope) error {
  562. // If there is a beta annotation, copy to alpha key.
  563. // See commit log for PR #31026 for why we do this.
  564. if valueBeta, okBeta := in.Annotations[PodInitContainersBetaAnnotationKey]; okBeta {
  565. in.Annotations[PodInitContainersAnnotationKey] = valueBeta
  566. }
  567. // TODO: sometime after we move init container to stable, remove these conversions
  568. // Move the annotation to the internal repr. field
  569. if value, ok := in.Annotations[PodInitContainersAnnotationKey]; ok {
  570. var values []Container
  571. if err := json.Unmarshal([]byte(value), &values); err != nil {
  572. return err
  573. }
  574. // Conversion from external to internal version exists more to
  575. // satisfy the needs of the decoder than it does to be a general
  576. // purpose tool. And Decode always creates an intermediate object
  577. // to decode to. Thus the caller of UnsafeConvertToVersion is
  578. // taking responsibility to ensure mutation of in is not exposed
  579. // back to the caller.
  580. in.Spec.InitContainers = values
  581. }
  582. if value, ok := in.Annotations[PodInitContainerStatusesAnnotationKey]; ok {
  583. var values []ContainerStatus
  584. if err := json.Unmarshal([]byte(value), &values); err != nil {
  585. return err
  586. }
  587. // Conversion from external to internal version exists more to
  588. // satisfy the needs of the decoder than it does to be a general
  589. // purpose tool. And Decode always creates an intermediate object
  590. // to decode to. Thus the caller of UnsafeConvertToVersion is
  591. // taking responsibility to ensure mutation of in is not exposed
  592. // back to the caller.
  593. in.Status.InitContainerStatuses = values
  594. }
  595. if err := autoConvert_v1_Pod_To_api_Pod(in, out, s); err != nil {
  596. return err
  597. }
  598. if len(out.Annotations) > 0 {
  599. old := out.Annotations
  600. out.Annotations = make(map[string]string, len(old))
  601. for k, v := range old {
  602. out.Annotations[k] = v
  603. }
  604. delete(out.Annotations, PodInitContainersAnnotationKey)
  605. delete(out.Annotations, PodInitContainersBetaAnnotationKey)
  606. delete(out.Annotations, PodInitContainerStatusesAnnotationKey)
  607. }
  608. return nil
  609. }
  610. func Convert_api_ServiceSpec_To_v1_ServiceSpec(in *api.ServiceSpec, out *ServiceSpec, s conversion.Scope) error {
  611. if err := autoConvert_api_ServiceSpec_To_v1_ServiceSpec(in, out, s); err != nil {
  612. return err
  613. }
  614. // Publish both externalIPs and deprecatedPublicIPs fields in v1.
  615. out.DeprecatedPublicIPs = in.ExternalIPs
  616. return nil
  617. }
  618. func Convert_v1_Secret_To_api_Secret(in *Secret, out *api.Secret, s conversion.Scope) error {
  619. if err := autoConvert_v1_Secret_To_api_Secret(in, out, s); err != nil {
  620. return err
  621. }
  622. // StringData overwrites Data
  623. if len(in.StringData) > 0 {
  624. if out.Data == nil {
  625. out.Data = map[string][]byte{}
  626. }
  627. for k, v := range in.StringData {
  628. out.Data[k] = []byte(v)
  629. }
  630. }
  631. return nil
  632. }
  633. func Convert_v1_ServiceSpec_To_api_ServiceSpec(in *ServiceSpec, out *api.ServiceSpec, s conversion.Scope) error {
  634. if err := autoConvert_v1_ServiceSpec_To_api_ServiceSpec(in, out, s); err != nil {
  635. return err
  636. }
  637. // Prefer the legacy deprecatedPublicIPs field, if provided.
  638. if len(in.DeprecatedPublicIPs) > 0 {
  639. out.ExternalIPs = in.DeprecatedPublicIPs
  640. }
  641. return nil
  642. }
  643. func Convert_api_PodSecurityContext_To_v1_PodSecurityContext(in *api.PodSecurityContext, out *PodSecurityContext, s conversion.Scope) error {
  644. out.SupplementalGroups = in.SupplementalGroups
  645. if in.SELinuxOptions != nil {
  646. out.SELinuxOptions = new(SELinuxOptions)
  647. if err := Convert_api_SELinuxOptions_To_v1_SELinuxOptions(in.SELinuxOptions, out.SELinuxOptions, s); err != nil {
  648. return err
  649. }
  650. } else {
  651. out.SELinuxOptions = nil
  652. }
  653. out.RunAsUser = in.RunAsUser
  654. out.RunAsNonRoot = in.RunAsNonRoot
  655. out.FSGroup = in.FSGroup
  656. return nil
  657. }
  658. func Convert_v1_PodSecurityContext_To_api_PodSecurityContext(in *PodSecurityContext, out *api.PodSecurityContext, s conversion.Scope) error {
  659. out.SupplementalGroups = in.SupplementalGroups
  660. if in.SELinuxOptions != nil {
  661. out.SELinuxOptions = new(api.SELinuxOptions)
  662. if err := Convert_v1_SELinuxOptions_To_api_SELinuxOptions(in.SELinuxOptions, out.SELinuxOptions, s); err != nil {
  663. return err
  664. }
  665. } else {
  666. out.SELinuxOptions = nil
  667. }
  668. out.RunAsUser = in.RunAsUser
  669. out.RunAsNonRoot = in.RunAsNonRoot
  670. out.FSGroup = in.FSGroup
  671. return nil
  672. }
  673. func Convert_v1_ResourceList_To_api_ResourceList(in *ResourceList, out *api.ResourceList, s conversion.Scope) error {
  674. if *in == nil {
  675. return nil
  676. }
  677. if *out == nil {
  678. *out = make(api.ResourceList, len(*in))
  679. }
  680. for key, val := range *in {
  681. // TODO(#18538): We round up resource values to milli scale to maintain API compatibility.
  682. // In the future, we should instead reject values that need rounding.
  683. const milliScale = -3
  684. val.RoundUp(milliScale)
  685. (*out)[api.ResourceName(key)] = val
  686. }
  687. return nil
  688. }
  689. func AddFieldLabelConversionsForEvent(scheme *runtime.Scheme) error {
  690. return scheme.AddFieldLabelConversionFunc("v1", "Event",
  691. func(label, value string) (string, string, error) {
  692. switch label {
  693. case "involvedObject.kind",
  694. "involvedObject.namespace",
  695. "involvedObject.name",
  696. "involvedObject.uid",
  697. "involvedObject.apiVersion",
  698. "involvedObject.resourceVersion",
  699. "involvedObject.fieldPath",
  700. "reason",
  701. "source",
  702. "type",
  703. "metadata.namespace",
  704. "metadata.name":
  705. return label, value, nil
  706. default:
  707. return "", "", fmt.Errorf("field label not supported: %s", label)
  708. }
  709. })
  710. }
  711. func AddFieldLabelConversionsForNamespace(scheme *runtime.Scheme) error {
  712. return scheme.AddFieldLabelConversionFunc("v1", "Namespace",
  713. func(label, value string) (string, string, error) {
  714. switch label {
  715. case "status.phase",
  716. "metadata.name":
  717. return label, value, nil
  718. default:
  719. return "", "", fmt.Errorf("field label not supported: %s", label)
  720. }
  721. })
  722. }
  723. func AddFieldLabelConversionsForSecret(scheme *runtime.Scheme) error {
  724. return scheme.AddFieldLabelConversionFunc("v1", "Secret",
  725. func(label, value string) (string, string, error) {
  726. switch label {
  727. case "type",
  728. "metadata.namespace",
  729. "metadata.name":
  730. return label, value, nil
  731. default:
  732. return "", "", fmt.Errorf("field label not supported: %s", label)
  733. }
  734. })
  735. }