@@ -33,6 +33,7 @@ import (
33
33
"github.com/fatih/camelcase"
34
34
35
35
appsv1 "k8s.io/api/apps/v1"
36
+ autoscalingv1 "k8s.io/api/autoscaling/v1"
36
37
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
37
38
batchv1 "k8s.io/api/batch/v1"
38
39
batchv1beta1 "k8s.io/api/batch/v1beta1"
@@ -3074,20 +3075,31 @@ type HorizontalPodAutoscalerDescriber struct {
3074
3075
}
3075
3076
3076
3077
func (d * HorizontalPodAutoscalerDescriber ) Describe (namespace , name string , describerSettings describe.DescriberSettings ) (string , error ) {
3077
- hpa , err := d .client .AutoscalingV2beta2 ().HorizontalPodAutoscalers (namespace ).Get (name , metav1.GetOptions {})
3078
- if err != nil {
3079
- return "" , err
3078
+ var events * corev1.EventList
3079
+
3080
+ // autoscaling/v2beta2 is introduced since v1.12 and autoscaling/v1 does not have full backward compatibility
3081
+ // with autoscaling/v2beta2, so describer will try to get and describe hpa v2beta2 object firstly, if it fails,
3082
+ // describer will fall back to do with hpa v1 object
3083
+ hpaV2beta2 , err := d .client .AutoscalingV2beta2 ().HorizontalPodAutoscalers (namespace ).Get (name , metav1.GetOptions {})
3084
+ if err == nil {
3085
+ if describerSettings .ShowEvents {
3086
+ events , _ = d .client .CoreV1 ().Events (namespace ).Search (scheme .Scheme , hpaV2beta2 )
3087
+ }
3088
+ return describeHorizontalPodAutoscalerV2beta2 (hpaV2beta2 , events , d )
3080
3089
}
3081
3090
3082
- var events * corev1.EventList
3083
- if describerSettings .ShowEvents {
3084
- events , _ = d .client .CoreV1 ().Events (namespace ).Search (scheme .Scheme , hpa )
3091
+ hpaV1 , err := d .client .AutoscalingV1 ().HorizontalPodAutoscalers (namespace ).Get (name , metav1.GetOptions {})
3092
+ if err == nil {
3093
+ if describerSettings .ShowEvents {
3094
+ events , _ = d .client .CoreV1 ().Events (namespace ).Search (scheme .Scheme , hpaV1 )
3095
+ }
3096
+ return describeHorizontalPodAutoscalerV1 (hpaV1 , events , d )
3085
3097
}
3086
3098
3087
- return describeHorizontalPodAutoscaler ( hpa , events , d )
3099
+ return "" , err
3088
3100
}
3089
3101
3090
- func describeHorizontalPodAutoscaler (hpa * autoscalingv2beta2.HorizontalPodAutoscaler , events * corev1.EventList , d * HorizontalPodAutoscalerDescriber ) (string , error ) {
3102
+ func describeHorizontalPodAutoscalerV2beta2 (hpa * autoscalingv2beta2.HorizontalPodAutoscaler , events * corev1.EventList , d * HorizontalPodAutoscalerDescriber ) (string , error ) {
3091
3103
return tabbedString (func (out io.Writer ) error {
3092
3104
w := NewPrefixWriter (out )
3093
3105
w .Write (LEVEL_0 , "Name:\t %s\n " , hpa .Name )
@@ -3188,6 +3200,44 @@ func describeHorizontalPodAutoscaler(hpa *autoscalingv2beta2.HorizontalPodAutosc
3188
3200
})
3189
3201
}
3190
3202
3203
+ func describeHorizontalPodAutoscalerV1 (hpa * autoscalingv1.HorizontalPodAutoscaler , events * corev1.EventList , d * HorizontalPodAutoscalerDescriber ) (string , error ) {
3204
+ return tabbedString (func (out io.Writer ) error {
3205
+ w := NewPrefixWriter (out )
3206
+ w .Write (LEVEL_0 , "Name:\t %s\n " , hpa .Name )
3207
+ w .Write (LEVEL_0 , "Namespace:\t %s\n " , hpa .Namespace )
3208
+ printLabelsMultiline (w , "Labels" , hpa .Labels )
3209
+ printAnnotationsMultiline (w , "Annotations" , hpa .Annotations )
3210
+ w .Write (LEVEL_0 , "CreationTimestamp:\t %s\n " , hpa .CreationTimestamp .Time .Format (time .RFC1123Z ))
3211
+ w .Write (LEVEL_0 , "Reference:\t %s/%s\n " ,
3212
+ hpa .Spec .ScaleTargetRef .Kind ,
3213
+ hpa .Spec .ScaleTargetRef .Name )
3214
+
3215
+ if hpa .Spec .TargetCPUUtilizationPercentage != nil {
3216
+ w .Write (LEVEL_0 , "Target CPU utilization:\t %d%%\n " , * hpa .Spec .TargetCPUUtilizationPercentage )
3217
+ current := "<unknown>"
3218
+ if hpa .Status .CurrentCPUUtilizationPercentage != nil {
3219
+ current = fmt .Sprintf ("%d" , * hpa .Status .CurrentCPUUtilizationPercentage )
3220
+ }
3221
+ w .Write (LEVEL_0 , "Current CPU utilization:\t %s%%\n " , current )
3222
+ }
3223
+
3224
+ minReplicas := "<unset>"
3225
+ if hpa .Spec .MinReplicas != nil {
3226
+ minReplicas = fmt .Sprintf ("%d" , * hpa .Spec .MinReplicas )
3227
+ }
3228
+ w .Write (LEVEL_0 , "Min replicas:\t %s\n " , minReplicas )
3229
+ w .Write (LEVEL_0 , "Max replicas:\t %d\n " , hpa .Spec .MaxReplicas )
3230
+ w .Write (LEVEL_0 , "%s pods:\t " , hpa .Spec .ScaleTargetRef .Kind )
3231
+ w .Write (LEVEL_0 , "%d current / %d desired\n " , hpa .Status .CurrentReplicas , hpa .Status .DesiredReplicas )
3232
+
3233
+ if events != nil {
3234
+ DescribeEvents (events , w )
3235
+ }
3236
+
3237
+ return nil
3238
+ })
3239
+ }
3240
+
3191
3241
func describeNodeResource (nodeNonTerminatedPodsList * corev1.PodList , node * corev1.Node , w PrefixWriter ) {
3192
3242
w .Write (LEVEL_0 , "Non-terminated Pods:\t (%d in total)\n " , len (nodeNonTerminatedPodsList .Items ))
3193
3243
w .Write (LEVEL_1 , "Namespace\t Name\t \t CPU Requests\t CPU Limits\t Memory Requests\t Memory Limits\t AGE\n " )
0 commit comments