conversion.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. Copyright 2018 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 v2beta1
  14. import (
  15. "encoding/json"
  16. autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/conversion"
  19. "k8s.io/kubernetes/pkg/apis/autoscaling"
  20. core "k8s.io/kubernetes/pkg/apis/core"
  21. )
  22. func Convert_autoscaling_MetricTarget_To_v2beta1_CrossVersionObjectReference(in *autoscaling.MetricTarget, out *autoscalingv2beta1.CrossVersionObjectReference, s conversion.Scope) error {
  23. return nil
  24. }
  25. func Convert_v2beta1_CrossVersionObjectReference_To_autoscaling_MetricTarget(in *autoscalingv2beta1.CrossVersionObjectReference, out *autoscaling.MetricTarget, s conversion.Scope) error {
  26. return nil
  27. }
  28. func Convert_v2beta1_ResourceMetricStatus_To_autoscaling_ResourceMetricStatus(in *autoscalingv2beta1.ResourceMetricStatus, out *autoscaling.ResourceMetricStatus, s conversion.Scope) error {
  29. out.Name = core.ResourceName(in.Name)
  30. utilization := in.CurrentAverageUtilization
  31. averageValue := in.CurrentAverageValue
  32. out.Current = autoscaling.MetricValueStatus{
  33. AverageValue: &averageValue,
  34. AverageUtilization: utilization,
  35. }
  36. return nil
  37. }
  38. func Convert_autoscaling_ResourceMetricStatus_To_v2beta1_ResourceMetricStatus(in *autoscaling.ResourceMetricStatus, out *autoscalingv2beta1.ResourceMetricStatus, s conversion.Scope) error {
  39. out.Name = v1.ResourceName(in.Name)
  40. out.CurrentAverageUtilization = in.Current.AverageUtilization
  41. if in.Current.AverageValue != nil {
  42. out.CurrentAverageValue = *in.Current.AverageValue
  43. }
  44. return nil
  45. }
  46. func Convert_v2beta1_ResourceMetricSource_To_autoscaling_ResourceMetricSource(in *autoscalingv2beta1.ResourceMetricSource, out *autoscaling.ResourceMetricSource, s conversion.Scope) error {
  47. out.Name = core.ResourceName(in.Name)
  48. utilization := in.TargetAverageUtilization
  49. averageValue := in.TargetAverageValue
  50. var metricType autoscaling.MetricTargetType
  51. if utilization == nil {
  52. metricType = autoscaling.AverageValueMetricType
  53. } else {
  54. metricType = autoscaling.UtilizationMetricType
  55. }
  56. out.Target = autoscaling.MetricTarget{
  57. Type: metricType,
  58. AverageValue: averageValue,
  59. AverageUtilization: utilization,
  60. }
  61. return nil
  62. }
  63. func Convert_autoscaling_ResourceMetricSource_To_v2beta1_ResourceMetricSource(in *autoscaling.ResourceMetricSource, out *autoscalingv2beta1.ResourceMetricSource, s conversion.Scope) error {
  64. out.Name = v1.ResourceName(in.Name)
  65. out.TargetAverageUtilization = in.Target.AverageUtilization
  66. out.TargetAverageValue = in.Target.AverageValue
  67. return nil
  68. }
  69. func Convert_autoscaling_ExternalMetricSource_To_v2beta1_ExternalMetricSource(in *autoscaling.ExternalMetricSource, out *autoscalingv2beta1.ExternalMetricSource, s conversion.Scope) error {
  70. out.MetricName = in.Metric.Name
  71. out.TargetValue = in.Target.Value
  72. out.TargetAverageValue = in.Target.AverageValue
  73. out.MetricSelector = in.Metric.Selector
  74. return nil
  75. }
  76. func Convert_v2beta1_ExternalMetricSource_To_autoscaling_ExternalMetricSource(in *autoscalingv2beta1.ExternalMetricSource, out *autoscaling.ExternalMetricSource, s conversion.Scope) error {
  77. value := in.TargetValue
  78. averageValue := in.TargetAverageValue
  79. var metricType autoscaling.MetricTargetType
  80. if value == nil {
  81. metricType = autoscaling.AverageValueMetricType
  82. } else {
  83. metricType = autoscaling.ValueMetricType
  84. }
  85. out.Target = autoscaling.MetricTarget{
  86. Type: metricType,
  87. Value: value,
  88. AverageValue: averageValue,
  89. }
  90. out.Metric = autoscaling.MetricIdentifier{
  91. Name: in.MetricName,
  92. Selector: in.MetricSelector,
  93. }
  94. return nil
  95. }
  96. func Convert_autoscaling_ObjectMetricSource_To_v2beta1_ObjectMetricSource(in *autoscaling.ObjectMetricSource, out *autoscalingv2beta1.ObjectMetricSource, s conversion.Scope) error {
  97. if in.Target.Value != nil {
  98. out.TargetValue = *in.Target.Value
  99. }
  100. out.AverageValue = in.Target.AverageValue
  101. out.Target = autoscalingv2beta1.CrossVersionObjectReference{
  102. Kind: in.DescribedObject.Kind,
  103. Name: in.DescribedObject.Name,
  104. APIVersion: in.DescribedObject.APIVersion,
  105. }
  106. out.MetricName = in.Metric.Name
  107. out.Selector = in.Metric.Selector
  108. return nil
  109. }
  110. func Convert_v2beta1_ObjectMetricSource_To_autoscaling_ObjectMetricSource(in *autoscalingv2beta1.ObjectMetricSource, out *autoscaling.ObjectMetricSource, s conversion.Scope) error {
  111. var metricType autoscaling.MetricTargetType
  112. if in.AverageValue == nil {
  113. metricType = autoscaling.ValueMetricType
  114. } else {
  115. metricType = autoscaling.AverageValueMetricType
  116. }
  117. out.Target = autoscaling.MetricTarget{
  118. Type: metricType,
  119. Value: &in.TargetValue,
  120. AverageValue: in.AverageValue,
  121. }
  122. out.DescribedObject = autoscaling.CrossVersionObjectReference{
  123. Kind: in.Target.Kind,
  124. Name: in.Target.Name,
  125. APIVersion: in.Target.APIVersion,
  126. }
  127. out.Metric = autoscaling.MetricIdentifier{
  128. Name: in.MetricName,
  129. Selector: in.Selector,
  130. }
  131. return nil
  132. }
  133. func Convert_autoscaling_PodsMetricSource_To_v2beta1_PodsMetricSource(in *autoscaling.PodsMetricSource, out *autoscalingv2beta1.PodsMetricSource, s conversion.Scope) error {
  134. if in.Target.AverageValue != nil {
  135. targetAverageValue := *in.Target.AverageValue
  136. out.TargetAverageValue = targetAverageValue
  137. }
  138. out.MetricName = in.Metric.Name
  139. out.Selector = in.Metric.Selector
  140. return nil
  141. }
  142. func Convert_v2beta1_PodsMetricSource_To_autoscaling_PodsMetricSource(in *autoscalingv2beta1.PodsMetricSource, out *autoscaling.PodsMetricSource, s conversion.Scope) error {
  143. targetAverageValue := &in.TargetAverageValue
  144. metricType := autoscaling.AverageValueMetricType
  145. out.Target = autoscaling.MetricTarget{
  146. Type: metricType,
  147. AverageValue: targetAverageValue,
  148. }
  149. out.Metric = autoscaling.MetricIdentifier{
  150. Name: in.MetricName,
  151. Selector: in.Selector,
  152. }
  153. return nil
  154. }
  155. func Convert_autoscaling_ExternalMetricStatus_To_v2beta1_ExternalMetricStatus(in *autoscaling.ExternalMetricStatus, out *autoscalingv2beta1.ExternalMetricStatus, s conversion.Scope) error {
  156. out.CurrentAverageValue = in.Current.AverageValue
  157. out.MetricName = in.Metric.Name
  158. if in.Current.Value != nil {
  159. out.CurrentValue = *in.Current.Value
  160. }
  161. out.MetricSelector = in.Metric.Selector
  162. return nil
  163. }
  164. func Convert_v2beta1_ExternalMetricStatus_To_autoscaling_ExternalMetricStatus(in *autoscalingv2beta1.ExternalMetricStatus, out *autoscaling.ExternalMetricStatus, s conversion.Scope) error {
  165. value := in.CurrentValue
  166. averageValue := in.CurrentAverageValue
  167. out.Current = autoscaling.MetricValueStatus{
  168. Value: &value,
  169. AverageValue: averageValue,
  170. }
  171. out.Metric = autoscaling.MetricIdentifier{
  172. Name: in.MetricName,
  173. Selector: in.MetricSelector,
  174. }
  175. return nil
  176. }
  177. func Convert_autoscaling_ObjectMetricStatus_To_v2beta1_ObjectMetricStatus(in *autoscaling.ObjectMetricStatus, out *autoscalingv2beta1.ObjectMetricStatus, s conversion.Scope) error {
  178. if in.Current.Value != nil {
  179. out.CurrentValue = *in.Current.Value
  180. }
  181. out.Target = autoscalingv2beta1.CrossVersionObjectReference{
  182. Kind: in.DescribedObject.Kind,
  183. Name: in.DescribedObject.Name,
  184. APIVersion: in.DescribedObject.APIVersion,
  185. }
  186. out.MetricName = in.Metric.Name
  187. out.Selector = in.Metric.Selector
  188. if in.Current.AverageValue != nil {
  189. currentAverageValue := *in.Current.AverageValue
  190. out.AverageValue = &currentAverageValue
  191. }
  192. return nil
  193. }
  194. func Convert_v2beta1_ObjectMetricStatus_To_autoscaling_ObjectMetricStatus(in *autoscalingv2beta1.ObjectMetricStatus, out *autoscaling.ObjectMetricStatus, s conversion.Scope) error {
  195. out.Current = autoscaling.MetricValueStatus{
  196. Value: &in.CurrentValue,
  197. AverageValue: in.AverageValue,
  198. }
  199. out.DescribedObject = autoscaling.CrossVersionObjectReference{
  200. Kind: in.Target.Kind,
  201. Name: in.Target.Name,
  202. APIVersion: in.Target.APIVersion,
  203. }
  204. out.Metric = autoscaling.MetricIdentifier{
  205. Name: in.MetricName,
  206. Selector: in.Selector,
  207. }
  208. return nil
  209. }
  210. func Convert_autoscaling_PodsMetricStatus_To_v2beta1_PodsMetricStatus(in *autoscaling.PodsMetricStatus, out *autoscalingv2beta1.PodsMetricStatus, s conversion.Scope) error {
  211. if in.Current.AverageValue != nil {
  212. out.CurrentAverageValue = *in.Current.AverageValue
  213. }
  214. out.MetricName = in.Metric.Name
  215. out.Selector = in.Metric.Selector
  216. return nil
  217. }
  218. func Convert_v2beta1_PodsMetricStatus_To_autoscaling_PodsMetricStatus(in *autoscalingv2beta1.PodsMetricStatus, out *autoscaling.PodsMetricStatus, s conversion.Scope) error {
  219. out.Current = autoscaling.MetricValueStatus{
  220. AverageValue: &in.CurrentAverageValue,
  221. }
  222. out.Metric = autoscaling.MetricIdentifier{
  223. Name: in.MetricName,
  224. Selector: in.Selector,
  225. }
  226. return nil
  227. }
  228. func Convert_autoscaling_HorizontalPodAutoscaler_To_v2beta1_HorizontalPodAutoscaler(in *autoscaling.HorizontalPodAutoscaler, out *autoscalingv2beta1.HorizontalPodAutoscaler, s conversion.Scope) error {
  229. if err := autoConvert_autoscaling_HorizontalPodAutoscaler_To_v2beta1_HorizontalPodAutoscaler(in, out, s); err != nil {
  230. return err
  231. }
  232. if in.Spec.Behavior != nil {
  233. old := out.Annotations
  234. out.Annotations = make(map[string]string, len(old)+1)
  235. for k, v := range old {
  236. out.Annotations[k] = v
  237. }
  238. behaviorEnc, err := json.Marshal(in.Spec.Behavior)
  239. if err != nil {
  240. return err
  241. }
  242. // Even if the annotation for behavior exists, we will just overwrite it
  243. out.Annotations[autoscaling.BehaviorSpecsAnnotation] = string(behaviorEnc)
  244. }
  245. return nil
  246. }
  247. func Convert_v2beta1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(in *autoscalingv2beta1.HorizontalPodAutoscaler, out *autoscaling.HorizontalPodAutoscaler, s conversion.Scope) error {
  248. if err := autoConvert_v2beta1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(in, out, s); err != nil {
  249. return err
  250. }
  251. if behaviorEnc, hasBehaviors := out.Annotations[autoscaling.BehaviorSpecsAnnotation]; hasBehaviors {
  252. var behavior autoscaling.HorizontalPodAutoscalerBehavior
  253. if err := json.Unmarshal([]byte(behaviorEnc), &behavior); err != nil {
  254. return err
  255. }
  256. out.Spec.Behavior = &behavior
  257. delete(out.Annotations, autoscaling.BehaviorSpecsAnnotation)
  258. }
  259. return nil
  260. }
  261. func Convert_autoscaling_HorizontalPodAutoscalerSpec_To_v2beta1_HorizontalPodAutoscalerSpec(in *autoscaling.HorizontalPodAutoscalerSpec, out *autoscalingv2beta1.HorizontalPodAutoscalerSpec, s conversion.Scope) error {
  262. return autoConvert_autoscaling_HorizontalPodAutoscalerSpec_To_v2beta1_HorizontalPodAutoscalerSpec(in, out, s)
  263. }