<p>I have a Kubernetes cluster running in Google Kubernetes Engine where I wish to scale a deployment based on the number of messages a GCP Pub/Sub subscription has outstanding.
I found and followed <a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/external-metrics-autoscaling?hl=en_GB" rel="nofollow noreferrer">a guide from Google covering exactly this scenario</a> which involves deploying the <a href="https://github.com/GoogleCloudPlatform/k8s-stackdriver/tree/master/custom-metrics-stackdriver-adapter" rel="nofollow noreferrer">custom-metrics-stackdriver-adapter</a>.</p>
<p>However, because my subscriptions name is over 63 characters in length when I run <code>kubectl describe hpa &lt;MY_HPA_NAME&gt;</code> I get the following events.</p>
<pre><code>Events:
  Type     Reason                        Age                From                       Message
  ----     ------                        ----               ----                       -------
  Warning  FailedGetExternalMetric       12s (x2 over 27s)  horizontal-pod-autoscaler  invalid label value: &quot;&lt;MY_VERBOSE_SUBSCRIPTION_NAME&gt;&quot;: must be no more than 63 characters
  Warning  FailedComputeMetricsReplicas  12s (x2 over 27s)  horizontal-pod-autoscaler  failed to get pubsub.googleapis.com|subscription|num_undelivered_messages external metric: invalid label value: &quot;&lt;MY_VERBOSE_SUBSCRIPTION_NAME&gt;&quot;: must be no more than 63 characters
</code></pre>
<p>How do I go about using a Subscription name which is over 63 characters as a metric to trigger HPA?</p>
<h3>Extra questions</h3>
<p>I did try looking into what metric values where being exposed but when I queried Kubernetes Custom Metrics API I can't see any actual values for that metric by doing the following:</p>
<pre><code>$ kubectl get --raw &quot;http://localhost:8001/apis/custom.metrics.k8s.io/v1beta1/namespaces/*/pods/*/pubsub.googleapis.com|subscription|num_undelivered_messages&quot; | jq .

{
  &quot;kind&quot;: &quot;MetricValueList&quot;,
  &quot;apiVersion&quot;: &quot;custom.metrics.k8s.io/v1beta1&quot;,
  &quot;metadata&quot;: {
    &quot;selfLink&quot;: &quot;/apis/custom.metrics.k8s.io/v1beta1/namespaces/%2A/pods/%2A/pubsub.googleapis.com%7Csubscription%7Cnum_undelivered_messages&quot;
  },
  &quot;items&quot;: []
}
</code></pre>
<p>However, as you can see this returned an empty <code>&quot;items&quot;</code> lists suggesting there were no values recorded. I assumed that this was due to all of my subscription names being over 63 characters in length. To test this I made a new subscription with a very short name and recreated the HPA to be triggered by this new subscription.
This time the pods actually scaled how I expected <em>but</em> when I queried the Custom Metric API as above it still returned and empty <code>&quot;items&quot;</code> list.</p>
<p>Should I expect to see values in that response (and their labels)? How was Kubernetes able to scale the pods if no <code>&quot;items&quot;</code> were present?</p>

GavinE-OVO
<p>Here is <a href="https://github.com/kubernetes/community/blob/master/contributors/design-proposals/architecture/identifiers.md#identifiers-and-names-in-kubernetes" rel="nofollow noreferrer">design documentation</a> of the kubernetes. There is no way to avoid the restriction other than shortening the label name.</p>

<p>rfc1035/rfc1123 label (DNS_LABEL): An alphanumeric (a-z, and 0-9) string, with a maximum length of 63 characters, with the '-' character allowed anywhere except the first or last character, suitable for use as a hostname or segment in a domain name.</p>

<p>Check the External Metrics API instead of Custom Metrics API for metrics items.</p>

<pre><code>$ kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1" | jq
</code></pre>

<p><a href="https://itnext.io/google-kubernetes-engine-horizontalpodautoscaler-with-external-metrics-from-pubsub-28780c300305" rel="nofollow noreferrer">https://itnext.io/google-kubernetes-engine-horizontalpodautoscaler-with-external-metrics-from-pubsub-28780c300305</a></p>
