Merge pull request #114886 from a7i/kubectl-hpa-v2

kubectl: use autoscaling v2 for hpa

Kubernetes-commit: 1b95370225f1971c9e13beee24129d3191b0b3a8
This commit is contained in:
Kubernetes Publisher 2023-01-17 02:54:33 -08:00
commit 88d8e6045f
6 changed files with 308 additions and 308 deletions

4
go.mod
View File

@ -33,7 +33,7 @@ require (
k8s.io/api v0.0.0-20230112183318-59fcd23597fd
k8s.io/apimachinery v0.0.0-20230112182327-235f5123de4a
k8s.io/cli-runtime v0.0.0-20230112210707-723374f76971
k8s.io/client-go v0.0.0-20230112184258-895a515b59eb
k8s.io/client-go v0.0.0-20230116012449-228b004f4a92
k8s.io/component-base v0.0.0-20230112192324-33f62c7b2818
k8s.io/component-helpers v0.0.0-20230112192807-2c4994331fb8
k8s.io/klog/v2 v2.80.1
@ -94,7 +94,7 @@ replace (
k8s.io/api => k8s.io/api v0.0.0-20230112183318-59fcd23597fd
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230112182327-235f5123de4a
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20230112210707-723374f76971
k8s.io/client-go => k8s.io/client-go v0.0.0-20230112184258-895a515b59eb
k8s.io/client-go => k8s.io/client-go v0.0.0-20230116012449-228b004f4a92
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20230112181003-d37f85bc124e
k8s.io/component-base => k8s.io/component-base v0.0.0-20230112192324-33f62c7b2818
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20230112192807-2c4994331fb8

4
go.sum
View File

@ -538,8 +538,8 @@ k8s.io/apimachinery v0.0.0-20230112182327-235f5123de4a h1:ztM2zo2xo0w+WBKGJ/V/Tn
k8s.io/apimachinery v0.0.0-20230112182327-235f5123de4a/go.mod h1:Kq932aOItR+rI/aiAVDXxxdRFNf0eLnI0gZmXJ6hl3o=
k8s.io/cli-runtime v0.0.0-20230112210707-723374f76971 h1:WUFY78F2O94nzpjAmFKKfUDNcN0J41OQSb1TJHd8+4Y=
k8s.io/cli-runtime v0.0.0-20230112210707-723374f76971/go.mod h1:IzuO9LWCN40qSIrYpAKo9EsjW3NvnCic1IysWrg+Hmk=
k8s.io/client-go v0.0.0-20230112184258-895a515b59eb h1:pIB3Ec3d8++j09+98JsCwsHOQ6L+1snjo5ckejIb/rU=
k8s.io/client-go v0.0.0-20230112184258-895a515b59eb/go.mod h1:yGx/VWWKjooWFIfibkKEt0Vf6gG9wDALmEzcgJv5jPs=
k8s.io/client-go v0.0.0-20230116012449-228b004f4a92 h1:ufPl+2CEizC47k/ptJxbryxNF+EpJPG3aDIO6qUkXFE=
k8s.io/client-go v0.0.0-20230116012449-228b004f4a92/go.mod h1:yGx/VWWKjooWFIfibkKEt0Vf6gG9wDALmEzcgJv5jPs=
k8s.io/component-base v0.0.0-20230112192324-33f62c7b2818 h1:sZ5awJx2PpXaOwNSEu8Pq6zovP1+YG/nWZpagmpDRcs=
k8s.io/component-base v0.0.0-20230112192324-33f62c7b2818/go.mod h1:6IeVfLNdhjJkJFQHYMuwk7dgkOLA3Pt+Y7j7Md1d+CA=
k8s.io/component-helpers v0.0.0-20230112192807-2c4994331fb8 h1:DkCr496WIEUDXCYMe70ktyHui3zkVU9Oj4mAY00vRQk=

View File

@ -564,7 +564,7 @@ func (f *TestFactory) KubernetesClientSet() (*kubernetes.Clientset, error) {
clientset.AuthorizationV1beta1().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
clientset.AuthenticationV1alpha1().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
clientset.AutoscalingV1().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
clientset.AutoscalingV2beta1().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
clientset.AutoscalingV2().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
clientset.BatchV1().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
clientset.CertificatesV1().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
clientset.CertificatesV1beta1().RESTClient().(*restclient.RESTClient).Client = fakeClient.Client
@ -725,15 +725,15 @@ func testDynamicResources() []*restmapper.APIGroupResources {
Name: "autoscaling",
Versions: []metav1.GroupVersionForDiscovery{
{Version: "v1"},
{Version: "v2beta1"},
{Version: "v2"},
},
PreferredVersion: metav1.GroupVersionForDiscovery{Version: "v2beta1"},
PreferredVersion: metav1.GroupVersionForDiscovery{Version: "v2"},
},
VersionedResources: map[string][]metav1.APIResource{
"v1": {
{Name: "horizontalpodautoscalers", Namespaced: true, Kind: "HorizontalPodAutoscaler"},
},
"v2beta1": {
"v2": {
{Name: "horizontalpodautoscalers", Namespaced: true, Kind: "HorizontalPodAutoscaler"},
},
},

View File

@ -35,7 +35,7 @@ import (
"github.com/fatih/camelcase"
appsv1 "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
autoscalingv2 "k8s.io/api/autoscaling/v2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
certificatesv1beta1 "k8s.io/api/certificates/v1beta1"
@ -209,7 +209,7 @@ func describerMap(clientConfig *rest.Config) (map[schema.GroupKind]ResourceDescr
{Group: corev1.GroupName, Kind: "PriorityClass"}: &PriorityClassDescriber{c},
{Group: discoveryv1beta1.GroupName, Kind: "EndpointSlice"}: &EndpointSliceDescriber{c},
{Group: discoveryv1.GroupName, Kind: "EndpointSlice"}: &EndpointSliceDescriber{c},
{Group: autoscalingv2beta2.GroupName, Kind: "HorizontalPodAutoscaler"}: &HorizontalPodAutoscalerDescriber{c},
{Group: autoscalingv2.GroupName, Kind: "HorizontalPodAutoscaler"}: &HorizontalPodAutoscalerDescriber{c},
{Group: extensionsv1beta1.GroupName, Kind: "Ingress"}: &IngressDescriber{c},
{Group: networkingv1beta1.GroupName, Kind: "Ingress"}: &IngressDescriber{c},
{Group: networkingv1beta1.GroupName, Kind: "IngressClass"}: &IngressClassDescriber{c},
@ -3952,15 +3952,15 @@ type HorizontalPodAutoscalerDescriber struct {
func (d *HorizontalPodAutoscalerDescriber) Describe(namespace, name string, describerSettings DescriberSettings) (string, error) {
var events *corev1.EventList
// autoscaling/v2beta2 is introduced since v1.12 and autoscaling/v1 does not have full backward compatibility
// with autoscaling/v2beta2, so describer will try to get and describe hpa v2beta2 object firstly, if it fails,
// autoscaling/v2 is introduced since v1.23 and autoscaling/v1 does not have full backward compatibility
// with autoscaling/v2, so describer will try to get and describe hpa v2 object firstly, if it fails,
// describer will fall back to do with hpa v1 object
hpaV2beta2, err := d.client.AutoscalingV2beta2().HorizontalPodAutoscalers(namespace).Get(context.TODO(), name, metav1.GetOptions{})
hpaV2, err := d.client.AutoscalingV2().HorizontalPodAutoscalers(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err == nil {
if describerSettings.ShowEvents {
events, _ = searchEvents(d.client.CoreV1(), hpaV2beta2, describerSettings.ChunkSize)
events, _ = searchEvents(d.client.CoreV1(), hpaV2, describerSettings.ChunkSize)
}
return describeHorizontalPodAutoscalerV2beta2(hpaV2beta2, events, d)
return describeHorizontalPodAutoscalerV2(hpaV2, events, d)
}
hpaV1, err := d.client.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(context.TODO(), name, metav1.GetOptions{})
@ -3974,7 +3974,7 @@ func (d *HorizontalPodAutoscalerDescriber) Describe(namespace, name string, desc
return "", err
}
func describeHorizontalPodAutoscalerV2beta2(hpa *autoscalingv2beta2.HorizontalPodAutoscaler, events *corev1.EventList, d *HorizontalPodAutoscalerDescriber) (string, error) {
func describeHorizontalPodAutoscalerV2(hpa *autoscalingv2.HorizontalPodAutoscaler, events *corev1.EventList, d *HorizontalPodAutoscalerDescriber) (string, error) {
return tabbedString(func(out io.Writer) error {
w := NewPrefixWriter(out)
w.Write(LEVEL_0, "Name:\t%s\n", hpa.Name)
@ -3988,7 +3988,7 @@ func describeHorizontalPodAutoscalerV2beta2(hpa *autoscalingv2beta2.HorizontalPo
w.Write(LEVEL_0, "Metrics:\t( current / target )\n")
for i, metric := range hpa.Spec.Metrics {
switch metric.Type {
case autoscalingv2beta2.ExternalMetricSourceType:
case autoscalingv2.ExternalMetricSourceType:
if metric.External.Target.AverageValue != nil {
current := "<unknown>"
if len(hpa.Status.CurrentMetrics) > i && hpa.Status.CurrentMetrics[i].External != nil &&
@ -4004,15 +4004,15 @@ func describeHorizontalPodAutoscalerV2beta2(hpa *autoscalingv2beta2.HorizontalPo
w.Write(LEVEL_1, "%q (target value):\t%s / %s\n", metric.External.Metric.Name, current, metric.External.Target.Value.String())
}
case autoscalingv2beta2.PodsMetricSourceType:
case autoscalingv2.PodsMetricSourceType:
current := "<unknown>"
if len(hpa.Status.CurrentMetrics) > i && hpa.Status.CurrentMetrics[i].Pods != nil {
current = hpa.Status.CurrentMetrics[i].Pods.Current.AverageValue.String()
}
w.Write(LEVEL_1, "%q on pods:\t%s / %s\n", metric.Pods.Metric.Name, current, metric.Pods.Target.AverageValue.String())
case autoscalingv2beta2.ObjectMetricSourceType:
case autoscalingv2.ObjectMetricSourceType:
w.Write(LEVEL_1, "\"%s\" on %s/%s ", metric.Object.Metric.Name, metric.Object.DescribedObject.Kind, metric.Object.DescribedObject.Name)
if metric.Object.Target.Type == autoscalingv2beta2.AverageValueMetricType {
if metric.Object.Target.Type == autoscalingv2.AverageValueMetricType {
current := "<unknown>"
if len(hpa.Status.CurrentMetrics) > i && hpa.Status.CurrentMetrics[i].Object != nil {
current = hpa.Status.CurrentMetrics[i].Object.Current.AverageValue.String()
@ -4025,7 +4025,7 @@ func describeHorizontalPodAutoscalerV2beta2(hpa *autoscalingv2beta2.HorizontalPo
}
w.Write(LEVEL_0, "(target value):\t%s / %s\n", current, metric.Object.Target.Value.String())
}
case autoscalingv2beta2.ResourceMetricSourceType:
case autoscalingv2.ResourceMetricSourceType:
w.Write(LEVEL_1, "resource %s on pods", string(metric.Resource.Name))
if metric.Resource.Target.AverageValue != nil {
current := "<unknown>"
@ -4045,7 +4045,7 @@ func describeHorizontalPodAutoscalerV2beta2(hpa *autoscalingv2beta2.HorizontalPo
}
w.Write(LEVEL_1, "(as a percentage of request):\t%s / %s\n", current, target)
}
case autoscalingv2beta2.ContainerResourceMetricSourceType:
case autoscalingv2.ContainerResourceMetricSourceType:
w.Write(LEVEL_1, "resource %s of container \"%s\" on pods", string(metric.ContainerResource.Name), metric.ContainerResource.Container)
if metric.ContainerResource.Target.AverageValue != nil {
current := "<unknown>"
@ -4101,7 +4101,7 @@ func describeHorizontalPodAutoscalerV2beta2(hpa *autoscalingv2beta2.HorizontalPo
})
}
func printDirectionBehavior(w PrefixWriter, direction string, rules *autoscalingv2beta2.HPAScalingRules) {
func printDirectionBehavior(w PrefixWriter, direction string, rules *autoscalingv2.HPAScalingRules) {
if rules != nil {
w.Write(LEVEL_1, "%s:\n", direction)
if rules.StabilizationWindowSeconds != nil {
@ -4111,7 +4111,7 @@ func printDirectionBehavior(w PrefixWriter, direction string, rules *autoscaling
if rules.SelectPolicy != nil {
w.Write(LEVEL_2, "Select Policy: %s\n", *rules.SelectPolicy)
} else {
w.Write(LEVEL_2, "Select Policy: %s\n", autoscalingv2beta2.MaxPolicySelect)
w.Write(LEVEL_2, "Select Policy: %s\n", autoscalingv2.MaxChangePolicySelect)
}
w.Write(LEVEL_2, "Policies:\n")
for _, p := range rules.Policies {

File diff suppressed because it is too large Load Diff

View File

@ -29,7 +29,7 @@ import (
authorizationv1 "k8s.io/api/authorization/v1"
authorizationv1beta1 "k8s.io/api/authorization/v1beta1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
certificatesv1 "k8s.io/api/certificates/v1"
@ -69,7 +69,7 @@ func init() {
utilruntime.Must(Scheme.SetVersionPriority(appsv1beta1.SchemeGroupVersion, appsv1beta2.SchemeGroupVersion, appsv1.SchemeGroupVersion))
utilruntime.Must(Scheme.SetVersionPriority(authenticationv1.SchemeGroupVersion, authenticationv1beta1.SchemeGroupVersion))
utilruntime.Must(Scheme.SetVersionPriority(authorizationv1.SchemeGroupVersion, authorizationv1beta1.SchemeGroupVersion))
utilruntime.Must(Scheme.SetVersionPriority(autoscalingv1.SchemeGroupVersion, autoscalingv2beta1.SchemeGroupVersion))
utilruntime.Must(Scheme.SetVersionPriority(autoscalingv1.SchemeGroupVersion, autoscalingv2.SchemeGroupVersion))
utilruntime.Must(Scheme.SetVersionPriority(batchv1.SchemeGroupVersion, batchv1beta1.SchemeGroupVersion))
utilruntime.Must(Scheme.SetVersionPriority(certificatesv1.SchemeGroupVersion, certificatesv1beta1.SchemeGroupVersion))
utilruntime.Must(Scheme.SetVersionPriority(extensionsv1beta1.SchemeGroupVersion))