<p>I am trying to use the <a href="https://github.com/kubernetes-client" rel="nofollow noreferrer">kubernetes-client</a> in python in order to create an horizontal pod autoscaler in kubernetes. To do so i make use of the <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AutoscalingV2beta2Api.md#create_namespaced_horizontal_pod_autoscaler" rel="nofollow noreferrer">create_namespaced_horizontal_pod_autoscaler</a>()  function that belongs to the <a href="https://github.com/kubernetes-client/python/tree/master/kubernetes" rel="nofollow noreferrer">AutoscalingV2beta2Api</a>.</p>
<p>My code is the following:</p>
<pre><code>my_metrics = []
my_metrics.append(client.V2beta2MetricSpec(type='Pods', pods= client.V2beta2PodsMetricSource(metric=client.V2beta2MetricIdentifier(name='cpu'), target=client.V2beta2MetricTarget(average_utilization='50',type='Utilization'))))


body = client.V2beta2HorizontalPodAutoscaler(
    api_version='autoscaling/v2beta2',
    kind='HorizontalPodAutoscaler',
    metadata=client.V1ObjectMeta(name='php-apache'),
    spec= client.V2beta2HorizontalPodAutoscalerSpec(
    max_replicas=10,
    min_replicas=1,
    metrics = my_metrics,
    scale_target_ref = client.V2beta2CrossVersionObjectReference(kind='Object',name='php-apache')
    )) 

v2 = client.AutoscalingV2beta2Api()
ret = v2.create_namespaced_horizontal_pod_autoscaler(namespace='default', body=body, pretty=True)
</code></pre>
<p>And the response i get:</p>
<pre><code>HTTP response body: {
  &quot;kind&quot;: &quot;Status&quot;,
  &quot;apiVersion&quot;: &quot;v1&quot;,
  &quot;metadata&quot;: {
    
  },
  &quot;status&quot;: &quot;Failure&quot;,
  &quot;message&quot;: &quot;HorizontalPodAutoscaler in version \&quot;v2beta2\&quot; cannot be handled as a HorizontalPodAutoscaler: v2beta2.HorizontalPodAutoscaler.Spec: v2beta2.HorizontalPodAutoscalerSpec.Metrics: []v2beta2.MetricSpec: v2beta2.MetricSpec.Pods: v2beta2.PodsMetricSource.Target: v2beta2.MetricTarget.AverageUtilization: readUint32: unexpected character: \ufffd, error found in #10 byte of ...|zation\&quot;: \&quot;50\&quot;, \&quot;type|..., bigger context ...|\&quot;name\&quot;: \&quot;cpu\&quot;}, \&quot;target\&quot;: {\&quot;averageUtilization\&quot;: \&quot;50\&quot;, \&quot;type\&quot;: \&quot;Utilization\&quot;}}, \&quot;type\&quot;: \&quot;Pods\&quot;}], \&quot;m|...&quot;,
  &quot;reason&quot;: &quot;BadRequest&quot;,
  &quot;code&quot;: 400
}
</code></pre>
<p>I just want to use the kubernetes-client to call the equivalent of:</p>
<pre><code>kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
</code></pre>
<p>Any working example would be more than welcome.</p>

<p>The above example permits the creation of a memory based hpa via the latest version of the <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">k8s python client</a> (v12.0.1).</p>
<pre><code>my_metrics = []
my_metrics.append(client.V2beta2MetricSpec(type='Resource', resource= client.V2beta2ResourceMetricSource(name='memory',target=client.V2beta2MetricTarget(average_utilization= 30,type='Utilization'))))

my_conditions = []
my_conditions.append(client.V2beta2HorizontalPodAutoscalerCondition(status = &quot;True&quot;, type = 'AbleToScale'))

status = client.V2beta2HorizontalPodAutoscalerStatus(conditions = my_conditions, current_replicas = 1, desired_replicas = 1)

body = client.V2beta2HorizontalPodAutoscaler(
              api_version='autoscaling/v2beta2',
              kind='HorizontalPodAutoscaler',
              metadata=client.V1ObjectMeta(name=self.app_name),
              spec= client.V2beta2HorizontalPodAutoscalerSpec(
                  max_replicas=self.MAX_PODS,
                  min_replicas=self.MIN_PODS,
                  metrics = my_metrics,
                  scale_target_ref = client.V2beta2CrossVersionObjectReference(kind = 'Deployment', name = self.app_name, api_version = 'apps/v1'),
              ),
              status = status)
v2 = client.AutoscalingV2beta2Api()
ret = v2.create_namespaced_horizontal_pod_autoscaler(namespace='default', body=body, pretty=True)
</code></pre>
