k8s.io/kubernetes@v1.29.3/pkg/apis/autoscaling/v2beta1/conversion.go (about) 1 /* 2 Copyright 2018 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 v2beta1 18 19 import ( 20 "encoding/json" 21 22 autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1" 23 24 v1 "k8s.io/api/core/v1" 25 "k8s.io/apimachinery/pkg/conversion" 26 "k8s.io/kubernetes/pkg/apis/autoscaling" 27 core "k8s.io/kubernetes/pkg/apis/core" 28 ) 29 30 func Convert_autoscaling_MetricTarget_To_v2beta1_CrossVersionObjectReference(in *autoscaling.MetricTarget, out *autoscalingv2beta1.CrossVersionObjectReference, s conversion.Scope) error { 31 return nil 32 } 33 34 func Convert_v2beta1_CrossVersionObjectReference_To_autoscaling_MetricTarget(in *autoscalingv2beta1.CrossVersionObjectReference, out *autoscaling.MetricTarget, s conversion.Scope) error { 35 return nil 36 } 37 38 func Convert_v2beta1_ContainerResourceMetricStatus_To_autoscaling_ContainerResourceMetricStatus(in *autoscalingv2beta1.ContainerResourceMetricStatus, out *autoscaling.ContainerResourceMetricStatus, s conversion.Scope) error { 39 out.Name = core.ResourceName(in.Name) 40 out.Container = in.Container 41 utilization := in.CurrentAverageUtilization 42 averageValue := in.CurrentAverageValue 43 out.Current = autoscaling.MetricValueStatus{ 44 AverageValue: &averageValue, 45 AverageUtilization: utilization, 46 } 47 return nil 48 } 49 50 func Convert_autoscaling_ContainerResourceMetricStatus_To_v2beta1_ContainerResourceMetricStatus(in *autoscaling.ContainerResourceMetricStatus, out *autoscalingv2beta1.ContainerResourceMetricStatus, s conversion.Scope) error { 51 out.Name = v1.ResourceName(in.Name) 52 out.Container = in.Container 53 out.CurrentAverageUtilization = in.Current.AverageUtilization 54 if in.Current.AverageValue != nil { 55 out.CurrentAverageValue = *in.Current.AverageValue 56 } 57 return nil 58 } 59 60 func Convert_v2beta1_ResourceMetricStatus_To_autoscaling_ResourceMetricStatus(in *autoscalingv2beta1.ResourceMetricStatus, out *autoscaling.ResourceMetricStatus, s conversion.Scope) error { 61 out.Name = core.ResourceName(in.Name) 62 utilization := in.CurrentAverageUtilization 63 averageValue := in.CurrentAverageValue 64 out.Current = autoscaling.MetricValueStatus{ 65 AverageValue: &averageValue, 66 AverageUtilization: utilization, 67 } 68 return nil 69 } 70 71 func Convert_autoscaling_ResourceMetricStatus_To_v2beta1_ResourceMetricStatus(in *autoscaling.ResourceMetricStatus, out *autoscalingv2beta1.ResourceMetricStatus, s conversion.Scope) error { 72 out.Name = v1.ResourceName(in.Name) 73 out.CurrentAverageUtilization = in.Current.AverageUtilization 74 if in.Current.AverageValue != nil { 75 out.CurrentAverageValue = *in.Current.AverageValue 76 } 77 return nil 78 } 79 80 func Convert_v2beta1_ResourceMetricSource_To_autoscaling_ResourceMetricSource(in *autoscalingv2beta1.ResourceMetricSource, out *autoscaling.ResourceMetricSource, s conversion.Scope) error { 81 out.Name = core.ResourceName(in.Name) 82 utilization := in.TargetAverageUtilization 83 averageValue := in.TargetAverageValue 84 85 var metricType autoscaling.MetricTargetType 86 if utilization == nil { 87 metricType = autoscaling.AverageValueMetricType 88 } else { 89 metricType = autoscaling.UtilizationMetricType 90 } 91 out.Target = autoscaling.MetricTarget{ 92 Type: metricType, 93 AverageValue: averageValue, 94 AverageUtilization: utilization, 95 } 96 return nil 97 } 98 99 func Convert_autoscaling_ResourceMetricSource_To_v2beta1_ResourceMetricSource(in *autoscaling.ResourceMetricSource, out *autoscalingv2beta1.ResourceMetricSource, s conversion.Scope) error { 100 out.Name = v1.ResourceName(in.Name) 101 out.TargetAverageUtilization = in.Target.AverageUtilization 102 out.TargetAverageValue = in.Target.AverageValue 103 return nil 104 } 105 106 func Convert_autoscaling_ExternalMetricSource_To_v2beta1_ExternalMetricSource(in *autoscaling.ExternalMetricSource, out *autoscalingv2beta1.ExternalMetricSource, s conversion.Scope) error { 107 out.MetricName = in.Metric.Name 108 out.TargetValue = in.Target.Value 109 out.TargetAverageValue = in.Target.AverageValue 110 out.MetricSelector = in.Metric.Selector 111 return nil 112 } 113 114 func Convert_v2beta1_ExternalMetricSource_To_autoscaling_ExternalMetricSource(in *autoscalingv2beta1.ExternalMetricSource, out *autoscaling.ExternalMetricSource, s conversion.Scope) error { 115 value := in.TargetValue 116 averageValue := in.TargetAverageValue 117 118 var metricType autoscaling.MetricTargetType 119 if value == nil { 120 metricType = autoscaling.AverageValueMetricType 121 } else { 122 metricType = autoscaling.ValueMetricType 123 } 124 125 out.Target = autoscaling.MetricTarget{ 126 Type: metricType, 127 Value: value, 128 AverageValue: averageValue, 129 } 130 131 out.Metric = autoscaling.MetricIdentifier{ 132 Name: in.MetricName, 133 Selector: in.MetricSelector, 134 } 135 return nil 136 } 137 138 func Convert_autoscaling_ObjectMetricSource_To_v2beta1_ObjectMetricSource(in *autoscaling.ObjectMetricSource, out *autoscalingv2beta1.ObjectMetricSource, s conversion.Scope) error { 139 if in.Target.Value != nil { 140 out.TargetValue = *in.Target.Value 141 } 142 out.AverageValue = in.Target.AverageValue 143 144 out.Target = autoscalingv2beta1.CrossVersionObjectReference{ 145 Kind: in.DescribedObject.Kind, 146 Name: in.DescribedObject.Name, 147 APIVersion: in.DescribedObject.APIVersion, 148 } 149 out.MetricName = in.Metric.Name 150 out.Selector = in.Metric.Selector 151 152 return nil 153 } 154 155 func Convert_v2beta1_ObjectMetricSource_To_autoscaling_ObjectMetricSource(in *autoscalingv2beta1.ObjectMetricSource, out *autoscaling.ObjectMetricSource, s conversion.Scope) error { 156 var metricType autoscaling.MetricTargetType 157 if in.AverageValue == nil { 158 metricType = autoscaling.ValueMetricType 159 } else { 160 metricType = autoscaling.AverageValueMetricType 161 } 162 out.Target = autoscaling.MetricTarget{ 163 Type: metricType, 164 Value: &in.TargetValue, 165 AverageValue: in.AverageValue, 166 } 167 out.DescribedObject = autoscaling.CrossVersionObjectReference{ 168 Kind: in.Target.Kind, 169 Name: in.Target.Name, 170 APIVersion: in.Target.APIVersion, 171 } 172 out.Metric = autoscaling.MetricIdentifier{ 173 Name: in.MetricName, 174 Selector: in.Selector, 175 } 176 return nil 177 } 178 179 func Convert_autoscaling_PodsMetricSource_To_v2beta1_PodsMetricSource(in *autoscaling.PodsMetricSource, out *autoscalingv2beta1.PodsMetricSource, s conversion.Scope) error { 180 if in.Target.AverageValue != nil { 181 targetAverageValue := *in.Target.AverageValue 182 out.TargetAverageValue = targetAverageValue 183 } 184 185 out.MetricName = in.Metric.Name 186 out.Selector = in.Metric.Selector 187 188 return nil 189 } 190 191 func Convert_v2beta1_PodsMetricSource_To_autoscaling_PodsMetricSource(in *autoscalingv2beta1.PodsMetricSource, out *autoscaling.PodsMetricSource, s conversion.Scope) error { 192 targetAverageValue := &in.TargetAverageValue 193 metricType := autoscaling.AverageValueMetricType 194 195 out.Target = autoscaling.MetricTarget{ 196 Type: metricType, 197 AverageValue: targetAverageValue, 198 } 199 out.Metric = autoscaling.MetricIdentifier{ 200 Name: in.MetricName, 201 Selector: in.Selector, 202 } 203 return nil 204 } 205 206 func Convert_autoscaling_ExternalMetricStatus_To_v2beta1_ExternalMetricStatus(in *autoscaling.ExternalMetricStatus, out *autoscalingv2beta1.ExternalMetricStatus, s conversion.Scope) error { 207 out.CurrentAverageValue = in.Current.AverageValue 208 out.MetricName = in.Metric.Name 209 if in.Current.Value != nil { 210 out.CurrentValue = *in.Current.Value 211 } 212 out.MetricSelector = in.Metric.Selector 213 return nil 214 } 215 216 func Convert_v2beta1_ExternalMetricStatus_To_autoscaling_ExternalMetricStatus(in *autoscalingv2beta1.ExternalMetricStatus, out *autoscaling.ExternalMetricStatus, s conversion.Scope) error { 217 value := in.CurrentValue 218 averageValue := in.CurrentAverageValue 219 out.Current = autoscaling.MetricValueStatus{ 220 Value: &value, 221 AverageValue: averageValue, 222 } 223 out.Metric = autoscaling.MetricIdentifier{ 224 Name: in.MetricName, 225 Selector: in.MetricSelector, 226 } 227 return nil 228 } 229 230 func Convert_autoscaling_ObjectMetricStatus_To_v2beta1_ObjectMetricStatus(in *autoscaling.ObjectMetricStatus, out *autoscalingv2beta1.ObjectMetricStatus, s conversion.Scope) error { 231 if in.Current.Value != nil { 232 out.CurrentValue = *in.Current.Value 233 } 234 out.Target = autoscalingv2beta1.CrossVersionObjectReference{ 235 Kind: in.DescribedObject.Kind, 236 Name: in.DescribedObject.Name, 237 APIVersion: in.DescribedObject.APIVersion, 238 } 239 out.MetricName = in.Metric.Name 240 out.Selector = in.Metric.Selector 241 if in.Current.AverageValue != nil { 242 currentAverageValue := *in.Current.AverageValue 243 out.AverageValue = ¤tAverageValue 244 } 245 return nil 246 } 247 248 func Convert_v2beta1_ObjectMetricStatus_To_autoscaling_ObjectMetricStatus(in *autoscalingv2beta1.ObjectMetricStatus, out *autoscaling.ObjectMetricStatus, s conversion.Scope) error { 249 out.Current = autoscaling.MetricValueStatus{ 250 Value: &in.CurrentValue, 251 AverageValue: in.AverageValue, 252 } 253 out.DescribedObject = autoscaling.CrossVersionObjectReference{ 254 Kind: in.Target.Kind, 255 Name: in.Target.Name, 256 APIVersion: in.Target.APIVersion, 257 } 258 out.Metric = autoscaling.MetricIdentifier{ 259 Name: in.MetricName, 260 Selector: in.Selector, 261 } 262 return nil 263 } 264 265 func Convert_autoscaling_PodsMetricStatus_To_v2beta1_PodsMetricStatus(in *autoscaling.PodsMetricStatus, out *autoscalingv2beta1.PodsMetricStatus, s conversion.Scope) error { 266 if in.Current.AverageValue != nil { 267 out.CurrentAverageValue = *in.Current.AverageValue 268 } 269 out.MetricName = in.Metric.Name 270 out.Selector = in.Metric.Selector 271 return nil 272 } 273 274 func Convert_v2beta1_PodsMetricStatus_To_autoscaling_PodsMetricStatus(in *autoscalingv2beta1.PodsMetricStatus, out *autoscaling.PodsMetricStatus, s conversion.Scope) error { 275 out.Current = autoscaling.MetricValueStatus{ 276 AverageValue: &in.CurrentAverageValue, 277 } 278 out.Metric = autoscaling.MetricIdentifier{ 279 Name: in.MetricName, 280 Selector: in.Selector, 281 } 282 return nil 283 } 284 285 func Convert_autoscaling_HorizontalPodAutoscaler_To_v2beta1_HorizontalPodAutoscaler(in *autoscaling.HorizontalPodAutoscaler, out *autoscalingv2beta1.HorizontalPodAutoscaler, s conversion.Scope) error { 286 if err := autoConvert_autoscaling_HorizontalPodAutoscaler_To_v2beta1_HorizontalPodAutoscaler(in, out, s); err != nil { 287 return err 288 } 289 290 // clear any pre-existing round-trip annotations to make sure the only ones set are ones we produced during conversion 291 annotations, copiedAnnotations := autoscaling.DropRoundTripHorizontalPodAutoscalerAnnotations(out.Annotations) 292 out.Annotations = annotations 293 294 if in.Spec.Behavior != nil { 295 // TODO: this is marshaling an internal type. Fix this without breaking backwards compatibility with n-1 API servers. 296 behaviorEnc, err := json.Marshal(in.Spec.Behavior) 297 if err != nil { 298 return err 299 } 300 // copy before mutating 301 if !copiedAnnotations { 302 //nolint:ineffassign 303 copiedAnnotations = true 304 out.Annotations = autoscaling.DeepCopyStringMap(out.Annotations) 305 } 306 out.Annotations[autoscaling.BehaviorSpecsAnnotation] = string(behaviorEnc) 307 } 308 309 return nil 310 } 311 312 func Convert_v2beta1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(in *autoscalingv2beta1.HorizontalPodAutoscaler, out *autoscaling.HorizontalPodAutoscaler, s conversion.Scope) error { 313 if err := autoConvert_v2beta1_HorizontalPodAutoscaler_To_autoscaling_HorizontalPodAutoscaler(in, out, s); err != nil { 314 return err 315 } 316 317 if behaviorEnc, hasBehaviors := out.Annotations[autoscaling.BehaviorSpecsAnnotation]; hasBehaviors { 318 // TODO: this is unmarshaling an internal type. Fix this without breaking backwards compatibility with n-1 API servers. 319 var behavior autoscaling.HorizontalPodAutoscalerBehavior 320 if err := json.Unmarshal([]byte(behaviorEnc), &behavior); err == nil && behavior != (autoscaling.HorizontalPodAutoscalerBehavior{}) { 321 // only move well-formed data from annotations to fields 322 out.Spec.Behavior = &behavior 323 } 324 } 325 326 // drop round-tripping annotations after converting to internal 327 out.Annotations, _ = autoscaling.DropRoundTripHorizontalPodAutoscalerAnnotations(out.Annotations) 328 329 return nil 330 } 331 332 func Convert_autoscaling_HorizontalPodAutoscalerSpec_To_v2beta1_HorizontalPodAutoscalerSpec(in *autoscaling.HorizontalPodAutoscalerSpec, out *autoscalingv2beta1.HorizontalPodAutoscalerSpec, s conversion.Scope) error { 333 return autoConvert_autoscaling_HorizontalPodAutoscalerSpec_To_v2beta1_HorizontalPodAutoscalerSpec(in, out, s) 334 } 335 336 func Convert_v2beta1_ContainerResourceMetricSource_To_autoscaling_ContainerResourceMetricSource(in *autoscalingv2beta1.ContainerResourceMetricSource, out *autoscaling.ContainerResourceMetricSource, s conversion.Scope) error { 337 out.Name = core.ResourceName(in.Name) 338 utilization := in.TargetAverageUtilization 339 averageValue := in.TargetAverageValue 340 341 var metricType autoscaling.MetricTargetType 342 if utilization == nil { 343 metricType = autoscaling.AverageValueMetricType 344 } else { 345 metricType = autoscaling.UtilizationMetricType 346 } 347 out.Target = autoscaling.MetricTarget{ 348 Type: metricType, 349 AverageValue: averageValue, 350 AverageUtilization: utilization, 351 } 352 return nil 353 } 354 355 func Convert_autoscaling_ContainerResourceMetricSource_To_v2beta1_ContainerResourceMetricSource(in *autoscaling.ContainerResourceMetricSource, out *autoscalingv2beta1.ContainerResourceMetricSource, s conversion.Scope) error { 356 out.Name = v1.ResourceName(in.Name) 357 out.TargetAverageUtilization = in.Target.AverageUtilization 358 out.TargetAverageValue = in.Target.AverageValue 359 return nil 360 }